kubernetes持久化存储

一、Volumes

1、容器的弊端

1、Container (容器) 中的磁盘文件是短暂的,当容器崩 溃时,kubelet 会重新启动容器,但最初的文件将丢 失,Container 会以最干净的状态启动。

2、当一个 Pod 运行多个 Container 时,各个容器可能需 要共享一些文件。

2、Kubernetes Volume

Kubernetes Volume可以解决这两个问题。

一些需要持久化数据的程序才会用到 Volumes,或者一 些需要共享数据的容器需要 volumes。

如:Redis 分片集群模式,Redis-Cluster:nodes.conf

日志收集的需求:需要在应用程序的容器里面加一个 sidecar,这个容器是一个收集日志的容器,比如 filebeat,它通过 volumes 共享应用程序的日志文件目 录。

Kubernetes Volumes 官方文档:Volumes | Kubernetes

3、Docker 和 K8S 的 Volume 区别

1、生命周期管理:Docker的卷生命周期由其容器决定, 如果容器被删除,卷并不会被自动删除。而 Kubernetes的卷与Pod生命周期绑定,当Pod被删除 时,其卷也会被自动删除。

2、共享与访问:在Docker中,每个容器都有自己的卷, 无法共享卷。而在Kubernetes中,多个容器可以共享 同一个卷,并且通过在每个容器的volumeMounts字 段中指定相同的卷,可以实现多容器共享数据。

3、卷类型和功能:Kubernetes支持多种类型的卷,如 EmptyDir、Secret、ConfigMap、 PersistentVolumeClaim等,提供了丰富的选择和功 能。而Docker的卷功能相对基础,主要关注持久存储 数据的解决方案。

4、数据持久性:Kubernetes的卷在容器重启或销毁后仍 然可以保留数据,因为其生命周期与Pod相同。而 Docker的卷在容器删除后数据可能会丢失。

5、扩展性和灵活性:Kubernetes的卷架构更加开放和灵 活,支持多种存储后端和插件机制,方便扩展和定 制。而Docker的卷功能相对固定,扩展性和灵活性相 对较低。

4、EmptyDir 容器卷

EmptyDir 卷用于 Pod 中的不同 Container 共享数据, 如果删除 Pod,EmptyDir 卷中的数据也将被删除。

使用方式:

默认情况下,EmptyDir 卷支持节点上的任何介质, 可以是 SSD、磁盘或网络存储,具体如何使用取决于 自身的环境。

还可以将 emptyDir.medium 字段设置为 Memory, 让 Kubernetes 使用 tmpfs(内存支持的文件系 统),不过这样虽然提高性能,但因为是运行在内存 中,所以数据无法持久化,且设置的大小会被计入到 Container 的内存限制中。

