一、nfs 网络存储

pod重启,数据依然存在

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

(1)安装nfs

yum install -y nfs-utils

(2)设置挂载路径

vi /etc/exports

/data/nfs *(rw,no_root_squash)

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

mkdir data

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

//查看nfs进程
ps -ef | grep nfs
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

二、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

//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

kubectl get pv,pvc

kubectl get pods

kubectl exec -it pod名称 bash

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