博客
关于我
Kubernetes实战总结 - 动态存储管理StorageClass
阅读量:438 次
发布时间:2019-03-06

本文共 4506 字,大约阅读时间需要 15 分钟。

Kubernetes存储类(StorageClass)部署指南

概述

StorageClass 为管理员提供了描述存储“类”的方法。不同类型的存储类可能会映射到不同的服务质量等级或备份策略,或者由集群管理员制定的任意策略。每个 StorageClass 包含 provisioner、parameters 和 reclaimPolicy 字段,这些字段会在动态分配 PersistentVolume 时使用到。

StorageClass 命名

StorageClass 对象的命名很重要,用户使用这个命名来请求生成一个特定的类。当创建 StorageClass 对象时,管理员设置 StorageClass 对象的命名和其他参数,一旦创建后不能再对其进行更新。

以 NFS 服务为例

安装 NFS 服务

# 安装 NFS 服务并启动
yum -y install nfs-common nfs-utils
systemctl start nfs && systemctl enable nfs
systemctl start rpcbind && systemctl enable rpcbind

创建共享目录

mkdir -p /data/nfsprovisioner
chmod 777 /data/nfsprovisioner
chown nfsnobody /data/nfsprovisioner

修改 exports 文件

cat > /etc/exports << EOF
# 将共享目录添加到 exports 文件中
/data/nfsprovisioner *(rw,async)
EOF

部署 NFS 客户端 provisioner

修改配置参数

# 修改对应的 NFS 地址和目录

部署 YAML 文件

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: managed-nfs-storage
provisioner: fuseim.pri/ifs # 或其他名称,需与部署的环境相关联
parameters:
archiveOnDelete: "false"
# 例:服务账号配置
apiVersion: v1
kind: ServiceAccount
metadata:
name: nfs-client-provisioner
namespace: default
# 例:角色配置
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: nfs-client-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"]
# 例:角色绑定
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: run-nfs-client-provisioner
subjects:
- kind: ServiceAccount
name: nfs-client-provisioner
namespace: default
roleRef:
kind: ClusterRole
name: nfs-client-provisioner-runner
apiGroup: rbac.authorization.k8s.io
# 例:角色配置
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: leader-locking-nfs-client-provisioner
namespace: default
rules:
- apiGroups: [""]
resources: ["endpoints"]
verbs: ["get", "list", "watch", "create", "update", "patch"]
# 例:角色绑定
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: leader-locking-nfs-client-provisioner
namespace: default
subjects:
- kind: ServiceAccount
name: nfs-client-provisioner
namespace: default
roleRef:
kind: Role
name: leader-locking-nfs-client-provisioner
apiGroup: rbac.authorization.k8s.io
# 例:部署配置
apiVersion: apps/v1
kind: Deployment
metadata:
name: nfs-client-provisioner
labels:
app: 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:
serviceAccountName: nfs-client-provisioner
containers:
- name: nfs-client-provisioner
image: registry.cn-shanghai.aliyuncs.com/leozhanggg/storage/nfs-client-provisioner:latest
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: fuseim.pri/ifs
- name: NFS_SERVER
value: 服务器IP地址
- name: NFS_PATH
value: /data/nfsprovisioner
volumes:
- name: nfs-client-root
nfs:
server: 服务器IP地址
path: /data/nfsprovisioner

测试部署

创建 PersistentVolumeClaim

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: test-claim
annotations:
volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Mi

创建测试 Pod

apiVersion: v1
kind: Pod
metadata:
name: test-pod
spec:
containers:
- name: test-pod
image: busybox
command:
- "/bin/sh"
args:
- "-c"
- "touch /mnt/SUCCESS && exit 0 || exit 1"
volumeMounts:
- name: nfs-pvc
mountPath: "/mnt"
restartPolicy: "Never"
volumes:
- name: nfs-pvc
persistentVolumeClaim:
claimName: test-claim

命令示例

# 部署配置
kubectl apply -f nfs-client-provisioner.yaml
# 检查 Pod 状态
kubectl get pod | grep nfs-client-provisioner

资源清理

# 删除测试 Pod
kubectl delete -f /root/test-pod.yaml
# 删除 PersistentVolumeClaim
kubectl delete persistentvolumeclaim "test-claim"

注意事项

请根据实际环境调整 NFS 服务器地址和共享目录路径。部署完成后,可以通过 kubectl get pod 检查 NFS 客户端 Provisioner 的运行状态。

转载地址:http://zknyz.baihongyu.com/

你可能感兴趣的文章
npm,yarn,cnpm 的区别
查看>>
NPOI
查看>>
NPOI之Excel——合并单元格、设置样式、输入公式
查看>>
NPOI初级教程
查看>>
NPOI利用多任务模式分批写入多个Excel
查看>>
NPOI在Excel中插入图片
查看>>
NPOI将某个程序段耗时插入Excel
查看>>
NPOI格式设置
查看>>
NPOI设置单元格格式
查看>>
Npp删除选中行的Macro录制方式
查看>>
NR,NF,FNR
查看>>
nrf24l01+arduino
查看>>
nrf开发笔记一开发软件
查看>>
nrm —— 快速切换 NPM 源 (附带测速功能)
查看>>
nrm报错 [ERR_INVALID_ARG_TYPE]
查看>>
NS3 IP首部校验和
查看>>
NSDateFormatter的替代方法
查看>>
NSError 的使用方法
查看>>
NSGA-Ⅲ源代码
查看>>
nsis 安装脚本示例(转)
查看>>