使用 emptyDir 卷的示例,直接指定 emptyDir 为 {} 即 可:

 # 创建一个deployment项目,在项目中部署两个nginx容器,第一个容器的mnt目录与第二个容器的opt目录共享
 # 1、在一个pod中有两个nginx,需要另一个nginx等待一会,要不然会报错
 [root@k8s-master bb]# vim test001.yaml
 apiVersion: apps/v1
 kind: Deployment
 metadata:
         name: test001
         namespace: default
         labels:
                 app: test001
 spec:
         selector:
                 matchLabels:
                         app: test001
         replicas: 1
         strategy:
                 rollingUpdate:
                         maxSurge: 25%
                         maxUnavailable: 25%
                 type: RollingUpdate
         template:
                 metadata:
                         labels:
                                 app: test001
                 spec:
                         containers:
                         -       name: nginx1
                                 image: docker.io/library/nginx:latest
                                 imagePullPolicy: Never
                                 resources:
                                         requests:
                                                 cpu: 100m
                                                 memory: 100Mi
                                         limits:
                                                 cpu: 100m
                                                 memory: 100Mi
                                 volumeMounts:   # 为01容器配置emptyDir参数
                                 -       name: con-share
                                         mountPath: /mnt
                                 command:   # 当两个nginx同pod启动时,要加入sleep命令,否则会冲突
                                 -       sleep
                                 -       "300"
                         -       name: nginx2
                                 image: docker.io/library/nginx:latest
                                 imagePullPolicy: Never
                                 resources:
                                         requests:
                                                 cpu: 100m
                                                 memory: 100Mi
                                         limits:
                                                 cpu: 100m
                                                 memory: 100Mi
                                 volumeMounts:  # 为02容器配置emptyDir参数
                                 -       name: con-share
                                         mountPath: /opt
                         volumes: 
                         -       name: con-share    # 设定enptyDir卷名称
                                 emptyDir: {}    # emptyDir格式
                         restartPolicy: Always
 [root@k8s-master bb]# kubectl create -f test001.yaml 
 deployment.apps/test001 created
 [root@k8s-master bb]# kubectl get pod
 NAME                                   READY   STATUS    RESTARTS        AGE
 test001-fdfd6b6c8-xbbkk                2/2     Running   0               79s
 [root@k8s-master bb]# kubectl exec pods/test001-fdfd6b6c8-xbbkk -c nginx1 -- ls
 bin
 .....
 [root@k8s-master bb]# kubectl exec pods/test001-fdfd6b6c8-xbbkk -c nginx2 -- ls
 bin
 ......
 # 挂载验证
 [root@k8s-master bb]# kubectl exec pods/test001-fdfd6b6c8-xbbkk -c nginx1 touch /mnt/nginx.txt
 kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
 [root@k8s-master bb]# kubectl exec pods/test001-fdfd6b6c8-xbbkk -c nginx2 ls /opt
 kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
 nginx.txt
 [root@k8s-master bb]# kubectl exec pods/test001-fdfd6b6c8-xbbkk -c nginx2 touch /opt/nginx2.txt
 kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
 [root@k8s-master bb]# kubectl exec pods/test001-fdfd6b6c8-xbbkk -c nginx1 ls /mnt
 kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
 nginx.txt
 nginx2.txt

5、HostPath 本地卷

HostPath 卷可将将宿主机的文件或目录挂载到 Pod 上,用于 Pod 自定义日志输出或访问 Docker 内部的容 器等。

在挂载时,要注意该 Pod 是在哪个节点上运行的,要在 对应的 node 上创建需要挂载的文件或目录。

使用 hostPath 卷的示例。将主机的 /root/test.txt 挂 载到 Pod 的 /tmp 下:

文件

# 在家目录中创建一个文件,作为本地文件挂载一个容器
 # 1、找不到文件,原因是pod部署在node上,而test.txt文件在master上
 # 2、没有指定文件名,直接挂载目录上,被挂载的文件是个file不是目录,需要挂载的文件需要指定文件名
 [root@k8s-master bb]# touch test.txt
 [root@k8s-master bb]# ls
 test001.yaml  test.txt
 [root@k8s-master bb]# scp test.txt k8s-node01:/root
 test.txt                                                   100%    0     0.0KB/s   00:00    
 [root@k8s-master bb]# scp test.txt k8s-node02:/root
 test.txt                                                   100%    0     0.0KB/s   00:00 
 [root@k8s-master bb]# vim test002.yaml
 apiVersion: apps/v1
 kind: Deployment
 metadata:
         name: test002
         namespace: default
         labels:
                 app: test002
 spec:
         selector:
                 matchLabels:
                         app: abc
         replicas: 1
         strategy:
                 rollingUpdate:
                         maxSurge: 25%
                         maxUnavailable: 25%
                 type: RollingUpdate
         template:
                 metadata:
                         labels:
                                 app: abc
                 spec:
                         containers:
                         -       name: nginx
                                 image: docker.io/library/nginx:latest
                                 imagePullPolicy: Never
                                 resources:
                                         requests:
                                                 cpu: 100m
                                                 memory: 100Mi
                                         limits:
                                                 cpu: 100m
                                                 memory: 100Mi
                                 ports:
                                 -       containerPort: 80
                                         name: zzzz
                                 volumeMounts:
                                 -       name: aaa
                                         mountPath: /opt/test.txt
                         volumes:
                         -       name: aaa
                                 hostPath:
                                         path: /root/test.txt
                                         type: File
                         restartPolicy: Always
 [root@k8s-master bb]# kubectl create -f test002.yaml 
 deployment.apps/test002 created
 [root@k8s-master bb]# kubectl get pod
 NAME                                   READY   STATUS             RESTARTS         AGE
 test001-fdfd6b6c8-xbbkk                1/2     CrashLoopBackOff   5 (77s ago)      34m
 test002-784b789878-pfnln               1/1     Running            0                85s
 [root@k8s-master bb]# kubectl exec pods/test002-784b789878-pfnln ls /opt/
 kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
 test.txt

