k8s StatefulSet控制器-独立存储

时间:2023-03-09 07:52:03
k8s StatefulSet控制器-独立存储

k8s-StatefulSet控制器-独立存储

1. StatefulSet控制器-独立存储

  • 独享存储:StatefulSet的存储卷使用VolumeClaimTemplate创建,称为卷申请模板,当StatefulSet使用VolumeClaimTemplate创建一个PersistentVolume时,同样也会为每个Pod分配并创建一个编号的PVC,每
  • 个PVC绑定对应的PV,从而保证每个Pod都拥有独立的存储。

1.1 headless service示例

apiVersion: v1
kind: Service
metadata:
name: web
namespace: default
spec:
clusterIP: None #无头服务
selector: #容器选择器
app: nginx # 指定关联pod名称
ports:
- protocol: TCP # 协议
port: 80 # service 端口
targetPort: 80 # 容器端口
type: ClusterIP # 服务类型

1.2 statefulset 独享存储文件

apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
serviceName: "web" # 绑定的服务
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
name: web
# 独享存储
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
spec:
storageClassName: "managed-nfs-storage"
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi

2. 案例

2. 1 编写headless service示例

[root@k8s-master statefulset]# vim headless-service.yaml
[root@k8s-master statefulset]# cat headless-service.yaml
apiVersion: v1
kind: Service
metadata:
name: web
namespace: default
spec:
clusterIP: None #无头服务
selector: #容器选择器
app: nginx # 指定关联pod名称
ports:
- protocol: TCP # 协议
port: 80 # service 端口
targetPort: 80 # 容器端口
type: ClusterIP # 服务类型

2.2 编写statefulset 独享存储文件

[root@k8s-master statefulset]# vim statefulset_pod.yaml
[root@k8s-master statefulset]# cat statefulset_pod.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
serviceName: "web" # 绑定的服务
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
spec:
storageClassName: "managed-nfs-storage"
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi

2.3 启动配置文件

[root@k8s-master statefulset]# kubectl apply -f statefulset_pod.yaml
statefulset.apps/web configured

2.4 验证是否启动

[root@k8s-master statefulset]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nfs-client-provisioner-95c9579-qslkg 1/1 Running 0 5h6m
web-0 1/1 Running 0 17m
web-1 1/1 Running 0 17m
web-2 1/1 Running 0 16m

2.5 验证pvc和pv

[root@k8s-master statefulset]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
my-pvc Bound my-pv 5Gi RWX 3d12h
test-claim Bound pvc-998a4c34-f686-4e46-80bf-6d32c005111d 1Gi RWX managed-nfs-storage 2d9h
www-web-0 Bound pvc-3494846e-cfaa-4334-8080-1479a583cb53 1Gi RWO managed-nfs-storage 15m
www-web-1 Bound pvc-fe224ed6-b3e2-4359-975d-d2a5bd22a3a1 1Gi RWO managed-nfs-storage 15m
www-web-2 Bound pvc-a56d4952-bbbe-49c0-8208-7126fc392b20 1Gi RWO managed-nfs-storage 15m [root@k8s-master statefulset]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
my-pv 5Gi RWX Retain Terminating default/my-pvc 3d12h
pvc-3494846e-cfaa-4334-8080-1479a583cb53 1Gi RWO Delete Bound default/www-web-0 managed-nfs-storage 15m
pvc-998a4c34-f686-4e46-80bf-6d32c005111d 1Gi RWX Delete Bound default/test-claim managed-nfs-storage 2d9h
pvc-a56d4952-bbbe-49c0-8208-7126fc392b20 1Gi RWO Delete Bound default/www-web-2 managed-nfs-storage 15m
pvc-fe224ed6-b3e2-4359-975d-d2a5bd22a3a1 1Gi RWO Delete Bound default/www-web-1 managed-nfs-storage 15m
test-pv 1Gi RWX Retain Available 22m
  • 验证nfs存储的

    [root@k8s-node3 ~]# cd /ifs/kubernetes/
    [root@k8s-node3 kubernetes]# ll -a
    总用量 4
    drwxr-xr-x 6 root root 289 1月 2 21:30 .
    drwxr-xr-x 3 root root 24 12月 28 17:36 ..
    drwxrwxrwx 2 root root 24 12月 31 12:37 default-test-claim-pvc-998a4c34-f686-4e46-80bf-6d32c005111d
    drwxrwxrwx 2 root root 6 1月 2 21:30 default-www-web-0-pvc-3494846e-cfaa-4334-8080-1479a583cb53
    drwxrwxrwx 2 root root 6 1月 2 21:30 default-www-web-1-pvc-fe224ed6-b3e2-4359-975d-d2a5bd22a3a1
    drwxrwxrwx 2 root root 6 1月 2 21:30 default-www-web-2-pvc-a56d4952-bbbe-49c0-8208-7126fc392b20
    -rw-r--r-- 1 root root 22 12月 28 20:17 index.html

    注释: 在这里我们可以看到有web的存储路径

  • 往这三个路径写入数据

    echo  ' hello pod 00' > default-www-web-0-pvc-3494846e-cfaa-4334-8080-1479a583cb53/index.html
    
    echo  ' hello pod 01' > default-www-web-1-pvc-fe224ed6-b3e2-4359-975d-d2a5bd22a3a1/index.html
    
    echo  ' hello pod 02' > default-www-web-2-pvc-a56d4952-bbbe-49c0-8208-7126fc392b20/index.html
  • 验证三个路径里面的数据

    [root@k8s-node3 kubernetes]# cat default-www-web-0-pvc-3494846e-cfaa-4334-8080-1479a583cb53/index.html
    hello pod 00
    [root@k8s-node3 kubernetes]# cat default-www-web-1-pvc-fe224ed6-b3e2-4359-975d-d2a5bd22a3a1/index.html
    hello pod 01
    [root@k8s-node3 kubernetes]# cat default-www-web-2-pvc-a56d4952-bbbe-49c0-8208-7126fc392b20/index.html
    hello pod 02

