一看必会系列:k8s 练习3 pod 的扩容和缩减

来源:本站原创 Kubernetes 超过159 views围观 0条评论

pod 的扩容和缩减

[root@k8s-master yaml]# kubectl get rc
NAME              DESIRED   CURRENT   READY   AGE
frontend-rc       3         3         3       18h
redis-master-rc   1         1         1       35h
redis-slave-rc    2         2         2       35h
[root@k8s-master yaml]# kubectl scale rc frontend-rc –replicas=4
replicationcontroller/frontend-rc scaled
[root@k8s-master yaml]# kubectl get rc
NAME              DESIRED   CURRENT   READY   AGE
frontend-rc       4         4         4       18h
redis-master-rc   1         1         1       35h
redis-slave-rc    2         2         2       35h
[root@k8s-master yaml]# kubectl get rc,pod
NAME                                    DESIRED   CURRENT   READY   AGE
replicationcontroller/frontend-rc       4         4         4       18h
replicationcontroller/redis-master-rc   1         1         1       35h
replicationcontroller/redis-slave-rc    2         2         2       35h

NAME                        READY   STATUS    RESTARTS   AGE
pod/frontend-rc-2h62f       1/1     Running   0          18h
pod/frontend-rc-5dwk2       1/1     Running   0          18h
pod/frontend-rc-dmxp8       1/1     Running   0          18h
pod/frontend-rc-flg9m       1/1     Running   0          9s
pod/redis-master-rc-jrrgx   1/1     Running   0          35h
pod/redis-slave-rc-f9svq    1/1     Running   0          23h
pod/redis-slave-rc-p6kbq    1/1     Running   0          35h
[root@k8s-master yaml]#

 

[root@k8s-master yaml]# kubectl scale rc frontend-rc –replicas=2
replicationcontroller/frontend-rc scaled
[root@k8s-master yaml]#
[root@k8s-master yaml]#
[root@k8s-master yaml]# kubectl get rc,pod
NAME                                    DESIRED   CURRENT   READY   AGE
replicationcontroller/frontend-rc       2         2         2       18h
replicationcontroller/redis-master-rc   1         1         1       35h
replicationcontroller/redis-slave-rc    2         2         2       35h

NAME                        READY   STATUS    RESTARTS   AGE
pod/frontend-rc-5dwk2       1/1     Running   0          18h
pod/frontend-rc-dmxp8       1/1     Running   0          18h
pod/redis-master-rc-jrrgx   1/1     Running   0          35h
pod/redis-slave-rc-f9svq    1/1     Running   0          23h
pod/redis-slave-rc-p6kbq    1/1     Running   0          35h
[root@k8s-master yaml]#
[root@k8s-master yaml]#

[root@k8s-master hpa]# kubectl autoscale rc hpa-apache-rc –min=1 –max=10 –cpu-percent=50
horizontalpodautoscaler.autoscaling/hpa-apache-rc autoscaled
[root@k8s-master hpa]# kubectl get rc,hpa
NAME                                    DESIRED   CURRENT   READY   AGE
replicationcontroller/frontend-rc       2         2         2       21h
replicationcontroller/hpa-apache-rc     1         1         1       113m
replicationcontroller/redis-master-rc   1         1         1       37h
replicationcontroller/redis-slave-rc    2         2         2       37h

NAME                                                REFERENCE                             TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
horizontalpodautoscaler.autoscaling/hpa-apache-rc   ReplicationController/hpa-apache-rc   <unknown>/50%   1         10        0          5s
[root@k8s-master hpa]#

进bosybox进行测试
[root@k8s-master hpa]# kubectl exec -it busybox-pod sh

/ # while true; do wget -q -O-  http://hpa-apache-svc > /dev/null;done

[root@k8s-master ~]# kubectl get rc,hpa
NAME                                    DESIRED   CURRENT   READY   AGE
replicationcontroller/frontend-rc       2         2         2       21h
replicationcontroller/hpa-apache-rc     3         3         3       128m
replicationcontroller/redis-master-rc   1         1         1       38h
replicationcontroller/redis-slave-rc    2         2         2       37h

NAME                                                REFERENCE                             TARGETS    MINPODS   MAXPODS   REPLICAS   AGE
horizontalpodautoscaler.autoscaling/hpa-apache-rc   ReplicationController/hpa-apache-rc   122%/50%   1         10        3          15m
[root@k8s-master ~]#

稳定在 3个POD CPU恢得到44%
[root@k8s-master ~]# kubectl get rc,hpa
NAME                                    DESIRED   CURRENT   READY   AGE
replicationcontroller/frontend-rc       2         2         2       21h
replicationcontroller/hpa-apache-rc     3         3         3       148m
replicationcontroller/redis-master-rc   1         1         1       38h
replicationcontroller/redis-slave-rc    2         2         2       38h

