博客园  :: 首页  :: 管理

本文的前置文章:《关于Kubernetes-v1.23.6-资源调度-StatefulSet-定义一个有状态服务》

https://www.cnblogs.com/5201351/p/17724823.html

 

扩容的缩容的方法一,只需要修改下方命令的数即可,其中web为sts的名称

kubectl scale statefulset web --replicas=5

然后我们可以通过查看pods的信息,看到数量上的变化

[root@k8s-master ~]# kubectl get po
NAME       READY   STATUS              RESTARTS        AGE
dns-test   1/1     Running             1 (3h24m ago)   3h26m
web-0      1/1     Running             0               3h33m
web-1      1/1     Running             0               3h33m
web-2      0/1     ContainerCreating   0               2s
[root@k8s-master ~]# kubectl get po
NAME       READY   STATUS              RESTARTS        AGE
dns-test   1/1     Running             1 (3h24m ago)   3h26m
web-0      1/1     Running             0               3h34m
web-1      1/1     Running             0               3h33m
web-2      1/1     Running             0               6s
web-3      1/1     Running             0               4s
web-4      0/1     ContainerCreating   0               1s
[root@k8s-master ~]# kubectl get po
NAME       READY   STATUS    RESTARTS        AGE
dns-test   1/1     Running   1 (3h24m ago)   3h26m
web-0      1/1     Running   0               3h34m
web-1      1/1     Running   0               3h33m
web-2      1/1     Running   0               8s
web-3      1/1     Running   0               6s
web-4      1/1     Running   0               3s
[root@k8s-master ~]#

另外一种扩容和缩容的方式为:kubectl patch statefulset web -p '{"spec":{"replicas":7}}'

[root@k8s-master ~]# kubectl patch statefulset web -p '{"spec":{"replicas":7}}'
statefulset.apps/web patched
[root@k8s-master ~]# kubectl get sts
NAME   READY   AGE
web    7/7     3h40m
[root@k8s-master ~]# kubectl patch statefulset web -p '{"spec":{"replicas":3}}'
statefulset.apps/web patched
[root@k8s-master ~]# kubectl get po
NAME       READY   STATUS    RESTARTS        AGE
dns-test   1/1     Running   1 (3h30m ago)   3h32m
web-0      1/1     Running   0               3h40m
web-1      1/1     Running   0               3h40m
web-2      1/1     Running   0               6m32s
[root@k8s-master ~]#

镜像更新(资料中提到目前还不支持直接更新 image,需要 patch 来间接实现)

kubectl patch sts web --type='json' -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/image", "value":"nginx:1.9.1"}]'

执行后,所有的pods的镜像版本都会升级成 1.9.1 

最后笔者也尝试过,直接kubectl edit sts web 的方式修改 - image: nginx:1.9.1 ,也是有效的

查看sts历史版本记录及更新信息:

[root@k8s-master ~]# kubectl rollout history sts web
statefulset.apps/web
REVISION  CHANGE-CAUSE
3         <none>
9         <none>
10        <none>

[root@k8s-master ~]# kubectl rollout history sts web --revision=10
statefulset.apps/web with revision #10
Pod Template:
  Labels:       app=nginx
  Containers:
   nginx:
    Image:      nginx:1.9.1
    Port:       80/TCP
    Host Port:  0/TCP
    Environment:        <none>
    Mounts:     <none>
  Volumes:      <none>

[root@k8s-master ~]#

 

  

 

尊重别人的劳动成果 转载请务必注明出处:https://www.cnblogs.com/5201351/p/17725087.html