国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

K8s(健康檢查+滾動更新+優(yōu)雅停機(jī)+彈性伸縮+Prometheus監(jiān)控+配置分離)

這篇具有很好參考價值的文章主要介紹了K8s(健康檢查+滾動更新+優(yōu)雅停機(jī)+彈性伸縮+Prometheus監(jiān)控+配置分離)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報違法"按鈕提交疑問。

前言

快速配置請直接跳轉(zhuǎn)至匯總配置

K8s + SpringBoot實(shí)現(xiàn)零宕機(jī)發(fā)布:健康檢查+滾動更新+優(yōu)雅停機(jī)+彈性伸縮+Prometheus監(jiān)控+配置分離(鏡像復(fù)用)
配置
健康檢查

健康檢查類型:就緒探針(readiness)+ 存活探針(liveness)

探針類型:exec(進(jìn)入容器執(zhí)行腳本)、tcpSocket(探測端口)、httpGet(調(diào)用接口)

業(yè)務(wù)層面

項目依賴 pom.xml
 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>

定義訪問端口、路徑及權(quán)限 application.yaml

management:
  server:
    port: 50000                         # 啟用獨(dú)立運(yùn)維端口
  endpoint:                             # 開啟health端點(diǎn)
    health:
      probes:
        enabled: true
  endpoints:
    web:
      exposure:
        base-path: /actuator            # 指定上下文路徑,啟用相應(yīng)端點(diǎn)
        include: health

將暴露/actuator/health/readiness和/actuator/health/liveness兩個接口,訪問方式如下:

http://127.0.0.1:50000/actuator/health/readiness
http://127.0.0.1:50000/actuator/health/liveness

運(yùn)維層面

k8s部署模版deployment.yaml
apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      containers:
      - name: {APP_NAME}
        image: {IMAGE_URL}
        imagePullPolicy: Always
        ports:
        - containerPort: {APP_PORT}
        - name: management-port
          containerPort: 50000         # 應(yīng)用管理端口
        readinessProbe:                # 就緒探針
          httpGet:
            path: /actuator/health/readiness
            port: management-port
          initialDelaySeconds: 30      # 延遲加載時間
          periodSeconds: 10            # 重試時間間隔
          timeoutSeconds: 1            # 超時時間設(shè)置
          successThreshold: 1          # 健康閾值
          failureThreshold: 6          # 不健康閾值
        livenessProbe:                 # 存活探針
          httpGet:
            path: /actuator/health/liveness
            port: management-port
          initialDelaySeconds: 30      # 延遲加載時間
          periodSeconds: 10            # 重試時間間隔
          timeoutSeconds: 1            # 超時時間設(shè)置
          successThreshold: 1          # 健康閾值
          failureThreshold: 6          # 不健康閾值

滾動更新

k8s資源調(diào)度之滾動更新策略,若要實(shí)現(xiàn)零宕機(jī)發(fā)布,需支持健康檢查

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {APP_NAME}
  labels:
    app: {APP_NAME}
spec:
  selector:
    matchLabels:
      app: {APP_NAME}
  replicas: {REPLICAS}				# Pod副本數(shù)
  strategy:
    type: RollingUpdate				# 滾動更新策略
    rollingUpdate:
      maxSurge: 1                   # 升級過程中最多可以比原先設(shè)置的副本數(shù)多出的數(shù)量
      maxUnavailable: 1             # 升級過程中最多有多少個POD處于無法提供服務(wù)的狀態(tài)

優(yōu)雅停機(jī)

在K8s中,當(dāng)我們實(shí)現(xiàn)滾動升級之前,務(wù)必要實(shí)現(xiàn)應(yīng)用級別的優(yōu)雅停機(jī)。否則滾動升級時,還是會影響到業(yè)務(wù)。使應(yīng)用關(guān)閉線程、釋放連接資源后再停止服務(wù)
業(yè)務(wù)層面

項目依賴 pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

定義訪問端口、路徑及權(quán)限 application.yaml

spring:
  application:
    name: <xxx>
  profiles:
    active: @profileActive@
  lifecycle:
    timeout-per-shutdown-phase: 30s     # 停機(jī)過程超時時長設(shè)置30s,超過30s,直接停機(jī)

