K8s之deploy挂载Volumes
一. 创建Host类型pv
1.1 创建pv yaml文件
-bash-4.2# cat abc_mount_pvc.yaml apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: abc name: abc spec: replicas: 3 selector: matchLabels: app: abc strategy: {} template: metadata: creationTimestamp: null labels: app: abc spec: containers: - image: registry.abc.com/abc/abc:1260cd6 name: iam imagePullPolicy: Always resources: {} volumeMounts: - name: abcvolume mountPath: /data/static volumes: - name: abcvolume persistentVolumeClaim: claimName: mypvc status: {}
本地挂载目录路径: `/data/static` 数据将会持久化到该路径下面
1.2 kubectl创建pv
-bash-4.2# kubectl create namespace abc#### 创建namespace做资源隔离 -bash-4.2# kubectl create -f abc_pv.yaml -n abc rsistentvolume/myvolume created -bash-4.2# kubectl get pv -n abc NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE myvolume 5Gi RWO,RWX Retain Available normal 4s -bash-4.2#
二、创建pvc
2.1 创建pvc yaml文件
-bash-4.2# vim abc_pvc.yaml kind: PersistentVolumeClaim apiVersion: v1 metadata: name: mypvc spec: storageClassName: normal accessModes: - ReadWriteOnce resources: requests: storage: 3Gi
2.2 kubectl创建pvc
-bash-4.2# kubectl create -f abc_pvc.yaml -n abc persistentvolumeclaim/mypvc created -bash-4.2# kubectl get pvc -n abc NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE mypvc Bound myvolume 5Gi RWO,RWX normal 6s -bash-4.2# -bash-4.2# kubectl get pv -n abc NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE myvolume 5Gi RWO,RWX Retain Bound iam/mypvc normal 73s -bash-4.2#
当`STATUS`状态为`Bound`说明`pvc`已经绑定到`pv`上,到这里已完成初步的`pvc`、`pv`的绑定
三、创建deploy yaml文件
3.1 挂载pvc
-bash-4.2# cat abc_mount_pvc.yaml apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: abc name: abc spec: replicas: 3 selector: matchLabels: app: abc strategy: {} template: metadata: creationTimestamp: null labels: app: abc spec: containers: - image: registry.abc.com/abc/abc:1260cd6 name: abc imagePullPolicy: Always resources: {} volumeMounts: - name: abcvolume mountPath: /data/static volumes: - name: abcvolume persistentVolumeClaim: claimName: mypvc status: {} -bash-4.2#
3.2 kubectl创建deploy
-bash-4.2# kubectl create -f abc_mount_pvc.yaml -n abc deployment.apps/iam created -bash-4.2# -bash-4.2# -bash-4.2# kubectl get po -n abc NAME READY STATUS RESTARTS AGE abc-666475bcf9-gxgfb 1/1 Running 0 8s abc-666475bcf9-lcljd 1/1 Running 0 8s abc-666475bcf9-t6tbv 1/1 Running 0 8s -bash-4.2#
3.3 检查是否挂载成功
a. 进入port检查
-bash-4.2# kubectl -n abc exec -it abc-666475bcf9-t6tbv -- /bin/sh ~/iam # ls AUTHORS api-ref doc nohup.out test-requirements.txt ChangeLog babel.cfg docker requirements.txt tools Dockerfile build etc setup tox.ini LICENSE build.sh iam setup.cfg README.rst build_k8s.sh iam.egg-info setup.py ~/iam #
b. 创建一个文件
~/iam # ls /data/static/ ~/iam # cat /etc/passwd >/data/static/pwd.txt ~/iam # ls /data/static/ pwd.txt
c. 检查持久化
-bash-4.2# ll /data/static/ total 8 -rw-r--r-- 1 root root 1230 Jul 19 22:44 pwd.txt
完成调试。