Storage Class璧�婧�

1锛�涓轰�涔�瑕�浣跨��Storage Class锛�

  • 涔���甯歌������ㄦ��潘斤���浼兼病��浠�涔���棰�锛�浣�缁��充�涓�锛�pvc�ㄥ��pv�宠�山���ㄧ┖�存�讹����规����疏浚���pv��绉帮�璁块��妯″�锛�疏浚归��澶у��ュ�冲���蜂����d锅pv�ョ�宠�风┖�寸��锛���璁�pv��疏浚归��涓�20G锛�疏浚�涔���璁块��妯″���WRO锛�����璁镐互璇诲�����瑰���潘藉�板��涓����戊寅�锛���pvc�宠�风��海洋��ㄧ┖�翠负10G锛��d�涓���杩�涓�pvc����涓��㈢��pv�宠�风��绌洪�达�涔�广大辨��璇达��d�pv��10涓�G��绌洪�磋�娴�璐逛�锛���涓哄�跺����璁歌���涓����规��潘姐��广大辩��涓�����杩�涓���棰�锛���浠�姣�娆℃���ㄥ�诲��寤�pv涔�广大辨��杈�杆塔荤����河渠���锛�杩��讹���浠�广大遍��瑕�涓�涓����ㄥ����宸ュ�锋�ユ�挎��浠���寤�pv��
  • 杩�涓�涓�瑗垮氨���块����岛���涓�涓�寮�婧�宸ュ�封��nfs-client-provisioner��锛�杩�涓�涓�瑗挎����杩�k8s��缃���nfs椹卞�ㄥ�杩�绔���NFS���″�ㄦ��潘藉�版���扮演��褰�锛��跺����韬�浣�涓�storage锛�海洋���锛���
  • 2锛�stroage class�ㄩ��缇や腑��浣���锛�

  • pvc����娉��存�ュ�诲��nfs-client-provisioner�宠�蜂娇�ㄧ��海洋��ㄧ┖�寸��锛�杩��讹�广大遍��瑕���杩�SC杩�涓�璧�婧�瀵硅薄�荤�宠�蜂�锛�SC���规��浣��ㄥ氨���规��pvc疏浚�涔����ュ�ㄦ����寤�pv锛�涓�浠�����河渠���浠�绠$�������堕�达�杩���浠ュ�瑁�涓���绫诲����海洋��ㄤ�pvc���ㄣ��

  • 姣�涓�sc�藉����浠ヤ�涓�涓���瑕���海洋�娈碉�杩�河渠�海洋�娈典���sc��瑕��ㄦ������pv�朵�浣跨�ㄥ�起�
    Provisioner锛�岛�缁��戊寅�锛���岛�河渠�海洋��ㄨ�婧���海洋��ㄧ郴缁���
    ReclaimPolicy锛�pv�����剁���ワ����ㄧ���兼��Delete锛�榛�璁わ���Retiain
    Parameters锛����起�锛�海洋��ㄧ把浣跨�ㄥ���版��杩拌��宠���板���ㄥ�枫��
  • 3锛�涓��㈠�轰�NFS���℃�ュ��璺�storage class
    1锛���寤�NFS���★���广大�master���逛�涓�nfs server锛�锛�

    [root@master ~]# yum -y install nfs-utils
    [root@master ~]# vim /etc/exports
    /nfsdata *(rw,sync,no_root_squash)
    [root@master ~]# mkdir /nfsdata    #��寤哄�变韩��褰�
    [root@master ~]# systemctl start rpcbind
    [root@master ~]# systemctl enable rpcbind
    [root@master ~]# systemctl start nfs-server
    [root@master ~]# systemctl enable nfs-server
    [root@master ~]# showmount -e  #纭�淇�������潘�
    Export list for master:
    /nfsdata *

    2锛���寤�rbac����锛�
    rbac锛��轰�瑙��茬��璁块���у�讹�锛�广大辨���ㄦ�烽��杩�瑙��蹭�����杩�琛��宠����
    ��涓�涓�浠�璁よ��-----> ����-----> ���ユ�哄�躲��

    [root@master sc]# vim rbac-rolebind.yaml 
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: nfs-provisioner
      namespace: default
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRole
    metadata:
      name: nfs-provisioner-runner
      namespace: default
    rules:
       -  apiGroups: [""]
          resources: ["persistentvolumes"]
          verbs: ["get", "list", "watch", "create", "delete"]
       -  apiGroups: [""]
          resources: ["persistentvolumeclaims"]
          verbs: ["get", "list", "watch", "update"]
       -  apiGroups: ["storage.k8s.io"]
          resources: ["storageclasses"]
          verbs: ["get", "list", "watch"]
       -  apiGroups: [""]
          resources: ["events"]
          verbs: ["watch", "create", "update", "patch"]
       -  apiGroups: [""]
          resources: ["services", "endpoints"]
          verbs: ["get","create","list", "watch","update"]
       -  apiGroups: ["extensions"]
          resources: ["podsecuritypolicies"]
          resourceNames: ["nfs-provisioner"]
          verbs: ["use"]
    ---
    kind: ClusterRoleBinding
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
      name: run-nfs-provisioner
    subjects:
      - kind: ServiceAccount
        name: nfs-provisioner
        namespace: default
    roleRef:
      kind: ClusterRole
      name: nfs-provisioner-runner
      apiGroup: rbac.authorization.k8s.io

    //�ц�yaml��浠讹�

    [root@master sc]# kubectl apply -f  rbac-rolebind.yaml 
    serviceaccount/nfs-provisioner created
    clusterrole.rbac.authorization.k8s.io/nfs-provisioner-runner created
    clusterrolebinding.rbac.authorization.k8s.io/run-nfs-provisioner created

    浠ヤ���浠��板缓��涓�涓���涓�nfs-provisioner��ServiceAccount锛��跺��缁�疏浚�河渠�涓�涓���涓�nfs-provisioner-runner��ClusterRole锛���璇�ClusterRole澹版��河渠�涓�河渠�����锛��朵腑灏卞����瀵�pv��澧�锛���锛��戊寅��ョ������锛���浠ユ��浠���浠ュ�╃�ㄨ��serviceAccount�ヨ���ㄥ��寤�pv��

    3锛���寤轰�涓�nfs��Deployment
    广大����㈢��瀵瑰������版�挎�㈡����浠���宸辩��nfs��缃���

    [root@master sc]# vim nfs-deployment.yaml 
    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      name: nfs-client-provisioner
      namespace: default
    spec:
      replicas: 1
      strategy:
        type: Recreate
      template:
        metadata:
          labels:
            app: nfs-client-provisioner
        spec:
          serviceAccount: nfs-provisioner
          containers:
            - name: nfs-client-provisioner
              image: registry.cn-hangzhou.aliyuncs.com/open-ali/nfs-client-provisioner
              volumeMounts:
                - name: nfs-client-root
                  mountPath:  /persistentvolumes
              env:
                - name: PROVISIONER_NAME
                  value: nfs-deploy    #岛�缁��圭����绉帮���疏浚�涔�锛�
                - name: NFS_SERVER
                  value: 172.16.1.30     #nfs���″�ㄧ��ip�板��
                - name: NFS_PATH
                  value: /nfsdata      #nfs�变韩����褰�
          volumes:  
            - name: nfs-client-root
              nfs:
                server: 172.16.1.30
                path: /nfsdata

    //瀵煎��nfs-client-provisioner����锛���缇や腑��姣�涓����归�介��瀵煎�ワ�����master锛�

    [root@master sc]# docker load --input  nfs-client-provisioner.tar 
    5bef08742407: Loading layer  4.221MB/4.221MB
    c21787dcfbf0: Loading layer  2.064MB/2.064MB
    00376105a0f3: Loading layer  41.08MB/41.08MB
    Loaded image: registry.cn-hangzhou.aliyuncs.com/open-ali/nfs-client-provisioner:latest

    k8s涔�StorageClass

    //�ц�yaml��浠讹�
    [root@master sc]# kubectl apply -f  nfs-deployment.yaml 
    deployment.extensions/nfs-client-provisioner created
    //纭�淇�pod姝e父杩�琛�
    [root@master sc]# kubectl  get pod 
    NAME                                      READY   STATUS    RESTARTS   AGE
    nfs-client-provisioner-5457694c8b-k8t4m   1/1     Running   0          23s

    nfs-client-provisionser宸ュ�风��浣���锛�疏浚���杩�k8s����缃���nfs椹卞�ㄦ��潘借�绔���nfs���″�ㄥ�版���扮演��褰�锛��跺��广大���韬�浣�涓�storage provide锛�海洋��ㄦ��岛�锛�锛��宠��sc��

    4锛���寤�storage class:

    [root@master sc]# vim test-sc.yaml
    apiVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
      name: statefu-nfs
      namespace: default
    provisioner: nfs-deploy  
    reclaimPolicy: Retain

    ��浠�澹版��河渠�涓�涓���涓�statefu-nfs��sc瀵硅薄锛�娉ㄦ��涓��㈢��provisioner海洋�娈靛�瑰����间�疏浚�瑕���涓��㈢��nfs��Deployment涓��㈢��PROVISIONER_NAME杩�涓���澧��������间��枫��

    //��寤鸿�ヨ�婧�瀵硅薄锛�

    [root@master sc]# kubectl apply -f  test-sc.yaml 
    storageclass.storage.k8s.io/statefu-nfs created

    5锛�涓��㈡��SC璧�婧�瀵硅薄��寤烘����河渠�锛��ヤ��ユ��浠�娴�璇������藉��ㄦ����寤�pv��
    //兄弟�����浠���寤轰�涓�pvc瀵硅薄锛�

    [root@master sc]# vim test-pvc.yaml
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: test-claim    
      namespace: default
    spec:
      storageClassName: statefu-nfs   #sc涓�疏浚�瑕�����涓��㈠��寤虹��sc��绉�
      accessModes:
        - ReadWriteMany   #����ReadWriteMany��璁块��妯″�
      resources:
        requests:
          storage: 50Mi    #璇锋�50M��绌洪��

    //�ц�yaml��浠舵�ュ��寤�pvc锛�

    [root@master sc]# kubectl  apply -f  test-pvc.yaml 
    persistentvolumeclaim/test-claim created

    k8s涔�StorageClass

    ��浠���浠ョ����pvc��寤烘����锛��舵��宸茬���Bound河渠�锛���涓���涔���河渠т�涓�涓�瀵瑰���volume瀵硅薄锛�����瑕���涓�����STORAGECLASS锛��板�ㄧ���煎氨����浠�������寤虹��sc瀵硅薄��绉扳��statefu-nfs����

    //�ヤ��ユ��浠��ョ��涓�涓�pv锛�楠�璇������ㄦ����寤猴�
    k8s涔�StorageClass

    ��浠���浠ョ���板凡缁����ㄧ����河渠�涓�涓��宠����pv瀵硅薄锛�璁块��妯″���RWX(��sc涓�疏浚�涔���妯″�)锛����剁����Delete 锛��舵�����锋��Bound锛�����杩�sc�ㄦ����寤虹��锛���骞朵�����浠����ㄥ��寤虹����

    4锛��ㄧ讲nginx�ュ��璺�pv锛�pvc

    ��浠���杩��ㄧ讲涓�涓�nginx���℃�ユ�璇���浠�涓��㈢��stroage class�瑰�澹版����pvc瀵硅薄锛�疏浚��版�版����涔���锛���

    [root@master sc]# vim nginx-pod.yaml
    kind: Pod
    apiVersion: v1
    metadata:
      name: nginx-pod
      namespace: default
    spec:
      containers:
        - name: nginx-pod
          image: nginx
          volumeMounts:    #疏浚�涔��版����涔���
            - name: nfs-pvc
              mountPath: /usr/share/nginx/html
      volumes:
        - name: nfs-pvc
          persistentVolumeClaim:   #��疏浚�pvc锛�娉ㄦ��涓��㈠0����pvc��������涓��㈠��涔���pvc��绉�
            claimName: test-claim   
    //杩�琛�nginx锛�骞舵�ョ��pod����姝e父杩�琛�锛�
    [root@master sc]# kubectl apply -f  nginx-pod.yaml 
    pod/nginx-pod created

    k8s涔�StorageClass

    //��浠�杩���pod涓�锛���寤烘�璇�缃�椤垫��浠讹�

    [root@master ~]# kubectl  exec  -it nginx-pod /bin/bash
    root@nginx-pod:/# cd /usr/share/nginx/html/
    root@nginx-pod:/usr/share/nginx/html# echo "<h1>welcome to Storage Class web</h1>" > index.html
    root@nginx-pod:/usr/share/nginx/html# cat index.html 
    <h1>welcome to Storage Class web</h1>
    root@nginx-pod:/usr/share/nginx/html# exit

    //��浠�����nfs���″�ㄧ���变韩�版����褰�涓��ョ����浠舵������姝ワ�
    k8s涔�StorageClass

    ��nfs��褰�涓���浠���浠ュ��浠ョ���版����海洋�寰�寰��跨����浠跺す锛�杩�涓���浠跺す���藉���瑰�广大辨����浠�涓��㈢��瑙�����������

    k8s涔�StorageClass

    ��浠�杩��ュ�拌�ョ��褰�涓�锛���浠ョ����nginx涓����版��宸茬�疏浚��板��姝ワ�涓�疏浚��颁��版����涔���锛�杩���广大变���娴�璇�河渠�锛�����琛�娴�璇�锛�

    ����娴�璇�nginx�����藉�姝e父璁块����浠�缂�����缃�椤碉�
    //��寤�service璧�婧�瀵硅薄锛��宠��涓�杈圭��pod锛���广大�绔��e�枫��
    涓�涓�疏浚��寸��yaml��浠跺��疏浚瑰�涓�锛�

    kind: Pod
    apiVersion: v1
    metadata:
      name: nginx-pod
      namespace: default
      labels:
        app: web
    spec:
      containers:
        - name: nginx-pod
          image: nginx
          volumeMounts:
            - name: nfs-pvc
              mountPath: /usr/share/nginx/html
      volumes:
        - name: nfs-pvc
          persistentVolumeClaim:
            claimName: test-claim
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: nginx-svc
      namespace: default
    spec:
      type: NodePort
      selector:
        app: web
      ports:
      - name: nginx
        port: 80
        targetPort: 80
        nodePort: 32134
    

    //���板��潘�nginx锛�骞惰�块��缃�椤碉�

    [root@master sc]# kubectl  apply -f  nginx-pod.yaml 
    pod/nginx-pod configured
    service/nginx-svc created

    k8s涔�StorageClass

    k8s涔�StorageClass

    nginx缃�椤垫�e父璁块��锛��ㄧ讲瀹�姣�锛�浠ヤ�广大辨��Storage Class��浣跨�ㄦ�规�锛���河渠х��澧�涓�浠���storage class杩���涓�澶���锛��朵���璧�婧�瀵硅薄浼��ㄥ��缁�涓�海洋�涔��般��