很多時候pod報錯CrashloopBackOff都表示為pod中的程序存在了某些問題,并且一直在重復(fù)的啟動 通過查看logs的方式又不能獲取到有用的信息。 這個時候我們需要在失敗的容器中運行一個shell,對失敗的原因進行排查, 但是沒有容器可以連接。
在這種情況下我們可以參考Run a command in a Shell documentation中的介紹, 覆蓋docker中的命令
修改deployment
$ kubectl edit deploy mydeployment -n mynamespace
在容器中找到定義command的位置, 如果你的啟動腳本為bootstrap.sh:
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
command: bootstrap.sh
如果在deployment中沒有找到相關(guān)的配置, 我們依據(jù)可以添加一個command, 因為他會覆蓋在image的默認配置。
修改會添加如下的參數(shù)到資源文件中, 如下所示:
spec:
containers:
- name: nginx
image: nginx:1.14.2
command: ["/bin/sh"]
args: ["-c", "while true; do echo hello; sleep 10;done"]
**注意: ** **args**需要定義為一個數(shù)組,當(dāng)我們保存后他會加載為如下所示內(nèi)容:
spec:
containers:
- name: nginx
image: nginx:1.14.2
command:
- /bin/sh
args:
- -c
- while true; do echo hello; sleep 60;done
同樣的道理我們也可以添加環(huán)境變量到pod中, 如下所示:
spec:
containers:
- name: nginx
image: nginx:1.14.2
command:
- /bin/sh
args:
- -c
- while true; do echo hello; sleep 60;done
env:
- name: MYAPP_DEBUG
value: "true"
保存資源的定義后,你會看到pod變?yōu)榱藃unning狀態(tài), 我們可以Get a shell to the running container:
$ kubectl exec -it mydeployment-65gvm -n mynamespace -- sh
這個時候我們就可以進入running的container中調(diào)試程序了
/ # echo $MYAPP_DEBUG
true
/ #