emptyDir

#定义yaml文件,创建容器
[root@k8s-master-01 volumeTest]# cat podEmptyDir.yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: www04
  name: www04
spec:
  volumes:
  - name: v1  #定义的volume的名字
    emptyDir: {} #随机定义宿主机中的挂载目录
  containers:
  - image: nginx
    imagePullPolicy: IfNotPresent
    name: www04
    resources: {}
    volumeMounts:
    - name: v1 #与预先定义的volumes名字相同
      mountPath: /data  #容器中的挂载点
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}
[root@k8s-master-01 volumeTest]# kubectl apply -f podEmptyDir.yaml
pod/www04 created
[root@k8s-master-01 volumeTest]# kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
www04   1/1     Running   0          32s
#在调度到的节点查看容器信息
[root@k8s-node-01 ~]# docker ps -a | grep www04
4181d673e28b   605c77e624dd                                        "/docker-entrypoint.…"   3 minutes ago   Up 3 minutes                         k8s_www04_www04_app01_bb2ba6e3-a88b-4f94-8700-310c57a453f4_0
c0d911e5b886   registry.aliyuncs.com/google_containers/pause:3.6   "/pause"                 3 minutes ago   Up 3 minutes                         k8s_POD_www04_app01_bb2ba6e3-a88b-4f94-8700-310c57a453f4_0
[root@k8s-node-01 ~]# docker inspect 4181d673e28b | grep data
                "/var/lib/kubelet/pods/bb2ba6e3-a88b-4f94-8700-310c57a453f4/volumes/kubernetes.io~empty-dir/v1:/data",
                "Destination": "/data",
[root@k8s-node-01 ~]# ls /var/lib/kubelet/pods/bb2ba6e3-a88b-4f94-8700-310c57a453f4/volumes/kubernetes.io~empty-dir/v1
[root@k8s-node-01 ~]# cd /var/lib/kubelet/pods/bb2ba6e3-a88b-4f94-8700-310c57a453f4/volumes/kubernetes.io~empty-dir/v1
[root@k8s-node-01 v1]# ls
[root@k8s-node-01 v1]#
#主节点进入容器并在/data目录创建文件,之后再查看node节点文件情况
[root@k8s-master-01 volumeTest]# kubectl exec -it www04 -- bash
root@www04:/# ls /data
root@www04:/# cd /data/
root@www04:/data# touch emptyDirTest.txt
root@www04:/data#
[root@k8s-node-01 v1]# ls
emptyDirTest.txt
#删除pod查看文件情况,emptyDir是以内存为介质的存储方式,故删除容器后文件也会消失。
[root@k8s-master-01 volumeTest]# kubectl delete pod www04
pod "www04" deleted
[root@k8s-node-01 v1]# cd /var/lib/kubelet/pods/bb2ba6e3-a88b-4f94-8700-310c57a453f4/volumes/kubernetes.io~empty-dir/v1
-bash: cd: /var/lib/kubelet/pods/bb2ba6e3-a88b-4f94-8700-310c57a453f4/volumes/kubernetes.io~empty-dir/v1: 没有那个文件或目录

 hostPath

#创建yaml文件并应用
[root@k8s-master-01 volumeTest]# cat podHostPath.yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: www04
  name: www04
spec:
  volumes:
  - name: v1
    hostPath:
      path: /hostPathTest
  containers:
  - image: nginx
    imagePullPolicy: IfNotPresent
    name: www04
    resources: {}
    volumeMounts:
    - name: v1
      mountPath: /data
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}
[root@k8s-master-01 volumeTest]# kubectl apply -f podHostPath.yaml
pod/www04 created
[root@k8s-master-01 volumeTest]# kubectl get pods -owide
NAME    READY   STATUS    RESTARTS   AGE   IP            NODE          NOMINATED NODE   READINESS GATES
www04   1/1     Running   0          20s   10.244.1.21   k8s-node-01   <none>           <none>
#在调度到的节点上可以看到/hostPathTest目录已被创建
[root@k8s-node-01 v1]# cd /hostPathTest/
[root@k8s-node-01 hostPathTest]#

NFS

#安装配置nfs
[root@k8s-node-01 ~]# yum install nfs-utils -y
[root@k8s-node-01 ~]# systemctl enable nfs-server.service --now
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@k8s-node-01 ~]# mkdir -p /nfs
[root@k8s-node-01 ~]# vim /etc/exports
[root@k8s-node-01 nfs]# cat /etc/exports
/nfs *(rw,no_root_squash)  #不压缩root权限
##刷新使nfs生效
[root@k8s-node-01 nfs]# exportfs -arv
exporting *:/nfs
##在worker上也需要安装nfs-utils。安装完后查看是否可访问
[root@k8s-node-01 nfs]# showmount -e 192.168.71.134
Export list for 192.168.71.134:
/nfs *
##挂载与umount测试,无任何报错说明服务正常
[root@k8s-node-01 nfs]# mount 192.168.71.134:/nfs /mnt ; umount /mnt
#创建使用nfs的容器
[root@k8s-master-01 volumeTest]# more podNfs.yaml

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: www04
  name: www04
spec:
  volumes:
  - name: nfsv1
    nfs:
      server: 192.168.71.134
      path: /nfs
  containers:
  - image: nginx
    imagePullPolicy: IfNotPresent
    name: www04
    resources: {}
    volumeMounts:
    - name: nfsv1
      mountPath: /data
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}
[root@k8s-master-01 volumeTest]# kubectl get pod -o wide
NAME    READY   STATUS    RESTARTS   AGE     IP            NODE          NOMINATED NODE   READINESS GATES
www04   1/1     Running   0          3m14s   10.244.1.41   k8s-node-01   <none>           <none>
#进入容器查看nfs挂在状况。挂载实际形式为宿主机挂载nfs目录后再在pod中挂载宿主机的目录。
[root@k8s-master-01 volumeTest]# kubectl exec -it www04 -- bash
root@www04:/# ls
bin  boot  data  dev  docker-entrypoint.d  docker-entrypoint.sh  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@www04:/# df -h
Filesystem               Size  Used Avail Use% Mounted on
overlay                   17G  8.0G  9.1G  47% /
tmpfs                     64M     0   64M   0% /dev
tmpfs                    3.9G     0  3.9G   0% /sys/fs/cgroup
192.168.71.134:/nfs       17G  8.0G  9.1G  47% /data
/dev/mapper/centos-root   17G  8.0G  9.1G  47% /etc/hosts
shm                       64M     0   64M   0% /dev/shm
tmpfs                    7.6G   12K  7.6G   1% /run/secrets/kubernetes.io/serviceaccount
tmpfs                    3.9G     0  3.9G   0% /proc/acpi
tmpfs                    3.9G     0  3.9G   0% /proc/scsi
tmpfs                    3.9G     0  3.9G   0% /sys/firmware
##在pod实际运行的worker节点查看挂载状态
[root@k8s-node-01 nfs]# df -h | grep 192
192.168.71.134:/nfs       17G  8.0G  9.1G   47% /var/lib/kubelet/pods/6d388500-0e70-4991-82f9-d21b82faed01/volumes/kubernetes.io~nfs/nfsv1
##进入容器创建文件后在nfs共享目录中查看
[root@k8s-master-01 volumeTest]# kubectl exec -it www04 -- bash
root@www04:/# touch /data/test.txt
root@www04:/#
[root@k8s-node-01 nfs]# ls
test.txt

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