GSP-318: Deploy to Kubernetes in Google Cloud

GSP-318: Deploy to Kubernetes in Google Cloud

Overview

Please enter the following commands:

Task - 1 : Create a Docker image and store the Dockerfile

 1gcloud auth list
 2gsutil cat gs://cloud-training/gsp318/marking/setup_marking_v2.sh | bash
 3gcloud source repos clone valkyrie-app
 4cd valkyrie-app
 5cat > Dockerfile <<EOF
 6FROM golang:1.10
 7WORKDIR /go/src/app
 8COPY source .
 9RUN go install -v
10ENTRYPOINT ["app","-single=true","-port=8080"]
11EOF
12docker build -t <Docker Image>:<Tag Name> .
13cd ..
14cd marking
15./step1_v2.sh

Task - 2 : Test the created Docker image

1cd ..
2cd valkyrie-app
3docker run -p 8080:8080 <Docker Image>:<Tag Name> &
4cd ..
5cd marking
6./step2_v2.sh

Task - 3 : Push the Docker image in the Google Container Repository

1cd ..
2cd valkyrie-app
3docker tag <Docker Image>:<Tag Name> gcr.io/$GOOGLE_CLOUD_PROJECT/<Docker Image>:<Tag Name>
4docker push gcr.io/$GOOGLE_CLOUD_PROJECT/<Docker Image>:<Tag Name>

Task - 4 : Create and expose a deployment in Kubernetes

1sed -i s#IMAGE_HERE#gcr.io/$GOOGLE_CLOUD_PROJECT/<Docker Image>:<Tag Name>#g k8s/deployment.yaml
2gcloud container clusters get-credentials valkyrie-dev --zone us-east1-d
3kubectl create -f k8s/deployment.yaml
4kubectl create -f k8s/service.yaml

Task - 5 : Update the deployment with a new version of valkyrie-app

1git merge origin/kurt-dev
2kubectl edit deployment valkyrie-dev
3### change replicas from 1 to <Replicas Count>
4### change <Tag Name> to <Updated Version> in two places
5docker build -t gcr.io/$GOOGLE_CLOUD_PROJECT/<Docker Image>:<Updated Version> .
6docker push gcr.io/$GOOGLE_CLOUD_PROJECT/<Docker Image>:<Updated Version>

Task - 6 : Create a pipeline in Jenkins to deploy your app

1docker ps
2docker kill <take container_id from above command>
3
4export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/component=jenkins-master" -l "app.kubernetes.io/instance=cd" -o jsonpath="{.items[0].metadata.name}")
5kubectl port-forward $POD_NAME 8080:8080 >> /dev/null &
6printf $(kubectl get secret cd-jenkins -o jsonpath="{.data.jenkins-admin-password}" | base64 --decode);echo
  • Open Jenkins Web View -> Preview on port 8080
1   Username : admin
2   Password : {Code output from previous command} 
3

Go through the following:

-> Manage Jenkins -> Manage Credentials -> Jenkins -> Global credentials (unrestricted) -> Add credentials -> Kind: Google Service Account from metadata -> OK

-> Jenkins -> New Item -> Name : valkyrie-app -> Pipeline -> Pipeline script from SCM -> Set SCM to git -> OK

-> Pipeline -> Script: Pipeline script from SCM -> SCM: Git

-> Repository URL: {find it using command: gcloud source repos list} -> Credentials: {Project id}

-> Apply -> Save

1sed -i "s/green/orange/g" source/html.go
2
3sed -i "s/YOUR_PROJECT/$GOOGLE_CLOUD_PROJECT/g" Jenkinsfile
4git config --global user.email "you@example.com"              // Email
5git config --global user.name "student..."                       // Username
6git add .
7git commit -m "built pipeline init"
8git push
  • In Jenkins click Build and wait to get score.

Congratulations, you're all done with the lab 😄