K8S k8s(6)__数据共享 发表于 2022-09-02 浏览量 393 没有评论 # K8S的数据共享 EmptyDir:同一个Pod中的容器实现数据共享。Pod删除,数据丢弃 HostDir:同一个Pod中容器实现数据共享。数据放在宿主机,有数据永久存储。但迁移pod的时候数据不会随着迁移 NFS:容器数据共享,Pod迁移后数据还在 # EmptyDir方式多个容器临时数据共享 ``` cat create.yml apiVersion: v1 kind: ReplicationController metadata: name: nginx spec: replicas: 1 template: metadata: name: nginx labels: app: nginx spec: containers: - name: mynginx imagePullPolicy: Always image: 192.168.237.50:5000/shijiange:nginx volumeMounts: - name: storage mountPath: /tmp/storage - name: myredis imagePullPolicy: Always image: 192.168.237.50:5000/shijiange:redis volumeMounts: - name: storage mountPath: /tmp/storage volumes: - name: storage emptyDir: {} ``` 同一Pod中容器间的文件共享,Pod删除后文件丢失,无法实现文件共享 # K8S容器数据共享HostDir ``` cat create.yml apiVersion: v1 kind: ReplicationController metadata: name: nginx spec: replicas: 1 template: metadata: name: nginx labels: app: nginx spec: containers: - name: mynginx imagePullPolicy: Always image: 192.168.237.50:5000/shijiange:nginx volumeMounts: - name: storage mountPath: /tmp/storage - name: myredis imagePullPolicy: Always image: 192.168.237.50:5000/shijiange:redis volumeMounts: - name: storage mountPath: /tmp/storage volumes: - name: storage hostPath: path: /usr/local/kubernetes/storage ``` 文件只保存在容器所有的宿主机,Pod转移服务器后文件会保存,但不会跟着转移 # NFS方式数据共享 ``` cat create.yml apiVersion: v1 kind: ReplicationController metadata: name: nginx spec: replicas: 1 template: metadata: name: nginx labels: app: nginx spec: containers: - name: mynginx imagePullPolicy: Always image: 192.168.237.50:5000/shijiange:nginx - name: myredis imagePullPolicy: Always image: 192.168.237.50:5000/shijiange:redis volumeMounts: - name: storage mountPath: /tmp/storage volumes: - name: storage nfs: server: 192.168.237.50 path: /usr/local/kubernetes/storage ``` - NFS共享服务器搭建 ``` yum install nfs-utils -y vim /etc/exports /usr/local/kubernetes/storage 192.168.0.0/16(rw,no_root_squash) ``` - NFS服务器的启动 ``` systemctl restart rpcbind systemctl restart nfs ``` 注意事项说明 注意需要在宿主机中安装`yum install nfs-utils -y` Pod转移到其它服务器文件也会跟着转移,因为文件是放到nfs服务器上