如何在Kubernetes中調(diào)試CrashloopBackOff pod

很多時候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
/ #
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容