参考文档:https://jimmysong.io/kubernetes-handbook/concepts/persistent-volume.html

1.安装nfs
#主节点操作
 #安装nfs,从节点安装nfs-utils即可
[root@k8s-master ~]# yum install -y nfs-utils rpcbind
创建nfs共享目录
[root@k8s-master ~]# mkdir /home/nfs/ ; chmod -R 777 /home/nfs
#配置NFS服务程序的配置文件/etc/exports。
 echo '/home/nfs/ *(rw,sync,no_root_squash)' > /etc/exports
[root@k8s-master ~]# echo '/home/nfs/ *(rw,sync,no_root_squash)' > /etc/exports
[root@k8s-master ~]# vi /etc/exports
[root@k8s-master ~]# systemctl restart rpcbind.service
[root@k8s-master ~]# systemctl restart nfs.service
[root@k8s-master ~]# showmount -e 10.242.0.180
Export list for 10.242.0.180:
/home/nfs *

#启动rpcbind服务并验证
 systemctl start  rpcinfo
 systemctl enable  rpcinfo
 rpcinfo -p localhost 
  
 #所有节点启动nfs服务
[root@k8s-master nfs]# systemctl start nfs.service
[root@k8s-master nfs]# systemctl enable nfs.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@k8s-master nfs]# systemctl status nfs.service
● nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)
   Active: active (exited) since 二 2022-08-09 17:36:00 CST; 19h ago
 Main PID: 27889 (code=exited, status=0/SUCCESS)
    Tasks: 0
   Memory: 0B
   CGroup: /system.slice/nfs-server.service

8月 09 17:36:00 k8s-master systemd[1]: Starting NFS server and services...
8月 09 17:36:00 k8s-master systemd[1]: Started NFS server and services.
 #从节点操作
 showmount -e 172.25.79.108 #查看是否加载主节点配置,此处填写主节点IP
     Export list for 172.25.79.108:
     /home/nfsdir 172.25.79.109/20

 #从节点创建nfs目录并挂载
 mkdir /home/nfs ;  mount 172.25.79.108:/home/nfs /home/nfs     
 #设置开机自动挂载
 echo 'mount -t nfs 172.25.79.108:/home/nfs /home/nfs' > /etc/rc.d/rc.local
 #写文件验证成功,数据互通。(过程略)
2.配置PV和PVC

PV和PVC具体概念参考:https://jimmysong.io/kubernetes-handbook/concepts/persistent-volume.html

**PersistentVolume(PV)**是由管理员设置的存储,它是群集的一部分。就像节点是集群中的资源一样,PV 也是集群中的资源。 PV 是 Volume 之类的卷插件,但具有独立于使用 PV 的 Pod 的生命周期。此 API 对象包含存储实现的细节,即 NFS、iSCSI 或特定于云供应商的存储系统。

**PersistentVolumeClaim(PVC)**是用户存储的请求。它与 Pod 相似。Pod 消耗节点资源,PVC 消耗 PV 资源。Pod 可以请求特定级别的资源(CPU 和内存)。声明可以请求特定的大小和访问模式(例如,可以以读/写一次或 只读多次模式挂载)。

创建PV的yaml pv-nginx.yaml

[root@k8s-master nfs]# mkdir -p /home/nfs/pv-nginx
[root@k8s-master ~]# vi pv-nginx.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-nginx   #pv的名字
spec:
  capacity:
    storage: 5Gi  #使用的空间大小
  volumeMode: Filesystem
  accessModes:
  - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs-nginx  #标识,用于pvc绑定pv使用
  nfs:
    path: /home/nfs/pv-nginx  #创建的nfs的路径,
    server: 10.240.0.180      #服务器的地址

创建PVC的yaml pvc-nginx.yaml


kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nginx-pvc  
spec:
  storageClassName: nfs-nginx  #填写pv的storageClassName项的值。
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 2Gi   #分配给nginx的储存大小

启动pv、pvc

kubectl apply -f nginx-pv.yaml

kubectl apply -f nginx-pvc.yaml

#查看pv、pvc

kubectl get pv,pvc

[root@k8s-master ~]# kubectl apply -f nginx-pv.yaml
persistentvolume/pv-nginx created
[root@k8s-master ~]# kubectl get pv
NAME       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
pv-nginx   5Gi        RWX            Retain           Available           nfs-nginx               5s
[root@k8s-master ~]# kubectl apply -f nginx-pvc.yaml
persistentvolumeclaim/nginx-pvc created
[root@k8s-master ~]# kubectl get pvc
NAME        STATUS   VOLUME     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
nginx-pvc   Bound    pv-nginx   5Gi        RWX            nfs-nginx      7s

