Kubernetes持久化存储

Kubernetes持久化存储

1、持久化存储

​ 之前通过数据库emptydir,是本地存储,pod重启后,数据不存在了。需要对数据持久化存储。

2、nfs网络存储

pod重启后,数据还是存在的。

2.1 安装nfs

yum install -y nfs-utils

2.2 设置挂载路径

vi /etc/exports

#设置为
/data/nfs *(rw,no_root_squash)

2.3 对外挂载的路径,需要提前创建

2.4 在K8S集群node节点安装nfs

yum install -y nfs-utils

2.5 在nfs服务器启动nfs

systemctl start nfs

2.6 在K8S集群部署应用使用nfs持久网络存储

在K8S的master节点部署应用

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-dep1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    meatada:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        images: nginx
        valumeMounts:
        - name: wwwroot
          mountPath: /usr/shar/nginx/html
        ports:
        - containerPort: 80
      volumes:
        - name: wwwroot
          nfs:
            servcer: 192.168.44.144
            path: /data/nfs

然后执行:

kubectl apply -f nfs-nginx.yaml
# 查看
kubectl get pods
3、PV和PVC

3.1 PV

PV:持久化存储,对存储资源进行抽象,对外提供可以调用的地方。(生产者)

3.2 PVC

PVC:用户调用,不需要关系内部实现细节。(消费者)

3.3 实现流程

Kubernetes持久化存储

pv.yaml

apiVersion: v1
kind: PersistemtVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  nfs:
    path: /k8s/nfs
    server: 192.168.44.144

执行命令:kubectl apply -f pv.yaml
查看:kubectl get pv,pvc

pvc.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-dep1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    meatada:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        images: nginx
        valumeMounts:
        - name: wwwroot
          mountPath: /usr/shar/nginx/html
        ports:
        - containerPort: 80
      volumes:
        - name: wwwroot
		  persistentVolumeClasin:
		    claimName: my-pvc
---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi

执行命令:kubectl apply -f pvc.yaml
查看:kubectl get pods

上一篇:几个demo用于练习


下一篇:Kubernetes 调度器 - 固定节点