現需構建一個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