二进制部署K8s集群第26节之storageClass动态挂载对接NFS存储

一、安装配置NFS

注意修改IP
exportfs #检查配置是否生效
mkdir /data/nfs-volume/ -p
yum -y install nfs-utils rpcbind
cat > /etc/exports <<'eof'
/data/nfs-volume 10.4.7.0/24(rw,no_root_squash)
eof
systemctl enable rpcbind
systemctl enable nfs-server
systemctl start rpcbind
systemctl start nfs-server
exportfs -r 
exportfs

二、客户端验证

客户端安装nfs-utifs才不会挂载失败,手动挂载验证没问题,再操作动态供应

yum install -y nfs-utils
systemctl start nfs-utils
systemctl enable nfs-utils
rpcinfo -p
showmount -e 10.4.7.11
mkdir /root/nfsmount
mount -t nfs 10.4.7.11:/data/nfs-volume /root/nfsmount

三 、设置动态供应

3.1 准备目录

mkdir /data/yaml/nfs/ -p

3.2 创建provisioner

value: storage.pri/nfs #名字虽然可以随便起,以后引用要一致
这个镜像中volume的mountPath默认为/persistentvolumes,不能修改,否则运行时会报错

cat > /data/yaml/nfs/provisioner-nfs.yaml <<'eof'
apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-provisioner
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
   name: nfs-provisioner-runner
rules:
   -  apiGroups: [""]
      resources: ["persistentvolumes"]
      verbs: ["get", "list", "watch", "create", "delete"]
   -  apiGroups: [""]
      resources: ["persistentvolumeclaims"]
      verbs: ["get", "list", "watch", "update"]
   -  apiGroups: ["storage.k8s.io"]
      resources: ["storageclasses"]
      verbs: ["get", "list", "watch"]
   -  apiGroups: [""]
      resources: ["events"]
      verbs: ["watch", "create", "update", "patch"]
   -  apiGroups: [""]
      resources: ["services", "endpoints"]
      verbs: ["get","create","list", "watch","update"]
   -  apiGroups: ["extensions"]
      resources: ["podsecuritypolicies"]
      resourceNames: ["nfs-provisioner"]
      verbs: ["use"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: run-nfs-provisioner
subjects:
  - kind: ServiceAccount
    name: nfs-provisioner
    namespace: default
roleRef:
  kind: ClusterRole
  name: nfs-provisioner-runner
  apiGroup: rbac.authorization.k8s.io
---
#vi nfs-deployment.yaml;创建nfs-client的授权
kind: Deployment
apiVersion: apps/v1
metadata:
   name: nfs-client-provisioner
spec:
   replicas: 1
   strategy:
     type: Recreate
   selector:
     matchLabels:
        app: nfs-client-provisioner
   template:
      metadata:
         labels:
            app: nfs-client-provisioner
      spec:
         serviceAccount: nfs-provisioner
         containers:
            -  name: nfs-client-provisioner
               image: lizhenliang/nfs-client-provisioner
               volumeMounts:
                 -  name: nfs-client-root
                    mountPath:  /persistentvolumes
               env:
                 -  name: PROVISIONER_NAME 
                    value: storage.pri/nfs 
                 -  name: NFS_SERVER
                    value: 10.4.7.11
                 -  name: NFS_PATH
                    value: /data/nfs-volume
         volumes:
           - name: nfs-client-root
             nfs:
               server: 10.4.7.11
               path: /data/nfs-volume
eof

3.3 创建storageclass

cat > /data/yaml/nfs/storageclass-nfs.yaml <<eof
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: storage-nfs
provisioner: storage.pri/nfs
reclaimPolicy: Delete
eof

3.4 改变默认sc

kubectl apply -f /data/yaml/nfs/
kubectl patch storageclass storage-nfs -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
kubectl get sc
kubectl get po -o wide

四、验证动态供应

4.1 创建pvc

storageClassName: storage-nfs #这个class一定注意要和sc的名字一样

cat > /data/yaml/nfs/pvc-nfs.yaml <<eof
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-nfs
spec:
  storageClassName: storage-nfs 
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
eof
kubectl apply -f /data/yaml/nfs/pvc-nfs.yaml
kubectl get pvc

4.2 使用pvc

...
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
          name: web
        volumeMounts:                        #挂载容器中的目录到pvc nfs中的目录
        - name: www
          mountPath: /usr/share/nginx/html
      volumes:
      - name: www
        persistentVolumeClaim:              #指定pvc
          claimName: pvc-nfs
...

猜你喜欢

转载自blog.51cto.com/yht1990/2630775