This tutorial shows how to achieve continuous deployment to container engine of google cloud using kubernetes and Jenkins
Kubernetes is an open-source system for automating deployment, scaling, and management of containerized applications.
Jenkins is a self-contained, open source automation server which can be used to automate all sorts of tasks related to building, testing, and deploying software.
Continuous Deployment to Container Engine using Kubernetes &Jenkins:
- Understanding a sample application
- Deploying an application to Kubernetes
- Uploading code to Google Code Repositories
- Creating development pipelines in Jenkins
- Deploying development environments
- Deploying a canary release
- Deploying production environments
Before you begin:
- Select or create a Google Platform project
- Enable billing for that project
- Enable the Google Compute Engine, Google Container Engine APIs
Preparing your environment:
A: Setting up Jenkins on Container Engine
- Creating a Kubernetes Cluster with Container Engine
- Creating a Jenkins deployment and services
- Configuring external Load Balancing
- Connecting to Jenkins
- Understanding the code
Preparing your environment:
- First, prepare your deployment environment by activating Google Cloud Shell (Cloud Shell gives you access to the command line in Cloud Platform Console, and includes Google Cloud SDK and other tools you need for Cloud Platform development)
Creating a Kubernetes Cluster:
- If the cluster is running, the URLs of where your Kubernetes components are accessible display.
Creating a Jenkins home volume:
To pre-populate Jenkins with the configurations discussed in Jenkins on Container Engine, you’ll need to create the volume from the supplied tarball. Container Engine mounts this volume into your Jenkins pod
Configuring Jenkins Credentials:
- Open the jenkins/k8s/optionsfile, and replace CHANGE_ME with a new password
- Save and exit the file
- Enter cd ../../to return to the project directory
Next, create a Kubernetes namespace for Jenkins
Finally, create a Kubernetes secret. Kubernetes uses this object to provide Jenkins with the default username and password when Jenkins boots
Creating the Jenkins deployment and services:
- Create the Jenkins deployment and services
- The following output displays
- Confirm that the pod is running and look for Runningin the STATUS column
Configuring external Load Balancing:
- List the Jenkins services and confirm that jenkins-discoveryand jenkins-ui display
Setting up encryption:
- If you don’t already have an SSL certificate for your domain, you can create a temporary SSL certificate and key pair by running the following command
- Upload the certificate to Kubernetes as a secretobject
Creating the Load Balancer:
- Create the load balancer. The command uses the ingress resource definition from the jenkins/k8s/lb/ingress.yamlfile in the sample code
Connecting to Jenkins:
- Check the status of the load balancer’s health checks
- From the same output, copy the [IP_ADDRESS]value of the Address This is the IP address you’ll use to connect to Jenkins. Address: [IP_ADDRESS]
- In a web browser, navigate to the IP address from the previous step
- Click log inon the top right of the window. Input jenkins for the User form field and the password value from the previous step for the Password form field
- Click the log inbutton
You now have access to Jenkins and a Kubernetes cluster managed by Container Engine. To take this solution further, you could use these components in your continuous delivery pipeline.
To avoid incurring charges to your Google Cloud Platform account for the resources used in this tutorial, the easiest way is to eliminating billing is to Delete the project you created for the tutorial. To delete the project:
- In the Cloud Platform Console, go to the Projects page.
- In the project list, select the project you want to delete and click Delete project.
- In the dialog, type the project ID, and then click Shut down to delete the project.
B: In Google Cloud Shell, navigate to the sample application directory
Deploying the Sample app to Kubernetes:
- Create the Kubernetes namespace to logically isolate the production deployment
- Create the dev and production deployments and services
- Scale up the production environment frontends
- Retrieve the external IP for the production services. It can take several minutes before you see the load balancer IP address
- When complete, an IP address displays in the EXTERNAL-IPcolumn
- Store the frontend service load balancer IP in an environment variable
- Confirm that both services are working by opening the frontend external IP address in your browser
- Open a separate terminal and poll the production endpoint’s /versionURL so you can observe rolling updates in the next section
Creating a repository to host the Sample app source code:
Next, create a copy of the sample app and push it to Cloud Source Repositories
- Create the Cloud Source Repository
- Initialize the local Git repository
- Replace [PROJECT_ID]with your current project ID in the following command. To find your current project ID you can run gcloud config list project
- Set the username and email address for your Git commits in this repository
- Replace [EMAIL_ADDRESS]with your Git email address
- Replace [USERNAME]with your Git username.
- Add, commit, and push the files
Creating a Pipeline:
Adding your service account details:
Configure your credentials to allow Jenkins to access the code repository
- In the Jenkins user interface, Click Credentials in the left navigation
- Click the Jenkinslink in the Credentials table
- Click Global Credentials
- Click Add Credentials in the left navigation
- Select Google Service Account from metadata from the Kind dropdown
- Click OK.
- There are now two global credentials. Make a note of second credential’s name for use later on in this tutorial.
Creating a Jenkins Job:
- Click the Jenkinslink in the top left of the interface
- Click the New Itemlink in the left navigation
- Name the project sample-app, then choose the Multibranch Pipelineoption, then click OK
- On the next page, click Add Sourceand select git
- Paste the HTTPS clone URLof your sample-app repo in Cloud Source Repositories into the Project Repository Replace [PROJECT_ID] with your project ID
- From the Credentialsdropdown, select the name of the credentials you created when adding your service account
- Select the checkbox Build Periodicallyunder Build Triggers, and enter five asterisks (* * * * *) into the Schedule This ensures that Jenkins checks your code repository for changes once every minute. This field uses the CRON expression syntax to define the schedule
- Click Save.
- After you complete these steps, a job named “Branch indexing” runs. This meta-job identifies the branches in your repository and ensures changes haven’t occurred in existing branches. If you refresh Jenkins, the master branch displays this job.
- The first run of the job fails until you make a few code changes in the next step.
Modifying the Pipeline definition:
Create a branch for the canary environment, called dev
Deploying a Dev release:
- Open goand replace the two instances of blue with orange
- Open goand change the version number from 1.0.0 to 2.0.0
- Next, add and commit those files to your local repository
- Finally, push your changes to the remote Git server
- After the change is pushed to the Git repository, navigate to the Jenkins user interface where you can see that your build started
- After the build is running, click the down arrow next to the build in the left navigation and select Console Output
- In approximately one minute, the masterjob in the sample-app folder kicks off