We
will learn how to automate Docker builds using Jenkins pipelines and Deploy into
AWS EKS - Kubernetes Cluster with help of Kubernetes Continuous Deploy plug-in.
We will use Springboot Microservices based Java application. I have already
created a repo with source code + Dockerfile. The repo also have
Jenkinsfile for automating the following:
- Automating builds using Jenkins
- Automating Docker image creation
- Automating Docker image upload into Docker Hub
- Automating Deployments to Kubernetes Cluster
Please watch the above steps in YouTube channel:
Pre-requisites:
1. Amazon EKS Cluster is setup and running. Click here to learn how to create Amazon EKS cluster.
1. Amazon EKS Cluster is setup and running. Click here to learn how to create Amazon EKS cluster.
3. Setup Jenkins slave, install docker in it.
4. Docker, Docker pipeline and Kubernetes Continuous Deploy plug-ins are installed in Jenkins
4. Docker, Docker pipeline and Kubernetes Continuous Deploy plug-ins are installed in Jenkins
6. Install kubectl on your instance
Step #1 -Make sure Jenkins can run Docker builds after validating per pre-requisites
Step #2 - Create Credentials for Docker Hub
Go to Jenkins UI, click on Credentials -->
Click on Global credentials
Now Create an entry for your Docker Hub account. Make sure you enter the ID as dockerhub
Step #3 - Create Credentials for Kubernetes Cluster
Click on Global credentials
Click on Add Credentials
Now Create an entry for your Docker Hub account. Make sure you enter the ID as dockerhub
Click on Add Credentials, use Kubernetes configuration from drop down.
execute the below command to get kubeconfig info, copy the entire content of the file:
sudo cat ~/.kube/config
Enter ID as K8S and choose enter directly and paste the above file content and save.
Step # 4 - Create Maven3 variable under Global tool configuration in Jenkins
Step #5 - set a clusterrole as cluster-admin
By default, clusterrolebinding has system:anonymous set which blocks the cluster access. Execute the following command to set a clusterrole as cluster-admin which will give you the required access.
kubectl create clusterrolebinding cluster-system-anonymous --clusterrole=cluster-admin --user=system:anonymous
Step # 6 - Create a pipeline in Jenkins
Step # 7- Copy the pipeline code from below
Make sure you change red highlighted values below as per your settings:
Your docker user id should be updated.
your registry credentials ID from Jenkins from step # 1 should be copied
Make sure you change red highlighted values below as per your settings:
Your docker user id should be updated.
your registry credentials ID from Jenkins from step # 1 should be copied
node ("slave") {
def image
def mvnHome = tool 'Maven3'
stage ('checkout') {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '', url: 'https://bitbucket.org/ananthkannan/myawesomeangularapprepo/']]])
}
stage ('Build') {
sh 'mvn -f MyAwesomeApp/pom.xml clean install'
}
stage ('Docker Build') {
// Build and push image with Jenkins' docker-plugin
withDockerRegistry([credentialsId: "dockerhub", url: "https://index.docker.io/v1/"]) {
image = docker.build("akdevopscoaching/mywebapp", "MyAwesomeApp")
image.push()
}
}
stage ('K8S Deploy') {
kubernetesDeploy(
configs: 'MyAwesomeApp/springboot-lb.yaml',
kubeconfigId: 'K8S',
enableConfigSubstitution: true
)
}
}
Step # 8 - Build the pipeline
Once you create the pipeline and changes values per your Docker user id and credentials ID, click on
Step # 9 - Verify deployments to K8S
kubectl get pods
kubectl get deployments
Steps # 10 - Access SpringBoot App in K8S cluster
Once build is successful, go to browser and enter master or worker node public ip address along with port number mentioned above
http://master_or_worker_node_public_ipaddress:port_no_from_above
You should see page like below:
Hi,
ReplyDeleteI am thoroughly follow this blog and remain able to push my docker image in public repository on docker hub. But I am facing following error with 'K8S Deploy' stage-
ERROR: java.lang.IllegalStateException: No Matching configuration files found for SampleApplication/deployment.yaml
While this file is already there. Could you please help me on this on urgent basis.
Thanks in advance.
I am facing the same error , Can I have the solution for that.
DeleteIts bit urgent
downgrade the kubernetes-cd plugin to 1.0.0 version it will work it worked for me
DeleteOne Update-
ReplyDeleteI am running jenkins on one ec2 machine and setup eks cluster on another ec2 instance. Not sure if its making any issue.
Hi There,
ReplyDeleteI've been following your CI/CD Jenkins with kubernetes which is much knowledgeable content. I have started executing the steps one by one as you said but now I can't able to create RSA Private key as ssh-keygen has been upgraded, because of that I can't enable the master-slave setup in it.
Please guide me on the right way to enable this setup. Thanks in advance.
great project
ReplyDeleteIt's breaking with error like below
ReplyDeleteERROR: ERROR: Can't construct a java object for tag:yaml.org,2002:io.kubernetes.client.openapi.models.V1Deployment; exception=Class not found: io.kubernetes.client.openapi.models.V1Deployment
in 'reader', line 1, column 1:
Please asking to downgrade the jenkins plugins at https://stackoverflow.com/questions/62688901/class-not-found-io-kubernetes-client-openapi-models-v1service
Any other quick fix for this ?
Thanks for detailed explanation , i couldnt find this kubernetes config file plugin now, what are the other options
ReplyDelete