k8s-Volume使用说明

Volume常用的几种卷:
  1.emptyDir:本地临时卷:
  2.hostPath:本地卷
  3.NFS:共享卷
  4.configmap: 配置文件

emptyDir: 当 Pod 被分配给节点时,首先创建 emptyDir 卷,并且只要该 Pod 在该节点上运行,该卷就会存在。正如卷的名字所述,它最初是空的。
Pod 中的容器可以读取和写入 emptyDir 卷中的相同文件,尽管该卷可以挂载到每个容器中的相同或不同路径上。当出于任何原因从节点中删除或重构Pod时,emptyDir 中的数据将被永久删除。

[root@localhost7C case3]# cat  deploy_empty.yml 
#apiVersion: extensions/v1beta1
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: linux39
spec:
  replicas: 1
  selector:
    matchLabels: 
      app: ng-deploy-80
  template:
    metadata:
      labels:
        app: ng-deploy-80
    spec:
      containers:
      - name: ng-deploy-80
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:        #容器卷
        - mountPath: /cache  #容器路径 ,会自动创建
          name: cache-volume #调用宿主机名
      volumes:               #node节点宿主机卷  
      - name: cache-volume   #node节点宿主机名,也是物理路径名称,会自动创建
        emptyDir: {}         #启用emptyDir功能 



#测试方法 在容器中创建一个文件,在节点上查找(/var/lib/kubelet/pods/) 。
[root@localhost7F k8s]# kubectl  apply  -f deploy_empty.yml 
deployment.apps/nginx-deployment created

[root@localhost7F k8s]# kubectl get pod -A -o wide
linux39      nginx-deployment-7cc86d98d5-csnx8       1/1    Running       0      47s     10.20.2.4       localhost7f.localdomain   <none>        <none>
[root@localhost7F k8s]# 
[root@localhost7F k8s]# kubectl exec -it -n linux39  nginx-deployment-7cc86d98d5-csnx8 bash
root@nginx-deployment-7cc86d98d5-csnx8:/# echo xxxxxx >> /cache/file.txt
root@nginx-deployment-7cc86d98d5-csnx8:/# ls  /cache/
file.txt

#宿主机
[root@localhost7J ~]# find /var/lib/kubelet/  -name file.txt
/var/lib/kubelet/pods/ce59b450-18b6-423b-8d1f-03afce8b8c54/volumes/kubernetes.io~empty-dir/cache-volume/file.txt       
[root@localhost7J ~]# cat /var/lib/kubelet/pods/ce59b450-18b6-423b-8d1f-03afce8b8c54/volumes/kubernetes.io~empty-dir/cache-volume/file.txt
xxxxxx

[root@localhost7F k8s]# kubectl  delete  -f deploy_empty.yml 
deployment.apps "nginx-deployment" deleted

#文件没有
[root@localhost7J ~]# ll /var/lib/kubelet/pods/
hostPath:hostPath 卷将主机节点的文件系统中的文件或目录挂载到集群中,pod删除的时候,卷不会被删除


#apiVersion: extensions/v1beta1
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ng-deploy-80
  template:
    metadata:
      labels:
        app: ng-deploy-80
    spec:
      containers:
      - name: ng-deploy-80
        image: nginx 
        ports:
        - containerPort: 80
        volumeMounts:               #容器卷
        - mountPath: /data/mysql    #容器路径 ,会自动创建
          name: data-volume         #调用宿主机名
      volumes:                      #node节点宿主机卷 
      - name: data-volume           #node节点宿主机名
        hostPath:                   #启用hostpath功能 
          path: /data/mysql         #node节点物理路径名称,会自动创建


#测试方法 在容器中创建一个文件,在节点上查找(/var/lib/kubelet/pods/) 。
#思考:如果pod重构后在另一个节点,数据文件会复制过去吗?答案是不能。
#思考:根据第一个问题,文件是不复制过去, 可以使用nodeSelector节点筛选器,使pod只运行某一节点。还有一方案是使用NFS共享卷。

[root@localhost7C k8s]# kubectl  apply -f deploy_hostPath.yml 
deployment.apps/nginx-deployment created
[root@localhost7C k8s]# kubectl get pod -A -o wide
NAMESPACE         NAME                           READY   STATUS              RESTARTS   AGE     IP               NODE         NOMINATED NODE   READINESS GATES
default   nginx-deployment-7944748bc4-f9vvl        1/1     Running            0       34s     10.20.2.5   localhost7f.localdomain   <none>           <none>


[root@localhost7C k8s]C kubectl  exec -it nginx-deployment-7944748bc4-f9vvl  bash
root@nginx-deployment-7944748bc4-f9vvl:/# echo aaaa >> /data/mysql/file.txt

#文件在
[root@localhost7F~]# cat  /data/mysql/file.txt 
aaaa

#drain命令:用于在维护期间排除节点,驱逐node上的业务pod,用于node节点下线。
[root@localhost7C k8s]# kubectl drain localhost7f.localdomain
[root@localhost7C case3]# kubectl get pods  -o wide
NAME                                READY   STATUS    RESTARTS   AGE   IP           NODE                      NOMINATED NODE   READINESS GATES
nginx-deployment-7944748bc4-dn7ss   1/1     Running   0          96s   10.10.4.28   localhost7g.localdomain   <none>           <none>

