236 lines
6.9 KiB
Markdown
236 lines
6.9 KiB
Markdown
|
<h1><center>Kubernetes存储类StorageClass</center></h1>
|
|||
|
|
|||
|
著作:行癫 <盗版必究>
|
|||
|
|
|||
|
------
|
|||
|
|
|||
|
## 一:StorageClass
|
|||
|
|
|||
|
StorageClass 为管理员提供了描述存储 "类" 的方法。 不同的类型可能会映射到不同的服务质量等级或备份策略,或是由集群管理员制定的任意策略。 Kubernetes 本身并不清楚各种类代表的什么。这个类的概念在其他存储系统中有时被称为 "配置文件"
|
|||
|
|
|||
|
#### 1.StorageClass 资源
|
|||
|
|
|||
|
每个 StorageClass 都包含 `provisioner`、`parameters` 和 `reclaimPolicy` 字段, 这些字段会在 StorageClass 需要动态分配 PersistentVolume 时会使用到
|
|||
|
|
|||
|
StorageClass 对象的命名很重要,用户使用这个命名来请求生成一个特定的类。 当创建 StorageClass 对象时,管理员设置 StorageClass 对象的命名和其他参数,一旦创建了对象就不能再对其更新
|
|||
|
|
|||
|
#### 2.创建Storageclass
|
|||
|
|
|||
|
```shell
|
|||
|
apiVersion: storage.k8s.io/v1
|
|||
|
kind: StorageClass
|
|||
|
metadata:
|
|||
|
name: example-nfs //名称
|
|||
|
provisioner: example.com/external-nfs
|
|||
|
parameters:
|
|||
|
server: nfs-server.example.com
|
|||
|
path: /share
|
|||
|
readOnly: "false"
|
|||
|
|
|||
|
server:NFS 服务器的主机名或 IP 地址
|
|||
|
path:NFS 服务器导出的路径
|
|||
|
readOnly:是否将存储挂载为只读的标志(默认为 false)
|
|||
|
```
|
|||
|
|
|||
|
注意:
|
|||
|
|
|||
|
provisioner参数值:
|
|||
|
|
|||
|
```yaml
|
|||
|
NFS example.com/external-nfs
|
|||
|
Glusterfs kubernetes.io/glusterfs
|
|||
|
AWS EBS kubernetes.io/aws-ebs
|
|||
|
......
|
|||
|
```
|
|||
|
|
|||
|
AWS EBS:
|
|||
|
|
|||
|
```shell
|
|||
|
apiVersion: storage.k8s.io/v1
|
|||
|
kind: StorageClass
|
|||
|
metadata:
|
|||
|
name: slow
|
|||
|
provisioner: kubernetes.io/aws-ebs
|
|||
|
parameters:
|
|||
|
type: io1
|
|||
|
iopsPerGB: "10" //这里需要输入一个字符串,即 "10",而不是 10
|
|||
|
fsType: ext4
|
|||
|
|
|||
|
type:io1,gp2,sc1,st1。详细信息参见 AWS 文档。默认值:gp2
|
|||
|
iopsPerGB:只适用于 io1 卷。每 GiB 每秒 I/O 操作。AWS卷插件将其与请求卷的大小相乘以计算IOPS的容量,并将其限制在 20000 IOPS
|
|||
|
fsType:受 Kubernetes 支持的文件类型。默认值:"ext4"
|
|||
|
```
|
|||
|
|
|||
|
Glusterfs:
|
|||
|
|
|||
|
```shell
|
|||
|
apiVersion: storage.k8s.io/v1
|
|||
|
kind: StorageClass
|
|||
|
metadata:
|
|||
|
name: slow
|
|||
|
provisioner: kubernetes.io/glusterfs
|
|||
|
parameters:
|
|||
|
resturl: "http://127.0.0.1:8081"
|
|||
|
clusterid: "630372ccdc720a92c681fb928f27b53f"
|
|||
|
restauthenabled: "true"
|
|||
|
restuser: "admin"
|
|||
|
secretNamespace: "default"
|
|||
|
secretName: "heketi-secret"
|
|||
|
gidMin: "40000"
|
|||
|
gidMax: "50000"
|
|||
|
volumetype: "replicate:3"
|
|||
|
|
|||
|
resturl:制备 gluster 卷的需求的Gluster REST服务/Heketi服务url,通用格式应该是 IPaddress:Port
|
|||
|
restauthenabled:Gluster REST 服务身份验证布尔值,用于启用对 REST 服务器的身份验证
|
|||
|
restuser:在 Gluster 可信池中有权创建卷的 Gluster REST服务/Heketi 用户
|
|||
|
restuserkey:服务器进行身份验证。 此参数已弃用,取而代之的是 secretNamespace + secretName
|
|||
|
secretNamespace,secretName:Secret 实例的标识,包含与 Gluster REST 服务交互时使用的用户密码;
|
|||
|
这些参数是可选的,secretNamespace 和 secretName 都省略时使用空密码,以这种方式创建:
|
|||
|
kubectl create secret generic heketi-secret \
|
|||
|
--type="kubernetes.io/glusterfs" --from-literal=key='opensesame' \
|
|||
|
--namespace=default
|
|||
|
clusterid:630372ccdc720a92c681fb928f27b53f 是集群的 ID,当制备卷时, Heketi 将会使用这个文件
|
|||
|
gidMin,gidMax:StorageClass GID 范围的最小值和最大值,这是 gidMin 和 gidMax 的默认值
|
|||
|
volumetype:卷的类型及其参数可以用这个可选值进行配置
|
|||
|
'Replica volume': volumetype: replicate:3 其中 '3' 是 replica 数量
|
|||
|
'Disperse/EC volume': volumetype: disperse:4:2 其中 '4' 是数据,'2' 是冗余数量
|
|||
|
'Distribute volume': volumetype: none
|
|||
|
```
|
|||
|
|
|||
|
#### 3.使用
|
|||
|
|
|||
|
创建storageclass文件:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@master class]# cat storageclass
|
|||
|
apiVersion: storage.k8s.io/v1
|
|||
|
kind: StorageClass
|
|||
|
metadata:
|
|||
|
name: example-nfs
|
|||
|
provisioner: example.com/external-nfs
|
|||
|
parameters:
|
|||
|
server: 10.0.0.230
|
|||
|
path: /kubernetes-3
|
|||
|
readOnly: "false"
|
|||
|
```
|
|||
|
|
|||
|
创建:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@master class]# kubectl create -f storageclass
|
|||
|
storageclass.storage.k8s.io/example-nfs created
|
|||
|
```
|
|||
|
|
|||
|
查看:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@master class]# kubectl get storageclass
|
|||
|
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
|
|||
|
example-nfs example.com/external-nfs Delete Immediate false 9s
|
|||
|
|
|||
|
RECLAIMPOLICY:回收策略 Delete
|
|||
|
VOLUMEBINDINGMODE:默认情况下, Immediate 模式表示一旦创建了PersistentVolumeClaim 也就完成了卷绑定和动态制备
|
|||
|
```
|
|||
|
|
|||
|
创建pv的yaml文件:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@master class]# cat pv.yaml
|
|||
|
apiVersion: v1
|
|||
|
kind: PersistentVolume
|
|||
|
metadata:
|
|||
|
name: xingdian-1
|
|||
|
spec:
|
|||
|
capacity:
|
|||
|
storage: 10Gi
|
|||
|
volumeMode: Filesystem
|
|||
|
accessModes:
|
|||
|
- ReadWriteOnce
|
|||
|
storageClassName: example-nfs
|
|||
|
nfs:
|
|||
|
path: /kubernetes-1
|
|||
|
server: 10.0.0.230
|
|||
|
```
|
|||
|
|
|||
|
创建:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@master class]# kubectl create -f pv.yaml
|
|||
|
```
|
|||
|
|
|||
|
查看pv:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@master class]# kubectl get pv
|
|||
|
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
|
|||
|
xingdian-1 10Gi RWO Retain Available example-nfs 3s
|
|||
|
```
|
|||
|
|
|||
|
创建应用使用:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@master class]# cat nginx.yaml
|
|||
|
apiVersion: apps/v1
|
|||
|
kind: StatefulSet
|
|||
|
metadata:
|
|||
|
name: web
|
|||
|
spec:
|
|||
|
selector:
|
|||
|
matchLabels:
|
|||
|
app: nginx
|
|||
|
serviceName: "nginx"
|
|||
|
replicas: 1
|
|||
|
template:
|
|||
|
metadata:
|
|||
|
labels:
|
|||
|
app: nginx
|
|||
|
spec:
|
|||
|
terminationGracePeriodSeconds: 10
|
|||
|
containers:
|
|||
|
- name: nginx
|
|||
|
image: 10.0.0.230/xingdian/nginx:v1
|
|||
|
ports:
|
|||
|
- containerPort: 80
|
|||
|
name: web
|
|||
|
volumeMounts:
|
|||
|
- name: www
|
|||
|
mountPath: /usr/share/nginx/html
|
|||
|
volumeClaimTemplates:
|
|||
|
- metadata:
|
|||
|
name: www
|
|||
|
spec:
|
|||
|
accessModes: [ "ReadWriteOnce" ]
|
|||
|
storageClassName: "example-nfs"
|
|||
|
resources:
|
|||
|
requests:
|
|||
|
storage: 10Gi
|
|||
|
```
|
|||
|
|
|||
|
创建:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@master class]# kubectl create -f nginx.yaml
|
|||
|
statefulset.apps/web created
|
|||
|
```
|
|||
|
|
|||
|
查看:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@master class]# kubectl get statefulset
|
|||
|
NAME READY AGE
|
|||
|
web 1/1 9s
|
|||
|
[root@master class]# kubectl get pod
|
|||
|
NAME READY STATUS RESTARTS AGE
|
|||
|
web-0 1/1 Running 0 13s
|
|||
|
```
|
|||
|
|
|||
|
验证pv:
|
|||
|
|
|||
|
```shell
|
|||
|
[root@master class]# kubectl get pv
|
|||
|
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
|
|||
|
xingdian-1 10Gi RWO Retain Bound default/www-web-0 example-nfs 52s
|
|||
|
```
|
|||
|
|
|||
|
![image-20220526224804444](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220526224804444-16535764908601.png)
|