StatefulSet有状态应用副本集

30 篇文章 2 订阅
19 篇文章 1 订阅

statefulset有状态应用副本集
PetSet -> StatefulSet
1.文档且唯一的网络标识符
2.稳定且持久的存储
3.有序,平滑的部署和扩展
4.有序,平滑的删除和终止
5.有序的滚动更新

三个组件:
headless service
StatefulSet
volumeClaimTemplate

实验的前期准备条件:
master;192.168.68.10
node1:    192.168.68.20
node2: 192.168.68.30
node3:    192.168.68.40

node3准备:解析node3到本机,同步其他的master和node
各个节点安装NFS
NFS目录为:
[root@node3 /]# tree data
data
└── volumes
    ├── index.html
    ├── v1
    ├── v2
    │   └── index.html
    ├── v3
    ├── v4
    └── v5

启动nfs
systemctl start nfs

查看pvc
[root@master configmap]# kubectl get pv
NAME      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM           STORAGECLASS   REASON    AGE
pv001     5Gi        RWO,RWX        Retain           Available                                            1d
pv002     7Gi        RWO,RWX        Retain           Bound       default/mypvc                            1d
pv003     8Gi        RWO,RWX        Retain           Available                                            1d
pv004     10Gi       RWO,RWX        Retain           Available                                            1d
pv005     12Gi       RWO,RWX        Retain           Available                                            1d
[root@master configmap]# kubectl get pvc
NAME      STATUS    VOLUME    CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mypvc     Bound     pv002     7Gi        RWO,RWX                       1d

删除已经挂载的pvc

kubectl get pvc
kubectl delete pvc/mypvc

kubectl get pv
[root@master configmap]# kubectl get pv
NAME      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM           STORAGECLASS   REASON    AGE
pv001     5Gi        RWO,RWX        Retain           Available                                            1d
pv002     7Gi        RWO,RWX        Retain           Released    default/mypvc                            1d
pv003     8Gi        RWO,RWX        Retain           Available                                            1d
pv004     10Gi       RWO,RWX        Retain           Available                                            1d
pv005     12Gi       RWO,RWX        Retain           Available                                            1d

Released    default/mypvc  状态是已经释放了


删除所有的pv
kubectl delete pv --all

[root@master configmap]# kubectl delete pv --all
persistentvolume "pv001" deleted
persistentvolume "pv002" deleted
persistentvolume "pv003" deleted
persistentvolume "pv004" deleted
persistentvolume "pv005" deleted
[root@master configmap]# kubectl get pv
No resources found.

开始重新创建pv
创建5个节点,5个PV

[root@master volumes]# cat pvs-demo.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv001
  labels:
    name: pv001
spec:
  nfs:
    path: /data/volumes/v1
    server: node3
  accessModes: ["ReadWriteOnce"]
  capacity:
    storage: 5Gi
--- 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv002
  labels:
    name: pv002
spec:
  nfs:
    path: /data/volumes/v2
    server: node3
  accessModes: ["ReadWriteMany","ReadWriteOnce"]
  capacity:
    storage: 5Gi
--- 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv003
  labels:
    name: pv003
spec:
  nfs:
    path: /data/volumes/v1
    server: node3
  accessModes: ["ReadWriteMany","ReadWriteOnce"]
  capacity:
    storage: 5Gi
--- 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv004
  labels:
    name: pv004
spec:
  nfs:
    path: /data/volumes/v4
    server: node3
  accessModes: ["ReadWriteMany","ReadWriteOnce"]
  capacity:
    storage: 5Gi
--- 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv005
  labels:
    name: pv005
spec:
  nfs:
    path: /data/volumes/v5
    server: node3
  accessModes: ["ReadWriteMany","ReadWriteOnce"]
  capacity:
    storage: 5Gi
--- 
[root@master volumes]# kubectl apply -f pvs-demo.yaml 
persistentvolume/pv001 created
persistentvolume/pv002 created
persistentvolume/pv003 created
persistentvolume/pv004 created
persistentvolume/pv005 created

[root@master volumes]# kubectl get pv
NAME      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM     STORAGECLASS   REASON    AGE
pv001     5Gi        RWO,RWX        Retain           Available                                      21s
pv002     5Gi        RWO            Retain           Available                                      21s
pv003     5Gi        RWO,RWX        Retain           Available                                      21s
pv004     5Gi        RWO,RWX        Retain           Available                                      21s
pv005     5Gi        RWO,RWX        Retain           Available                                      21s
yaml文件内容:
[root@master volumes]# cat stateful-demo-1.yaml 
apiVersion: v1
kind: Service
metadata:
  name: myapp
  labels:
    app: myapp
