How you can achieve Continuous Deployment through Kubernetes
Audio : Listen to This Blog.
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
- aws-cli
- Docker.
- Jenkins
- Git
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>
Example
$ssh -i "mypem.pem" [email protected]
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>; $export KUBERNETES_PROVIDER=aws;
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.
export KUBE_AWS_ZONE=eu-west-1c export NUM_NODES=2 export MASTER_SIZE=m3.medium export NODE_SIZE=m3.medium export AWS_S3_REGION=eu-west-1 export AWS_S3_BUCKET=mycompany-kubernetes-artifacts export KUBE_AWS_INSTANCE_PREFIX=k8s
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.
node { checkout scm stage('Run Test and Package') { sh 'mvn clean package' } stage('Create Docker Image') { docker.build("dockeraccout/myimage:1") } stage('Create Docker Image and push') { sh "docker login -u <username> -p <password> -e <emailid>" docker.build("").push() } Stage(Deploy on cluster) { sh 'kubectl apply -f ./docker/deployment.yml' sh 'kubectl apply -f ./docker/service.yml' } }