StorageClass

描述:
1、使用nfs集中式存储实现节点调度后的数据一致性。
2、创建k8s存储类(nfs)用于自动化创建pv、pvc,便于部署。
部署:
#创建nfs
ssh 172.16.1.128
yum -y install nfs-utils rpcbind
mkdir -p /var/nfs
chmod 777 -R /var/nfs
vim /etc/exports
/var/nfs    *(rw,sync,no_root_squash)
#配置生效:
exportfs -r
#启动:
systemctl start nfs
systemctl enabled nfs
systemctl start rpcbind
systemctl enabled rpcbind
#创建nfs存储类
wget http://raw.githubusercontent.com/kubernetes-incubator/external-storage/master/nfs-client/deploy/deployment.yaml
vim deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-client
  labels:
    app: nfs-client
  # replace with namespace where provisioner is deployed
  namespace: default
spec:
  replicas: 1            ###想做高可用的话这里可以改成3,一般为大于等于3的奇数
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: nfs-client
  template:
    metadata:
      labels:
        app: nfs-client
    spec:
      serviceAccountName: nfs-client
      containers:
        - name: nfs-client
          image: quay.io/external_storage/nfs-client-provisioner:latest
          image: quay.io/external_storage/nfs-subdir-external-provisioner:v4.0.2    ###这个镜像可以支持动态pv创建子目录,也就是本文档中的NFS,一般测试中两个镜像选其一即可
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME			   ###这个名字必须与storageclass里面的名字一致,不然storageclass找不到provisioner
              value: radondb-mysql-nfs
            - name: ENABLE_LEADER_ELECTION         ###设置高可用允许选举,如果replicas参数等于1,那这个参数可以不用配置
              value: "True"
            - name: NFS_SERVER
              value: 172.16.1.128                   ###修改nfsserver地址为:172.19.58.188
            - name: NFS_PATH                       ###修改共享地址:/data/nfs
              value: /var/nfs               
      volumes:
        - name: nfs-client-root
          nfs:
            server: 172.16.1.128                    ###修改nfsserver地址为:172.19.58.188
            path: /var/nfs                 ###修改共享地址为:/data/nfs

#################
#部署
kubectl apply -f deployment.yaml
#创建account并绑定角色
vim nfs-client-sa.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-client

---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: nfs-client-runner
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: ["list", "watch", "create", "update", "patch"]
  - apiGroups: [""]
    resources: ["endpoints"]
    verbs: ["create", "delete", "get", "list", "watch", "patch", "update"]

---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: run-nfs-provisioner
subjects:
  - kind: ServiceAccount
    name: nfs-client
    namespace: default
roleRef:
  kind: ClusterRole
  name: nfs-client-runner
  apiGroup: rbac.authorization.k8s.io
##############
#部署
kubectl apply -f nfs-client-sa.yaml
#创建storageclass
vim nfs-client-class.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: course-nfs-storage
provisioner: radondb-mysql-nfs  ##配置与PROVISIONER_NAME相同的名字
###以下两条配置默认没有,支持NFS创建子目录,用于不同应用使用NFS的不同目录
parameters:
  pathPattern: "${.PVC.namespace}/${.PVC.annotations.nfs.io/storage-path}"
##############
#部署
kubectl apply -f nfs-client-class.yaml
#查看:
kubectl get storageclass|grep course-nfs-storage
#测试:
vim test-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-test
  annotations:
    volume.beta.kubernetes.io/storage-class: "course-nfs-storage"
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
##############
#部署:
kubectl apply -f test-pvc.yaml
#查看:
kubectl get pvc -A |grep pvc-test
kubectl get pv
Logo

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

更多推荐