Continuous Deployment Through Kubernetes on AWS Cluster
To achieve continuous deployment on AWS cluster through kubernetes we have to undertake the following tasks
- Make an environment for continuous deployment
- Create Jenkins job for CD
First of all we will discuss about how to make an environment for continuous deployment. Before creating a cluster on AWS EC2 through kubernetes, we need to install some software in the client machine. The client machine could be an EC2 instance or desktop. We assume that our client machine is EC2 instance.
These are the software we need to install on client machine:
- Java 8
- Maven 3.x
Now connect the EC2 instance through command line and change user to Jenkins. To connect EC2 instance, use the given command from the command line.
$ssh -i "<pem file location> ubuntu@<ec2 instance dns name>
$ssh -i "mypem.pem" email@example.com
Change user to Jenkins. A Jenkins user should have access to all the above software.
Following are the Steps to Launch the Kubernetes Cluster on EC2:
A. Create AWS profile.
The command is given below for creating AWS profile
$aws configure --profile <user-define-name>
B. Now export AWS profile
$export AWS_DEFAULT_PROFILE = <user-define-name>;
C. Download kubernetes version 1.5
$wget -q -o - https://github.com/kubernetes/kubernetes/releases/download/v1.5.7/kubernetes.tar.gz | bash
$tar -xvzf kubernetes.tar.gz
After unzipping the downloaded zip file, we get a kubernetes folder, which contains all the default configuration file for launching cluster on AWS, also we can override these configurations.
The default setting is available in the location /kubernetes/cluster/aws/config-default.sh. Here are some configurations which are overridden.
A better way to export is to keep this configuration into “.bashrc” file.
D. Start and stop cluster
Go to the given location to get script for start and stop /kubernetes/cluster/
Here you will get kube-up.sh and kube-down.sh for start and stop respectively.
Also we can automate this to create a Jenkins job.
The next important thing is to create a Jenkins job for continuous deployment. So let’s consider an example of spring boot microservice to deploy on AWS cluster.
Assume a spring microservice has the following files which are mandatory for creating a Jenkins job.
- Dockerfile : This file is use for creating Docker container.
- Jenkinsfile: It has defined some steps on how Jenkins must perform a task in order to complete a job.
Following are the tasks performed which are contained in the Jenkinsfile
- Clone the workspace
- Build project with running Junit test cases.
- Create an docker image
- Push docker image to docker registry (docker hub)
- Execute kubectl command for deploy docker image to aws cluster.
Jenkinsfile code example.
stage('Run Test and Package')
sh 'mvn clean package'
stage('Create Docker Image')
stage('Create Docker Image and push')
sh "docker login -u <username> -p <password> -e <emailid>"
Stage(Deploy on cluster)
sh 'kubectl apply -f ./docker/deployment.yml'
sh 'kubectl apply -f ./docker/service.yml'
Subscribe for latest update