Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christopherdongo/e263092daff0ef7870f2510933e87df3 to your computer and use it in GitHub Desktop.
Save christopherdongo/e263092daff0ef7870f2510933e87df3 to your computer and use it in GitHub Desktop.
Summary of "Architecting with Google Kubernetes Engine: Workloads" from Coursera.Org

Learn about performing Kubernetes operations; creating and managing deployments; the tools of GKE networking; and how to give your Kubernetes workloads persistent storage.

Kubernetes Operations

Learn about the kubectl command, a command line utility used to interact with and manage the resources inside Kubernetes clusters. Connect it to Google Kubernetes Engine clusters, and use it to create, inspect, interact and delete Pods and other objects within Kubernetes clusters. Use kubectl to view a Pod’s console output, and sign in interactively to a Pod.

Lab: Creating a GKE Cluster via Cloud Shell, code

> gcloud container clusters get-credentials $my_cluster --zone $my_zone
> kubectl config view
> kubectl cluster-info
> kubectl config current-context
> kubectl config get-contexts
> kubectl config use-context gke_${GOOGLE_CLOUD_PROJECT}_us-central1-a_standard-cluster-1
> kubectl top nodes
> source <(kubectl completion bash)
 
> kubectl create deployment --image nginx nginx-1
> kubectl get/describe pods
> kubectl cp ~/test.html $my_nginx_pod:/usr/share/nginx/html/test.html
> kubectl expose pod $my_nginx_pod --port 80 --type LoadBalancer

> kubectl apply -f ./new-nginx-pod.yaml
> kubectl exec -it new-nginx /bin/bash
> kubectl port-forward new-nginx 10081:80
> curl http://127.0.0.1:10081/test.html
> kubectl logs new-nginx -f --timestamps

Lab: Upgrading a GKE Cluster

Deployments, Jobs, and Scaling

GKE works with containerized applications: applications packaged into hardware-independent, isolated user-space instances. In GKE and Kubernetes, these packaged applications are collectively called workloads. Learn about Deployments and Jobs, two of the main types of workload. Mechanisms that are used to scale the GKE clusters where you run your applications. Control on which Nodes Pods may and may not run.

Lab Creating GKE Deployments, code

> kubectl apply -f ./nginx-deployment.yaml
> kubectl scale --replicas=3 deployment nginx-deployment
> kubectl set image deployment.v1.apps/nginx-deployment nginx=nginx:1.9.1 --record
> kubectl rollout status deployment.v1.apps/nginx-deployment
> kubectl rollout history deployment nginx-deployment
> kubectl rollout undo deployments nginx-deployment
> kubectl rollout history deployment/nginx-deployment --revision=3
> kubectl apply -f service-nginx.yaml
> kubectl apply -f nginx-canary.yaml
> kubectl scale --replicas=0 deployment nginx-deployment
  # spec:
  #  type: LoadBalancer
  #  sessionAffinity: ClientIP

Lab Deploying Jobs on GKE, code

> kubectl apply -f example-job.yaml
> kubectl describe job example-job
> kubectl logs [POD-NAME]
> kubectl apply -f example-cronjob.yaml
> kubectl get cronjobs
> kubectl get jobs

Lab Configuring Pod autoscaling and node pools, code

> gcloud container clusters create $my_cluster --num-nodes 2 --enable-ip-alias --zone $my_zone
> kubectl create -f web.yaml --save-config
> kubectl expose deployment web --target-port=8080 --type=NodePort
> kubectl autoscale deployment web --max 4 --min 1 --cpu-percent 1
> kubectl get hpa
> kubectl describe horizontalpodautoscaler web
> kubectl apply -f loadgen.yaml
> gcloud container node-pools create "temp-pool-1" --cluster=$my_cluster --zone=$my_zone \
    --num-nodes "2" --node-labels=temp=true --preemptible
> kubectl get nodes -l temp=true
> kubectl taint node -l temp=true nodetype=preemptible:NoExecute
  # tolerations:
  # - key: "nodetype"
  #   operator: Equal
  #   value: "preemptible"

  # nodeSelector:
  #   temp: "true"

Lab Deploying to Kubernetes Engine via Helm Charts

> curl -LO https://git.io/get_helm.sh
> kubectl create clusterrolebinding user-admin-binding \
   --clusterrole=cluster-admin \
   --user=$(gcloud config get-value account)
> kubectl create serviceaccount tiller --namespace kube-system
> kubectl create clusterrolebinding tiller-admin-binding \
   --clusterrole=cluster-admin \
   --serviceaccount=kube-system:tiller
