首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >调整命名空间所有deploy和sts的副本数

调整命名空间所有deploy和sts的副本数

原创
作者头像
三耳听风
发布于 2026-11-12 17:02:26
发布于 2026-11-12 17:02:26
1360
举报

调整命名空间所有deploy和sts的副本数

原文出处:https://www.niewx.cn/mybook/tke/adjust-workload-replicas-under-the-namespace.html(原站栏目「TKE运维笔记」/《【tke】调整命名空间所有deploy和sts的副本数》)

有时候我们为了测试我们的服务,不需要他一直运行着,当我们测试完,就希望将服务都停掉,避免占用集群资源,一般都是将负载的副本数设置为0,但是又希望对应的deployment还存在集群中,这样下次测试直接调整副本数就可以运行服务了。

下面提供了一个脚本scale-workload-replicas.sh可以调整命名空间的所有的deployment和statefulset的副本数量

代码语言:plaintext
复制
#!/bin/sh

ns=$1
replicas=$2

if [ $# = 0 ];then
  echo "Run 'sh scale-workload-replicas.sh --h' for more information on a command."
fi

if [[ $1 = "--h" ]];then
  echo "Please enter the first parameter enters the namespace, the second parameter enters the number of replicas 

Usage: sh scale-workload-replicas.sh [namespace] [replicas]"
fi

main(){

for i in `kubectl get deploy,sts -n $ns |awk -F ' ' '{print $1}'  | grep -v NAME`;
do
   kubectl scale --replicas=$replicas -n $ns $i
done

}

if [ $# = 2 ];then

main

fi

如果我们需要停掉这个命名空间下所有服务,则执行脚本第一个参数输入命名空间,第二个参数输入副本数0

代码语言:bash
复制
[root@VM-0-13-centos script]# kubectl get pod -n mesh
NAME                        READY   STATUS    RESTARTS   AGE
client-6fd64cfc6b-q9ml7     2/2     Running   0          4d23h
nginx-5dbf784b68-bmr86      2/2     Running   0          16d
nginx-v1-647887d8fd-qllgx   2/2     Running   0          39d
nginx-v2-bcccc8f9f-jb926    2/2     Running   0          39d
sleep-7f474bbcbd-tdgmf      2/2     Running   0          39d
[root@VM-0-13-centos script]# sh scale-workload-replicas.sh mesh 0
deployment.apps/client scaled
deployment.apps/nginx scaled
deployment.apps/nginx-v1 scaled
deployment.apps/nginx-v2 scaled
deployment.apps/sleep scaled
[root@VM-0-13-centos script]# kubectl get pod -n mesh
No resources found.

如果需要拉起我们的服务,也只需要执行脚本,第一个参数输入命名空间,第二个参数输入副本数(输入1代表所有负载起一个副本)

代码语言:bash
复制
[root@VM-0-13-centos script]# sh scale-workload-replicas.sh mesh 1
deployment.apps/client scaled
deployment.apps/nginx scaled
deployment.apps/nginx-v1 scaled
deployment.apps/nginx-v2 scaled
deployment.apps/sleep scaled
[root@VM-0-13-centos script]# kubectl get pod -n mesh
NAME                        READY   STATUS    RESTARTS   AGE
client-6fd64cfc6b-ggz58     2/2     Running   0          23s
nginx-5dbf784b68-c22hl      2/2     Running   0          22s
nginx-v1-647887d8fd-n7bfh   2/2     Running   0          22s
nginx-v2-bcccc8f9f-lnm6q    2/2     Running   0          22s
sleep-7f474bbcbd-z2jj7      2/2     Running   0          22s

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

目录
  • 调整命名空间所有deploy和sts的副本数
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档