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

时间:2020-06-30 19:05:20   收藏:0   阅读:15
安装nfs服务这里安装在slave2一个节点上

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

[[email protected] ~]# chmod 777 /data/nfs/

先启动rpcbind服务在启动nfs
[[email protected] ~]# systemctl start rpcbind
[[email protected] ~]# systemctl start nfs
主节点验证
[[email protected] ~]# 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目录
[[email protected] deploy]# pwd
/root/external-storage/nfs-client/deploy

创建storageclass
[[email protected] deploy]# kubectl apply -f class.yaml
[[email protected] 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

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

[[email protected] 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

[[email protected] 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

[[email protected] 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

[[email protected] 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

依次生成:
[[email protected] mysql]# kubectl apply -f mysql-claim.yaml
[[email protected] mysql]# kubectl apply -f mysql-deployment.yaml
[[email protected] mysql]# kubectl apply -f nodeport.yaml
[[email protected] mysql]# kubectl apply -f service.yaml
查看服务
[[email protected] 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

[[email protected] 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服务端查看:
技术图片
本地验证一下:
技术图片

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!