重启策略 (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