NAME                                                REFERENCE                             TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
horizontalpodautoscaler.autoscaling/hpa-apache-rc   ReplicationController/hpa-apache-rc   44%/50%   1         10        3          35m
[root@k8s-master ~]#

退出测试后过段时间,pod恢复成一个
[root@k8s-master hpa]# kubectl get rc,hpa,svc
NAME                                    DESIRED   CURRENT   READY   AGE
replicationcontroller/hpa-apache-rc     1         1         1       159m

NAME                                                REFERENCE                             TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
horizontalpodautoscaler.autoscaling/hpa-apache-rc   ReplicationController/hpa-apache-rc   0%/50%    1         10        1          45m

NAME                     TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/hpa-apache-svc   ClusterIP   10.100.27.38     <none>        80/TCP         157m

使用yaml的方式进行autoscale

[root@k8s-master hpa]# kubectl create -f hpa-apache-autoscale.yaml
horizontalpodautoscaler.autoscaling/hpa-apache-autoscale created
[root@k8s-master hpa]# kubectl get hpa
NAME                   REFERENCE                                        TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
hpa-apache-autoscale   ReplicationController/hpa-apache-autoscale-pod   <unknown>/50%   1         10        0          9s
[root@k8s-master hpa]#
刚启动时  出现<unknown> 不过不要紧,过段时间就好了。

[root@k8s-master ~]# kubectl get hpa    #现在已读到CPU信息
NAME                   REFERENCE                             TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
hpa-apache-autoscale   ReplicationController/hpa-apache-rc   0%/50%    1         10        1          53s

继续测试
[root@k8s-master hpa]# kubectl exec -it busybox-pod sh
#这里地址用的service的名字   http://service 当然也可以用IP加端口的方式
/ # while true;do wget -q -O-  http://hpa-apache-svc > /dev/null;done

CPU上来了且生成到了3个POD来解决问题
[root@k8s-master ~]# kubectl get hpa
NAME                   REFERENCE                             TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
hpa-apache-autoscale   ReplicationController/hpa-apache-rc   44%/50%   1         10        3          4m48s

 

删除autoscale
[root@k8s-master hpa]# kubectl get hpa
NAME            REFERENCE                             TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
hpa-apache-rc   ReplicationController/hpa-apache-rc   0%/50%    1         10        1          51m

[root@k8s-master hpa]# kubectl delete hpa hpa-apache-rc
horizontalpodautoscaler.autoscaling "hpa-apache-rc" deleted

[root@k8s-master hpa]# kubectl get hpa
No resources found.
[root@k8s-master hpa]#

配置文件如下
[root@k8s-master hpa]# tree .
.
├── busybox-pod.yaml
├── hpa-apache-autoscale.yaml
├── hpa-apache-rc.yaml
└── hpa-apache-svc.yaml

├── busybox-pod.yaml
apiVersion: v1                                                                             
kind: Pod
metadata:                                                                                  
  name: busybox-pod                                                                  
spec:                                                                                      
  containers:
    – name: busybox
      image: busybox
      command: [ "sleep" , "3600"]
     
├── hpa-apache-autoscale.yaml

apiVersion: autoscaling/v1                                                                     
kind: HorizontalPodAutoscaler
metadata:                                                                                  
  name: hpa-apache-autoscale                                                                      
spec:                                                                                      
  scaleTargetRef:
    apiVersion: v1
    kind: ReplicationController
    name: hpa-apache-rc                                                 
  minReplicas: 1
  maxReplicas: 10
  targetCPUUtilizationPercentage: 50
 
├── hpa-apache-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: hpa-apache-rc
spec:
  replicas: 1
  template:
    metadata:
      name: hpa-apache-lb
      labels:
        name: hpa-apache-lb
    spec:
     containers:
     – name: hpa-apache-ctn
       image:  reg.ccie.wang/test/ubuntu:apache2.4.29
       resources:
         requests:
           cpu: 200m
       ports:
       – containerPort: 80
      
└── hpa-apache-svc.yaml
apiVersion: v1                                                                             
kind: Service
metadata:                                                                                  
  name: hpa-apache-svc                                                                       
spec:                                                                                      
  ports:
    – port: 80

文章出自:CCIE那点事 http://www.jdccie.com/ 版权所有。本站文章除注明出处外,皆为作者原创文章,可自由引用,但请注明来源。 禁止全文转载。
本文链接:http://www.jdccie.com/?p=4120转载请注明转自CCIE那点事
如果喜欢:点此订阅本站
  • 相关文章
  • 为您推荐
  • 各种观点

暂时还木有人评论,坐等沙发!
发表评论

您必须 [ 登录 ] 才能发表留言!