#没有文件
[root@localhost7G ~]# ll /data/mysql/


[root@localhost7C k8s]C kubectl  exec -it nginx-deployment-7944748bc4-dn7ss  bash
root@nginx-deployment-7944748bc4-f9vvl:/# echo aaaa >> /data/mysql/file.txt

[root@localhost7C k8s]# kubectl  delete  -f  deploy_hostPath.yml 
deployment.apps "nginx-deployment" deleted

#pod删除文件还在
[root@localhost7J ~]# cat  /data/mysql/file.txt 
aaaa
NFS:  nfs卷允许将现有的 NFS(网络文件系统)共享挂载到您的容器中。不像 emptyDir,当删除 Pod 时,nfs 卷的内容被保留,卷仅仅是
被卸载。这意味着 NFS 卷可以预填充数据,并且可以在 pod 之间“切换”数据。 NFS 可以被多个写入者同时挂载。

#apiVersion: extensions/v1beta1
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ng-deploy-80
  template:
    metadata:
      labels:
        app: ng-deploy-80
    spec:
      containers:
      - name: ng-deploy-80
        image: nginx 
        ports:
        - containerPort: 80
        volumeMounts:                                #启用卷挂载
        - mountPath: /usr/share/nginx/html/mysite    #容器挂载路径,mysite文件夹会自动创建。
          name: my-nfs-volume                        #卷名,调用NFS共享名

        - mountPath: /data/nginx/html
          name: linux39-nfs-volume 
      volumes:
      - name: my-nfs-volume                     #随机的NFS共享名
        nfs:
          server: 192.168.80.110                #NFS地址
          path: /nfs/datanfs                    #NFS路径名称,先创建。

      - name: linux39-nfs-volume
        nfs:
          server: 192.168.80.110
          path: /data/linux39


---
apiVersion: v1
kind: Service
metadata:
  name: ng-deploy-80
spec:
  ports:
  - name: http
    port: 81
    targetPort: 80
    nodePort: 30016
    protocol: TCP
  type: NodePort
  selector:
    app: ng-deploy-80




#查看下node宿主机的挂载情况。
#测试中发现,多个pod可以同时挂载同一个NFS
#测试中发现,NFS的地址范围除了pod地址,也要包括node节点的地址,因为NFS挂载是先挂载node宿主机,再node宿主机挂载到pod中的。

#安装NFS
[root@localhost7B ~]#yum install nfs-utils.x86_64  -y
[root@localhost7B ~]#systemctl  start  nfs.service 

[root@localhost7B ~]# mkdir /nfs/datanfs  -p
[root@localhost7B ~]# vim /etc/exports
/nfs/datanfs  *(rw)

[root@localhost7B ~]# systemctl  restart  nfs.service 

[root@localhost7B ~]# echo nginx config file >  /nfs/datanfs/nginx.conf


#另一机子测试
showmount  -e 192.168.80.110
mount  192.168.80.100:/nfs/datanfs   /mnt/cdrom/
umount /mnt/cdrom/



[root@localhost7F k8s]# kubectl  apply  -f deploy_nfs.yml 
deployment.apps/nginx-deployment created
service/ng-deploy-80 created
[root@localhost7F k8s]# kubectl  get pod -A
NAMESPACE              NAME                                         READY   STATUS             RESTARTS   AGE
default                nginx-deployment-76567bccb4-zg765            1/1     Running            0          64s

[root@localhost7F k8s]# kubectl exec  -it  nginx-deployment-76567bccb4-zg765 bash
root@nginx-deployment-76567bccb4-zg765:/# ll /usr/share/nginx/html/
50x.html    index.html  mysite/     
root@nginx-deployment-76567bccb4-zg765:/# cat  /usr/share/nginx/html/mysite/nginx.conf 
nginx config file

#容器的挂载信息
root@nginx-deployment-76567bccb4-zg765:/# mount   | grep datanfs
192.168.80.110:/nfs/datanfs on /usr/share/nginx/html/mysite type nfs4 (rw,relatime,vers=4.1,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.80.150,local_lock=none,addr=192.168.80.110)

#宿主机的挂载信息
[root@localhost7F ~]# mount | grep datanfs
192.168.80.110:/nfs/datanfs on /var/lib/kubelet/pods/bdae2519-de34-4219-8314-38caad08673a/volumes/kubernetes.io~nfs/my-nfs-volume type nfs4 (rw,relatime,vers=4.1,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.80.150,local_lock=none,addr=192.168.80.110)

 

configmap:将配置信息放到configmap对象中,然后在pod的对象中导入configmap对象,实现导入配置的操作
声明一个ConfigMap的对象,作为Volume挂载到pod中

[root@localhost7B k8s]# vim   case4/deploy_configmap.yml 

apiVersion: v1
kind: ConfigMap
metadata:
  # configmap名称,pod会调用。
  name: nginx-config1