server:
  port: 8080
  shutdown: graceful                    # 默認(rèn)為IMMEDIATE,表示立即關(guān)機(jī);GRACEFUL表示優(yōu)雅關(guān)機(jī)

management:
  server:
    port: 50000                         # 啟用獨(dú)立運(yùn)維端口
  endpoint:                             # 開啟shutdown和health端點(diǎn)
    shutdown:
      enabled: true
    health:
      probes:
        enabled: true
  endpoints:
    web:
      exposure:
        base-path: /actuator            # 指定上下文路徑,啟用相應(yīng)端點(diǎn)
        include: health,shutdown

將暴露/actuator/shutdown接口,調(diào)用方式如下:

curl -X POST 127.0.0.1:50000/actuator/shutdown

運(yùn)維層面

確保dockerfile模版集成curl工具,否則無法使用curl命令

FROM openjdk:8-jdk-alpine
#構(gòu)建參數(shù)
ARG JAR_FILE
ARG WORK_PATH="/app"
ARG EXPOSE_PORT=8080

#環(huán)境變量
ENV JAVA_OPTS=""\
    JAR_FILE=${JAR_FILE}

#設(shè)置時區(qū)
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories  \
    && apk add --no-cache curl
#將maven目錄的jar包拷貝到docker中,并命名為for_docker.jar
COPY target/$JAR_FILE $WORK_PATH/


#設(shè)置工作目錄
WORKDIR $WORK_PATH

指定于外界交互的端口

EXPOSE $EXPOSE_PORT

配置容器,使其可執(zhí)行化

ENTRYPOINT exec java $JAVA_OPTS -jar $JAR_FILE

k8s部署模版deployment.yaml

注:經(jīng)驗證,java項目可省略結(jié)束回調(diào)鉤子的配置

此外,若需使用回調(diào)鉤子,需保證鏡像中包含curl工具,且需注意應(yīng)用管理端口(50000)不能暴露到公網(wǎng)

apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      containers:
      - name: {APP_NAME}
        image: {IMAGE_URL}
        imagePullPolicy: Always
        ports:
        - containerPort: {APP_PORT}
        - containerPort: 50000
        lifecycle:
          preStop: 						# 結(jié)束回調(diào)鉤子
            exec:
              command: ["curl", "-XPOST", "127.0.0.1:50000/actuator/shutdown"]

彈性伸縮

為pod設(shè)置資源限制后,創(chuàng)建HPA

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {APP_NAME}
  labels:
    app: {APP_NAME}
spec:
  template:
    spec:
      containers:
      - name: {APP_NAME}
        image: {IMAGE_URL}
        imagePullPolicy: Always
        resources:                     # 容器資源管理
          limits:                      # 資源限制(監(jiān)控使用情況)
            cpu: 0.5
            memory: 1Gi
          requests:                    # 最小可用資源(靈活調(diào)度)
            cpu: 0.15
            memory: 300Mi
kind: HorizontalPodAutoscaler            # 彈性伸縮控制器
apiVersion: autoscaling/v2beta2
metadata:
  name: {APP_NAME}
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: {APP_NAME}
  minReplicas: {REPLICAS}                # 縮放范圍
  maxReplicas: 6
  metrics:
    - type: Resource
      resource:
        name: cpu                        # 指定資源指標(biāo)
        target:
          type: Utilization
          averageUtilization: 50

Prometheus集成
業(yè)務(wù)層面

項目依賴 pom.xml

<!-- 引入Spring boot的監(jiān)控機(jī)制-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

定義訪問端口、路徑及權(quán)限 application.yaml

management:
  server:
    port: 50000                         # 啟用獨(dú)立運(yùn)維端口
  metrics:
    tags:
      application: ${spring.application.name}
  endpoints:
    web:
      exposure:
        base-path: /actuator            # 指定上下文路徑,啟

用相應(yīng)端點(diǎn)
include: metrics,prometheus

將暴露/actuator/metric和/actuator/prometheus接口,訪問方式如下:

http://127.0.0.1:50000/actuator/metric
http://127.0.0.1:50000/actuator/prometheus

運(yùn)維層面 deployment.yaml

apiVersion: apps/v1
kind: Deployment
spec:
  template:
    metadata:
      annotations:
        prometheus:io/port: "50000"
        prometheus.io/path: /actuator/prometheus  # 在流水線中賦值
        prometheus.io/scrape: "true"              # 基于pod的服務(wù)發(fā)現(xiàn)

