Kubernetes Deployement Using Jenkins CI CD Pipeline 1689266904
Kubernetes Deployement Using Jenkins CI CD Pipeline 1689266904
In This article I will show you how to deploy Kubernetes cluster using Jenkins CI/CD pipeline. In this
demo project we are taking help of various DevOps tools like GitHub, Jenkins, Ansible, Docker Hub and
Kubernetes Minikube cluster.
GitHub: GitHub is a code hosting platform for version control and collaboration. And it will store our
source code into a repository.
Webhook: Webbook informs Jenkins whenever new code is available and tells Jenkins to build the new
code.
Jenkins: Jenkins will pull out the code from GitHub repository, then execute the CI/CD pipeline.
Ansible: Ansible server will execute the Kubernetes deployment and service yaml files to deploy the
application.
Prerequisite:
GitHub, Jenkins, Docker, Jenkins, Docker Hub Account, Ansible, Kubernetes (deployment & services yaml
files).
3 EC2 Ubuntu Instances required:
# java --version
Jenkins can easily be installed on Ubuntu by importing and adding the GPG keys to the system.
After adding GPG keys, add the Jenkins package address to the sources list by typing the command given
below:
# ansible –version
Run the following commands to update all system packages to the latest release:
You need to download the minikube binary. I will put the binary under
/usr/local/bin directory since it is inside $PATH.
# wget https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
# chmod +x minikube-linux-amd64
# minikube version
# chmod +x ./kubectl
Now that components are installed, you can start minikube. VM image will be downloaded and
configured for Kubernetes single node cluster.
# minikube start
Login to GitHub remote repository create below listed Dockerfile, deployment.yml, service,yml and
ansible.yml files.
Dockerfile
FROM centos:7
MAINTAINER VenkatKumar
RUN yum install -y httpd \
zip\
unzip
ADD https://www.free-css.com/assets/files/free-css-
templates/download/page254/photogenic.zip /var/www/html/
WORKDIR /var/www/html/
RUN unzip photogenic.zip
RUN cp -rvf photogenic/* .
RUN rm -rf photogenic photogenic.zip
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
EXPOSE 80
Deployment.yml
kind: Deployment
apiVersion: apps/v1
metadata:
name: kloudinsight
spec:
replicas: 2
selector: # tells the controller which pods to watch/belong to
matchLabels:
app: kloudinsight
template:
metadata:
labels:
app: kloudinsight
spec:
containers:
- name: kloudinsight
image: kloudinsight/pipeline-demo
imagePullPolicy: Always
ports:
- containerPort: 80
Service.yml
kind: Service
apiVersion: v1
metadata:
name: kloudinsight
labels:
app: kloudinsight
spec:
ports:
- port: 8080
targetPort: 80
nodePort: 31200
selector:
app: kloudinsight
type: LoadBalancer
ansible.yml
- hosts: all
tasks:
- name: create new deployment
command: kubectl apply -f /home/ubuntu/deployment.yml
- name: create new service
command: kubectl apply -f /home/ubuntu/service.yml
Pipeline Script Steps:
Login into Jenkins server and click on New Item, Enter pipeline name and select Pipeline and
click on OK.
Navigate to pipeline configuration select pipeline script and copy the pipeline groovy script
then click on apply and save.
stage('Git Checkout'){
sshagent(['ansibledemo']) {
sshagent(['ansibledemo']){
sshagent(['ansibledemo']){
sh 'ssh -o StrictHostKeyChecking=no [email protected] cd /home/ubuntu/'
sshagent(['ansibledemo']){
sshagent(['ansibledemo']){
sshagent(['ansibledemo']){
}
Login into Jenkins server navigate to pipeline click on build now.
http://54.144.213.63:31200
------------------------------------------------------END-------------------------------------------------------------
Venkat Kumar