{"id":2067,"date":"2024-04-30T01:54:24","date_gmt":"2024-04-30T01:54:24","guid":{"rendered":"https:\/\/www.aviator.co\/blog\/?p=2067"},"modified":"2025-09-23T12:37:31","modified_gmt":"2025-09-23T12:37:31","slug":"how-to-set-up-automated-deployment-pipelines-on-kubernetes-using-jenkins","status":"publish","type":"post","link":"https:\/\/www.aviator.co\/blog\/how-to-set-up-automated-deployment-pipelines-on-kubernetes-using-jenkins\/","title":{"rendered":"How to set up automated deployment pipelines on Kubernetes using Jenkins"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"574\" src=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/kubjenk-1024x574.png\" alt=\"\" class=\"wp-image-2084\" srcset=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/kubjenk-1024x574.png 1024w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/kubjenk-300x168.png 300w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/kubjenk-768x430.png 768w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/kubjenk.png 1456w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized workloads. There are two core pieces in a Kubernetes cluster: The first is the control plane, which is responsible for managing the state of the cluster in production environments. And the second is a set of worker nodes that run the containerized application workloads. The Jenkins server is deployed inside pods running on these nodes.<\/p>\n\n\n\n<p>Jenkins is an automation server that\u2019s used to build, test, and deploy software. Kuberentes works seamlessly with Jenkins to enable end-to-end automation. When developers commit code changes to a GitHub repository, the Jenkins pipeline is automatically triggered. Jenkins then uses <a href=\"https:\/\/github.com\/GoogleContainerTools\/kaniko\">Kaniko<\/a>, which is an open-source tool created by Google as a way to build container images in environments, such as Kubernetes, that may not have access to a Docker daemon. It allows you to build container images from a Dockerfile inside a container or Kubernetes cluster without the need for a Docker daemon. It executes each command from the Dockerfile entirely in userspace, making it more secure and efficient than a typical Docker build.<\/p>\n\n\n\n<p>The Jenkins pipeline then updates the Kubernetes deployment manifest with the new container image tag and applies the changes to the Kubernetes cluster using kubectl. Kubernetes automatically orchestrates the deployment of the new container image, scaling and load balancing the application based on the defined configurations. This automation flow ensures that any changes pushed to the GitHub repository are automatically built, tested, and deployed to the Kubernetes environment, providing a streamlined and reliable continuous deployment process.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"971\" src=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/k8-diagram-1024x971.png\" alt=\"\" class=\"wp-image-2068\" srcset=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/k8-diagram-1024x971.png 1024w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/k8-diagram-300x284.png 300w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/k8-diagram-768x728.png 768w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/k8-diagram-1536x1456.png 1536w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/k8-diagram.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Kubernetes deployments using Jenkins<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Setting up Jenkins and Kubernetes<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Prerequisites<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>A running Kubernetes cluster and kubectl installed on your workstation<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Configuring Jenkins on the Kubernetes cluster<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. Create a Jenkins namespace in your Kubernetes cluster<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl create namespace jenkins<\/code><\/pre>\n\n\n\n<p>When you\u2019re installing Jenkins, it\u2019s important to set a namespace that can be dedicated to all the Jenkins-related deployments.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. Createa YAML file to deploy<em> jenkins-aviator.yaml, <\/em>using this manifest:&nbsp;<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>apiVersion: apps\/v1\nkind: Deployment\nmetadata:\n&nbsp; name: jenkins\nspec:\n&nbsp; replicas: 1\n&nbsp; selector:\n&nbsp; &nbsp; matchLabels:\n&nbsp; &nbsp; &nbsp; app: jenkins\n&nbsp; template:\n&nbsp; &nbsp; metadata:\n&nbsp; &nbsp; &nbsp; labels:\n&nbsp; &nbsp; &nbsp; &nbsp; app: jenkins\n&nbsp; &nbsp; spec:\n&nbsp; &nbsp; &nbsp; containers:\n&nbsp; &nbsp; &nbsp; - name: jenkins\n&nbsp; &nbsp; &nbsp; &nbsp; image: jenkins\/jenkins:lts\n&nbsp; &nbsp; &nbsp; &nbsp; ports:\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - name: http-port\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; containerPort: 8080\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - name: jnlp-port\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; containerPort: 50000\n&nbsp; &nbsp; &nbsp; &nbsp; volumeMounts:\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - name: jenkins-vol\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mountPath: \/var\/jenkins_vol\n&nbsp; &nbsp; &nbsp; volumes:\n&nbsp; &nbsp; &nbsp; &nbsp; - name: jenkins-vol\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; emptyDir: {}<\/code><\/pre>\n\n\n\n<p>On Kubernetes, this manifest will create a deployment using the Jenkins LTS image and expose ports 8080 and 50000, which can be used to access Jenkins.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3. Create the deployment using kubectl<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl create -f jenkins-aviator.yaml --namespace jenkins<\/code><\/pre>\n\n\n\n<p>Once the deployment is successfully created, give the cluster a couple of minutes to pull the Jenkins image and set up pods. To verify that the pods are up and running, you can use the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl get pods -n jenkins<\/code><\/pre>\n\n\n\n<p>You should receive an output similar to this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; READY &nbsp; STATUS&nbsp; &nbsp; RESTARTS &nbsp; AGE\njenkins-655f6c69dd-bwqm9 &nbsp; 1\/1 &nbsp; &nbsp; Running &nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2m42s<\/code><\/pre>\n\n\n\n<p>Note: The pod name might be different for your environment.<\/p>\n\n\n\n<p>Now that Jenkins is running inside the pod, you need to expose the running Jenkins pod with a service to access it externally.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4. Create a YAML to deploy jenkins-service-aviator.yaml service, using this manifest:&nbsp;<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>apiVersion: v1\nkind: Service\nmetadata:\n&nbsp; name: jenkins\nspec:\n&nbsp; type: NodePort\n&nbsp; ports:\n&nbsp; &nbsp; - port: 8080\n&nbsp; &nbsp; &nbsp; targetPort: 8080\n&nbsp; &nbsp; &nbsp; nodePort: 30000\n&nbsp; selector:\n&nbsp; &nbsp; app: jenkins\n\n---\n\napiVersion: v1\nkind: Service\nmetadata:\n&nbsp; name: jenkins-jnlp\nspec:\n&nbsp; type: ClusterIP\n&nbsp; ports:\n&nbsp; &nbsp; - port: 50000\n&nbsp; &nbsp; &nbsp; targetPort: 50000\n&nbsp; selector:\n&nbsp; &nbsp; app: jenkins<\/code><\/pre>\n\n\n\n<p>In the YAML above, we\u2019re simply defining our service (NodePort) and then exposing it to ports 8080 and 50000.<\/p>\n\n\n\n<p>Create the service using kubectl using the same namespace:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl create -f jenkins-service-aviator.yaml --namespace jenkins<\/code><\/pre>\n\n\n\n<p>You should get an output similar to this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>service\/jenkins created\nservice\/jenkins-jnlp created<\/code><\/pre>\n\n\n\n<p>Service names in your environment can be different. To verify if the service is running, you can use:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl get services --namespace jenkins<\/code><\/pre>\n\n\n\n<p>The expected output should be:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TYPE&nbsp; &nbsp; &nbsp; &nbsp; CLUSTER-IP&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EXTERNAL-IP &nbsp; PORT(S)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AGE<br>jenkins&nbsp; &nbsp; &nbsp; &nbsp; NodePort&nbsp; &nbsp; &lt;your_cluster_ip&gt; &nbsp; &lt;none&gt;&nbsp; &nbsp; &nbsp; &nbsp; 8080:30000\/TCP &nbsp; 95s<br>jenkins-jnlp &nbsp; ClusterIP &nbsp; &lt;your_cluster_ip&gt; &nbsp; &lt;none&gt;&nbsp; &nbsp; &nbsp; &nbsp; 50000\/TCP&nbsp; &nbsp; &nbsp; &nbsp; 94s<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">5. AccessJenkins UI<\/h4>\n\n\n\n<p>To access Jenkins UI, you need to retrieve node IP, which can be done using:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl get nodes -o wide<\/code><\/pre>\n\n\n\n<p>This command will give you your cluster&#8217;s external IP, which can be used to access the Jenkins UI. Copy the external IP and open it in your browser tab with the 30000 port \u2014 for instance http:\/\/&lt;your_external_IP&gt;:30000<br><\/p>\n\n\n\n<p>Once it\u2019s open, you should get a Jenkins administrator page asking for the administrator password.<img decoding=\"async\" width=\"1600\" height=\"894\" class=\"wp-image-2069\" style=\"\" src=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/unlock-jenkins.png\" alt=\"Jenkins getting started\" srcset=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/unlock-jenkins.png 1600w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/unlock-jenkins-300x168.png 300w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/unlock-jenkins-1024x572.png 1024w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/unlock-jenkins-768x429.png 768w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/unlock-jenkins-1536x858.png 1536w\" sizes=\"(max-width: 1600px) 100vw, 1600px\" \/><\/p>\n\n\n\n<p>The administrator password can be retrievedfrom the Jenkins pod running on the Kubernetes cluster using the following commands:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Get the running Jenkins pod names using:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl get pods -n jenkins<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>Copy the name of the pod and use the following command to fetch this pod\u2019s logs:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl logs &lt;pod name&gt; -n jenkins<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li>Scroll down to see the password and copy it.<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1600\" height=\"400\" src=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-31.png\" alt=\"\" class=\"wp-image-2086\" srcset=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-31.png 1600w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-31-300x75.png 300w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-31-1024x256.png 1024w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-31-768x192.png 768w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-31-1536x384.png 1536w\" sizes=\"(max-width: 1600px) 100vw, 1600px\" \/><\/figure>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li>Once you type your password, you can install suggested plugins, or you can manually select which plugins to install. For this tutorial, we will go with the Install Suggested Plugins option.<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"572\" src=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-20-1024x572.png\" alt=\"Customize Jenkins\" class=\"wp-image-2070\" srcset=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-20-1024x572.png 1024w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-20-300x168.png 300w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-20-768x429.png 768w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-20-1536x858.png 1536w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-20.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>It will take a few minutes for Jenkins to install the plugins. <img loading=\"lazy\" decoding=\"async\" width=\"1600\" height=\"894\" class=\"wp-image-2071\" style=\"\" src=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/jenkins-getting-started.png\" alt=\"Jenkins getting started\" srcset=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/jenkins-getting-started.png 1600w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/jenkins-getting-started-300x168.png 300w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/jenkins-getting-started-1024x572.png 1024w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/jenkins-getting-started-768x429.png 768w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/jenkins-getting-started-1536x858.png 1536w\" sizes=\"(max-width: 1600px) 100vw, 1600px\" \/><\/p>\n\n\n\n<p>Once the installation is done, Jenkins will ask you to create a new admin user. You can simply fill out all the fields and create a new user, or you can skip creating a new user. In this case, the default username will be admin, and the password will be <em>your_password<\/em> retrieved from the logs.<\/p>\n\n\n\n<p>When you\u2019re done, you\u2019ll get a screen where Jenkins asks about instance configuration. You can click on Not Now and continue. After this, Jenkins will prompt you with a message: &#8220;Jenkins is ready!&#8221;<\/p>\n\n\n\n<p>Jenkins will take a few minutes to install the plugins.&nbsp;<\/p>\n\n\n\n<ol start=\"5\" class=\"wp-block-list\">\n<li>Click on Start Using Jenkins, and you have successfully installed and configured Jenkins on the Kubernetes cluster.<\/li>\n<\/ol>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1600\" height=\"894\" class=\"wp-image-2072\" style=\"\" src=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/jenkins-welcome.png\" alt=\"jenkins welcome\" srcset=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/jenkins-welcome.png 1600w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/jenkins-welcome-300x168.png 300w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/jenkins-welcome-1024x572.png 1024w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/jenkins-welcome-768x429.png 768w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/jenkins-welcome-1536x858.png 1536w\" sizes=\"(max-width: 1600px) 100vw, 1600px\" \/><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a Jenkins pipeline for automated deployments on Kubernetes<\/h2>\n\n\n\n<p>Now that you\u2019ve installed Jenkins on the Kubernetes cluster, you can create a pipeline to deploy things on Kubernetes using Jenkins. For this article, we\u2019ll deploy a small HTML application. But before starting the development of the pipeline, we need to make sure we have the following plugins installed on Jenkins:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Kubernetes<\/li>\n\n\n\n<li>AnsiColor<\/li>\n\n\n\n<li>Git<\/li>\n<\/ul>\n\n\n\n<p>We will create a two-stage Jenkins pipeline for our application.&nbsp;<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>First stage: Build &amp; Push Image.<\/li>\n\n\n\n<li>Second stage: Deploy.&nbsp;<\/li>\n<\/ol>\n\n\n\n<p>You can look at the sample application we created and the Jenkinsfile we will use <a href=\"https:\/\/github.com\/viveksonar\/aviator-k8-jenkins\">here<\/a>.<\/p>\n\n\n\n<p>Jenkinsfile<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pipeline {\n\n&nbsp; options {\n&nbsp; &nbsp; ansiColor('xterm')\n&nbsp; }\n\n&nbsp; agent {\n&nbsp; &nbsp; kubernetes {\n&nbsp; &nbsp; &nbsp; yamlFile 'builder.yaml'\n&nbsp; &nbsp; }\n&nbsp; }\n\n&nbsp; stages {\n\n&nbsp; &nbsp; stage('Kaniko Build &amp; Push Image') {\n&nbsp; &nbsp; &nbsp; steps {\n&nbsp; &nbsp; &nbsp; &nbsp; container('kaniko') {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; script {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sh '''\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/kaniko\/executor --dockerfile `pwd`\/Dockerfile \\\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --context `pwd` \\\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --destination={Docker Hub Username}:${BUILD_NUMBER}\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '''\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; stage('Deploy App to Kubernetes') { &nbsp; &nbsp;\n&nbsp; &nbsp; &nbsp; steps {\n&nbsp; &nbsp; &nbsp; &nbsp; container('kubectl') {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; withCredentials(&#91;file(credentialsId: 'kubeconfig', variable: 'KUBECONFIG')]) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sh 'sed -i \"s\/&lt;TAG&gt;\/${BUILD_NUMBER}\/\" web-app.yaml'\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sh 'kubectl apply -f web-app.yaml'\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; }\n&nbsp;\n&nbsp; }\n}<\/code><\/pre>\n\n\n\n<p>If you look at the Jenkinsfile, you can see the two stages: Kaniko Build &amp; Push Image and Deploy App to Kubernetes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Stage 1: Build &amp; Push Image<\/h3>\n\n\n\n<p>In the Kaniko Build &amp; Push Image stage, you pull the container, which has Kaniko pre-installed and configured. Kaniko will help you build a container from the Dockerfile you created and will also push the Docker image to the container registry, Docker Hub. To do that, you must set your Docker Hub secrets in your Kubernetes cluster. You can do that using the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl create secret docker-registry docker-hub-secret --docker-username=user --docker-password=password --docker-email=email -n jenkins<\/code><\/pre>\n\n\n\n<p>To push the containers to your Docker Hub account, you have to mention your username in the destination under&nbsp; <code>\/kaniko\/executor<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>script {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sh '''\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/kaniko\/executor --dockerfile `pwd`\/Dockerfile \\\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --context `pwd` \\\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --destination={Docker Hub Username}:${BUILD_NUMBER}\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '''\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<\/code><\/pre>\n\n\n\n<p>That&#8217;s it, stage one is ready to be executed!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Stage 2: Deploy to Kubernetes<\/h3>\n\n\n\n<p>In stage 2, you\u2019re pulling a container with kubectl pre-installed and configured to execute the application\u2019s deploy manifest files. However, Jenkins cannot execute the kubectl command in your Kubernetes cluster. To run the kubectl command, you have to permit Jenkins to set up the configuration. To do this, follow these simple steps:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 1: Click on Manage Jenkins<\/h4>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"572\" src=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-21-1024x572.png\" alt=\"Manage Jenkins\" class=\"wp-image-2073\" srcset=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-21-1024x572.png 1024w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-21-300x168.png 300w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-21-768x429.png 768w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-21-1536x858.png 1536w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-21.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Step 2: Go to Credentials<\/h4>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"572\" src=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-22-1024x572.png\" alt=\"Jenkins Credentials\" class=\"wp-image-2074\" srcset=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-22-1024x572.png 1024w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-22-300x168.png 300w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-22-768x429.png 768w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-22-1536x858.png 1536w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-22.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Step 3: Click on Global<\/h4>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"572\" src=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-23-1024x572.png\" alt=\"global credentials jenkins\" class=\"wp-image-2075\" srcset=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-23-1024x572.png 1024w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-23-300x168.png 300w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-23-768x429.png 768w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-23-1536x858.png 1536w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-23.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Step 4: Navigate to Add Credentials<\/h4>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"572\" src=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-24-1024x572.png\" alt=\"add credentials jenkins\" class=\"wp-image-2076\" srcset=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-24-1024x572.png 1024w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-24-300x168.png 300w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-24-768x429.png 768w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-24-1536x858.png 1536w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-24.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Step 5: Create the credentials<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Select the Secret file in the Kind.<\/li>\n\n\n\n<li>Global for the scope.<\/li>\n\n\n\n<li>In file, select the config file from \/.kube (Your Kubernetes clusters config file).<\/li>\n\n\n\n<li><strong>Important:<\/strong> ID should match with the <strong>credentialsId<\/strong> value passed in the <strong>Jenkinsfile<\/strong>.<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"572\" src=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-25-1024x572.png\" alt=\"create credentials jenkins\" class=\"wp-image-2077\" srcset=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-25-1024x572.png 1024w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-25-300x168.png 300w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-25-768x429.png 768w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-25-1536x858.png 1536w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-25.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Once the credentials are added successfully, you can start creating a pipeline in Jenkins to run your Jenkinsfile.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating a pipeline in Jenkins<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Step 1: Go to the dashboard and Click on New Item<\/h4>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"572\" src=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-26-1024x572.png\" alt=\"new item jenkins\" class=\"wp-image-2078\" srcset=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-26-1024x572.png 1024w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-26-300x168.png 300w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-26-768x429.png 768w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-26-1536x858.png 1536w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-26.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Step 2: Give your pipeline a name and select Pipeline from the options<\/h4>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"572\" src=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-27-1024x572.png\" alt=\"jenkins pipeline\" class=\"wp-image-2079\" srcset=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-27-1024x572.png 1024w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-27-300x168.png 300w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-27-768x429.png 768w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-27-1536x858.png 1536w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-27.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Step 3: Scroll down and configure the pipeline<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Select Pipeline script from SCM in the Definition.<\/li>\n\n\n\n<li>Git in SCM.<\/li>\n\n\n\n<li>Enter the repository URL containing the code you want to deploy on Kubernetes and Jenkinsfile.&nbsp;<\/li>\n\n\n\n<li>Note that you have to modify the Docker file according to your application requirement; the Jenkinsfile in the git repo is for example purposes.<\/li>\n\n\n\n<li>Give Jenkinsfile as the script path.&nbsp;<\/li>\n\n\n\n<li>Click Save.<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"893\" height=\"1024\" src=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-28-893x1024.png\" alt=\"pipeline definition\" class=\"wp-image-2080\" srcset=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-28-893x1024.png 893w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-28-262x300.png 262w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-28-768x880.png 768w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-28-1340x1536.png 1340w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-28.png 1396w\" sizes=\"(max-width: 893px) 100vw, 893px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Running your pipeline<\/h3>\n\n\n\n<p>Click Build Now to run the configured pipeline.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"572\" src=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-29-1024x572.png\" alt=\"running pipeline jenkins\" class=\"wp-image-2081\" srcset=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-29-1024x572.png 1024w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-29-300x168.png 300w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-29-768x429.png 768w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-29-1536x858.png 1536w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-29.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Once the process starts, Jenkins will pull the code from the GitHub repo, and then Kaniko will build an image using the Dockerfile provided and push it to the Docker Hub. In the second stage of the pipeline, the latest image will be configured in the Kubernetes pod configured using kubectl.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"572\" src=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-30-1024x572.png\" alt=\"welcome page jenkins\" class=\"wp-image-2082\" srcset=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-30-1024x572.png 1024w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-30-300x168.png 300w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-30-768x429.png 768w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-30-1536x858.png 1536w, https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/image-30.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>And that&#8217;s how you automate deployments using Jenkins on Kubernetes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Best practices and tips<\/h2>\n\n\n\n<p>Now that you know how to set up Jenkins on Kubernetes and automate the deployment, let&#8217;s examine ways to improve our process.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Documentation and version control: <\/strong>Documenting your Jenkins pipeline&#8217;s workflows, structure, and dependencies is crucial. Proper documentation will make your onboarding and troubleshooting easier. You should always try to keep your Jenkins file in version control so tracking changes and collaboration is easier.<\/li>\n\n\n\n<li><strong>Parameterization<\/strong>: Parameterization can be used to make the Jenkins pipeline flexible and reusable across different environments.<\/li>\n\n\n\n<li><strong>Agent management and parallel execution: <\/strong>Effectively manage your Jenkins agent and distribute the workloads evenly to prevent bottlenecks. In your Jenkins file, you can use parallel stages to speed up your deployment process and maximize the allocated resource utilization.&nbsp;<\/li>\n\n\n\n<li><strong>Monitoring and metrics<\/strong>: Monitor your Jenkins pipeline performance metrics and use them to identify and address performance bottlenecks proactively.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this blog post, we explored the power of Jenkins to automate and simplify the CI\/CD efforts. We covered the fundamental concepts of Jenkins and Kubernetes and walked through the steps to set up Jenkins on a Kubernetes cluster. We then demonstrated how to create a Jenkins pipeline to build, package, and deploy a containerized application to Kubernetes. Organizations can leverage the capabilities of Jenkins and Kubernetes to achieve faster release cycles and to improve scalability and reliability in their software delivery process. Lastly, we covered best practices and tips to ensure the robustness of the deployment pipeline.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.aviator.co\/releases\"><img decoding=\"async\" src=\"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/08\/blog-cta-9Release_CTA.svg\" alt=\"aviator releases\" class=\"wp-image-2489\"\/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we\u2019ll explain how to leverage the power of Jenkins to build a robust CD pipeline on Kubernetes, enabling seamless deployment within Kubernetes.<\/p>\n","protected":false},"author":31,"featured_media":2084,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[58],"tags":[69,97],"class_list":["post-2067","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ci-cd-deployment"],"blocksy_meta":[],"acf":[],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/www.aviator.co\/blog\/wp-content\/uploads\/2024\/04\/kubjenk.png","post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/www.aviator.co\/blog\/wp-json\/wp\/v2\/posts\/2067","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aviator.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aviator.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aviator.co\/blog\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aviator.co\/blog\/wp-json\/wp\/v2\/comments?post=2067"}],"version-history":[{"count":4,"href":"https:\/\/www.aviator.co\/blog\/wp-json\/wp\/v2\/posts\/2067\/revisions"}],"predecessor-version":[{"id":2510,"href":"https:\/\/www.aviator.co\/blog\/wp-json\/wp\/v2\/posts\/2067\/revisions\/2510"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.aviator.co\/blog\/wp-json\/wp\/v2\/media\/2084"}],"wp:attachment":[{"href":"https:\/\/www.aviator.co\/blog\/wp-json\/wp\/v2\/media?parent=2067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aviator.co\/blog\/wp-json\/wp\/v2\/categories?post=2067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aviator.co\/blog\/wp-json\/wp\/v2\/tags?post=2067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}