【K8s】Nginx容器自定義啓動命名無法重定向文件

現需構建一個Pod(含兩個容器),代碼中通過指定的主機名實現彼此的訪問,現需給nginx添加硬解析。重新定義啓動命令。

錯誤定義:

apiVersion: v1
kind: Pod
metadata:
  name: test-2
  labels:
    name: test2
spec:
  volumes:
  - name: uwsgi-config
    configMap:
      name: uwsgi-1-alpine-conf  
  containers:
  - name: main
    image: nginx 
    command: ["/bin/bash"]
    args:
    - -c 
    - echo test;echo 127.0.0.1 os >>/etc/hosts;nginx -g 'daemon off;'  !使用該命名將無法無法重定向內容到文件
    volumeMounts:
    - name: uwsgi-config
      mountPath: /mnt/default.conf
      subPath: default.conf
    - name: uwsgi-config
      mountPath: /tmp/server.py
      subPath: server.py
  - name: os
    image: centos
    command: ["/bin/bash"]
    args:
    - -c 
    - echo test;echo 127.0.0.1 os >>/etc/hosts;tail -f /dev/null

正確定義

apiVersion: v1
kind: Pod
metadata:
  name: test-2
  labels:
    name: test2
spec:
  volumes:
  - name: uwsgi-config
    configMap:
      name: uwsgi-1-alpine-conf  
  containers:
  - name: main
    image: nginx
    command: ["/bin/bash"]
    args:
    - -c 
    - echo 127.0.0.1 os >>/etc/hosts;nginx;tail -f /dev/null !nginx去掉啓動參數
    volumeMounts:
    - name: uwsgi-config
      mountPath: /mnt/default.conf
      subPath: default.conf
    - name: uwsgi-config
      mountPath: /tmp/server.py
      subPath: server.py
  - name: os
    image: centos
    command: ["/bin/bash"]
    args:
    - -c 
    - echo 127.0.0.1 os >>/etc/hosts;tail -f /dev/null

雖然這種方式啓動的容器,主進程不是nginx,nginx掛了容器也不會重啓,但可通過添加livenessProbe屬性監聽。
方法二:加入while循環判斷

command: ["/bin/bash"]
        # args: ["-c","nginx;while [ $? -ne 0 ];do sleep 1;echo '127.0.0.1 php'>>/etc/hosts;nginx;done;tail -f /dev/null"]
args:
- -c
- |
  nginx
  while [ $? -ne  0 ]
  do
       sleep 1
       echo '127.0.0.1 php' >> /etc/hosts
       nginx -g 'daemon off;'
  done
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章