配置分離

方案:通過configmap掛載外部配置文件,并指定激活環(huán)境運(yùn)行

作用:配置分離,避免敏感信息泄露;鏡像復(fù)用,提高交付效率

通過文件生成configmap

通過dry-run的方式生成yaml文件

kubectl create cm -n <APP_NAME> --from-file=application-test.yaml --dry-run=1 -oyaml > configmap.yaml

更新

kubectl apply -f configmap.yaml

掛載configmap并指定激活環(huán)境

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {APP_NAME}
  labels:
    app: {APP_NAME}
spec:
  template:
    spec:
      containers:
      - name: {APP_NAME}
        image: {IMAGE_URL}
        imagePullPolicy: Always
        env:
          - name: SPRING_PROFILES_ACTIVE   # 指定激活環(huán)境
            value: test
        volumeMounts:                      # 掛載configmap
        - name: conf
          mountPath: "/app/config"         # 與Dockerfile中工作目錄一致
          readOnly: true
      volumes:
      - name: conf
        configMap:
          name: {APP_NAME}

匯總配置

業(yè)務(wù)層面

項目依賴 pom.xml

<!-- 引入Spring boot的監(jiān)控機(jī)制-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

定義訪問端口、路徑及權(quán)限 application.yaml

spring:
  application:
    name: project-sample
  profiles:
    active: @profileActive@
  lifecycle:
    timeout-per-shutdown-phase: 30s     # 停機(jī)過程超時時長設(shè)置30s,超過30s,直接停機(jī)

server:
  port: 8080
  shutdown: graceful                    # 默認(rèn)為IMMEDIATE,表示立即關(guān)機(jī);GRACEFUL表示優(yōu)雅關(guān)機(jī)

management:
  server:
    port: 50000                         # 啟用獨(dú)立運(yùn)維端口
  metrics:
    tags:
      application: ${spring.application.name}
  endpoint:                             # 開啟shutdown和health端點(diǎn)
    shutdown:
      enabled: true
    health:
      probes:
        enabled: true
  endpoints:
    web:
      exposure:
        base-path: /actuator            # 指定上下文路徑,啟用相應(yīng)端點(diǎn)
        include: health,shutdown,metrics,prometheus

運(yùn)維層面

確保dockerfile模版集成curl工具,否則無法使用curl命令文章來源地址http://www.zghlxwxcb.cn/news/detail-626034.html

FROM openjdk:8-jdk-alpine
#構(gòu)建參數(shù)
ARG JAR_FILE
ARG WORK_PATH="/app"
ARG EXPOSE_PORT=8080

#環(huán)境變量
ENV JAVA_OPTS=""\
    JAR_FILE=${JAR_FILE}

#設(shè)置時區(qū)
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories  \
    && apk add --no-cache curl
#將maven目錄的jar包拷貝到docker中,并命名為for_docker.jar
COPY target/$JAR_FILE $WORK_PATH/


#設(shè)置工作目錄
WORKDIR $WORK_PATH


# 指定于外界交互的端口
EXPOSE $EXPOSE_PORT
# 配置容器,使其可執(zhí)行化
ENTRYPOINT exec java $JAVA_OPTS -jar $JAR_FILE

k8s部署模版deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {APP_NAME}
  labels:
    app: {APP_NAME}