spec:
  ports: 
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: myapp-pod
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: myapp
spec:
  serviceName: myapp
  replicas: 3
  selector:
    matchLabels:
      app: myapp-pod
  template:
    metadata:
      labels:
        app: myapp-pod
    spec:
      containers:
      - name: myapp
        image: ikubernetes/myapp:v1
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: myappdata
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: myappdata
    spec:
      accessModes: [ "ReadWriteOnce" ]  
      resources:
        requests:
          storage: 2Gi

[root@master volumes]# kubectl apply -f stateful-demo.yaml 
service/myapp created
statefulset.apps/myapp created


查看状态

[root@master volumes]# kubectl get pods
NAME      READY     STATUS    RESTARTS   AGE
myapp-0   1/1       Running   0          6s
myapp-1   1/1       Running   0          5s
myapp-2   1/1       Running   0          3s
[root@master volumes]# kubectl get pods
NAME      READY     STATUS    RESTARTS   AGE
myapp-0   1/1       Running   0          11s
myapp-1   1/1       Running   0          10s
myapp-2   1/1       Running   0          8s
[root@master volumes]# kubectl get pvc
NAME                STATUS    VOLUME    CAPACITY   ACCESS MODES   STORAGECLASS   AGE
myappdata-myapp-0   Bound     pv002     5Gi        RWO                           24s
myappdata-myapp-1   Bound     pv004     5Gi        RWO,RWX                       23s
myappdata-myapp-2   Bound     pv001     5Gi        RWO,RWX                       21s
[root@master volumes]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   8d
myapp        ClusterIP   None         <none>        80/TCP    1m



[root@master volumes]# kubectl get pv
NAME      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM                       STORAGECLASS   REASON    AGE
pv001     5Gi        RWO,RWX        Retain           Bound       default/myappdata-myapp-2                            15m
pv002     5Gi        RWO            Retain           Bound       default/myappdata-myapp-0                            15m
pv003     5Gi        RWO,RWX        Retain           Available                                                        15m
pv004     5Gi        RWO,RWX        Retain           Bound       default/myappdata-myapp-1                            15m
pv005     5Gi        RWO,RWX        Retain           Available                                                        15m

通过上面发现:
pods 的名字是自己定义的
自动关联三个5G的空间
自动绑定空间

[root@master volumes]# kubectl get sts
NAME      DESIRED   CURRENT   AGE
myapp     3         3         15m

测试删除:

[root@master volumes]# kubectl delete -f stateful-demo-1.yaml 
service "myapp" deleted
statefulset.apps "myapp" deleted

通过监控发现:
kubectl get pods -w

[root@master ~]# kubectl get pods -w
NAME      READY     STATUS    RESTARTS   AGE
myapp-0   1/1       Running   0          21m
myapp-1   1/1       Running   0          20m
myapp-2   1/1       Running   0          20m
myapp-1   1/1       Terminating   0         21m
myapp-0   1/1       Terminating   0         21m
myapp-2   1/1       Terminating   0         21m
myapp-1   0/1       Terminating   0         21m
myapp-2   0/1       Terminating   0         21m
myapp-0   0/1       Terminating   0         21m
myapp-0   0/1       Terminating   0         21m
myapp-0   0/1       Terminating   0         21m
myapp-2   0/1       Terminating   0         21m
myapp-2   0/1       Terminating   0         21m
myapp-1   0/1       Terminating   0         21m
myapp-1   0/1       Terminating   0         21m

创建测试

[root@master volumes]# kubectl apply -f stateful-demo-1.yaml 
service/myapp created
statefulset.apps/myapp created

[root@master ~]# kubectl get pods -w
NAME      READY     STATUS    RESTARTS   AGE
myapp-0   0/1       Pending   0          0s
myapp-0   0/1       Pending   0         0s
myapp-0   0/1       ContainerCreating   0         0s
myapp-0   1/1       Running   0         1s
myapp-1   0/1       Pending   0         0s
myapp-1   0/1       Pending   0         0s
myapp-1   0/1       ContainerCreating   0         0s
myapp-1   1/1       Running   0         0s
myapp-2   0/1       Pending   0         0s
myapp-2   0/1       Pending   0         0s
myapp-2   0/1       ContainerCreating   0         0s
myapp-2   1/1       Running   0         2s

