您的位置:首页 > 财经 > 金融 > 成都旅游最佳季节_网站测试方法_百度代发排名_微信营销软件免费版

成都旅游最佳季节_网站测试方法_百度代发排名_微信营销软件免费版

2025/1/4 17:25:15 来源:https://blog.csdn.net/Cyan_Jiang/article/details/144143983  浏览:    关键词:成都旅游最佳季节_网站测试方法_百度代发排名_微信营销软件免费版
成都旅游最佳季节_网站测试方法_百度代发排名_微信营销软件免费版

在实际生产系统中,经常会遇到某个服务需要扩容的场景,也可能会遇到由于资源紧张或者工作负载降低而需要减少服务实例数量的场景。此时可以利用Deployment/RC的Scale机制来完成这些工作。

Kubernetes对Pod的扩缩容操作提供了手动和自动两种模式,手动模式通过运行kubectl scale命令或通过RESTful API对一个Deployment/RC进行Pod副本数量的设置,即可一键完成。自动模式则需要用户根据某个性能指标或者自定义业务指标,并指定Pod副本数量的范围,系统将自动在这个范围内根据性能指标的变化进行调整。

一、手动扩缩容机制

测试文件,pod副本数量为3个

apiVersion: apps/v1
kind: Deployment                         # Deployment类型
metadata:name: nginx-deployment                 # Deployment名称labels:                                # Deployment标签app: nginx
spec:                                    # Deployment配置replicas: 3                            # Deployment的副本selector:matchLabels:app: nginx                         # Deployment管理的Pod标签template:                              # Pod的定义metadata:labels:                            # Pod的标签app: nginxspec:                                # Pod的配置containers:- name: nginximage: registry.cn-hangzhou.aliyuncs.com/cyanjiang/nginx:1.0ports:- containerPort: 80

创建并查看

[root@k8s-master01 scahpa]#kubectl create -f deploy.yaml
deployment.apps/nginx-deployment created
[root@k8s-master01 scahpa]#kubectl get po
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-5ddfc76b5c-fqlgt   1/1     Running   0          8s
nginx-deployment-5ddfc76b5c-fxldw   1/1     Running   0          8s
nginx-deployment-5ddfc76b5c-qfs2q   1/1     Running   0          8s
[root@k8s-master01 scahpa]#kubectl get deploy
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3/3     3            3           92s

方式一:scale

通过kubectl scale命令可以将Pod副本数量从初始的3个更新为5个:

[root@k8s-master01 scahpa]#kubectl scale deployment nginx-deployment --replicas=6
deployment.apps/nginx-deployment scaled
[root@k8s-master01 scahpa]#kubectl get po
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-5ddfc76b5c-7xkj2   1/1     Running   0          7s
nginx-deployment-5ddfc76b5c-fqlgt   1/1     Running   0          2m24s
nginx-deployment-5ddfc76b5c-fxldw   1/1     Running   0          2m24s
nginx-deployment-5ddfc76b5c-nckgx   1/1     Running   0          7s
nginx-deployment-5ddfc76b5c-qfs2q   1/1     Running   0          2m24s
nginx-deployment-5ddfc76b5c-xfgdm   1/1     Running   0          7s

将–replicas的值设置为比当前Pod副本数量更小的数字,系统将会“杀掉”一些运行中的Pod,以实现应用集群缩容:

[root@k8s-master01 scahpa]#kubectl scale deployment nginx-deployment --replicas=1
deployment.apps/nginx-deployment scaled
[root@k8s-master01 scahpa]#kubectl get po
NAME                                READY   STATUS        RESTARTS   AGE
nginx-deployment-5ddfc76b5c-fqlgt   1/1     Terminating   0          3m22s
nginx-deployment-5ddfc76b5c-fxldw   0/1     Terminating   0          3m22s
nginx-deployment-5ddfc76b5c-nckgx   0/1     Terminating   0          65s
nginx-deployment-5ddfc76b5c-qfs2q   1/1     Running       0          3m22s
nginx-deployment-5ddfc76b5c-xfgdm   1/1     Terminating   0          65s
[root@k8s-master01 scahpa]#kubectl get po
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-5ddfc76b5c-qfs2q   1/1     Running   0          3m33s

方式二:edit

使用kubectl edit,编辑正在运行的deploy中replicas数量。

[root@k8s-master01 scahpa]#kubectl edit deploy nginx-deployment
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: apps/v1
kind: Deployment
metadata:annotations:deployment.kubernetes.io/revision: "1"creationTimestamp: "2024-11-29T11:15:19Z"generation: 3labels:app: nginxname: nginx-deploymentnamespace: defaultresourceVersion: "28395402"uid: 4124b756-6803-4671-8fd7-35b59f6040ae

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com