1、搭建NFS服务

安装NFS

yum -y install nfs-utils rpcbind

新建文件/etc/exports

vi /etc/exports

填写以下内容到此文件

# /etc/exports: the access control list for filesystems which may be exported
#               to NFS clients.  See exports(5).
#
# Example for NFSv2 and NFSv3:
# /srv/homes       hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)
#
# Example for NFSv4:
# /srv/nfs4        gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
# /srv/nfs4/homes  gss/krb5i(rw,sync,no_subtree_check)

/home/nfs *(rw,async,no_root_squash)

创建以下配置的相应目录

mkdir -p /home/nfs
chmod 777 /home/nfs

重启NFS和rpcbind服务

systemctl restart rpcbind && systemctl restart nfs &&systemctl enable nfs

检查挂载的文件目录

[root@localhost nfs]# exportfs -v
/home/nfs     	<world>(async,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)

在其他机器或本机查看共享情况

[root@localhost ~]# showmount -e 192.168.1.150
Export list for 192.168.1.150:
/home/nfs         

2、创建NFS存储卷(PV)

1、nfs-deployment.yaml

kind: Deployment
apiVersion: apps/v1
metadata:
   name: nfs-client-provisioner
   namespace: default
spec:
   replicas: 1
   strategy:
     type: Recreate
   selector:
     matchLabels:
       app: nfs-client-provisioner
   template:
      metadata:
         labels:
            app: nfs-client-provisioner
      spec:
         serviceAccount: nfs-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.1.150
                 -  name: NFS_PATH
                    value: /home/nfs
         volumes:
           - name: nfs-client-root
             nfs:
               server: 192.168.1.150
               path: /home/nfs

2、rbac.yaml

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-provisioner
  namespace: default
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
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

3、storageClass.yaml

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-storage
  namespace: default
provisioner: fuseim.pri/ifs
reclaimPolicy: Retain

3、创建服务   redis-headless-service.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: redis-cluster
  namespace: redis  
  labels:
    app: redis
spec:
  #type: NodePort
  ports:
  - port: 6379
    targetPort: 6379
    name: client
  clusterIP: None
  selector:
    app: redis
    appCluster: redis-cluster

4、创建configmap和statefulSet  redis-sts.yaml

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-cluster
  namespace: redis
data:
  update-node.sh: |
    #!/bin/sh
    REDIS_NODES="/data/nodes.conf"
    sed -i -e "/myself/ s/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/${POD_IP}/" ${REDIS_NODES} 
    exec "$@"   #把含有myself这一行的ip换成新的pod ip
  redis.conf: |+
    # 开启集群模式
    cluster-enabled yes
    #no代表可以在slot没有全部分配的时候提供服务
    cluster-require-full-coverage no 
    #节点互连超时时间,单位为毫秒
    cluster-node-timeout 15000  
    #设置当前节点集群配置文件路径
    cluster-config-file /data/nodes.conf  
    cluster-migration-barrier 1
    # 开启aof日志,即持久化
    appendonly yes 
    #关闭保护模式
    protected-mode no  
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis-cluster
  namespace: redis
spec:
  serviceName: redis-cluster
  replicas: 6
  selector:
    matchLabels:
      app: redis
      appCluster: redis-cluster
  template:
    metadata:
      labels:
        app: redis
        appCluster: redis-cluster
    spec:
      containers:
      - name: redis
        image: redis:6.0.9
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 6379
          name: client
        - containerPort: 16379
          name: cluster
        command: ["/conf/update-node.sh", "redis-server", "/conf/redis.conf"]
        env:
        - name: POD_IP  
          valueFrom:
            fieldRef:
              fieldPath: status.podIP  #获取pod的id,传给POD_IP
        - name: METADATA_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name #获取pod名
        volumeMounts:
        - name: conf
          mountPath: /conf
          readOnly: false
        - name: data
          mountPath: /data
          readOnly: false
      volumes:
      - name: conf
        configMap:
          name: redis-cluster
          defaultMode: 0755
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi
      storageClassName: nfs-storage 

5、创建对外提供服务 redis-access-service.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: redis-access-service
  namespace: redis
  labels:
    app: redis
spec:
  type: NodePort
  ports:
  - port: 6379
    targetPort: 6379
    name: client
    protocol: "TCP"
  - port: 16379
    targetPort: 16379
    name: cluster
  #clusterIP: None
  selector:
    app: redis
    appCluster: redis-cluster

 

Logo

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

更多推荐