###################################
由此发现,删除是2,1,0
创建时0,1,2
无论怎么创建都是绑定的固定的存储卷

###################################

###################################
它支持滚动更新
###################################

注意:每个的pod名称都是可以被解析的
myapp-0都是可以解析的

验证

验证:
[root@master ~]# kubectl get pods
NAME      READY     STATUS    RESTARTS   AGE
myapp-0   1/1       Running   0          5m
myapp-1   1/1       Running   0          5m
myapp-2   1/1       Running   0          5m
[root@master ~]# kubectl exec -it myapp-0 /bin/sh

/ # nslookup myapp-0.myapp.default.svc.cluster.local
nslookup: can't resolve '(null)': Name does not resolve

Name:      myapp-0.myapp.default.svc.cluster.local
Address 1: 10.244.2.65 myapp-0.myapp.default.svc.cluster.local
/ # nslookup myapp-1.myapp.default.svc.cluster.local
nslookup: can't resolve '(null)': Name does not resolve

Name:      myapp-1.myapp.default.svc.cluster.local
Address 1: 10.244.1.67 myapp-1.myapp.default.svc.cluster.local
/ # nslookup myapp-2.myapp.default.svc.cluster.local
nslookup: can't resolve '(null)': Name does not resolve

Name:      myapp-2.myapp.default.svc.cluster.local
Address 1: 10.244.2.66 myapp-2.myapp.default.svc.cluster.local

注意:解析的时候必须跟无头服务
myapp-0 					pod名
myapp   					服务名
default.svc.cluster.local 	名称空间

规则格式:
pod_name.service_name.ns_name.svc.cluster.local

#################
扩容实验:
将myapp服务扩容到5个
[root@master volumes]# kubectl scale sts myapp --replicas=5
statefulset.apps/myapp scale

监控:
[root@master ~]# kubectl get pods -w
NAME      READY     STATUS    RESTARTS   AGE
myapp-0   1/1       Running   0          14m
myapp-1   1/1       Running   0          14m
myapp-2   1/1       Running   0          14m
myapp-3   0/1       Pending   0         0s
myapp-3   0/1       Pending   0         0s
myapp-3   0/1       Pending   0         0s
myapp-3   0/1       ContainerCreating   0         0s
myapp-3   1/1       Running   0         1s
myapp-4   0/1       Pending   0         0s
myapp-4   0/1       Pending   0         0s
myapp-4   0/1       Pending   0         0s
myapp-4   0/1       ContainerCreating   0         0s
myapp-4   1/1       Running   0         1s


[root@master volumes]# kubectl get pods
NAME      READY     STATUS    RESTARTS   AGE
myapp-0   1/1       Running   0          15m
myapp-1   1/1       Running   0          15m
myapp-2   1/1       Running   0          15m
myapp-3   1/1       Running   0          1m
myapp-4   1/1       Running   0          1m

[root@master volumes]# kubectl get pvc
NAME                STATUS    VOLUME    CAPACITY   ACCESS MODES   STORAGECLASS   AGE
myappdata-myapp-0   Bound     pv002     5Gi        RWO                           39m
myappdata-myapp-1   Bound     pv004     5Gi        RWO,RWX                       39m
myappdata-myapp-2   Bound     pv001     5Gi        RWO,RWX                       39m
myappdata-myapp-3   Bound     pv003     5Gi        RWO,RWX                       1m
myappdata-myapp-4   Bound     pv005     5Gi        RWO,RWX                       1m


#################

缩减实验:

[root@master volumes]# kubectl scale sts myapp --replicas=2
或者:
[root@master volumes]# kubectl patch sts myapp -p '{"spec":{"replicas":2}}'
statefulset.apps/myapp patched

