gcloud config list
: List the current configuration
E.g. output:
[compute]
zone = asia-east1-a
[core]
account = vish@example.com
disable_usage_reporting = False
project = gcloud-testing-
Your active configuration is: [example]
gcloud config set [ARGS]
: Set a configuration value. Use--help
to list the configuration values you can set.- E.g.
gcloud config set compute/zone asia-east1-a
sets thecompute
category,zone
sub-category toasia-east-1a
. gcloud compute zones list
: list all zones. The value of thezone
above must be one of the zones from the output of this command.gcloud container clusters create [cluster]
: Create a Google Cloud Container cluster for use with Kubernetes.gcloud container clusters list
: List Google Cloud Container Clusters.
E.g. output
NAME LOCATION MASTER_VERSION MASTER_IP MACHINE_TYPE NODE_VERSION NUM_NODES STATUS
petclinic asia-east1-a 1.7.8-gke.0 35.1.1.73 n1-standard-1 1.7.8-gke.0 3 RUNNING
Create a Google Cloud Cluster
# Set project name, email etc.
gcloud init
# Go to the console and add a credit card.
# Enable billing.
# Set compute zone
gcloud config set compute/zone asia-east1-a
# create cluster named petclinic
gcloud container clusters create petclinic
# generate a kubeconfig entry in your environment.
gcloud container clusters get-credentials cluster-name
Create and run a Kubernetes 'Deployment' to run a Dockerized application
Make sure kubectl can see the cluster. Output below should point to the cluster IP address.
kubectl cluster-info
E.g. output:
Kubernetes master is running at https://35.185.138.73
GLBCDefaultBackend is running at https://35.185.138.73/api/v1/namespaces/kube-system/services/default-http-backend/proxy
Heapster is running at https://35.185.138.73/api/v1/namespaces/kube-system/services/heapster/proxy
KubeDNS is running at https://35.185.138.73/api/v1/namespaces/kube-system/services/kube-dns/proxy
kubernetes-dashboard is running at https://35.185.138.73/api/v1/namespaces/kube-system/services/kubernetes-dashboard/proxy
Run a Dockerized application in a pod.
kubectl run tomcat-petclinic --image=docker.io/savishy/tomcat-petclinic:latest --port 8080
E.g. output:
deployment "tomcat-petclinic" created
Wait until the deployment is available. Keep running the following commands.
kubectl get deployments
kubectl get pods
You will see this output when deployment is not ready:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
tomcat-petclinic-375607832-gw6gp 0/1 ContainerCreating 0 1m
$ kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
tomcat-petclinic 1 1 1 0 1m
Once the deployment is ready you will see:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
tomcat-petclinic-375607832-gw6gp 1/1 Running 0 2m
$ kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
tomcat-petclinic 1 1 1 1 2m
Access the application via a Load Balancer
Create a load-balancer for the deployment tomcat-petclinic
:
kubectl expose deployment tomcat-petclinic --type="LoadBalancer"
Wait for the External IP to show up in output below:
kubectl get service tomcat-petclinic
E.g.
$ kubectl get service tomcat-petclinic
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
tomcat-petclinic LoadBalancer 10.39.252.121 35.194.224.53 8080:31394/TCP 1m
Now access the application on http://35.194.224.53:8080
!
Pause/Resume the deployment
kubectl rollout status deployment/tomcat-petclinic
Update CPU and Memory limits for each pod:
kubectl set resources deployment tomcat-petclinic --limits=cpu=200m,memory=300Mi
You will see that this triggers another rollout
$ kubectl rollout status deployment/tomcat-petclinic
Waiting for rollout to finish: 0 of 1 updated replicas are available...
deployment "tomcat-petclinic" successfully rolled out
Instead, you can pause
a deployment, modify resources, then resume
the deployment:
$ kubectl rollout pause deployment/tomcat-petclinic
deployment "tomcat-petclinic" paused
$ kubectl set resources deployment tomcat-petclinic --limits=cpu=200m,memory=200Mi
deployment "tomcat-petclinic" resource requirements updated
$ kubectl rollout resume deployment/tomcat-petclinic
deployment "tomcat-petclinic" resumed
❗ Make sure you set a valid value for CPU or Memory resources. Too low values can auto-kill and auto-restart your pods. Monitor events using kubectl get pods
and kubectl get events
.
Create an autoscaler
Auto-scale the deployment when CPU percent reaches 50% of the cpu resources specified above (50% of 200m, i.e 100m
).
kubectl autoscale deployment tomcat-petclinic --max=3 --cpu-percent=50
Get status of autoscaling. Right now, you will only see replicas=1.
kubectl get hpa
Generate Load
Open a different command prompt. Type:
$ kubectl run -i --tty load-generator --image=busybox /bin/sh
Hit enter for command prompt
$ while true; do wget -q -O- http://ADDRESS_FOR_PETCLINIC; done
The above command starts generating load on the URL ADDRESS_FOR_PETCLINIC
.
Start monitoring the AutoScaler, the Deployment, and the Pods.
hpa
: before
$ kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
tomcat-petclinic Deployment/tomcat-petclinic 0% / 50% 1 3 1 2m
hpa
: 2 replica created after 7 minutes.
$ kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
tomcat-petclinic Deployment/tomcat-petclinic 66% / 50% 1 3 2 7m
hpa
: 3 replicas created after 13 minutes.
$ kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
tomcat-petclinic Deployment/tomcat-petclinic 36% / 50% 1 3 3 13m
deployments
: before
$ kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
load-generator 1 1 1 1 1m
tomcat-petclinic 1 1 1 1 53m
deployments
: after
$ kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
load-generator 1 1 1 1 4m
tomcat-petclinic 2 2 2 2 56m
pods
: before
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
load-generator-3044827360-xvxz5 1/1 Running 0 3m
tomcat-petclinic-1800253800-55kdt 1/1 Running 0 21s
pods
: after
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
load-generator-3044827360-xvxz5 1/1 Running 0 3m
tomcat-petclinic-1800253800-55kdt 1/1 Running 0 21s
tomcat-petclinic-1800253800-p8wg3 1/1 Running 0 27m
You have successfully autoscaled the application!
If you try to execute a command e.g. gcloud compute zones list
without API being enabled, you might be asked to enable API (e.g. output below).
API [compute.googleapis.com] not enabled on project [50875613725].
Would you like to enable and retry? (Y/n)? Y
Otherwise you might receive an error like:
Enabling service compute.googleapis.com on project 50875613725...
ERROR: (gcloud.compute.zones.list) FAILED_PRECONDITION: Operation does not satisfy the following requirements: billing-enabled {Billing must be enabled for activation of service '' in project 'gcloud-testing-vish' to proceed., https://console.developers.google.com/project/gcloud-testing-vish/settings}