> helm init --service-account=tiller
> helm repo update
> helm version
> helm install stable/redis
> helm inspect stable/redis
> kubectl get services -l app=redis -o json | jq -r '.items[].spec | select(.selector.role=="master") | .clusterIP'
> kubectl get secret -l app=redis -o jsonpath="{.items[0].data.redis-password}"  | base64 --decode
> kubectl run redis-test --rm --tty -i --restart='Never' \
    --env REDIS_PW=$REDIS_PW \
    --env REDIS_IP=$REDIS_IP \
    --image docker.io/bitnami/redis:4.0.12 -- bash
> redis-cli -h $REDIS_IP -a $REDIS_PW
> set mykey this_amazing_value

Google Kubernetes Engine Networking

Create Services to expose applications running within Pods, which allows them to communicate with the outside world. Create Ingress resources for HTTP or HTTPS load balancing. GKE's container-native load balancing, which allows you to directly configure Pods as network endpoints with Google Cloud Load Balancing.

Lab Configuring Google Kubernetes Engine Networking, code

> gcloud container clusters describe private-cluster --zone us-central1-a
> gcloud container clusters update private-cluster --zone us-central1-a \
  --enable-master-authorized-networks --master-authorized-networks=192.168.1.0/24

> kubectl run hello-web --labels app=hello \
  --image=gcr.io/google-samples/hello-app:1.0 --port 8080 --expose
> kubectl get networkpolicies
> kubectl run test-1 --labels app=foo --image=alpine --restart=Never --rm --stdin --tty
> wget -qO- --timeout=2 http://hello-web:8080
Hello, world!
> kubectl run test-1 --labels app=other --image=alpine --restart=Never --rm --stdin --tty
> wget -qO- --timeout=2 http://hello-web:8080
wget: download timed out
> 
> kubectl run hello-web-2 --labels app=hello-2 \
  --image=gcr.io/google-samples/hello-app:1.0 --port 8080 --expose
> kubectl run test-3 --labels app=foo --image=alpine --restart=Never --rm --stdin --tty
> wget -qO- --timeout=2 http://hello-web:8080
Hello, world!
> wget -qO- --timeout=2 http://hello-web-2:8080
wget: download timed out
> wget -qO- --timeout=2 http://www.example.com
wget: download timed out

Lab Creating Services and Ingress Resources, code

> kubectl exec -it dns-demo-1 /bin/bash
> ping dns-demo-2.dns-demo.default.svc.cluster.local
> curl hello-svc.default.svc.cluster.local
> kubectl create -f hello-v1.yaml
> kubectl apply -f ./hello-nodeport-svc.yaml
> kubectl create -f hello-v2.yaml
> gcloud compute addresses list
> kubectl apply -f ./hello-lb-svc.yaml
> kubectl apply -f hello-ingress.yaml

Persistent Data and Storage

Learn about the different types of Kubernetes storage abstractions. StatefulSets and how to use them to manage ordered deployments of Pods and storage. How ConfigMaps can save you time during application deployment by decoupling configuration artifacts from container definitions. Keep sensitive information safer from accidental exposure using Kubernetes Secrets.

Lab Configuring Persistent Storage for GKE, code

> kubectl apply -f pvc-demo.yaml
> kubectl get persistentvolumeclaim
> kubectl apply -f pod-volume-demo.yaml
> 
> kubectl apply -f statefulset-demo.yaml
> kubectl describe statefulset statefulset-demo

Lab Working with GKE Secrets and ConfigMaps, code

> my_service_account="no-permissions@qwiklabs-gcp-02-70b304838792.iam.gserviceaccount.com"
> gcloud container clusters create $my_cluster \
  --num-nodes 2 --zone $my_zone \
  --service-account=$my_service_account
> gcloud pubsub topics create $my_pubsub_topic
> gcloud pubsub subscriptions create $my_pubsub_subscription \
  --topic=$my_pubsub_topic
> kubectl get pods -l app=pubsub
> kubectl logs -l app=pubsub
> kubectl create secret generic pubsub-key \
  --from-file=key.json=$HOME/credentials.json
> kubectl get pods -l app=pubsub
> gcloud pubsub topics publish $my_pubsub_topic --message="Hello, world!"
> kubectl create configmap sample --from-literal=message=hello
> kubectl describe configmaps sample
> kubectl create configmap sample2 --from-file=sample2.properties
> kubectl apply -f config-map-3.yaml
> kubectl apply -f pubsub-configmap2.yaml
> kubectl exec -it pubsub-747cf8c545-ngsrf -- sh
> cat /etc/config/airspeed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment