①. 基本概念与NFS环境搭建
①. 在docker里面可以将容器里面的文件挂载在外面,可以很好的修改文件。在k8s中,如果我们使用目录挂载,当node1节点中的pod挂了,会发生故障转移到别的node2上面,这时node2中并没有node1中的文件。这时引入了NFS网络文件系统,当master节点的文件改变了,会同步到node节点,node节点文件发生了改变,也会同步到master
②. 安装NFS
# (1). 所有节点 yum install -y nfs-utils #(2). 主节点 #nfs主节点 echo "/nfs/data/ *(insecure,rw,sync,no_root_squash)" > /etc/exports mkdir -p /nfs/data systemctl enable rpcbind --now systemctl enable nfs-server --now #配置生效 exportfs -r # (3). 从节点 showmount -e 192.168.68.144 #执行以下命令挂载 nfs 服务器上的共享目录到本机路径 /root/nfsmount mkdir -p /nfs/data mount -t nfs 192.168.68.144:/nfs/data /nfs/data # (4). 在主节点 写入一个测试文件 echo "hello nfs server" > /nfs/data/test.txt # 去node节点查看/nfs/data/下是不是有一个test.txt
②. Deployment使用NFS进行挂载
- ①. 创建文件夹 cd /nfs/data/html,在master节点执行 kubectl apply -f deploy.yaml(yaml文件内容如下)
apiVersion: apps/v1 kind: Deployment metadata: labels: app: nginx-pv-demo name: nginx-pv-demo spec: replicas: 2 selector: matchLabels: app: nginx-pv-demo template: metadata: labels: app: nginx-pv-demo spec: containers: - image: nginx name: nginx volumeMounts: - name: html mountPath: /usr/share/nginx/html volumes: - name: html nfs: server: 192.168.68.144 path: /nfs/data/nginx-pv
[root@k8smaster nginx-pv]# echo "hello NFS " >index.html [root@k8smaster nginx-pv]# kubectl get pods -owide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES hello-server-6cbb679d85-pxwld 1/1 Running 1 3h56m 11.168.249.22 k8snode1 <none> <none> hello-server-6cbb679d85-rslbx 1/1 Running 1 3h56m 11.168.185.223 k8snode2 <none> <none> my-dep-01-54d8bdbf78-4xmbj 1/1 Running 1 4h54m 11.168.185.220 k8snode2 <none> <none> my-dep-01-54d8bdbf78-h2w7x 1/1 Running 1 4h54m 11.168.185.222 k8snode2 <none> <none> my-dep-01-54d8bdbf78-xfrm2 1/1 Running 1 4h54m 11.168.249.25 k8snode1 <none> <none> mynginx 1/1 Running 1 136m 11.168.185.221 k8snode2 <none> <none> nginx-demo-7d56b74b84-mdcks 1/1 Running 1 3h56m 11.168.185.224 k8snode2 <none> <none> nginx-demo-7d56b74b84-rpzvn 1/1 Running 1 3h56m 11.168.249.26 k8snode1 <none> <none> nginx-pv-demo-bc8c96c6b-6jwtx 1/1 Running 0 97s 11.168.185.227 k8snode2 <none> <none> nginx-pv-demo-bc8c96c6b-jb6d8 1/1 Running 0 97s 11.168.249.28 k8snode1 <none> <none> [root@k8smaster nginx-pv]# curl 11.168.185.227 hello NFS [root@k8smaster nginx-pv]#
- ②. 缺点
- 需要自己手动创建/nfs/data/html文件夹
- 如果pod不要了,这个时候删除了node节点中的pod,这个时候的html中文件不会随着pod的删除而删除