Kubernetes has become the preferred platform of choice for container orchestration and deliver maximum operational efficiency. To understand how K8s works, one must understand its most basic execution unit – a pod.
Kubernetes doesn’t run containers directly, rather through a higher-level structure called a pod. A pod has an application’s container (or, in some cases, multiple containers), storage resources, a unique network IP, and options that govern how the container(s) should run.
Pods can hold multiple containers or just one. Every container in a pod will share the same resources and network. A pod is used as a replication unit in Kubernetes; hence, it is advisable to not add too many containers in one pod. Future scaling up would lead to unnecessary and expensive duplication.
To maximize the ease and speed of Kubernetes, DevOps teams like to add automation using Jenkins CI/CD pipelines. Not only does this make the entire process of building, testing, and deploying software go faster, but it also minimizes human error. Here is how Jenkins CI/CD pipeline is used to deploy a spring boot application in K8s.
Create a Jenkins pipeline to dockerize a spring application, build docker image, push it to the dockerhub repo and then pull the image into an AKS cluster to run it in a pod.
All the files required for this task are available in this repository:
https://github.com/saiachyuth5/simple-spring
A spring-boot application, dockerfile to containerize the application.
STEPS:
1. Install Jenkins :
2. Connect host docker daemon to Jenkins:
3. Adding global credentials:
4. Create the Jenkinsfile :
Jenkinsfile:
NOTE: While this example uses actual id to login to Azure, its recommended to use credentials to avoid using exact parameters.
pipeline {
environment {
registryCredential = "docker"
}
agent any
stages {
stage(‘Build’) {
steps{
script {
sh 'mvn clean install'
}
}
}
stage(‘Load’) {
steps{
script {
app = docker.build("cloud007/simple-spring")
}
}
}
stage(‘Deploy’) {
steps{
script {
docker.withRegistry( "https://registry.hub.docker.com", registryCredential ) {
// dockerImage.push()
app.push("latest")
}
}
}
}
stage('Deploy to ACS'){
steps{
withCredentials([azureServicePrincipal('dbb6d63b-41ab-4e71-b9ed-32b3be06eeb8')]) {
sh 'echo "logging in" '
sh 'az login --service-principal -u **************************** -p ********************************* -t **********************************’
sh 'az account set -s ****************************'
sh 'az aks get-credentials --resource-group ilink --name mycluster'
sh 'kubectl apply -f sample.yaml'
}
}
}
}
}
5. Create the Jenkins project:
6. Create and connect to Azure Kubernetes cluster:
YAML used:
apiVersion: apps/v1
kind: Deployment
metadata:
name: spring-helloworld
spec:
replicas: 1
selector:
matchLabels:
app: spring-helloworld
template:
metadata:
labels:
app: spring-helloworld
spec:
containers:
- name: spring-helloworld
image: cloud007/simple-spring:latest
imagePullPolicy: Always
ports:
- containerPort: 80
Here are some common problems faced during this process and the troubleshooting procedure.
In an upcoming article we will show you how to deploy a pod containing three applications using Jenkins ci/cd pipeline and update them selectively.
Share this:
CloudIQ is a leading Cloud Consulting and Solutions firm that helps businesses solve today’s problems and plan the enterprise of tomorrow by integrating intelligent cloud solutions. We help you leverage the technologies that make your people more productive, your infrastructure more intelligent, and your business more profitable.
LATEST THINKING
INDIA
Chennai One IT SEZ,
Module No:5-C, Phase ll, 2nd Floor, North Block, Pallavaram-Thoraipakkam 200 ft road, Thoraipakkam, Chennai – 600097
© 2023 CloudIQ Technologies. All rights reserved.
Get in touch
Please contact us using the form below
USA
INDIA