pod绑定pvc

kubectl delete -f svc-nginx.yaml

kubectl delete -f deploy-nginx.yaml

[root@k8s-master ~]# vi deploy-pv-nginx.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: dev
spec:
  replicas: 1
  selector:
    matchLabels:
      run: nginx
  template:
    metadata:
      labels:
        run: nginx
    spec:
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/zoeyqq/nginx
        name: nginx
        ports:
        - containerPort: 80
          protocol: TCP
        volumeMounts:
        - name: html-files
          mountPath: /usr/share/nginx/html
      volumes:
      - name: html-files
        persistentVolumeClaim:
          claimName: nginx-pvc   #填写pvc的名称

执行kubectl create -f deploy-pv-nginx.yaml

[root@k8s-master ~]# kubectl create -f deploy-pv-nginx.yaml
[root@k8s-master ~]# kubectl describe deploy nginx -n dev
Name:                   nginx
Namespace:              dev
CreationTimestamp:      Thu, 11 Aug 2022 11:14:37 +0800
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               run=nginx
Replicas:               1 desired | 1 updated | 1 total | 0 available | 1 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  run=nginx
  Containers:
   nginx:
    Image:        registry.cn-hangzhou.aliyuncs.com/zoeyqq/nginx
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:
      /usr/share/nginx/html from html-files (rw)
  Volumes:
   html-files:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  nginx-pvc
    ReadOnly:   false
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      False   MinimumReplicasUnavailable
  Progressing    False   ProgressDeadlineExceeded
OldReplicaSets:  <none>
NewReplicaSet:   nginx-5565f44c68 (1/1 replicas created)
Events:          <none>
[root@k8s-master ~]# kubectl get pod -n dev
NAME                     READY   STATUS              RESTARTS   AGE
nginx-5565f44c68-zbxn4   0/1     ContainerCreating   0          29h
[root@k8s-master ~]# kubectl describe pod nginx-5565f44c68-zbxn4 -n dev
Name:           nginx-5565f44c68-zbxn4
Namespace:      dev
Priority:       0
Node:           k8s-master/10.242.0.180
Start Time:     Thu, 11 Aug 2022 11:14:38 +0800
Labels:         pod-template-hash=5565f44c68
                run=nginx
Annotations:    <none>
Status:         Pending
IP:
IPs:            <none>
Controlled By:  ReplicaSet/nginx-5565f44c68
Containers:
  nginx:
    Container ID:
    Image:          registry.cn-hangzhou.aliyuncs.com/zoeyqq/nginx
    Image ID:
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Waiting
      Reason:       ContainerCreating
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /usr/share/nginx/html from html-files (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-7tkps (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             False
  ContainersReady   False
  PodScheduled      True
Volumes:
  html-files:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  nginx-pvc
    ReadOnly:   false
  default-token-7tkps:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-7tkps
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason       Age                  From                 Message
  ----     ------       ----                 ----                 -------
  Warning  FailedMount  17m (x155 over 29h)  kubelet, k8s-master  Unable to attach or mount volumes: unmounted volumes=[html-files], unattached volumes=[default-token-7tkps html-files]: timed out waiting for the condition
  Warning  FailedMount  12m (x342 over 28h)  kubelet, k8s-master  (combined from similar events): MountVolume.SetUp failed for volume "pv-nginx" : mount failed: exit status 32
Mounting command: systemd-run
Mounting arguments: --description=Kubernetes transient mount for /var/lib/kubelet/pods/1ace9744-c191-4bc5-a9de-09cb319d061b/volumes/kubernetes.io~nfs/pv-nginx --scope -- mount -t nfs 10.240.0.180:/home/nfs/pv-nginx /var/lib/kubelet/pods/1ace9744-c191-4bc5-a9de-09cb319d061b/volumes/kubernetes.io~nfs/pv-nginx
Output: Running scope as unit run-14943.scope.
mount.nfs: Connection timed out
  Warning  FailedMount  116s (x548 over 29h)  kubelet, k8s-master  Unable to attach or mount volumes: unmounted volumes=[html-files], unattached volumes=[html-files default-token-7tkps]: timed out waiting for the condition


Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