码迷,mamicode.com
首页 > 其他好文 > 详细

k8s缩放应用程序

时间:2019-04-09 15:19:11      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:有一个   ace   desc   k8s   set   服务   echo   cluster   ini   

步骤1:扩展部署

要列出部署,请使用GET部署命令:kubectl get deployments

我们应该有一个POD。如果没有,请再次运行该命令。这显示:所需的状态显示的是已配置的副本数量,当前的状态显示了现在运行的副本的数量,最新的是更新以匹配所需(配置的)状态的副本的数量,可用状态显示用户实际可用的副本数量。

输出的自字段含义:
NAME: Deployments名称
DESIRED:创建Deployments时指定的副本数量。
CURRENT:当前正在运行的副本数量。
UP-TO-DATE:成功升级的副本数量。
AVAILABLE:可用的副本数量,注意副本在运行不等于可用。
AGE:应用运行时间。 

接下来,让我们将部署扩展到4个副本。我们将使用kubectl Scale命令,后面跟着deployment类型、名称和所需的实例数:

$ kubectl scale deployments/kubernetes-bootcamp --replicas=4
deployment.extensions/kubernetes-bootcamp scaled

  

$ kubectl get deployments
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   4/4     4            4           15m

现在我们有4个可用的应用程序实例。接下来,让我们检查一下Pods的数量是否发生了变化:

$ kubectl get pods -o wide
NAME                                   READY   STATUS    RESTARTS   AGE     IP           NODE       NOMINATED NODE   READINESS GATES
kubernetes-bootcamp-6bf84cb898-7mcsw   1/1     Running   0          2m53s   172.18.0.7   minikube   <none>           <none>
kubernetes-bootcamp-6bf84cb898-92gcb   1/1     Running   0          17m     172.18.0.4   minikube   <none>           <none>
kubernetes-bootcamp-6bf84cb898-949rh   1/1     Running   0          2m53s   172.18.0.6   minikube   <none>           <none>
kubernetes-bootcamp-6bf84cb898-r678j   1/1     Running   0          2m53s   172.18.0.5   minikube   <none>           <none>

  

现在有4种不同的IP地址。更改已在deployments事件日志中注册。要检查这一点,请使用Description命令:

$ kubectl describe deployments/kubernetes-bootcamp
Name:                   kubernetes-bootcamp
Namespace:              default
CreationTimestamp:      Sun, 07 Apr 2019 08:48:24 +0000
Labels:                 run=kubernetes-bootcamp
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               run=kubernetes-bootcamp
Replicas:               4 desired | 4 updated | 4 total | 4 available | 0 unavailable #这里
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  run=kubernetes-bootcamp
  Containers:
   kubernetes-bootcamp:
    Image:        gcr.io/google-samples/kubernetes-bootcamp:v1
    Port:         8080/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Progressing    True    NewReplicaSetAvailable
  Available      True    MinimumReplicasAvailable
OldReplicaSets:  <none>
NewReplicaSet:   kubernetes-bootcamp-6bf84cb898 (4/4 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  20m    deployment-controller  Scaled up replica set kubernetes-bootcamp-6bf84cb898 to 1
  Normal  ScalingReplicaSet  5m39s  deployment-controller  Scaled up replica set kubernetes-bootcamp-6bf84cb898 to 4

您还可以在此命令的输出中查看现在有4个副本。

 

步骤2:负载平衡

让我们检查一下service是否是负载平衡的流量。为了找到公开的IP和端口,我们可以使用 describe service:

$ kubectl describe services/kubernetes-bootcamp
Name:                     kubernetes-bootcamp
Namespace:                default
Labels:                   run=kubernetes-bootcamp
Annotations:              <none>
Selector:                 run=kubernetes-bootcamp
Type:                     NodePort
IP:                       10.96.175.74
Port:                     <unset>  8080/TCP
TargetPort:               8080/TCP
NodePort:                 <unset>  31242/TCP
Endpoints:                172.18.0.4:8080,172.18.0.5:8080,172.18.0.6:8080 + 1 more...
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

创建一个名为Node_Port的环境变量,其值为Node端口:

$ export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template=‘{{(index .spec.ports 0).nodePort}}‘)
$ echo NODE_PORT=$NODE_PORT
NODE_PORT=31242

接下来,我们将对公开的IP和端口进行curl。多次执行该命令

$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-949rh | v=1
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-r678j | v=1
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-949rh | v=1
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-r678j | v=1

我们每次请求都会碰到不同的情况。这表明负载平衡是有效的。

查看Deployments的推进状态(rollout status):

$ kubectl rollout status deployments/kubernetes-bootcamp
deployment "kubernetes-bootcamp" successfully rolled out

  

第三步:缩小规模

若要将服务缩小到2个副本,请再次运行Scale命令:

$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 4/4 4 4 35m
$ kubectl scale deployments/kubernetes-bootcamp --replicas=2
deployment.extensions/kubernetes-bootcamp scaled
$ kubectl get deployments
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   2/2     2            2           42m

复制数减少到2个。列出Pods的数量:

$ kubectl get pods -o wide
NAME                                   READY   STATUS    RESTARTS   AGE     IP           NODE       NOMINATED NODE   READINESS GATES
kubernetes-bootcamp-6bf84cb898-4rhbr   1/1     Running   0          5m35s   172.18.0.6   minikube   <none>           <none>
kubernetes-bootcamp-6bf84cb898-v6zf2   1/1     Running   0          5m39s   172.18.0.3   minikube   <none>           <none>

  

  

k8s缩放应用程序

标签:有一个   ace   desc   k8s   set   服务   echo   cluster   ini   

原文地址:https://www.cnblogs.com/linuxws/p/10676926.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!