Volume:
Kubernetes中的Volume提供了在容器中挂载外部存储的能力
Pod需要设置卷来源(spec.volume)和挂载点(spec.containers.volumeMounts)两个信息后才可以使用相应的Volume

官方参考地址:https://kubernetes.io/docs/concepts/storage/volumes/

1、emptyDir

创建一个空卷,挂载到Pod中的容器。Pod删除该卷也会被删除。
应用场景:Pod中容器之间数据共享

[root@master01 volume]# cat emptyDir.yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
#  imagePullSecrets:
#  - name: registry-pull-secret
  containers:
  - name: write   #一个写容器
    image: 10.192.27.111/library/busybox:1.28.4
    command: ["sh","-c","for i in {1..100};do echo $i >> /data/hello;sleep 1;done"]
    volumeMounts:
      - name: data  #使用数据卷  名字与下面一致
        mountPath: /data
  - name: read  #一个读容器
    image: 10.192.27.111/library/centos:7
    command: ["bash","-c","tail -f /data/hello"]
    volumeMounts:
      - name: data   #使用数据卷 名字与下面一致
        mountPath: /data
  volumes:
  - name: data  #新建一个数据卷
    emptyDir: {}
[root@master01 volume]# 
2、hostPath

挂载Node文件系统上文件或者目录到Pod中的容器。
应用场景:Pod中容器需要访问宿主机文件

[root@master01 volume]# cat hostpath.yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: busybox
    image: 10.192.27.111/library/busybox:1.28.4
    args:
    - /bin/sh
    - -c
    - sleep 36000
    volumeMounts:
    - name: data
      mountPath: /data
  volumes:
  - name: data
    hostPath:
      path: /tmp
      type: Directory
[root@master01 volume]#


[root@master01 volume]# kubectl apply -f hostpath.yaml
[root@master01 volume]# kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
my-pod                              1/1     Running   0          10s
[root@master01 volume]# kubectl get pod -o wide
NAME                                READY   STATUS    RESTARTS   AGE     IP            NODE            NOMINATED NODE   READINESS GATES
my-pod                              1/1     Running   0          2m19s   172.17.43.2   10.192.27.115   <none>           <none>   #查看挂载在哪个节点
[root@master01 volume]# kubectl exec -it my-pod sh  #pod里面的
/ # ls /data/
systemd-private-16f6a1d6621b4a6d96d2c21f44c6bff3-chronyd.service-6Evm1r
/ #
[root@node01 ~]# ls /tmp/  #node01 10.192.27.115上面的  两者一样
systemd-private-16f6a1d6621b4a6d96d2c21f44c6bff3-chronyd.service-6Evm1r
[root@node01 ~]# 
3、nfs
#各个node节点安装NFS
[root@node01 cfg]# yum -y install nfs-utils
[root@node02 cfg]# yum -y install nfs-utils


[root@master01 volume]# cat nfs.yaml
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: 10.192.27.111/library/nginx:latest
        volumeMounts:
        - name: wwwroot  #使用数据卷
          mountPath: /usr/share/nginx/html
        ports:
        - containerPort: 80
      volumes:
      - name: wwwroot  #数据卷
        nfs:
          server: 10.192.27.113
          path: /home/kdzd
[root@master01 volume]#
[root@master01 volume]# kubectl apply -f nfs.yaml
deployment.apps/nginx-deployment created
[root@master01 volume]# kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-5dfc75bdc4-5wjmj   1/1     Running   0          16s
nginx-deployment-5dfc75bdc4-q5mrz   1/1     Running   0          16s
nginx-deployment-5dfc75bdc4-qkmxk   1/1     Running   0          16s
[root@master01 volume]# kubectl get ep
NAME         ENDPOINTS                               AGE
kubernetes   10.192.27.100:6443,10.192.27.114:6443   18d
[root@master01 volume]# kubectl get pod -o wide
NAME                                READY   STATUS    RESTARTS   AGE     IP            NODE            NOMINATED NODE   READINESS GATES
nginx-deployment-5dfc75bdc4-5wjmj   1/1     Running   0          2m23s   172.17.43.3   10.192.27.115   <none>           <none>
nginx-deployment-5dfc75bdc4-q5mrz   1/1     Running   0          2m23s   172.17.43.2   10.192.27.115   <none>           <none>
nginx-deployment-5dfc75bdc4-qkmxk   1/1     Running   0          2m23s   172.17.46.4   10.192.27.116   <none>           <none>

#测试一下
[root@nfs-server ~]# cd /home/kdzd/
[root@nfs-server kdzd]# vim index.html
[root@nfs-server kdzd]# cat index.html   #10.192.27.113
<h1>hello world!</h1>
[root@nfs-server kdzd]#


[root@node02 cfg]# curl 172.17.43.2
<h1>hello world!</h1>

[root@node02 cfg]# curl 172.17.43.3
<h1>hello world!</h1>

[root@node02 cfg]# curl 172.17.46.4
<h1>hello world!</h1>
12-20 10:10