k8s-数据卷

2021-11-23  本文已影响0人  小李飞刀_lql

数据卷

为什么需要存储卷

容器部署过程中一般有以下三种数据:
001 启动时需要的初始数据,例如配置文件
002 启动过程中产生的临时数据,该临时数据需要多个容器间共享
003 启动过程中产生的持久化数据,例如MySQL的data目录
1637620041066.png

数据卷概述

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

数据卷大致分类

001 本地(hostPath,emptyDir等)
002 网络(NFS,Ceph,GlusterFS等)
003 公有云(AWS EBS等)
004 K8S资源(configmap,secret等)

数据卷:emptyDir

概述

emptyDir卷:是一个临时存储卷,与Pod生命周期绑定一起,如果Pod删除了卷也会被删除。

应用场景

Pod中容器之间数据共享

示例

apiVersion: v1
kind: Pod
metadata:
  name: emptydir-pod 
spec:
  containers:
  - name: write
    image: centos
    command: ["bash","-c","for i in {1..100};do echo $i >> /data/hello;sleep 1;done"]
    volumeMounts:
      - name: data
        mountPath: /data

  - name: read
    image: centos
    command: ["bash","-c","tail -f /data/hello"]
    volumeMounts:
      - name: data
        mountPath: /data
  
  volumes:
  - name: data
    emptyDir: {}
 ----------------------------------------------------------------------------

[root@k8smaster volume]# kubectl apply -f emptydir.yaml 
pod/emptydir-pod created
[root@k8smaster volume]# kubectl get pod
NAME           READY   STATUS    RESTARTS   AGE
emptydir-pod   2/2     Running   0          76s

[root@k8smaster volume]# kubectl exec -it emptydir-pod -c read bash
[root@emptydir-pod /]# tail -f /data/hello 
21
22
23
24
25
26
27
28

数据卷:hostPath

概述

hostPath卷:挂载Node文件系统(Pod所在节点)上文件或者目录到Pod中的容器。

应用场景

Pod中容器需要访问宿主机文件

示例:将宿主机/tmp目录挂载到容器/data目录

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: busybox
    image: busybox
    args:
    - /bin/sh
    - -c
    - sleep 36000
    volumeMounts:
    - name: data
      mountPath: /data
    - name: data2
      mountPath: /opt
  volumes:
  - name: data
    hostPath:
      path: /tmp
      type: Directory
  - name: data2
    hostPath: 
      path: /
      type: Directory
      
 ----------------------------------------------------------------------------
[root@k8smaster volume]# kubectl apply -f hostpath.yaml 
pod/my-pod created

[root@k8smaster volume]# kubectl get pod
NAME     READY   STATUS    RESTARTS   AGE
my-pod   1/1     Running   0          40s

[root@k8smaster volume]# kubectl exec -it my-pod -- sh
/ # cd data/
/data # ls
runc-process199876077 

[root@k8snode1 ~]# cd /tmp
[root@k8snode1 tmp]# ls
runc-process199876077  

#此时容器中目录和宿主机的目录已经绑定

数据卷:NFS

概述

NFS数据卷:提供对NFS挂载支持,可以自动将NFS共享路径挂载到Pod中
1637622473777.png

NFS

#NFS是一个主流的文件共享服务器

[root@k8smaster volume]# yum install nfs-utils
[root@k8smaster volume]# vi /etc/exports
/ifs/kubernetes *(rw,no_root_squash)

[root@k8smaster volume]# mkdir -p /ifs/kubernetes
[root@k8smaster volume]# systemctl start nfs
[root@k8smaster volume]#  systemctl enable nfs

注:每个Node上都要安装nfs-utils包

NFS挂载与卸载

#挂载 node挂载到master节点的服务器上
[root@k8snode1 ~]# mount -t nfs 192.168.153.21:/ifs/kubernetes /mnt
[root@k8snode1 ~]# cd /mnt
[root@k8snode1 mnt]# ls
[root@k8snode1 mnt]# touch a.txt

[root@k8smaster volume]# cd /ifs/kubernetes/
[root@k8smaster kubernetes]# ls
a.txt

[root@k8snode1 ~]# umount /mnt

示例:将网站程序通过NFS数据卷共享,让所有Pod使用

apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-nfs
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-nfs
  template:
    metadata:
      labels:
        app: nginx-nfs
    spec:
      containers:
      - name: nginx
        image: nginx
        volumeMounts:
        - name: wwwroot
          mountPath: /usr/share/nginx/html
        ports:
        - containerPort: 80
      volumes:
      - name: wwwroot
        nfs:
          server: 192.168.153.21
          path: /ifs/kubernetes
          
          
-------------------------------------------------------------------
[root@k8smaster volume]# kubectl apply -f nfs.yaml 
deployment.apps/deployment-nfs created

[root@k8smaster kubernetes]# kubectl get pods -o wide
NAME                              READY   STATUS    IP              NODE       
deployment-nfs-56b4b97db4-9t5kb   1/1     Running   10.244.249.34   k8snode1  
deployment-nfs-56b4b97db4-r6grv   1/1     Running   10.244.249.29   k8snode1   
deployment-nfs-56b4b97db4-sckf5   1/1     Running   10.244.249.42   k8snode1   

[root@k8smaster volume]# cd /ifs/kubernetes/
[root@k8smaster kubernetes]# vi index.html
hello ,小李飞刀

[root@k8smaster kubernetes]# curl 10.244.249.34
hello ,小李飞刀
[root@k8smaster kubernetes]# curl 10.244.249.29
hello ,小李飞刀
[root@k8smaster kubernetes]# curl 10.244.249.42
hello ,小李飞刀

[root@k8smaster kubernetes]# kubectl exec -it deployment-nfs-56b4b97db4-9t5kb -- bash
root@deployment-nfs-56b4b97db4-9t5kb:/# cd /usr/share/nginx/html/
root@deployment-nfs-56b4b97db4-9t5kb:/usr/share/nginx/html# ls
index.html
上一篇 下一篇

猜你喜欢

热点阅读