k8s 存活探針(健康檢查)


重啟策略 (RestartPolicy )

Always:當容器終止退出后,總是重啟容器,默認策略。
OnFailure:當容器異常退出(退出狀態碼非0)時,才重啟容器。
Never:當容器終止退出,從不重啟容器。
 
probe有以下兩種類型:
livenessProbe:如果檢查失敗,將殺死容器,根據Pod的restartPolicy來操作。
readinessProbe: 如果檢查失敗,Kubernetes會把Pod從service endpoints中剔除
 
Probe支持以下三種檢查方法:
httpGet:發送HTTP請求,返回200-400范圍狀態碼為成功。
exec:執行Shell命令返回狀態碼是0為成功。
tcpSocket:發起TCP Socket建立成功。
 
 

方法一 httpGet

nginx使用 httpGet健康檢查的方法
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  minReadySeconds: 1
  progressDeadlineSeconds: 60
  revisionHistoryLimit: 2
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  template:
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.19
        ports:
        - containerPort: 80
        resources:
          requests:
            memory: "500Mi"
            cpu: "250m"
          limits: 
            memory: "500Mi" 
            cpu: "500m"
        livenessProbe:
           httpGet:
             path: /
             port: 80
           initialDelaySeconds: 10  #pod啟動10秒執行第一次檢查
           periodSeconds: 5         #第一次檢查后每隔5秒檢查一次
        volumeMounts:
        - name: html
          mountPath: /usr/share/nginx/html
      volumes:
      - name: html
        hostPath:
          path: /home/k8s/data/nginx
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  type: NodePort
  ports:
   - port: 8080
     nodePort: 30080
  selector:
    app: nginx

 

 

方法二:tcpSocket

nginx使用 tcpSocket健康檢查的方法
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  minReadySeconds: 1
  progressDeadlineSeconds: 60
  revisionHistoryLimit: 2
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  template:
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.19
        ports:
        - containerPort: 80
        resources:
          requests:
            memory: "500Mi"
            cpu: "250m"
          limits: 
            memory: "500Mi" 
            cpu: "500m"
        livenessProbe:
           tcpSocket:
             port: 80
           initialDelaySeconds: 10
           periodSeconds: 5
        volumeMounts:
        - name: html
          mountPath: /usr/share/nginx/html
      volumes:
      - name: html
        hostPath:
          path: /home/k8s/data/nginx
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  type: NodePort
  ports:
   - port: 8080
     nodePort: 30080
  selector:
    app: nginx

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM