当前位置:   article > 正文

k8s-数据持久化存储_pvvi

pvvi

一、nfs 网络存储

pod重启,数据依然存在

1、找一台服务器作为nfs服务端

(1)安装nfs

yum install -y nfs-utils
  • 1

(2)设置挂载路径

vi /etc/exports

/data/nfs *(rw,no_root_squash)
  • 1
  • 2
  • 3

(3)设置的挂载路径首先保证有这个路径,没有则创建

mkdir data

mkdir nfs
  • 1
  • 2
  • 3
2、在k8s集群node节点上安装nfs
yum install -y nfs-utils
  • 1
3、在nfs服务器启动nfs服务
systemctl start nfs

//查看nfs进程
ps -ef | grep nfs
  • 1
  • 2
  • 3
  • 4
4、在k8s集群部署应用,使用nfs网络存储
mkdir pv

cd pv

vi nfs-nginx.yaml

//nfs-nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-dep1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        volumeMounts:
        - name: wwwroot
          mountPath: /usr/share/nginx/html
        ports:
        - containerPort: 80
      volumes:
        - name: wwwroot
          nfs:
            server: 192.168.44.134 #nfs服务器ip
            path: /data/nfs


kubectl apply -f nfs-nginx.yaml

kubectl describe pod (pod名称)

//进入pod
kubectl exec -it (pod名称) bash

ls /usr/share/nginx/html

kubectl expose deployment nginx-dep1 --port=80 --target-port=80 --type=NodePort
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

二、PV和PVC

PV:持久化存储(生产者)

对存储的资源进行抽象,对外提供可以调用的地方

PVC:持久化存储调用(消费者)

用于调用。不需要关心内部实现的细节

//pvc.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-dep1
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        volumeMounts:
        - name: wwwroot
          mountPath: /usr/share/nginx/html
        ports:
        - containerPort: 80
      volumes:
      - name: wwwroot
        persistentVolumeClaim:
          claimName: my-pvc

---

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


kubectl apply -f pvc.yaml
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

//pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  nfs:
    path: /k8s/nfs
    server: 192.168.44.134


kubectl apply -f pvc.yaml
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

kubectl get pv,pvc

kubectl get pods

kubectl exec -it pod名称 bash

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/981391
推荐阅读
相关标签
  

闽ICP备14008679号