多个pod共享一个volume

1.yaml文件

[root@k8s-matser01 nfs.rbac]# cat  nginx.yaml 
apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  selector:
    app: nginx
  type: ClusterIP
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nginx-share-pvc
spec:
  storageClassName: managed-nfs-storage 
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment 
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: nginx # has to match .spec.template.metadata.labels
  replicas: 3 # by default is 1
  template:
    metadata:
      labels:
        app: nginx # has to match .spec.selector.matchLabels
    spec:
      containers:
      - name: nginx
        image: nginx 
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
      volumes:
        - name: www
          persistentVolumeClaim:
            claimName: nginx-share-pvc

其他容器查看

[root@k8s-matser01 ~]# kubectl get pod |grep web
web-7bf54cbc8d-b2wwp                      1/1     Running   0          8m10s
web-7bf54cbc8d-pz4f4                      1/1     Running   0          8m10s
web-7bf54cbc8d-tw9wg                      1/1     Running   0          8m10s

[root@k8s-matser01 ~]# kubectl exec -it web-7bf54cbc8d-b2wwp -- /bin/bash

root@web-7bf54cbc8d-pz4f4:/# echo $(date) > /usr/share/nginx/html/index.html

root@web-7bf54cbc8d-pz4f4:/# cat /usr/share/nginx/html/index.html
Mon Nov 15 05:33:56 UTC 2021

3.其他容器查看

[root@k8s-matser01 ~]# kubectl exec web-7bf54cbc8d-pz4f4 -- cat /usr/share/nginx/html/index.html
Mon Nov 15 05:33:56 UTC 2021

[root@k8s-matser01 ~]# kubectl exec web-7bf54cbc8d-tw9wg -- cat /usr/share/nginx/html/index.html
Mon Nov 15 05:33:56 UTC 2021

原文地址:https://www.cnblogs.com/Applogize/p/15555892.html