一看必會系列:k8s 練習3 pod 的擴容和縮減

來源:本站原創 Kubernetes 超過196 views圍觀 0條評論

pod 的擴容和縮減

[[email protected] 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
[[email protected] yaml]# kubectl scale rc frontend-rc –replicas=4
replicationcontroller/frontend-rc scaled
[[email protected] 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
[[email protected] 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
[[email protected] yaml]#

 

[[email protected] yaml]# kubectl scale rc frontend-rc –replicas=2
replicationcontroller/frontend-rc scaled
[[email protected] yaml]#
[[email protected] yaml]#
[[email protected] 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
[[email protected] yaml]#
[[email protected] yaml]#

[[email protected] hpa]# kubectl autoscale rc hpa-apache-rc –min=1 –max=10 –cpu-percent=50
horizontalpodautoscaler.autoscaling/hpa-apache-rc autoscaled
[[email protected] 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
[[email protected] hpa]#

進bosybox進行測試
[[email protected] hpa]# kubectl exec -it busybox-pod sh

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

[[email protected] ~]# 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
[[email protected] ~]#

穩定在 3個POD CPU恢得到44%
[[email protected] ~]# 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
[[email protected] ~]#

退出測試后過段時間,pod恢復成一個
[[email protected] 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

[[email protected] hpa]# kubectl create -f hpa-apache-autoscale.yaml
horizontalpodautoscaler.autoscaling/hpa-apache-autoscale created
[[email protected] 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
[[email protected] hpa]#
剛啟動時  出現<unknown> 不過不要緊,過段時間就好了。

[[email protected] ~]# kubectl get hpa    #現在已讀到CPU信息
NAME                   REFERENCE                             TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
hpa-apache-autoscale   ReplicationController/hpa-apache-rc   0%/50%    1         10        1          53s

繼續測試
[[email protected] 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來解決問題
[[email protected] ~]# kubectl get hpa
NAME                   REFERENCE                             TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
hpa-apache-autoscale   ReplicationController/hpa-apache-rc   44%/50%   1         10        3          4m48s

 

刪除autoscale
[[email protected] hpa]# kubectl get hpa
NAME            REFERENCE                             TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
hpa-apache-rc   ReplicationController/hpa-apache-rc   0%/50%    1         10        1          51m

[[email protected] hpa]# kubectl delete hpa hpa-apache-rc
horizontalpodautoscaler.autoscaling "hpa-apache-rc" deleted

[[email protected] hpa]# kubectl get hpa
No resources found.
[[email protected] hpa]#

配置文件如下
[[email protected] 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.qdxgqk.live/ 版權所有。本站文章除注明出處外,皆為作者原創文章,可自由引用,但請注明來源。 禁止全文轉載。
本文鏈接:http://www.qdxgqk.live/?p=4120轉載請注明轉自CCIE那點事
如果喜歡:點此訂閱本站
  • 相關文章
  • 為您推薦
  • 各種觀點
?
暫時還木有人評論,坐等沙發!
發表評論

您必須 [ 登錄 ] 才能發表留言!

?
?
萌宠夺宝游戏