我有一个 K8s 集群,其中有一个主节点和一个工作节点。场景 – 1 我正在同一个 POD 中部署一个简单的 sidecar 容器,其中我已将 emptyDir 定义为共享卷,并且它运行良好。清单文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: pod-with-sidecar
namespace: test
spec:
replicas: 1
selector:
matchLabels:
app: debian-nginx
template:
metadata:
labels:
app: debian-nginx
spec:
containers:
- name: main-debian
image: debian
command: ["/bin/sh"]
args: ["-c", "while true; do date >> /var/log/index.html; sleep
2;done"]
volumeMounts:
- mountPath: /var/log
name: shared-log
- name: sidecar-nginx
image: nginx
volumeMounts:
- mountPath: /usr/share/nginx/index.html
name: shared-log
volumes:
- name: shared-log
emptyDir: {}
在容器 – 1 中创建文件,内容可用,/car/log/index.html
同样在容器 – 2 中创建文件,内容可用usr/share/nginx/html/index.html
然而,在场景 2 中,我在同一个 POD 中部署了相同的简单 sidecar 容器,其中我将 hosPath 定义为共享卷,但它不起作用。清单文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: pod-with-sidecar
namespace: test
spec:
replicas: 1
selector:
matchLabels:
app: debian
app: nginx
template:
metadata:
labels:
app: debian
app: nginx
spec:
containers:
# Main application container
- name: main-debian
image: debian
command: ["/bin/sh", "-c"]
args: ["while true; do data >> /var/log/index.html; sleep 2;done"]
volumeMounts:
- mountPath: /var/log
name: shared-vol
# Sidecar container
- name: sidecar-nginx
image: nginx:1.7.9
ports:
- containerPort: 80
volumeMounts:
- mountPath: /usr/share/nginx/html # nginx-specific mount path
name: shared-vol
volumes:
- name: shared-vol
hostPath:
path: /mydata
type: DirectoryOrCreate
在容器 – 1 中创建了文件,但没有可用的内容/car/log/index.html
testuser@kmasterl:~$ kubectl exec -it pod-with-sidecar-85f88c9d5d-xjcgh -c main-debian -n test -- /bin/bash
root@pod-with-sidecar-85f88c9d5d-xjcgh:/# ls -lrt var/log
total 0
-rw-r--r-- 1 root root 0 Dec 14 21:28 index.html
root@pod-with-sidecar-85f88c9d5d-xjcgh:/# cat /var/log/index.html
root@pod-with-sidecar-85f88c9d5d-xjcgh:/#
同样,在容器-2中创建了文件,但没有可用的内容usr/share/nginx/html/index.html
testuser@kmasterl:~$ kubectl exec -it pod-with-sidecar-85f88c9d5d-xjcgh -c sidecar-nginx -n test -- /bin/bash
root@pod-with-sidecar-85f88c9d5d-xjcgh:/# ls -lrt /usr/share/nginx/html
total 0
-rw-r--r-- 1 root root 0 Dec 14 21:28 index.html
root@pod-with-sidecar-85f88c9d5d-xjcgh:/# cat usr/share/nginx/html/index.html
root@pod-with-sidecar-85f88c9d5d-xjcgh:/#
有人能解释为什么它不工作吗主机路径被定义为共享卷??