k8s之持续化存储卷nfs动态绑定

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了k8s之持续化存储卷nfs动态绑定相关的知识,希望对你有一定的参考价值。

安装nfs服务这里安装在slave2一个节点上

[root@k8s-slave2 ~]# yum -y install nfs-utils rpcbind
[root@k8s-slave2 ~]# cat /etc/exports
/data/nfs/ *(insecure,rw,async,no_root_squash)

[root@k8s-slave2 ~]# chmod 777 /data/nfs/

先启动rpcbind服务在启动nfs
[root@k8s-slave2 ~]# systemctl start rpcbind
[root@k8s-slave2 ~]# systemctl start nfs
主节点验证
[root@k8s-master ~]# showmount -e 192.168.60.102
Export list for 192.168.60.102:
/data/nfs *

下载外部存储卷的供应器代码

git clone https://github.com/kubernetes-incubator/external-storage
下载完成进入external-storage目录
[root@k8s-master deploy]# pwd
/root/external-storage/nfs-client/deploy

创建storageclass
[root@k8s-master deploy]# kubectl apply -f class.yaml
[root@k8s-master deploy]# kubectl apply -f rbac.yaml

修改deloyment.yaml中的nfs挂载参数

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-client-provisioner
  labels:
    app: nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: default
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: nfs-client-provisioner
  template:
    metadata:
      labels:
        app: nfs-client-provisioner
    spec:
      serviceAccountName: nfs-client-provisioner
      containers:
        - name: nfs-client-provisioner
          image: quay.io/external_storage/nfs-client-provisioner:latest
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: fuseim.pri/ifs
            - name: NFS_SERVER
              value: 192.168.60.102
            - name: NFS_PATH
              value: /data/nfs
      volumes:
        - name: nfs-client-root
          nfs:
            server: 192.168.60.102
            path: /data/nfs

[root@k8s-master deploy]# kubectl apply -f deployment.yaml
查看 storageclass
[root@k8s-master deploy]# kubectl get storageclass
NAME PROVISIONER AGE
managed-nfs-storage fuseim.pri/ifs 18h
也可以通过dashboard查看
技术图片
生成资源类型:
[root@k8s-master deploy]# kubectl apply -f test-claim.yaml
生成一个测试pod:
[root@k8s-master deploy]# kubectl apply -f test-pod.yaml
查看服务,也可通过dashborad查看,这里不做展示下面通过一个mysql实例演示一下
[root@k8s-master deploy]# kubectl get pv
[root@k8s-master deploy]# kubectl get pvc

mysql pod动态存储的生成

这里nodeport端口还是mysql的3306 kubernets默认映射端口3000端口起步
这里修改kube-api参数
vim /etc/kubernetes/manifests/kube-apiserver.yaml
添加 - --service-node-port-range=1-65535
技术图片
重启服务
systemctl restart kubelet

[root@k8s-master mysql]# cat mysql-claim.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-claim
  annotations:
    volume.beta.kubernetes.io/storage-class: "managed-nfs-storage" 
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

[root@k8s-master mysql]# cat mysql-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
  namespace: default
  labels:
    k8s-app: mysql
spec:
  replicas: 1
  selector: 
    matchLabels:
      k8s-app: mysql
  template:
    metadata:
      labels:
        k8s-app: mysql
    spec:
      nodeName: k8s-slave1
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/mysql5-7/mysql5-7:5.7
        name: mysql
        imagePullPolicy: Always
        env:
          - name: MYSQL_ROOT_PASSWORD
            value: "123.com" 
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: nfs-pv
          mountPath: /var/lib/mysql
      volumes:
        - name: nfs-pv
          persistentVolumeClaim:
            claimName: mysql-claim

[root@k8s-master mysql]# cat nodeport.yaml

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  selector:
    k8s-app: mysql
  ports:
  - nodePort: 3306
    protocol: TCP
    port: 3306
    targetPort: 3306
  type: NodePort

[root@k8s-master mysql]# cat service.yaml

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  type: NodePort

  ports:

  - port: 3306

    protocol: TCP

    targetPort: 3306

    name: http

    nodePort: 3306

  selector:
    k8s-app: mysql

依次生成:
[root@k8s-master mysql]# kubectl apply -f mysql-claim.yaml
[root@k8s-master mysql]# kubectl apply -f mysql-deployment.yaml
[root@k8s-master mysql]# kubectl apply -f nodeport.yaml
[root@k8s-master mysql]# kubectl apply -f service.yaml
查看服务
[root@k8s-master mysql]# kubectl get pv

NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                 STORAGECLASS          REASON   AGE
pvc-f9c4bb26-f788-4e08-820b-d128d3921604   10Gi       RWO            Delete           Bound    default/mysql-claim   managed-nfs-storage            18h

[root@k8s-master mysql]# kubectl get pvc

NAME          STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS          AGE
mysql-claim   Bound    pvc-f9c4bb26-f788-4e08-820b-d128d3921604   10Gi       RWO            managed-nfs-storage   18h

现在到nfs服务端查看:
技术图片
本地验证一下:
技术图片

以上是关于k8s之持续化存储卷nfs动态绑定的主要内容,如果未能解决你的问题,请参考以下文章

kubernetes之NFS动态提供Kubernetes后端存储卷

K8S 之 使用NFS卷作为POD存储卷

容器技术:用NFS,成共享存储。

k8s实践17:kubernetes对接nfs存储实现pvc动态按需创建分配绑定pv

k8s之存储抽象nfs挂载

Kubernetes核心概念之Volume存储数据卷详解