跳转至

26. deployment挂载pvc

1. 提前创建PV用的目录

# 必须提前把pv的目录创建好!!!
cd /nfs/sharefolder
mkdir nginx

2. 打开pv书签,得到pv和pvc模板

# https://kubernetes.io/zh/docs/tasks/configure-pod-container/configure-persistent-volume-storage/#%E5%88%9B%E5%BB%BA-persistentvolume
#pv模板
apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
#pvc模板
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi

3. 创建nfs类型的pv

mkdir /k8s/deploy-with-pvc
cd /k8s/deploy-with-pvc
vim nfs-pv-for-nginx.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv-for-nginx
spec:
  capacity: 
    storage: 1Gi
  accessModes:
  - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  nfs:
    path: /nfs/sharefolder/nginx
    server: 81.70.4.171

4. 创建pvc

vim nfs-pvc-for-nginx.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-pvc-for-nginx
  namespace: default
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 1Gi

5. 创建资源

kubectl create -f .

6. 查看pv和pvc

kubectl get pvc
root@k8s-master:/k8s/deploy-with-pvc# kubectl get pv
NAME               CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM                       STORAGECLASS   REASON   AGE
app-config         1Gi        ROX            Retain           Available                                                       4d5h
nfs-pv-for-nginx   1Gi        RWX            Retain           Bound       default/nfs-pvc-for-nginx                           5s
pv-nfs             1Gi        RWX            Retain           Bound       default/pvc-nfs                                     46m
root@k8s-master:/k8s/deploy-with-pvc#
root@k8s-master:/k8s/deploy-with-pvc#
root@k8s-master:/k8s/deploy-with-pvc# kubectl get pvc
NAME                STATUS    VOLUME             CAPACITY   ACCESS MODES   STORAGECLASS      AGE
nfs-pvc-for-nginx   Bound     nfs-pv-for-nginx   1Gi        RWX                              8s
pv-volume           Pending                                                csi-hostpath-sc   4d5h
pvc-nfs             Bound     pv-nfs             1Gi        RWX                              37m
test-pvc            Pending                                                nfs               92m

7. 打开PV相关书签,得到pod绑定pvc的模板

# https://kubernetes.io/zh/docs/tasks/configure-pod-container/configure-persistent-volume-storage/#%E5%88%9B%E5%BB%BA-persistentvolume
apiVersion: v1
kind: Pod
metadata:
  name: task-pv-pod
spec:
  volumes:
    - name: task-pv-storage
      persistentVolumeClaim:
        claimName: task-pv-claim
  containers:
    - name: task-pv-container
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: task-pv-storage

8. 创建绑定PVC的deployment

cd /k8s/deploy-with-pvc
vim  deploy-with-pvc.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-dpl-with-nfs-pvc
spec:
  replicas: 1
  selector:     #指定Pod的选择器
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    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: nfs-pvc-for-nginx
kubectl create -f deployment.yaml

9. 查看pod

kubectl get pod
root@k8s-master:/k8s/deploy-with-pvc# kubectl get pod
NAME                                     READY   STATUS             RESTARTS   AGE
bar                                      2/2     Running            4          38h
counter                                  1/1     Running            0          37h
front-end-5f64577768-c5ffj               1/1     Running            2          38h
front-end-5f64577768-z64sh               1/1     Running            0          46h
mysql-596b96985c-6rx5j                   1/1     Running            2          46h
myweb-6584f8dc59-rrzxz                   1/1     Running            0          46h
nginx-dpl-with-nfs-pvc-88cb78c6f-zzqtl   1/1     Running            0          12s
presentation-5d465968cb-xnbjx            0/1     CrashLoopBackOff   547        46h

10. 进入pv的目录,新建文件

#master
cd /nfs/sharefolder/nginx
echo 1 >index.html

11. 进入pod查看index.html

kubectl exec -it nnginx-dpl-with-nfs-pvc-88cb78c6f-zzqtl -- bash
cd /usr/share/nginx && ls
root@k8s-master:/nfs/sharefolder/nginx# kubectl exec -it nginx-dpl-with-nfs-pvc-88cb78c6f-zzqtl -- ls /usr/share/nginx/html
index.html
root@k8s-master:/nfs/sharefolder/nginx# kubectl exec -it nginx-dpl-with-nfs-pvc-88cb78c6f-zzqtl -- cat /usr/share/nginx/html/index.html
1

12. 疑问

# 为何curl上面pod的IP得不到信息呢?
root@k8s-master:/nfs/sharefolder/nginx# curl 10.244.1.53
^C
root@k8s-master:/nfs/sharefolder/nginx# curl 10.244.1.53:80
^C

最后更新: 2022-02-22 04:55:01