Scaling Your App
The goal of this interactive scenario is to scale a deployment with kubectl scale and to see the load balancing in action
Scaling a deployment
kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 1 1 1 1 40m
- The DESIRED state is showing the configured number of replicas
- The CURRENT state show how many replicas are running now
- The UP-TO-DATE is the number of replicas that were updated to match the desired (configured) state
- The AVAILABLE state shows how many replicas are actually AVAILABLE to the users
kubectl scale deployments/kubernetes-bootcamp --replicas=4
deployment.extensions "kubernetes-bootcamp" scaled
kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 4 4 4 4 41m
The change was applied, and we have 4 instances of the application available
kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
kubernetes-bootcamp-5c69669756-f7dnz 1/1 Running 0 35s 172.17.0.6 minikube
kubernetes-bootcamp-5c69669756-hrcqx 1/1 Running 0 35s 172.17.0.5 minikube
kubernetes-bootcamp-5c69669756-jbdr4 1/1 Running 0 35s 172.17.0.7 minikube
kubernetes-bootcamp-5c69669756-vdwgm 1/1 Running 0 41m 172.17.0.4 minikube
There are 4 Pods now, with different IP addresses
To check that the change was registered in the Deployment events log
kubectl describe deployments/kubernetes-bootcamp
Name: kubernetes-bootcamp
Namespace: default
CreationTimestamp: Sun, 10 Jun 2018 15:08:14 +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: 1 max unavailable, 1 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-5c69669756 (4/4 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 42m deployment-controller Scaled up replica set kubernetes-bootcamp-5c69669756 to 1
Normal ScalingReplicaSet 57s deployment-controller Scaled up replica set kubernetes-bootcamp-5c69669756 to 4
Load Balancing
kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080
service "kubernetes-bootcamp" exposed
kubectl describe services/kubernetes-bootcamp
Name: kubernetes-bootcamp
Namespace: default
Labels: run=kubernetes-bootcamp
Annotations: <none>
Selector: run=kubernetes-bootcamp
Type: NodePort
IP: 10.101.238.220
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 31431/TCP
Endpoints: 172.17.0.4:8080,172.17.0.5:8080,172.17.0.6:8080 + 1 more...
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
echo NODE_PORT=$NODE_PORT
NODE_PORT=31431
We’ll do a curl to the exposed IP and port. Execute the command multiple times
curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5c69669756-hrcqx | v=1
curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5c69669756-jbdr4 | v=1
curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5c69669756-vdwgm | v=1
curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5c69669756-f7dnz | v=1
We hit a different Pod with every request. This demonstrates that the load-balancing is working
Scale Down
kubectl scale deployments/kubernetes-bootcamp --replicas=2
deployment.extensions "kubernetes-bootcamp" scaled
kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 2 2 2 2 59m
kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
kubernetes-bootcamp-5c69669756-f7dnz 1/1 Terminating 0 18m 172.17.0.6 minikube
kubernetes-bootcamp-5c69669756-hrcqx 1/1 Terminating 0 18m 172.17.0.5 minikube
kubernetes-bootcamp-5c69669756-jbdr4 1/1 Running 0 18m 172.17.0.7 minikube
kubernetes-bootcamp-5c69669756-vdwgm 1/1 Running 0 59m 172.17.0.4 minikube
kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
kubernetes-bootcamp-5c69669756-jbdr4 1/1 Running 0 18m 172.17.0.7 minikube
kubernetes-bootcamp-5c69669756-vdwgm 1/1 Running 0 1h 172.17.0.4 minikube
This confirms that 2 Pods were terminated.