spec:
  selector:
    matchLabels:
      app: {APP_NAME}
  replicas: {REPLICAS}                            # Pod副本數(shù)
  strategy:
    type: RollingUpdate                           # 滾動更新策略
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      name: {APP_NAME}
      labels:
        app: {APP_NAME}
      annotations:
        timestamp: {TIMESTAMP}
        prometheus.io/port: "50000"               # 不能動態(tài)賦值
        prometheus.io/path: /actuator/prometheus
        prometheus.io/scrape: "true"              # 基于pod的服務(wù)發(fā)現(xiàn)
    spec:
      affinity:                                   # 設(shè)置調(diào)度策略,采取多主機(jī)/多可用區(qū)部署
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - {APP_NAME}
              topologyKey: "kubernetes.io/hostname" # 多可用區(qū)為"topology.kubernetes.io/zone"
      terminationGracePeriodSeconds: 30             # 優(yōu)雅終止寬限期
      containers:
      - name: {APP_NAME}
        image: {IMAGE_URL}
        imagePullPolicy: Always
        ports:
        - containerPort: {APP_PORT}
        - name: management-port
          containerPort: 50000         # 應(yīng)用管理端口
        readinessProbe:                # 就緒探針
          httpGet:
            path: /actuator/health/readiness
            port: management-port
          initialDelaySeconds: 30      # 延遲加載時間
          periodSeconds: 10            # 重試時間間隔
          timeoutSeconds: 1            # 超時時間設(shè)置
          successThreshold: 1          # 健康閾值
          failureThreshold: 9          # 不健康閾值
        livenessProbe:                 # 存活探針
          httpGet:
            path: /actuator/health/liveness
            port: management-port
          initialDelaySeconds: 30      # 延遲加載時間
          periodSeconds: 10            # 重試時間間隔
          timeoutSeconds: 1            # 超時時間設(shè)置
          successThreshold: 1          # 健康閾值
          failureThreshold: 6          # 不健康閾值
        resources:                     # 容器資源管理
          limits:                      # 資源限制(監(jiān)控使用情況)
            cpu: 0.5
            memory: 1Gi
          requests:                    # 最小可用資源(靈活調(diào)度)
            cpu: 0.1
            memory: 200Mi
        env:
          - name: TZ
            value: Asia/Shanghai
---
kind: HorizontalPodAutoscaler            # 彈性伸縮控制器
apiVersion: autoscaling/v2beta2
metadata:
  name: {APP_NAME}
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: {APP_NAME}
  minReplicas: {REPLICAS}                # 縮放范圍
  maxReplicas: 6
  metrics:
    - type: Resource
      resource:
        name: cpu                        # 指定資源指標(biāo)
        target:
          type: Utilization
          averageUtilization: 50

