美文网首页
k8s-StatefulSet

k8s-StatefulSet

作者: 小李飞刀_lql | 来源:发表于2021-11-24 07:01 被阅读0次

无状态与有状态

001 Deployment控制器设计原则:管理的所有Pod一模一样,提供同一个服务,也不考虑在哪台Node运行,可随意     扩容和缩容。这种应用称为“无状态”,例如Web服务
002 在实际的场景中,并不能满足所有应用,尤其是分布式应用,会部署多个实例,这些实例之间往往有依赖关系,例     如主从关系、主备关系,这种应用称为“有状态”,例如MySQL主从、Etcd集群

StatefulSet 控制器概述

StatefulSet控制器用于部署有状态应用,满足一些有状态应用的需求:
001 Pod有序的部署、扩容、删除和停止
002 Pod分配一个稳定的且唯一的网络标识
003 Pod分配一个独享的存储

StatefulSet 控制器:网络标识

稳定的网络标识

001 使用Headless Service(相比普通Service只是将spec.clusterIP定义为None)来维护Pod网络身份,会     为每个Pod分配一个数字编号并且按照编号顺序部署
002 还需要在StatefulSet添加serviceName: “xxx”字段指定StatefulSet控制器要使用这个Headless         Service

稳定主要体现在主机名和Pod A记录

001 主机名:<statefulset名称>-<编号>
002 Pod DNS A记录:<statefulset名称-编号>.<service-name> .<namespace>.svc.cluster.local

StatefulSet 控制器:独享存储

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

StatefulSet示例

Service

apiVersion: v1
kind: Service
metadata:
  name: statefulweb
  namespace: default
spec:
  clusterIP: None
  ports:
  - port: 80       # service端口
    protocol: TCP  # 协议
    targetPort: 80 # 容器端口
  selector:        # 标签选择器
    app: nginx      # 指定关联Pod的标签
  type: ClusterIP  # 服务类型

Statefulpod

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: statefulpod
spec:
  serviceName: "statefulweb"
  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
          
-----------------------------------------------------------------------------
先启动StorageClass相关对象,即【基于NFS实现PV动态供给】
[root@k8smaster nfs]# kubectl get pod
NAME                                      READY   STATUS    RESTARTS   AGE
nfs-client-provisioner-5f98b5cdfb-rm7dd   1/1     Running   0          84s

服务启动

#服务按序号依次启动
[root@k8smaster statefulset]# kubectl get pod 
NAME                                      READY   STATUS    RESTARTS   AGE
statefulpod-0                             1/1     Running   0          85s
statefulpod-1                             1/1     Running   0          65s
statefulpod-2                             1/1     Running   0          44s

#CLUSTER-IP为None
[root@k8smaster statefulset]# kubectl get  svc
NAME          TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes    ClusterIP   10.96.0.1    <none>        443/TCP   24d
statefulweb   ClusterIP   None         <none>        80/TCP    2m32s

验证网络标志

#查看主机名
[root@k8smaster statefulset]# kubectl exec statefulpod-0 -- hostname
statefulpod-0
[root@k8smaster statefulset]# kubectl exec statefulpod-1 -- hostname 
statefulpod-1
[root@k8smaster statefulset]# kubectl exec statefulpod-2 -- hostname 
statefulpod-2

#测试A记录解析
[root@k8smaster statefulset]# kubectl run -i -t dns-test  --rm --image busybox:1.28.4 -- sh
If you don't see a command prompt, try pressing enter.
/ # nslookup statefulweb
Server:    10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name:      statefulweb
Address 1: 10.244.249.41 statefulpod-0.statefulweb.default.svc.cluster.local
Address 2: 10.244.249.60 statefulpod-2.statefulweb.default.svc.cluster.local
Address 3: 10.244.249.43 statefulpod-1.statefulweb.default.svc.cluster.local

/ # ping statefulpod-0.statefulweb.default.svc.cluster.local
PING statefulpod-0.statefulweb.default.svc.cluster.local (10.244.249.41): 56 data bytes
64 bytes from 10.244.249.41: seq=0 ttl=63 time=0.105 ms
64 bytes from 10.244.249.41: seq=1 ttl=63 time=0.162 ms
 
/ # ping statefulpod-1.statefulweb.default.svc.cluster.local
PING statefulpod-1.statefulweb.default.svc.cluster.local (10.244.249.43): 56 data bytes
64 bytes from 10.244.249.43: seq=0 ttl=63 time=0.087 ms
64 bytes from 10.244.249.43: seq=1 ttl=63 time=0.090 ms
 
/ # ping statefulpod-2.statefulweb.default.svc.cluster.local
PING statefulpod-2.statefulweb.default.svc.cluster.local (10.244.249.60): 56 data bytes
64 bytes from 10.244.249.60: seq=0 ttl=63 time=0.081 ms
64 bytes from 10.244.249.60: seq=1 ttl=63 time=0.115 ms

验证独立存储

[root@k8smaster statefulset]# kubectl get  pvc
NAME                STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS          
www-statefulpod-0   Bound    pvc-1d88760c-8207-4e6e-9f81-520cce33051f   1Gi        RWO            managed-nfs-storage  
www-statefulpod-1   Bound    pvc-bd7d64f7-31af-4484-a5e4-d7d3bc8ecf2f   1Gi        RWO            managed-nfs-storage   
www-statefulpod-2   Bound    pvc-6256144d-7dac-4162-8d85-fda6e128b899   1Gi        RWO            managed-nfs-storage   

[root@k8smaster statefulset]# kubectl get  pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM                       STORAGECLASS          REASON   
pvc-1d88760c-8207-4e6e-9f81-520cce33051f   1Gi        RWO            Delete           Bound       default/www-statefulpod-0   managed-nfs-storage            
pvc-6256144d-7dac-4162-8d85-fda6e128b899   1Gi        RWO            Delete           Bound       default/www-statefulpod-2   managed-nfs-storage            
pvc-bd7d64f7-31af-4484-a5e4-d7d3bc8ecf2f   1Gi        RWO            Delete           Bound       default/www-statefulpod-1   managed-nfs-storage            


[root@k8smaster statefulset]# kubectl get pod -o wide

statefulpod-0  1/1     Running  10.244.249.41   k8snode1   
statefulpod-1  1/1     Running  10.244.249.43   k8snode1   
statefulpod-2  1/1     Running  10.244.249.60   k8snode1   
[root@k8smaster statefulset]# cd /ifs/kubernetes/
#在对应的3个目录下分别编辑index.html

[root@k8smaster statefulset]# curl 10.244.249.41
hello statefulpod-0
[root@k8smaster statefulset]# curl 10.244.249.43
hello statefulpod-1
[root@k8smaster statefulset]# curl 10.244.249.60
hello statefulpod-2

StatefulSet 控制器:小结

StatefulSet与Deployment区别:有身份的!
身份三要素:
001 域名
002 主机名
003 存储(PVC)

相关文章

  • k8s-StatefulSet

    无状态与有状态 StatefulSet 控制器概述 StatefulSet 控制器:网络标识 稳定的网络标识 稳定...

网友评论

      本文标题:k8s-StatefulSet

      本文链接:https://www.haomeiwen.com/subject/sptntrtx.html