K8s的持久化


容器内部的的存储在生命周期是短暂的,会随着容器环境的销毁而销毁,具有不稳定性。在k8s里将对容器应用所需的存储资源抽象为存储卷(Volume)概念来解决这些问题。

image-20240925160825391

特定资源存储

ConfigMap:存储配置文件 (nacos)
Secret:存储加密数据
ServiceAccountToken:token数据

本地存储

EmptDir: 临时存储(宿主机随机生成一个存储目录)
HostPath: 宿主机目录

网络共享存储

CephFS: 开源共享存储系统
GlusterFS: 开源共享存储系统 FastDFS
NFS:开源共享存储



网络共享存储实战

EmptyDir类型(本地存储)

apiVersion: v1
kind: Pod
metadata:
  name: mysql57
  namespace: default
  labels:
    app: mysql57
spec:
  volumes:
  - name: mysql-data
    emptyDir: {}
  containers:
  - name: mysql57
    image: mysql:5.7
    imagePullPolicy: IfNotPresent
    env:
    - name: MYSQL_ROOT_PASSWORD
      value: '123'
    volumeMounts:
    - name: mysql-data
      mountPath: /var/lib/mysql
### 运行 
[root@master zh]# kubectl get pod -owide mysql57
NAME      READY   STATUS    RESTARTS   AGE   IP          NODE 
mysql57   1/1     Running   0          92s   10.2.3.36   node03
#### 查看
[root@node03 ~]# docker ps
CONTAINER ID        IMAGE                   
bc43dcd1843e  	mysql:5.7

[root@node03 ~]# docker inspect bc43dcd1843e 
            {
                "Type": "bind",
                "Source": "/var/lib/kubelet/pods/51ca2d1b-5f9e-4521-80f2-40b5b66970c6/volumes/kubernetes.io~empty-dir/mysql-data",
                "Destination": "/var/lib/mysql",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            },
            
# 删除pod 目录消失
[root@master zh]# kubectl delete pod mysql57
pod "mysql57" deleted 

image-20240925162554006

hostPath (本地存储)

apiVersion: v1
kind: Pod
metadata:
  name: mysql57
  namespace: default
  labels:
    app: mysql57

spec:
  volumes:
  - name: mysql-data
    hostPath:
      path: /data/mysql

  containers:
  - name: mysql57
    image: mysql:5.7
    imagePullPolicy: IfNotPresent
    env:
    - name: MYSQL_ROOT_PASSWORD
      value: '123'
    volumeMounts:
    - name: mysql-data
      mountPath: /var/lib/mysql

[root@master cjh]# kubectl get pod -owide
NAME      READY   STATUS    RESTARTS   AGE   IP          NODE   
mysql57   1/1     Running   0          19s   10.2.3.39   node03  


[root@node03 mysql]# ls
auto.cnf    ca.pem           client-key.pem  ibdata1      ib_logfile1  mysql       performance_schema  public_key.pem   server-key.pem
ca-key.pem  client-cert.pem  ib_buffer_pool  ib_logfile0  ibtmp1       mysql.sock  private_key.pem     server-cert.pem  sys


# 删除容器 文件存在

image-20240925164237591


NFS类型 (共享存储)

# 1.NFS服务端安装NFS
[root@nfs ~]# yum install -y nfs-utils
# 2.所有node节点安装NFS
[root@node01 ~]# yum install -y nfs-utils
[root@node02 ~]# yum install -y nfs-utils
[root@node03 ~]# yum install -y nfs-utils

[root@nfs ~]# cat /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash)


chown 65534.65534 /data -R

[root@nfs ~]# cat /var/lib/nfs/etab 
/data   172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,all_squash)
apiVersion: v1
kind: Pod
metadata:
  name: centos-nginx-pod
spec:
  volumes:
  - name: nginx-nfs
    nfs:
      server: 172.16.1.31
      path: /data
  
  containers:
  - image: nginx:alpine
    imagePullPolicy: IfNotPresent
    name: nginx-containers
  
    volumeMounts:
    - name: nginx-nfs
      mountPath: /opt
可以做到共享存储要注意用户权限问题

image-20240925165344141