到了這里,關(guān)于K8s(健康檢查+滾動更新+優(yōu)雅停機(jī)+彈性伸縮+Prometheus監(jiān)控+配置分離)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Kubernetes(k8s)健康性檢查:livenessprobe探測和readinessprobe探測

    目錄 一.系統(tǒng)環(huán)境 二.前言 三.Kubernetes健康性檢查簡介 四.創(chuàng)建沒有探測機(jī)制的pod 五.添加livenessprobe探測 5.1 使用command的方式進(jìn)行l(wèi)ivenessprobe探測 5.2 使用httpGet的方式進(jìn)行l(wèi)ivenessprobe探測 5.3 使用tcpSocket的方式進(jìn)行l(wèi)ivenessprobe探測 六.readinessprobe探測 七.總結(jié) 本文主要基于Kubernetes1.

    2024年02月07日
    瀏覽(24)
  • 基于k8s Deployment的彈性擴(kuò)縮容及滾動發(fā)布機(jī)制詳解

    基于k8s Deployment的彈性擴(kuò)縮容及滾動發(fā)布機(jī)制詳解

    k8s第一個重要設(shè)計思想:控制器模式。k8s里第一個控制器模式的完整實(shí)現(xiàn):Deployment。它實(shí)現(xiàn)了k8s一大重要功能:Pod的“水平擴(kuò)展/收縮”(horizontal scaling out/in)。該功能從PaaS時代開始就是一個平臺級項目必備編排能力。 若你更新了Deployment的Pod模板(如修改容器的鏡像),則

    2024年02月03日
    瀏覽(26)
  • k8s 滾動更新控制(一)

    k8s 滾動更新控制(一)

    在傳統(tǒng)的應(yīng)用升級時,通常采用的方式是先停止服務(wù),然后升級部署,最后將新應(yīng)用啟動。這個過程面臨一個問題,就是在某段時間內(nèi),服務(wù)是不可用的,對于用戶來說是非常不友好的。而kubernetes滾動更新,將避免這種情況的發(fā)生。 對于Kubernetes集群來說,一個service可能有多

    2024年02月13日
    瀏覽(26)
  • K8S滾動更新守護(hù)進(jìn)程集

    DaemonSets are great for deploying services across an entire cluster, but what about upgrades? Prior to Kubernetes 1.6, the only way to update Pods managed by a DaemonSet was to update the DaemonSet and then manually delete each Pod that was managed by the DaemonSet so that it would be re-created with the new configuration. With the release of Kubernetes 1.6

    2024年01月21日
    瀏覽(17)
  • k8s擴(kuò)縮容與滾動更新

    k8s擴(kuò)縮容與滾動更新

    使用kubectl run創(chuàng)建 應(yīng)用 端口暴露出去 使用kubectl? create 創(chuàng)建應(yīng)用 端口暴露出去 run 與 create 區(qū)別: run 為一次性的創(chuàng)建運(yùn)行,刪除后不會自動生成,沒有 pod 控制器 create 創(chuàng)建的,刪除后還會自動生成,有 pod 控制器,擁有副本集控制 scale 應(yīng)用 默認(rèn)情況下 應(yīng)用只會運(yùn)行一個副

    2024年02月12日
    瀏覽(46)
  • Kubeadm 部署k8s實(shí)現(xiàn)并且演示滾動不停服務(wù)更新

    Kubeadm 部署k8s實(shí)現(xiàn)并且演示滾動不停服務(wù)更新

    在開始之前,部署Kubernetes集群機(jī)器需要滿足以下幾個條件: 一臺或多臺機(jī)器,操作系統(tǒng) CentOS7.x-86_x64; 硬件配置:2GB或更多RAM,2個CPU或更多CPU,硬盤30GB或更多; 集群中所有機(jī)器之間網(wǎng)絡(luò)互通; 可以訪問外網(wǎng),需要拉取鏡像; 禁止swap分區(qū)。 主機(jī)詳情 IP Hostname 備注 192.168

    2024年02月11日
    瀏覽(28)
  • 使用k8s 配置 RollingUpdate 滾動更新實(shí)現(xiàn)應(yīng)用的灰度發(fā)布

    方案實(shí)現(xiàn)方式: RollingUpdate 滾動更新機(jī)制 當(dāng)某個服務(wù)需要升級時,傳統(tǒng)的做法是,先將要更新的服務(wù)下線,業(yè)務(wù)停止后再更新版本和配置,然后重新啟動服務(wù)。 如果業(yè)務(wù)集群規(guī)模較大時,這個工作就變成了一個挑戰(zhàn),而且全部停止了服務(wù),再逐步升級的方式會導(dǎo)致服務(wù)較長

    2024年01月24日
    瀏覽(29)
  • 滴滴彈性云基于 K8S 的調(diào)度實(shí)踐

    滴滴彈性云基于 K8S 的調(diào)度實(shí)踐

    上篇文章詳細(xì)介紹了彈性云混部的落地歷程,彈性云是滴滴內(nèi)部提供給網(wǎng)約車等核心服務(wù)的容器平臺,其基于 k8s 實(shí)現(xiàn)了對海量 node 的管理和 pod 的調(diào)度。本文重點(diǎn)介紹彈性云的調(diào)度能力,分為以下部分: 調(diào)度鏈路圖 :介紹當(dāng)前彈性云調(diào)度體系鏈路,對架構(gòu)體系有一個初步的

    2024年02月05日
    瀏覽(25)
  • 如何優(yōu)化k8s中HPA的彈性速率?

    如何優(yōu)化k8s中HPA的彈性速率?

    本文分享自華為云社區(qū)《K8s 核心資源指標(biāo)HPA性能優(yōu)化之路》,作者:可以交個朋友。 以彈性指標(biāo)為cpu、memory為例。在Kubernetes 1.7版本中引入了聚合層,允許第三方應(yīng)用程序注冊相關(guān)API接口到kube-apiserver上。其中?/apis/metrics.k8s.io/v1beta1?一般由metrics-server程序提供,以插件的形式

    2024年02月22日
    瀏覽(22)
  • k8s優(yōu)雅終止pod

    k8s優(yōu)雅終止pod

    Pod 銷毀時,會停止容器內(nèi)的進(jìn)程,通常在停止的過程中我們需要執(zhí)行一些善后邏輯,比如等待存量請求處理完以避免連接中斷,或通知相關(guān)依賴進(jìn)行清理等,從而實(shí)現(xiàn)優(yōu)雅終止目的。本文介紹在 Kubernetes 場景下,實(shí)現(xiàn)容器優(yōu)雅終止的最佳實(shí)踐。 我們先了解下容器在 Kubernete

    2024年02月15日
    瀏覽(27)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包