目录(考试系统的nginx)

 [root@k8s-master bb]# scp -r /root/pes k8s-node01:/root
 [root@k8s-master bb]# scp -r /root/pes k8s-node02:/root
 [root@k8s-master bb]# vim test003.yaml 
 apiVersion: apps/v1
 kind: Deployment
 metadata:
         name: test003
         namespace: default
         labels:
                 app: test003
 spec:
         selector:
                 matchLabels:
                         app: abc
         replicas: 3
         strategy:
                 rollingUpdate:
                         maxSurge: 25%
                         maxUnavailable: 25%
                 type: RollingUpdate
         template:
                 metadata:
                         labels:
                                 app: abc
                 spec:
                         containers:
                         -       name: nginx
                                 image: docker.io/library/nginx:latest
                                 imagePullPolicy: Never
                                 resources:
                                         requests:
                                                 cpu: 100m
                                                 memory: 100Mi
                                         limits:
                                                 cpu: 100m
                                                 memory: 100Mi
                                 ports:
                                 -       containerPort: 80
                                         name: zzzz
                                 volumeMounts:
                                 -       name: pesdist
                                         mountPath: /usr/share/nginx/html
                         volumes:
                         -       name: pesdist
                                 hostPath:
                                         path: /root/pes/web/src/dist
                                         type: Directory
                         restartPolicy: Always
 ​
 [root@k8s-master bb]# kubectl create -f test003.yaml 
 deployment.apps/test003 created
 [root@k8s-master bb]# kubectl get pod -o wide
 NAME                                   READY   STATUS    RESTARTS        AGE   IP              NODE         NOMINATED NODE   READINESS GATES
 test003-6b4f8bddd9-jn6t4               1/1     Running   0               4s    172.16.58.234   k8s-node02   <none>           <none>
 test003-6b4f8bddd9-m5dsl               1/1     Running   0               4s    172.16.85.240   k8s-node01   <none>           <none>
 test003-6b4f8bddd9-r7zmp               1/1     Running   0               4s    172.16.85.239   k8s-node01   <none>           <none>
 # 查看 Pod 是否挂载成功
 [root@k8s-master bb]# curl 172.16.85.239
 <!DOCTYPE html>
 <html lang="en">
   <head>
     <meta charset="UTF-8">
     <link rel="icon" href="/favicon.ico">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>Vite App</title>
     <script type="module" crossorigin src="/assets/index-C4kAShR5.js"></script>
     <link rel="stylesheet" crossorigin href="/assets/index-CSz7ARPP.css">
   </head>
   <body>
     <div id="app"></div>
   </body>
 </html>

service代理deployment

[root@k8s-master bb]# vim test004.yaml
 apiVersion: v1
 kind: Service
 metadata:
         name: nginxservice
         namespace: default
 spec:
         selector:
                 app: abc
         type: NodePort
         sessionAffinity: None
         sessionAffinityConfig:
                 clientIP:
                         timeoutSeconds: 10800
         ports:
         -       name: nginxport
                 protocol: TCP
                 port: 80
                 targetPort: 80
                 nodePort: 32001
 [root@k8s-master bb]# kubectl create -f test004.yaml 
 service/nginxservice created
 [root@k8s-master bb]# kubectl get svc
 NAME                TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
 kubernetes          ClusterIP   10.96.0.1       <none>        443/TCP          12d
 mariadb-service     NodePort    10.96.247.148   <none>        3306:30318/TCP   23h
 nginxservice        NodePort    10.96.172.134   <none>        80:32001/TCP     28s
 wordpress-service   NodePort    10.96.67.149    <none>        80:32000/TCP     21h

为node节点打标签

