Docker Kubernetes 容器重啓策略nginx
當容器被建立時,容器會根據重啓策略來進行容器重啓。vim
支持三種策略:centos
環境:api
一、管理節點:建立Pod時添加劇啓策略bash
vim pod.yamlapp
apiVersion: v1 kind: Pod metadata: name: pod-test labels: test: centos spec: containers: - name: hello image: centos:6 command: ["bash","-c","while true;do date;sleep 1;done"] restartPolicy: OnFilure
# api版本 apiVersion: v1 # 指定建立資源對象 kind: Pod # 源數據、能夠寫name,命名空間,對象標籤 metadata: # 服務名稱 name: pod-test # 標籤 labels: # 標籤名 os: centos # 容器資源信息 spec: # 容器管理 containers: # 容器名稱 - name: hello # 容器鏡像 image: centos:6 # 添加環境變量 env: # 建立key - name: Test # 建立value value: "123456" # 啓動容器後執行命令 command: ["bash","-c","while true;do date;sleep 1;done"] # 重啓策略 可添加(Always,OnFailure,Never) restartPolicy: OnFilure
二、管理節點:建立Podide
kubectl create -f pod.yaml
命令:kubectl describe pods pod-test # 重啓統計 Restart Count: 0
注:OnFailure狀態可在相關節點kill掉容器服務測試重啓。測試
# 查看默認重啓策略
kubectl edit deployment nginx
restartPolicy: Alwaysspa
# 語法格式3d
apiVersion: v1 kind: Pod metadata: name: foo namespace: awesomeapps spec: containers: - name: foo image: janedoe/awesomeapp:v1 restartPolicy: Always
一、建立測試yaml;每一個10秒發出異常退出重啓容器
apiVersion: v1 kind: Pod metadata: name: foo spec: containers: - name: busybox image: busybox args: - /bin/sh - -c - sleep 10
二、執行文件
kubectl create -f restart.yaml
三、設置重啓策略,當容器異常退出時直接銷燬,不重啓
apiVersion: v1 kind: Pod metadata: name: foo spec: containers: - name: busybox image: busybox args: - /bin/sh - -c - sleep 10 restartPolicy: Never
四、執行文件
kubectl create -f restart.yaml
五、測試
10秒後查看