初学 k8s ,记录一下

安装并配置 nfs

  1. 在 master 节点 和 worker 节点都需要执行以下安装

    yum -y install nfs-utils rpcbind
    systemctl start rpcbind.service
    systemctl start nfs
    systemctl enable rpcbind.service
    systemctl enable nfs

  2. 在 master 节点配置 nfs 目录并设置 export 范围
    mkdir /nfs/data -p
    chown nfsnobody.nfsnobody /nfs/data

    设置配置文件

     cat>>/etc/exports<<EOF
     /nfs/data 192.168.1.0/24(rw,sync,no_root_squash,no_all_squash)
     EOF
    
  3. 测试:
    在 master 节点上

     [root@centos7-189 working]# showmount -e 127.0.0.1
     Export list for 127.0.0.1:
     [root@centos7-189 working]# showmount
     Hosts on centos7-189:
     [root@centos7-189 working]# showmount -e 192.168.1.189
     Export list for 192.168.1.189:
     [root@centos7-189 working]# showmount -e 192.168.1.185
     Export list for 192.168.1.185:
     [root@centos7-189 working]# showmount -e 192.168.1.186
     Export list for 192.168.1.186:
    

    在 worker 节点上

     [root@centos7-185 ~]# showmount
     Hosts on centos7-185:
     [root@centos7-185 ~]# showmount -e 127.0.0.1
     Export list for 127.0.0.1:
     [root@centos7-185 ~]# showmount -e 192.168.1.185
     Export list for 192.168.1.185:
    

创建 pv、pvc

  1. 创建pv配置文件
    # cat pv.yaml

     apiVersion: v1
     kind: PersistentVolume
     metadata:
       name: nfs-pv
       namespace: default
       labels:
         pv: nfs-pv
     spec:
       capacity:
         storage: 100Mi
       accessModes:
         - ReadWriteMany
       persistentVolumeReclaimPolicy: Retain
       storageClassName: nfs-pv
       nfs:  
         server: 192.168.0.141
         path: "/nfs/data/nginx"   #NFS目录,需要该目录在NFS上存在
    
  2. 创建pv
    # kubectl create -f pv.yaml

     persistentvolume/nfs-pv created
    
  3. 获取pv
    # kubectl get pv

     NAME     CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
     nfs-pv   100Mi      RWX            Retain           Available           nfs-pv                  11s
    
  4. 创建 pvc.yaml
    # cat pvc.yaml

     apiVersion: v1
     kind: PersistentVolumeClaim
     metadata:
       name: nfs-pvc
       namespace: default
     spec:
       storageClassName: nfs-pv
       accessModes:
         - ReadWriteMany
       resources:
         requests:
           storage: 50Mi  #容量
    
  5. 创建 pvc
    # kubectl create -f pvc.yaml

     persistentvolumeclaim/nfs-pvc created
    
  6. 获取pvc
    # kubectl get pvc

     	NAME         STATUS    VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS          AGE
     	nfs-pvc      Bound     nfs-pv   100Mi      RWX            nfs-pv                83s
    
  7. 再次获取pv
    # kubectl get pv

     NAME     CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM             STORAGECLASS   REASON   AGE
    

nfs-pv 100Mi RWX Retain Bound default/nfs-pvc nfs-pv 3m8s

删除pv、pvc

  1. 命令
    kubectl delete pvc pvc-name -n namespace
    kubectl delete pv pv-name

    先删除 pvc ,后删除 pv
    如果已经应用到 pod ,那就先删除pod

  2. 查询pvc状况
    刚开始创建的 nfs-pvc 一直都是Pending,需要找到原因,学习完之后删除他!
    # kubectl describe pvc nfs-pvc

     Name:          nfs-pvc
     Namespace:     default
     StorageClass:  
     Status:        Pending
     Volume:        
     Labels:        <none>
     Annotations:   <none>
     Finalizers:    [kubernetes.io/pvc-protection]
     Capacity:      
     Access Modes:  
     VolumeMode:    Filesystem
     Used By:       <none>
     Events:
       Type    Reason         Age                   From                         Message
       ----    ------         ----                  ----                         -------
       Normal  FailedBinding  31s (x3927 over 16h)  persistentvolume-controller  no persistent volumes available for this claim and no storage class is set
    

看到错误原因应该是对应的 pv 不存在,因为开始创建的 pv 和 pvc 文件中名称不一致!

  1. 回收删除 nfc-pvc
    # kubectl delete -f pvc.yaml

     persistentvolumeclaim "nfs-pvc" deleted
    
  2. 验证一下
    # kubectl get pvc

     NAME       STATUS   VOLUME    CAPACITY   ACCESS MODES   STORAGECLASS   AGE
     ...
    

    那个 pending 的 nfs-pvc 删除了!

  3. 删除 pv
    # kubectl delete -f pv.yaml

作为练习,复制2 份 pv/pvc 文件

# ls

 pv1.yaml  pv2.yaml  pvc1.yaml  pvc2.yaml  pvc.yaml  pv.yaml

pv1.yaml
# cat pv1.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv1
  namespace: default
  labels:
    pv: nfs-pv1
spec:
  capacity:
    storage: 100Mi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs-pv1
  nfs:  
    server: 192.168.0.141
    path: "/nfs/data/nginx"   #NFS目录,需要该目录在NFS上存在

pvc1.yaml
# cat pvc1.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-pvc2
  namespace: default
spec:
  storageClassName: nfs-pv2
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 50Mi  #容量
Logo

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

更多推荐