2.6 验证一下请求数据

  • 查看pod的IP

    [root@k8s-master statefulset]# kubectl get pods -o wide
    NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
    nfs-client-provisioner-95c9579-qslkg 1/1 Running 0 5h19m 10.244.36.92 k8s-node1 <none> <none>
    web-0 1/1 Running 0 29m 10.244.107.220 k8s-node3 <none> <none>
    web-1 1/1 Running 0 29m 10.244.36.94 k8s-node1 <none> <none>
    web-2 1/1 Running 0 29m 10.244.169.149 k8s-node2 <none> <none>
  • 模拟浏览器请求

    [root@k8s-master statefulset]# curl 10.244.107.220
    hello pod 00
    [root@k8s-master statefulset]# curl 10.244.36.94
    hello pod 01
    [root@k8s-master statefulset]# curl 10.244.169.149
    hello pod 02

3 案例-删除应用测试

3.1 删除pod服务

[root@k8s-master statefulset]# kubectl delete -f statefulset_pod.yaml
statefulset.apps "web" deleted

3.2 验证服务是否存在

[root@k8s-master statefulset]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nfs-client-provisioner-95c9579-qslkg 1/1 Running 0 5h27m

注释:发现服务已经删除了

3.3 验证pv和pvc是否存在

[root@k8s-master statefulset]# kubectl get pv,pvc
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/my-pv 5Gi RWX Retain Terminating default/my-pvc 3d12h
persistentvolume/pvc-3494846e-cfaa-4334-8080-1479a583cb53 1Gi RWO Delete Bound default/www-web-0 managed-nfs-storage 39m
persistentvolume/pvc-998a4c34-f686-4e46-80bf-6d32c005111d 1Gi RWX Delete Bound default/test-claim managed-nfs-storage 2d9h
persistentvolume/pvc-a56d4952-bbbe-49c0-8208-7126fc392b20 1Gi RWO Delete Bound default/www-web-2 managed-nfs-storage 39m
persistentvolume/pvc-fe224ed6-b3e2-4359-975d-d2a5bd22a3a1 1Gi RWO Delete Bound default/www-web-1 managed-nfs-storage 39m
persistentvolume/test-pv 1Gi RWX Retain Available 45m NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/my-pvc Bound my-pv 5Gi RWX 3d12h
persistentvolumeclaim/test-claim Bound pvc-998a4c34-f686-4e46-80bf-6d32c005111d 1Gi RWX managed-nfs-storage 2d9h
persistentvolumeclaim/www-web-0 Bound pvc-3494846e-cfaa-4334-8080-1479a583cb53 1Gi RWO managed-nfs-storage 39m
persistentvolumeclaim/www-web-1 Bound pvc-fe224ed6-b3e2-4359-975d-d2a5bd22a3a1 1Gi RWO managed-nfs-storage 39m
persistentvolumeclaim/www-web-2 Bound pvc-a56d4952-bbbe-49c0-8208-7126fc392b20 1Gi RWO managed-nfs-storage 39m

注释:发现服务还存在

3.4 重新启动服务看看

  • 重启服务

    [root@k8s-master statefulset]# kubectl apply -f statefulset_pod.yaml
    statefulset.apps/web created
  • 验证IP是否发生变化

    # 验证IP
    [root@k8s-master statefulset]# kubectl get pods -o wide
    NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
    nfs-client-provisioner-95c9579-qslkg 1/1 Running 0 5h19m 10.244.36.92 k8s-node1 <none> <none>
    web-0 1/1 Running 0 29m 10.244.107.220 k8s-node3 <none> <none>
    web-1 1/1 Running 0 29m 10.244.36.94 k8s-node1 <none> <none>
    web-2 1/1 Running 0 29m 10.244.169.149 k8s-node2 <none> <none> #重启后的
    [root@k8s-master statefulset]# kubectl get pods -o wide
    NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
    nfs-client-provisioner-95c9579-qslkg 1/1 Running 0 5h31m 10.244.36.92 k8s-node1 <none> <none>
    web-0 1/1 Running 0 82s 10.244.107.221 k8s-node3 <none> <none>
    web-1 1/1 Running 0 77s 10.244.36.95 k8s-node1 <none> <none>
    web-2 1/1 Running 0 71s 10.244.169.150 k8s-node2 <none> <none>

    注释: 发现已经发现变化了

  • 我们验证一下数据是否一样

    [root@k8s-master statefulset]# curl 10.244.107.221
    hello pod 00
    [root@k8s-master statefulset]# curl 10.244.36.95
    hello pod 01
    [root@k8s-master statefulset]# curl 10.244.169.150
    hello pod 02

    注释: 看到数据是没有发生变化的