Kubernetes1.18实践及踩坑(之五)持久化(NFS)

一、部署NFS服务

       找一台新的机器,也可利用集群中现有的一台机器,部署NFS服务端。

       步骤略。请参见: https://blog.csdn.net/boling_cavalry/article/details/79498346。

       然后在集群中的所有节点安装:       

yum -y install nfs-utils

二、创建PV

      编辑 pv-nginx.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-nginx
  namespace: default
  labels:
    pv: pv-nginx
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs
  nfs:
    path: /usr/local/work/nfs/root4nginx
    server: 192.168.100.51

      在master节点上执行:

[root@master01 app-deploy]# kubectl apply -f pv-nginx.yaml

三、创建PVC

      编辑 pvc-nginx.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-nginx
  namespace: default
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
  storageClassName: nfs
  selector:
    matchLabels:
      pv: pv-nginx

      在master节点上执行:

[root@master01 app-deploy]# kubectl apply -f pvc-nginx.yaml 

四、部署一个应用(nginx)

      编辑 nginx-deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-zkhy-deploy
  labels:
    app: nginx-deploy
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-pod
  template:
    metadata:
      labels:
        app: nginx-pod
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
        volumeMounts:
        - name: nginx-persistent-storage
          mountPath: /usr/share/nginx/html
      volumes:
        - name: nginx-persistent-storage
          persistentVolumeClaim:
            claimName: pvc-nginx
---

kind: Service
apiVersion: v1
metadata:
  labels:
    app: nginx-svc
  name: nginx-zkhy-svc
  namespace: default
spec:
  type: NodePort
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30080
  selector:
    app: nginx-pod

      其中通过volumeMounts 和  volumes 关联PVC。

      在master节点上执行:

[root@master01 app-deploy]# kubectl apply -f nginx-deploy.yaml
[root@master01 app-deploy]# kubectl get svc -o wide | grep nginx
nginx-zkhy-svc   NodePort       10.10.186.162           80:30080/TCP   5d22h   app=nginx-pod
[root@master01 app-deploy]# kubectl get pod -o wide | grep nginx
nginx-zkhy-deploy-cd7fcc88b-ndlbz   1/1     Running       0          38m     10.122.153.217   master01.huiyan.com              
nginx-zkhy-deploy-cd7fcc88b-zc8gz   1/1     Running       0          37m     10.122.153.220   master01.huiyan.com              
nginx-zkhy-deploy-cd7fcc88b-zd65f   1/1     Running       0          38m     10.122.153.222   master01.huiyan.com              


      可以看到三个nginx的实例已经运行。

      在nfs目录/usr/local/work/nfs/root4nginx 中放置一些文件,如:

      Kubernetes1.18实践及踩坑(之五)持久化(NFS)_第1张图片

      现在可以用Cluster IP 访问:

       Kubernetes1.18实践及踩坑(之五)持久化(NFS)_第2张图片

 

       Kubernetes1.18实践及踩坑(之五)持久化(NFS)_第3张图片

你可能感兴趣的:(k8s,云计算,DevOps)