Skip to content

Latest commit

 

History

History
197 lines (156 loc) · 5.4 KB

6. Deploy to Kubernetes in Google Cloud.md

File metadata and controls

197 lines (156 loc) · 5.4 KB

Deploy to Kubernetes in Google Cloud: Challenge Lab

Launch the lab here

Your challenge

You are expected to create container images, store the images in a repository, and configure a Jenkins CI/CD pipeline to automate the build for the product. Your know that Kurt, your supervisor, will ask you to complete these tasks:

  • Create a Docker image and store the Dockerfile.
  • Test the created Docker image.
  • Push the Docker image into the Container Repository.
  • Use the image to create and expose a deployment in Kubernetes
  • Update the image and push a change to the deployment.
  • Create a pipeline in Jenkins to deploy a new version of your image when the source code changes.

Some Jooli Inc. standards you should follow:

  • Create all resources in the us-east1 region and us-east1-b zone, unless otherwise directed.
  • Use the project VPCs.
  • Naming is normally team-resource, e.g. an instance could be named kraken-webserver1.
  • Allocate cost effective resource sizes. Projects are monitored and excessive resource use will result in the containing project's termination (and possibly yours), so beware. This is the guidance the monitoring team is willing to share: unless directed, use n1-standard-1.

Task 1: Create a Docker image and store the Dockerfile

gsutil cat gs://cloud-training/gsp318/marking/setup_marking.sh | bash
gcloud source repos clone valkyrie-app
cd valkyrie-app
cat > Dockerfile <<EOF
FROM golang:1.10
WORKDIR /go/src/app
COPY source .
RUN go install -v
ENTRYPOINT ["app","-single=true","-port=8080"]
EOF
docker build -t valkyrie-app:v0.0.1 .
cd ..
cd marking
./step1.sh

Task 2: Test the created Docker image

cd ..
cd valkyrie-app
docker run -p 8080:8080 valkyrie-app:v0.0.1 &
cd ..
cd marking
./step2.sh

Task 3: Push the Docker image in the Container Repository

cd ..
cd valkyrie-app
docker tag valkyrie-app:v0.0.1 gcr.io/$GOOGLE_CLOUD_PROJECT/valkyrie-app:v0.0.1
docker push gcr.io/$GOOGLE_CLOUD_PROJECT/valkyrie-app:v0.0.1
sed -i s#IMAGE_HERE#gcr.io/$GOOGLE_CLOUD_PROJECT/valkyrie-app:v0.0.1#g k8s/deployment.yaml

Task 4: Create and expose a deployment in Kubernetes

sed -i s#IMAGE_HERE#gcr.io/$GOOGLE_CLOUD_PROJECT/valkyrie-app:v0.0.1#g k8s/deployment.yaml
gcloud container clusters get-credentials valkyrie-dev --zone us-east1-d
kubectl create -f k8s/deployment.yaml
kubectl create -f k8s/service.yaml
git merge origin/kurt-dev
kubectl edit deployment valkyrie-dev
  • Now in the editor, press i to start editing
  • Replace replicas: 1 with replicas: 3
  • One change is at the beginning and one at the end (Total 2 changes)
  • Press ESC and type :wq and hit Enter

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

docker build -t gcr.io/$GOOGLE_CLOUD_PROJECT/valkyrie-app:v0.0.2 . 
docker push gcr.io/$GOOGLE_CLOUD_PROJECT/valkyrie-app:v0.0.2
kubectl edit deployment valkyrie-dev
  • Now in the editor, press i to start editing
  • Replace image: gcr.io/DO_NOT_CHANGE_THIS/valkyrie-app:v0.0.1 with image: gcr.io/DO_NOT_CHANGE_THIS/valkyrie-app:v0.0.2
  • One change is at the beginning and one at the end (Total 2 changes)
  • Press ESC and type :wq and hit Enter
docker ps
  • Copy the CONTAINER ID and replace below

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

docker kill [CONTAINER_ID]
export 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}")
kubectl port-forward $POD_NAME 8080:8080 >> /dev/null &
printf $(kubectl get secret cd-jenkins -o jsonpath="{.data.jenkins-admin-password}" | base64 --decode);echo
  • Copy the password generated above
gcloud source repos list
  • Copy the URL generated above

  • Open Preview on port 8080 in Web Preview on Cloud Shell Terminal

  • Enter admin as username

  • Enter Copied Password from the previous command

  • In left Manage Jenkins > Manage Credentials > Global > adding some credentials?

  • In Kind > Google Service Account from Metadata and wait 15 seconds and hit OK

  • Dashboard > New Item > Type valkyrie-app/Jenkinsfile

  • Select Pipeline and hit OK

  • Build Triggers > Pipeline Script from SCM

  • SCM > Git

  • Add the URL copied from above command

  • In Credentials, select your account

  • Then click Apply and then Save

  • Hit Ctrl + C in Cloud Shell Terminal

sed -i "s/green/orange/g" source/html.go

# Update project in Jenkinsfile

sed -i "s/YOUR_PROJECT/$GOOGLE_CLOUD_PROJECT/g" Jenkinsfile
  • Replace [ADD_EMAIL below with your Qwiklabs Email name.
git config --global user.email "[ADD_EMAIL]"     <------ put from first consol 
  • Look at the top right corner and click on the account

  • Replace [ADD_NAME] below with your STUDENT name.

git config --global user.name "student"               <--------- from login status
git add .
git commit -m "built pipeline init"
git push 
  • Now, open the Jenkins window and run the Build Pipeline in left menu.
  • Check your Progress after 5 - 10 minutes.