k8s--storageClass自动创建PV

一、storageClass自动创建PV

这里使用NFS实现

1.1 安装NFS

安装nfs-server: sh nfs_install.sh /mnt/data03 10.60.41.0/24

nfs_install.sh

#!/bin/bash

### How to install it? ###
### 安装nfs-server,需要两个参数:1、挂载点  2、允许访问nfs-server的网段 ###

### How to use it? ###
### Client节点`yum -y install nfs-utils rpcbind`,然后挂载nfs-server目录到本地 ###
### 如:echo "192.168.0.20:/mnt/data01  /mnt/data01  nfs  defaults  0 0" >> /etc/fstab && mount -a ###

mount_point=$1
subnet=$2

function nfs_server() {
  systemctl stop firewalld
  systemctl disable firewalld
  setenforce 0
  sed -i 's/^SELINUX.*/SELINUX\=disabled/' /etc/selinux/config
  yum -y install nfs-utils rpcbind
  mkdir -p $mount_point
  echo "$mount_point ${subnet}(rw,sync,no_root_squash)" >> /etc/exports
  systemctl start rpcbind && systemctl enable rpcbind
  systemctl restart nfs-server && systemctl enable nfs-server
  chown -R nfsnobody:nfsnobody $mount_point
}

function usage() {
echo "Require 2 argument: [mount_point] [subnet]
eg: sh $0 /mnt/data01 192.168.10.0/24"
}

declare -i arg_nums
arg_nums=$#
if [ $arg_nums -eq 2 ];then
  nfs_server
else
  usage
  exit 1
fi

1.2 创建nfs storageClass

创建: kubectl apply -f storageclass_deploy.yaml

storageclass_deploy.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-provisioner
  namespace: devops
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: nfs-provisioner-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: ["create", "update", "patch"]
- apiGroups: [""]
  resources: ["services", "endpoints"]
  verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: run-nfs-provisioner
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: nfs-provisioner-runner
subjects:
- kind: ServiceAccount
  name: nfs-provisioner
  namespace: devops
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: leader-locking-nfs-provisioner
  namespace: devops
rules:
- apiGroups: [""]
  resources: ["endpoints"]
  verbs: ["get", "list", "watch", "create", "update", "patch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: leader-locking-nfs-provisioner
  namespace: devops
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: leader-locking-nfs-provisioner
subjects:
- kind: ServiceAccount
  name: nfs-provisioner
  namespace: devops
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-provisioner
spec:
  selector:
    matchLabels:
       app: nfs-provisioner
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: nfs-provisioner
    spec:
      serviceAccountName: nfs-provisioner
      containers:
        - name: nfs-provisioner
          image: docker.io/gmoney23/nfs-client-provisioner:latest
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: example.com/nfs
            - name: NFS_SERVER
              value: 10.60.41.248
            - name: NFS_PATH
              value: /mnt/data03
      volumes:
        - name: nfs-client-root
          nfs:
            server: 10.60.41.248
            path: /mnt/data03
---
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: nfs
provisioner: example.com/nfs

1.3 测试自动创建pv

创建: kubectl apply -f test_auto_generate_pv.yaml
创建pvc之后发现pv自动创建了,在/mnt/data03/ 目录下

在这里插入图片描述

test_auto_generate_pv.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: test-claim1
  namespace: devops
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
  storageClassName: nfs
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      name: nginx-pod
  template:
    metadata:
      labels:
        name: nginx-pod
      namespace: myapp
    spec:
      containers:
      - name: nginx
        image: docker.io/library/nginx:latest
        imagePullPolicy: IfNotPresent
        volumeMounts:
        - name: nfs-pvc
          mountPath: /usr/share/nginx/html
      volumes:
      - name: nfs-pvc
        persistentVolumeClaim:
          claimName: test-claim1
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Kubernetes中,StorageClass是一种用于定义持久化存储的类别的资源对象。它允许管理员定义不同类型的存储,以满足各种应用程序的需求。 StorageClass定义了存储提供商、存储类型(如网络附加存储、云存储等)以及相关的参数配置。通过使用StorageClass,管理员可以将不同类型和配置的存储资源进行抽象和管理,并为应用程序提供不同的存储选择。 使用StorageClass的好处包括: 1. 抽象和管理多个存储提供商:Kubernetes支持多种存储提供商,如AWS EBS、Azure Disk、Google Persistent Disk等。通过StorageClass,可以将这些不同的存储提供商进行抽象和管理,使得应用程序可以跨不同云平台或存储系统进行迁移和部署。 2. 动态供应和绑定:StorageClass可以配置动态供应和绑定,使得PVC(Persistent Volume Claim)可以在创建自动创建匹配要求的PV(Persistent Volume)。这样可以简化存储资源的管理,并实现动态地分配和绑定存储。 3. 灵活的存储配置:通过StorageClass,可以指定存储提供商特定的参数配置,如访问模式、存储容量等。这样可以根据应用程序的需求,为不同的PV/PVC提供不同的存储配置。 要使用StorageClass,需要满足以下条件: 1. Kubernetes集群中已经配置了相应的存储插件和驱动程序。 2. 存储提供商的Credentials和相关配置信息已经正确配置。 3. 创建PV/PVC时,指定了所需的StorageClass名称。 通过StorageClass,管理员可以在Kubernetes中定义和管理各种类型和配置的存储资源,使得应用程序可以方便地选择和使用适合自己需求的持久化存储。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值