监控:可以发现是逆序的
[root@master ~]# kubectl get pods -w
NAME      READY     STATUS    RESTARTS   AGE
myapp-0   1/1       Running   0          17m
myapp-1   1/1       Running   0          17m
myapp-2   1/1       Running   0          17m
myapp-3   1/1       Running   0          2m
myapp-4   1/1       Running   0          2m
myapp-4   1/1       Terminating   0         3m
myapp-4   0/1       Terminating   0         3m
myapp-4   0/1       Terminating   0         3m
myapp-4   0/1       Terminating   0         3m
myapp-3   1/1       Terminating   0         3m
myapp-3   0/1       Terminating   0         3m
myapp-3   0/1       Terminating   0         3m
myapp-3   0/1       Terminating   0         3m
myapp-2   1/1       Terminating   0         18m
myapp-2   0/1       Terminating   0         18m
myapp-2   0/1       Terminating   0         18m
myapp-2   0/1       Terminating   0         18m

[root@master volumes]# kubectl get pods
NAME      READY     STATUS    RESTARTS   AGE
myapp-0   1/1       Running   0          19m
myapp-1   1/1       Running   0          19m
[root@master volumes]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   8d
myapp        ClusterIP   None         <none>        80/TCP    19m
[root@master volumes]# kubectl get pv
NAME      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS    CLAIM                       STORAGECLASS   REASON    AGE
pv001     5Gi        RWO,RWX        Retain           Bound     default/myappdata-myapp-2                            43m
pv002     5Gi        RWO            Retain           Bound     default/myappdata-myapp-0                            43m
pv003     5Gi        RWO,RWX        Retain           Bound     default/myappdata-myapp-3                            43m
pv004     5Gi        RWO,RWX        Retain           Bound     default/myappdata-myapp-1                            43m
pv005     5Gi        RWO,RWX        Retain           Bound     default/myappdata-myapp-4                            43m

#################
更新实验:
金丝雀更新:
先更新部分版本,如果使用没有问题,然后再手动继续更新后面的
例如有五个myapp:
myapp1,myapp2,myapp3,myapp4,myapp5,
我想更新大于3的
partition:N
    >=N
    >=0  所有的都更新


扩展Pod到5个

[root@master volumes]# kubectl patch sts myapp -p '{"spec":{"replicas":5}}'

kubectl patch sts myapp -p '{"spec":{"updateStrategy":{"rollingUpdate":{"partition":4}}}}'

查看更新策略:
[root@master volumes]# kubectl describe sts myapp
Partition:        4


开始更新到V2版本
[root@master volumes]# kubectl set image sts/myapp myapp=ikubernetes/myapp:v2
statefulset.apps/myapp image updated

[root@master volumes]# kubectl get sts -o wide  控制器已经更新到v2
NAME      DESIRED   CURRENT   AGE       CONTAINERS   IMAGES
myapp     5         5         37m       myapp        ikubernetes/myapp:v2

kubectl describe pods myapp-0  到myapp-3
Image:          ikubernetes/myapp:v1

kubectl describe pods myapp-4
Image:          ikubernetes/myapp:v2

如果想更新全部到v2版本:
kubectl patch sts myapp -p '{"spec":{"updateStrategy":{"rollingUpdate":{"partition":0}}}}'
kubectl set image sts/myapp myapp=ikubernetes/myapp:v2
这样就全部更新成功了

 

 

 

StatefulSet是Kubernetes中的版本控制器,用于管理有状态应用程序的部署和更新。它提供以下功能和用途: 1. 有序的实例部署:StatefulSet可以按照定义的顺序逐个启动和停止应用程序的实例。这对于有状态应用程序来说非常重要,因为它们通常具有相互依赖关系,需要按顺序启动和停止。 2. 稳定的网络标识符:每个StatefulSet实例都有一个唯一的标识符,称为稳定的网络标识符(Stable Network Identifier)。这个标识符可以是一个DNS名称或一个持久性标签。它确保了实例在重新启动或迁移后仍然具有相同的标识符,从而使其它应用程序或服务能够可靠地访问它们。 3. 持久化存储:StatefulSet通常与持久卷(Persistent Volume)和持久卷声明(Persistent Volume Claim)结合使用,以提供有状态应用程序的持久化存储。每个实例可以被分配一个独立的持久卷,使得实例在重新启动或迁移后能够保留其数据。 4. 高可用性:StatefulSet确保有状态应用程序的指定副本数始终保持运行。如果有任何实例失败或被删除,StatefulSet会自动创建新的实例来替代它们,以确保应用程序的可用性和健壮性。 总之,StatefulSet作为版本控制器,提供了管理有状态应用程序的部署和更新的能力。它通过有序的实例部署、稳定的网络标识符、持久化存储和高可用性支持,使得管理有状态应用程序变得更加可靠和方便。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值