Skip to content

Instantly share code, notes, and snippets.

@rpappalax
Last active March 1, 2021 23:08
Show Gist options
  • Save rpappalax/da2350aa226aea2cd7d65cd7b88f5413 to your computer and use it in GitHub Desktop.
Save rpappalax/da2350aa226aea2cd7d65cd7b88f5413 to your computer and use it in GitHub Desktop.
#https://cloud.google.com/kubernetes-engine/docs/tutorials/hello-appo
# eval (sed -n '2p' < file.txt)
# will evaluate the command on a given line of a file (i.e. line 2)
#==================================================
# NOTES
#==================================================
We are going to create the following entities:
1. container: "hello-app"
2. cluster: "hello-cluster"
3. deployment: "hello-app" (we should make this something unique like hello-deployment)
4. service: "hello-app-service"
- LoadBalancer
- port assignment, etc.
- exposing service to the internet, etc.
#----------------------------------------------
OTHER ACTIVIES
#----------------------------------------------
0. Pre-requisites
- setup service account custom role & user
1. scale (manually) and/or autoscale
2. kubectl get service
3. kubectl get pods
4. watch kubectl get pods
5. create/build and deploy new image to container registry and then update GKE cluster
6. cleanup service
#==================================================
# PRE-REQS
#==================================================
# create a custom service account role
- must include the following permissions:
* Storage Admin
* Container Registry Service Agent
# create a new service account
# assign custom role to service account
* create a new key (JSON format)
* copy key to /root/.config/gcloud/my-gke-project.json
#==================================================
# INSTALLATION
#==================================================
gcloud components install kubectl
# install Docker Community Edition: https://docs.docker.com/engine/installation/
# install git source control
# https://git-scm.com/download
git clone https://github.com/GoogleCloudPlatform/kubernetes-engine-samples
cd kubernetes-engine-samples/hello-app
export PROJECT_ID=<project_id>
echo ${PROJECT_ID}
docker build -t gcr.io/${PROJECT_ID}/hello-app:v1 .
# verify build created
docker images
docker run --rm -p 8080:8080 gcr.io/${PROJECT_ID}/hello-app:v1
# verify endpoint
curl http://localhost:8080
#==================================================
# PUSH TO CONTAINER REGISTRY
#==================================================
# enable container register api
gcloud services enable containerregistry.googleapis.com
# configure the Docker command-line tool to auth to Container Registry
(not the default docker hub)
gcloud auth configure-docker
# push docker image to container registry
sudo docker push gcr.io/${PROJECT_ID}/hello-app:v1
#==================================================
# CREATE GKE CLUSTER
#==================================================
# now create a GKE cluster to run app
gcloud config set project $PROJECT_ID
gcloud config set compute/zone <compute-zone>
gcloud container clusters create hello-cluster --num-nodes=1
gcloud compute instances list
#==================================================
# DEPLOY SAMPLE APP TO GKE
#==================================================
# You are now ready to deploy the Docker image you built to your GKE cluster.
# Kubernetes represents applications as Pods, which are scalable units holding one or
# more containers. The Pod is the smallest deployable unit in Kubernetes. Usually,
# you deploy Pods as a set of replicas that can be scaled and distributed together
# across your cluster. One way to deploy a set of replicas is through a Kubernetes Deployment.
# In this section, you create a Kubernetes Deployment to run hello-app on your cluster. This
# Deployment has replicas (Pods). One Deployment Pod contains only one container: the hello-app
# Docker image. You also create a HorizontalPodAutoscaler resource that scales the
# number of Pods from 3 to a number between 1 and 5, based on CPU load.
#----------------------------------------------
# Deploy the sample app to GKE
#----------------------------------------------
kubectl create deployment hello-app --image=gcr.io/${PROJECT_ID}/hello-app:v1
kubectl scale deployment hello-app --replicas=3
kubectl autoscale deployment hello-app --cpu-percent=80 --min=1 --max=5
kubectl get pods
# now expose the cluster to the internet using a loadbalancer
kubectl expose deployment hello-app --name=hello-app-service --type=LoadBalancer --port 80 --target-port 8080
kubectl get service
#----------------------------------------------
# ready to update your container?
#----------------------------------------------
# update Dockerfile
docker build -t gcr.io/${PROJECT_ID}/hello-app:v2 .
docker push gcr.io/${PROJECT_ID}/hello-app:v2
kubectl set image deployment/hello-app hello-app=gcr.io/${PROJECT_ID}/hello-app:v2
watch kubectl get pods
# cleanup
kubectl delete service hello-app-service
gcloud container clusters delete hello-cluster
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment