(四)查询sc是否正常
[root@master nfs-sc]# k get sc NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE managed-nfs-storage (default) fuseim.pri/ifs Delete Immediate true 9d
这四步完成后,基本nfs持久化数据卷就已经认为可用了,可以算作一个阶段了。剩下的就是建立pvc,然后查看pvc是否自动生成pv。
为什么要说这四步完成就是一个阶段了呢?因为,后面的pvc pv的建立是实际的应用阶段了,现有的nfs持久卷挂载服务已经可用了。后面的步骤都是测试的啦!!!!
(五)建立pvc并测试挂载pv是否正常,这个pvc名称是test-claim:
[root@master nfs-sc]# cat test-claim.yaml kind: PersistentVolumeClaim apiVersion: v1 metadata: name: test-claim annotations: volume.beta.kubernetes.io/storage-class: "managed-nfs-storage" spec: accessModes: - ReadWriteMany resources: requests: storage: 1Mi --- kind: Pod apiVersion: v1 metadata: name: test-pod spec: containers: - name: test-pod image: busybox:1.24 command: - "/bin/sh" args: - "-c" - "touch /mnt/SUCCESS && exit 0 || exit 1" volumeMounts: - name: nfs-pvc mountPath: "/mnt" restartPolicy: "Never" volumes: - name: nfs-pvc persistentVolumeClaim: claimName: test-claim
检测功能是否正常:
[root@master nfs-sc]# k get pods -A NAMESPACE NAME READY STATUS RESTARTS AGE database mysql2-7b77d5f496-8r5tr 1/1 Running 1 7h36m default nfs-client-provisioner-6fc484bd4f-pjxm7 1/1 Running 14 8d default nginx-7b54d48599-x2zc5 1/1 Running 10 5d13h default read-pod 0/1 Error 0 9h default test-pod 0/1 Completed 0 30s
有一个名字叫test-pod 的pod 是completed状态,在/data/nfs-sc/目录下:
[root@master nfs-sc]# pwd /data/nfs-sc [root@master nfs-sc]# ls default-test-claim-pvc-a17177ba-d1df-452e-84ec-12499f453b88
可以看到一个SUCCESS文件,表示功能正常
[root@master nfs-sc]# cd default-test-claim-pvc-a17177ba-d1df-452e-84ec-12499f453b88/ [root@master default-test-claim-pvc-a17177ba-d1df-452e-84ec-12499f453b88]# ls SUCCESS
可以看到pvc名称是test-claim,所在命名空间是default,这说明其它的命名空间可以自由使用kube-system下的pod nfs-client-provisioner :
[root@master ~]# k get pvc -A NAMESPACE NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE default test-claim Bound pvc-9e72b552-f3dd-432e-af7d-4d0a848447bf 1Mi RWX managed-nfs-storage 77m
可以看到pv的名称是pvc-9e72b552-f3dd-432e-af7d-4d0a848447bf,capacity,也就是申请的存储最大容量是1M,这个数值可以和前面的pvc定义文件相符:
[root@master ~]# k get pv -A NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE pvc-9e72b552-f3dd-432e-af7d-4d0a848447bf 1Mi RWX Delete Bound default/test-claim managed-nfs-storage 81m
(六)部署一个单实例MySQL并挂载
建立一个pvc ,名称为test-claim1:
kind: PersistentVolumeClaim apiVersion: v1 metadata: name: test-claim1 namespace: database spec: accessModes: ["ReadWriteOnce"] resources: requests: storage: 1Gi storageClassName: managed-nfs-storage
[root@master mysql]# cat deploy_mysql.yaml apiVersion: apps/v1 kind: Deployment metadata: name: mysql2 namespace: database spec: selector: matchLabels: app: mysql2 template: metadata: labels: app: mysql2 spec: containers: - name: mysql2 image: mysql:5.7.23 env: - name: MYSQL_ROOT_PASSWORD value: "123456" ports: - containerPort: 3306 volumeMounts: - name: nfs-pvc-test mountPath: /var/lib/mysql subPath: mysql volumes: - name: nfs-pvc-test persistentVolumeClaim: claimName: test-claim1
(七)mysql2这个pod 服务发布:
[root@master mysql]# cat svc_mysql.yaml apiVersion: v1 kind: Service metadata: name: mysql2 namespace: database spec: type: NodePort ports: - port: 3306 targetPort: 3306 nodePort: 32222 selector: app: mysql2 selector: app: mysql2
执行此文件即可: kubectl apply -f svc_mysql.yaml
(八)Navicat连接数据库,IP地址填写任意一个集群内IP,比如,192.168.217.16,端口号是32222
(九)单元测试
查看pod:
[root@master ~]# k get po -A NAMESPACE NAME READY STATUS RESTARTS AGE database mysql2-7b77d5f496-8r5tr 1/1 Running 2 15h
查看pod的详细信息:
[root@master ~]# k describe pod mysql2-7b77d5f496-2p6bl -n database Name: mysql2-7b77d5f496-2p6bl Namespace: database Priority: 0 Node: slave1/192.168.217.17 Start Time: Sat, 16 Jul 2022 12:48:05 +0800 Labels: app=mysql2 pod-template-hash=7b77d5f496 Annotations: <none> Status: Running IP: 10.244.1.6 IPs: IP: 10.244.1.6 Controlled By: ReplicaSet/mysql2-7b77d5f496 Containers: mysql2: Container ID: docker://95c44eb4ed328ea8993190450c753bd8bfaae573929f81f25c1adf3d8f3d7ab9 Image: mysql:5.7.23 Image ID: docker://sha256:1b30b36ae96ace2d29cd9c7a724cbb9d1ce59424a79cad4d117175d273d1689b Port: 3306/TCP Host Port: 0/TCP State: Running Started: Sat, 16 Jul 2022 12:48:06 +0800 Ready: True Restart Count: 0 Environment: MYSQL_ROOT_PASSWORD: 123456 Mounts: /var/lib/mysql from nfs-pvc-test (rw,path="mysql") /var/run/secrets/kubernetes.io/serviceaccount from default-token-v2llp (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: nfs-pvc-test: Type: PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace) ClaimName: test-claim1 ReadOnly: false default-token-v2llp: Type: Secret (a volume populated by a Secret) SecretName: default-token-v2llp Optional: false QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s node.kubernetes.io/unreachable:NoExecute op=Exists for 300s Events: <none>
这里可以看到,使用的pvc名称是test-claim1,并没有手动建立pv,总体功能是完全正常的。
总结:
一二三四步骤是第一个阶段,五以及后面的步骤都是pvc的创建以及pv的自动创建测试。PVC创建的时候使用的namespace和后面部署pod的namespace要一致,第六步有详细的演示,两个文件内定义的namespace必须要一致哦,和最开始的rbac角色创建和nfs-client-provisioner的pod的部署的时候的namespace没有关系了。PROVISIONER_NAME 这个需要两个文件相互呼应,storageclass-nfs.yaml 这个文件会引用到这个值。pvc创建的时候只需要storageClassName这个名称了,因为StorageClass里已经定义过了。这里提到的两个值可以通过命令快速查出:
[root@master ~]# k get sc -A NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE managed-nfs-storager fuseim.pri/ifs Delete Immediate true 4h6m