Kubernetes部分Volume类型介绍及yaml示例--NFS(网络数据卷)
- 说明 nfs 卷允许将现有的 NFS(网络文件系统)共享挂载到你的容器中。不像 emptyDir,当删除 Pod 时,nfs 卷的内容被保留,卷仅仅是被卸载。这意味着 NFS 卷可以预填充数据,并且可以在 pod 之间“切换”数据。 NFS 可以被多个写入者同时挂载。
- 部署NFS服务器 关于NFS的部署,请参考NFS部署及优化(一)
- 实战pod内的文件共享
#创建yaml文件
cat >> nginx_volume.yaml << EOF
apiVersion: v1
kind: Pod
metadata:
name: nginx-test
namespace: test
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
volumeMounts:
#Mount the path to the container
- mountPath: "/tmp/"
name: pv0003
volumes:
- name: pv0003
nfs:
#fixed:This ip is the address of the nfs server
server: 192.168.246.169
#fixed:This path is shared externally by the nfs server
path: "/data"
#启动nginx_volume.yaml
kubectl create -f ./nginx_volume.yaml
#查看Pod运行状态
kubectl get po -n test
NAME READY STATUS RESTARTS AGE
nginx-test 1/1 Running 0 29s
#进入容器查看共享卷volume
kubectl exec -it nginx-test /bin/bash --namespace=test
root@nginx-test:/# cd tmp
root@nginx-test:/tmp# ls
lost+found sys
##我们可以看到NFS服务器共享卷/data已经挂载进来了
- 实战多个Pod内的容器间共享
#创建deployment
cat >> deploy_volume.yaml << EOF
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-wtf
namespace: test
spec:
replicas: 2
template:
metadata:
labels:
app: wtf
spec:
containers:
- name: nginx-wtf
image: nginx:1.7.9
ports:
- containerPort: 81
volumeMounts:
- name: pv0003
mountPath: /tmp/
volumes:
- name: pv0003
nfs:
server: 192.168.246.169
path: "/data/"
#启动deployment
kubectl create -f ./deploy_volume.yaml
#查看Pods的运行状态
NAME READY STATUS RESTARTS AGE
nginx-wtf-5774b87bdc-n97gr 1/1 Running 0 32m
nginx-wtf-5774b87bdc-xpsnr 1/1 Running 0 32m
#进入容器查看共享卷volume
kubectl exec -it nginx-wtf-5774b87bdc-n97gr /bin/bash --namespace=test
root@nginx-wtf-5774b87bdc-n97gr:/# cd tmp
root@nginx-wtf-5774b87bdc-n97gr:/tmp# ls
lost+found sys
- 实战volume的操作
#查看下当前命名空间运行有挂载NFS卷的Pods
kubectl get po -n test
NAME READY STATUS RESTARTS AGE
nginx-test 1/1 Running 0 2m
nginx-wtf-5774b87bdc-n97gr 1/1 Running 0 36m
nginx-wtf-5774b87bdc-xpsnr 1/1 Running 0 36m
##在命名空间test里运行有三个Pods
#在NFS服务端对共享卷进行操作
##创建目录datagrand
mkdir /data/datagrand
#查看各Pod中容器内卷的变化
##nginx-test
kubectl exec -it nginx-test /bin/bash --namespace=test
root@nginx-test:/# cd tmp
root@nginx-test:/tmp# ls
datagrand lost+found sys
##nginx-wtf-5774b87bdc-n97gr
kubectl exec -it nginx-wtf-5774b87bdc-n97gr /bin/bash --namespace=test
root@nginx-wtf-5774b87bdc-n97gr:/# cd tmp
root@nginx-wtf-5774b87bdc-n97gr:/tmp# ls
datagrand lost+found sys
##nginx-wtf-5774b87bdc-xpsnr
kubectl exec -it nginx-wtf-5774b87bdc-xpsnr /bin/bash --namespace=test
root@nginx-wtf-5774b87bdc-xpsnr:/# cd tmp
root@nginx-wtf-5774b87bdc-xpsnr:/tmp# ls
datagrand lost+found sys
##说明:我们可以看到各Pod中容器内卷已有datagrand