data:
 default: |  #数据键名,可以有多个,pod会调用。
    server {
       listen       80;
       server_name  www.mysite.com;
       index        index.html;

       location / {
           root /data/nginx/html; #hostPath 路径
           if (!-e $request_filename) {
               rewrite ^/(.*) /index.html last;
           }
       }
    }
---
#apiVersion: extensions/v1beta1
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ng-deploy-80
  template:
    metadata:
      labels:
        app: ng-deploy-80
    spec:
      containers:
      - name: ng-deploy-80
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /data/nginx/html  #hostPath Pod路径
          name: nginx-static-dir       #hostPath名

        - name: nginx-config2  #调用configmap2,
          mountPath:  /etc/nginx/conf.d  #存放路径 :配置文件最终存在/etc/nginx/conf.d/mysite.conf 

      volumes:
      - name: nginx-static-dir   #hostPath名
        hostPath:
          path: /data/nginx/linux39  #hostPath物理路径


      - name: nginx-config2  #configmap2名 
        configMap:
          name: nginx-config1  #  调用数据的名称
          items:
             - key: default # 调用数据键名
               path: mysite.conf  #配置文件存放的文件名,最终是存放在pod内的。

---
apiVersion: v1
kind: Service
metadata:
  name: ng-deploy-80
spec:
  ports:
  - name: http
    port: 81
    targetPort: 80
    nodePort: 30016
    protocol: TCP
  type: NodePort
  selector:
    app: ng-deploy-80




#查看下node宿主机的挂载情况。
#测试中发现,多个pod可以同时挂载同一个NFS
#测试中发现,NFS的地址范围除了pod地址,也要包括node节点的地址,因为NFS挂载是先挂载node宿主机,再node宿主机挂载到pod中的。

#安装NFS
[root@localhost7B ~]#yum install nfs-utils.x86_64  -y
[root@localhost7B ~]#systemctl  start  nfs.service 

[root@localhost7B ~]# mkdir /nfs/datanfs  -p
[root@localhost7B ~]# vim /etc/exports
/nfs/datanfs  *(rw)

[root@localhost7B ~]# systemctl  restart  nfs.service 

[root@localhost7B ~]# echo nginx config file >  /nfs/datanfs/nginx.conf


#另一机子测试
showmount  -e 192.168.80.110
mount  192.168.80.100:/nfs/datanfs   /mnt/cdrom/
umount /mnt/cdrom/





[root@localhost7F k8s]# kubectl  apply  -f deploy_nfs.yml 
deployment.apps/nginx-deployment created
service/ng-deploy-80 created
[root@localhost7F k8s]# kubectl  get pod -A
NAMESPACE              NAME                                         READY   STATUS             RESTARTS   AGE
default                nginx-deployment-76567bccb4-zg765            1/1     Running            0          64s

[root@localhost7F k8s]# kubectl exec  -it  nginx-deployment-76567bccb4-zg765 bash
root@nginx-deployment-76567bccb4-zg765:/# ll /usr/share/nginx/html/
50x.html    index.html  mysite/     
root@nginx-deployment-76567bccb4-zg765:/# cat  /usr/share/nginx/html/mysite/nginx.conf 
nginx config file

#容器的挂载信息
root@nginx-deployment-76567bccb4-zg765:/# mount   | grep datanfs
192.168.80.110:/nfs/datanfs on /usr/share/nginx/html/mysite type nfs4 (rw,relatime,vers=4.1,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.80.150,local_lock=none,addr=192.168.80.110)

#宿主机的挂载信息
[root@localhost7F ~]# mount | grep datanfs
192.168.80.110:/nfs/datanfs on /var/lib/kubelet/pods/bdae2519-de34-4219-8314-38caad08673a/volumes/kubernetes.io~nfs/my-nfs-volume type nfs4 (rw,relatime,vers=4.1,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.80.150,local_lock=none,addr=192.168.80.110)




实例2:将ConfigMap中的数据设置为容器的环境变量

apiVersion: v1
kind: ConfigMap

#定义configmap名称,
metadata:
  name: nginx-config
data:
  username: user1   #pod中变量的值,可以定义多个。
   


---
#apiVersion: extensions/v1beta1
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ng-deploy-80
  template:
    metadata:
      labels:
        app: ng-deploy-80
    spec:
      containers:
      - name: ng-deploy-80
        image: nginx 
        env:
        - name: MY_USERNAME  #pod中定义的变量名
          valueFrom:
            configMapKeyRef:      #
              name: nginx-config  #调用configmap名称
              key: username
        ports:
        - containerPort: 80


[root@localhost7F k8s]# kubectl apply  -f deploy_configmap.yml 
root@nginx-deployment-669d88577-vf4nh:/# kubectl  exec -it nginx-deployment-669d88577-vf4nh bash

root@nginx-deployment-669d88577-vf4nh:/# echo $MY_USERNAME
user1

 

posted @ 2023-03-07 16:34  yuanbangchen  阅读(137)  评论(0编辑  收藏  举报