减少资源浪费,指定一个节点保存挂载卷文件,指定pod部署的节点

[root@k8s-master bb]# kubectl delete -f test003.yaml 
 deployment.apps "test003" deleted
 [root@k8s-master bb]# kubectl get nodes
 NAME         STATUS   ROLES           AGE   VERSION
 k8s-master   Ready    control-plane   12d   v1.28.2
 k8s-node01   Ready    <none>          12d   v1.28.2
 k8s-node02   Ready    <none>          12d   v1.28.2
 [root@k8s-master bb]# kubectl label nodes k8s-node01 role=n0
 node/k8s-node01 labeled
 [root@k8s-master bb]# kubectl label nodes k8s-node02 role=n1
 node/k8s-node02 labeled
 [root@k8s-master bb]# vim test003.yaml 
 在containers中添加以下内容
                         nodeSelector:
                                 role: n0
 [root@k8s-master bb]# kubectl create -f test003.yaml 
 deployment.apps/test003 created
 [root@k8s-master bb]# kubectl get pod -o wide| grep test003 
 test003-7c8bd76b44-48lqx               1/1     Running            0               49s    172.16.85.243   k8s-node01   <none>           <none>
 test003-7c8bd76b44-kffgb               1/1     Running            0               49s    172.16.85.242   k8s-node01   <none>           <none>
 test003-7c8bd76b44-qcbcx               1/1     Running            0               49s    172.16.85.241   k8s-node01   <none>           <none>

6、NFS 网络卷

部署nfs服务器

 [root@nfs ~]# yum -y install rpcbind
 [root@nfs ~]# yum -y install nfs-utils
 [root@k8s-master ~]# scp -r /root/pes 10.0.0.99:/root
 [root@nfs ~]# vim /etc/exports
 /root/pes/      *(rw,sync)
 [root@nginx ~]# systemctl start rpcbind nfs-server
 # 测试服务是否真正发布,不安装也是可以在3个节点上使用的
 [root@k8s-node02 src]# yum -y install nfs-utils
 [root@k8s-node02 src]# showmount -e 10.0.0.99
 Export list for 10.0.0.99:
 /root/pes *

直接代理nfs服务器上的目录

[root@k8s-node01 ~]# rm -rf pes/
 [root@k8s-node02 ~]# rm -rf pes/
 [root@k8s-master bb]# vim test005.yaml 
 apiVersion: apps/v1
 kind: Deployment
 metadata:
         name: test005
         namespace: default
         labels:
                 app: test005
 spec:
         selector:
                 matchLabels:
                         app: nfsvolumes
         replicas: 3
         strategy:
                 rollingUpdate:
                         maxSurge: 25%
                         maxUnavailable: 25%
                 type: RollingUpdate
         template:
                 metadata:
                         labels:
                                 app: nfsvolumes
                 spec:
                         containers:
                         -       name: nginx
                                 image: docker.io/library/nginx:latest
                                 imagePullPolicy: Never
                                 resources:
                                         requests:
                                                 cpu: 100m
                                                 memory: 100Mi
                                         limits:
                                                 cpu: 100m
                                                 memory: 100Mi
                                 ports:
                                 -       containerPort: 80
                                         name: nginxport
                                 volumeMounts:
                                 -       name: nfs0
                                         mountPath: /opt
                         volumes:
                         -       name: nfs0
                                 nfs:
                                         server: 10.0.0.99
                                         path: /root/pes
                         restartPolicy: Always
 [root@k8s-master bb]# kubectl create -f test005.yaml 
 deployment.apps/test005 created
 [root@k8s-master bb]# kubectl get pod -owide
 test005-55688754df-99vwn               1/1     Running   0              12s   172.16.85.248   k8s-node01   <none>           <none>
 test005-55688754df-9fs4k               1/1     Running   0              12s   172.16.58.237   k8s-node02   <none>           <none>
 test005-55688754df-fjr7v               1/1     Running   0              12s   172.16.58.238   k8s-node02   <none>           <none>
 [root@k8s-master bb]# kubectl exec pods/test005-55688754df-99vwn ls /opt
 kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
 docker-compose.yml
 haproxy
 java
 mysql
 web
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值