{"id":10889,"date":"2025-11-04T17:32:40","date_gmt":"2025-11-04T17:32:40","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=10889"},"modified":"2025-11-04T17:32:40","modified_gmt":"2025-11-04T17:32:40","slug":"building-a-complete-ci-cd-pipeline-with-jenkins-and-kubernetes","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/building-a-complete-ci-cd-pipeline-with-jenkins-and-kubernetes\/","title":{"rendered":"Building a Complete CI\/CD Pipeline with Jenkins and Kubernetes"},"content":{"rendered":"<h1>Building a Complete CI\/CD Pipeline with Jenkins and Kubernetes<\/h1>\n<p>In the fast-paced world of software development, Continuous Integration\/Continuous Deployment (CI\/CD) has become an essential practice to streamline workflows and accelerate delivery cycles. When paired with powerful tools like Jenkins and Kubernetes, creating a robust CI\/CD pipeline becomes not just feasible but efficient. In this article, we\u2019ll explore how to set up a complete CI\/CD pipeline using Jenkins and Kubernetes, step by step.<\/p>\n<h2>What is CI\/CD?<\/h2>\n<p>Continuous Integration (CI) refers to the practice of automatically integrating code changes from multiple contributors into a single software project. Continuous Deployment (CD), on the other hand, is the practice of automatically deploying all code changes to a production environment after the build stage. Together, they form a pivotal workflow that enhances collaboration and reduces the time it takes to deliver software.<\/p>\n<h2>The Role of Jenkins in CI\/CD<\/h2>\n<p>Jenkins is an open-source automation server that facilitates the CI\/CD pipeline. With its wide array of plugins and integrations, Jenkins allows you to automate the testing and deployment of applications, thus speeding up development cycles. Let\u2019s cover the steps to set up Jenkins to work with our Kubernetes-based infrastructure.<\/p>\n<h2>Setting Up Jenkins<\/h2>\n<p>To get started, we need to have Jenkins up and running. You can choose to install Jenkins locally or deploy it within a Kubernetes cluster. For our example, we will deploy Jenkins on Kubernetes.<\/p>\n<h3>Step 1: Deploy Jenkins on Kubernetes<\/h3>\n<p>To deploy Jenkins on Kubernetes, we will leverage Helm, a package manager for Kubernetes.<\/p>\n<pre><code>#!\/bin\/bash\n# Add the Jenkins Helm repository\nhelm repo add jenkins https:\/\/charts.jenkins.io\n# Update Helm repositories\nhelm repo update\n# Create a namespace for Jenkins\nkubectl create namespace jenkins\n# Install Jenkins using Helm\nhelm install jenkins jenkins\/jenkins --namespace jenkins --set serviceType=NodePort\n<\/code><\/pre>\n<p>This command will install Jenkins in a namespace called `jenkins`. You can access Jenkins through the NodePort exposed by Kubernetes.<\/p>\n<h3>Step 2: Accessing the Jenkins UI<\/h3>\n<p>After running the command, you can get the Jenkins UI URL and the initial admin password.<\/p>\n<pre><code># Get Jenkins service\nkubectl get services --namespace jenkins\n# Get the administrator password\nkubectl get secret --namespace jenkins jenkins -o jsonpath=\"{.data.jenkins-admin-password}\" | base64 --decode; echo\n<\/code><\/pre>\n<p>Visit the URL you retrieved under SERVICES, enter the admin username (`admin`) and the password you just decoded, and you will be greeted by Jenkins\u2019 setup wizard.<\/p>\n<h2>Configuring Jenkins for Our CI\/CD Pipeline<\/h2>\n<p>Jenkins supports pipelines as code using the Jenkinsfile. This allows you to define your CI\/CD process within your repository. Let\u2019s create a Jenkinsfile for a sample application.<\/p>\n<h3>Step 3: Create a Sample Application<\/h3>\n<p>For our example, let\u2019s consider a basic Node.js application.<\/p>\n<pre><code>const express = require(\"express\");\nconst app = express();\nconst PORT = process.env.PORT || 3000;\n\napp.get(\"\/\", (req, res) =&gt; {\n  res.send(\"Hello World!\");\n});\n\napp.listen(PORT, () =&gt; {\n  console.log(`Server is running on port ${PORT}`);\n});\n<\/code><\/pre>\n<p>Next, we will set up our Jenkinsfile for CI\/CD.<\/p>\n<h3>Step 4: Creating the Jenkinsfile<\/h3>\n<p>Create a file named `Jenkinsfile` in the root of your Node.js application.<\/p>\n<pre><code>pipeline {\n    agent any \n\n    stages {\n        stage('Build') {\n            steps {\n                script {\n                    docker.build(\"my-node-app:${env.BUILD_ID}\")\n                }\n            }\n        }\n        stage('Test') {\n            steps {\n                sh 'npm install'\n                sh 'npm test'\n            }\n        }\n        stage('Deploy') {\n            steps {\n                sh 'kubectl apply -f k8s\/deployment.yaml'\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<h2>Creating Kubernetes Deployment and Service<\/h2>\n<p>To deploy the application on Kubernetes, you will need a `deployment.yaml` file. Create a directory named `k8s` in your project root, and add the following:<\/p>\n<pre><code>apiVersion: apps\/v1\nkind: Deployment\nmetadata:\n  name: my-node-app\nspec:\n  replicas: 2\n  selector:\n    matchLabels:\n      app: my-node-app\n  template:\n    metadata:\n      labels:\n        app: my-node-app\n    spec:\n      containers:\n      - name: my-node-app\n        image: my-node-app:${BUILD_ID}\n        ports:\n        - containerPort: 3000\n\n---\napiVersion: v1\nkind: Service\nmetadata:\n  name: my-node-app-service\nspec:\n  type: NodePort\n  ports:\n    - port: 3000\n  selector:\n    app: my-node-app\n<\/code><\/pre>\n<h2>Integrating Jenkins with Kubernetes<\/h2>\n<p>To enable Jenkins to communicate with Kubernetes, you need to set up a Kubernetes plugin in Jenkins.<\/p>\n<h3>Step 5: Install Kubernetes Plugin<\/h3>\n<p>1. Navigate to &#8220;Manage Jenkins&#8221; &gt; &#8220;Manage Plugins&#8221;.<br \/>\n2. Search for &#8220;Kubernetes&#8221; in the Available tab and install it.<br \/>\n3. Configure the Kubernetes credentials by going to &#8220;Manage Jenkins&#8221; &gt; &#8220;Configure System&#8221; and adding your Kubernetes API URL and credentials.<\/p>\n<h2>Triggering the Pipeline<\/h2>\n<p>To automate our CI\/CD pipeline, we can set up triggers. Go to your Jenkins dashboard and configure the pipeline in your job settings to build based on SCM changes (e.g., GitHub webhooks).<\/p>\n<h3>Step 6: Set Up Webhooks in GitHub<\/h3>\n<p>1. Go to your GitHub repository, and click on &#8220;Settings&#8221;.<br \/>\n2. Under &#8220;Webhooks&#8221;, click &#8220;Add webhook&#8221;.<br \/>\n3. Enter your Jenkins server\u2019s address followed by &#8220;\/github-webhook\/&#8221;.<br \/>\n4. Select &#8220;Just the push event,&#8221; and click &#8220;Add webhook&#8221;.<\/p>\n<h2>Conclusion<\/h2>\n<p>Congratulations! You have successfully set up a complete CI\/CD pipeline using Jenkins and Kubernetes. In this, we utilized Jenkins for CI\/CD automation and Kubernetes for deploying containerized applications with ease.<\/p>\n<p>This setup allows development teams to work more efficiently, minimize the chances of errors, and focus more on building features rather than managing deployment complexities. As CI\/CD becomes more integral to software development, tools like Jenkins and Kubernetes will remain pivotal.<\/p>\n<p>Feel free to explore more about Jenkins&#8217; extensive plugins, advanced configurations, and Kubernetes&#8217; orchestration features to enhance your CI\/CD pipelines further.<\/p>\n<h2>Additional Resources<\/h2>\n<p>&#8211; <a href=\"https:\/\/www.jenkins.io\/doc\/\">Jenkins Documentation<\/a><br \/>\n&#8211; <a href=\"https:\/\/kubernetes.io\/docs\/home\/\">Kubernetes Documentation<\/a><br \/>\n&#8211; <a href=\"https:\/\/www.docker.com\/resources\/what-container\">Docker Overview<\/a><br \/>\n&#8211; <a href=\"https:\/\/github.com\/\">GitHub Documentation<\/a><\/p>\n<p>Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Building a Complete CI\/CD Pipeline with Jenkins and Kubernetes In the fast-paced world of software development, Continuous Integration\/Continuous Deployment (CI\/CD) has become an essential practice to streamline workflows and accelerate delivery cycles. When paired with powerful tools like Jenkins and Kubernetes, creating a robust CI\/CD pipeline becomes not just feasible but efficient. In this article,<\/p>\n","protected":false},"author":81,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[289,274],"tags":[1124,1297,374,376,1122],"class_list":{"0":"post-10889","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-continuous-integration-continuous-deployment","7":"category-kubernetes","8":"tag-automation","9":"tag-ci-cd","10":"tag-devops","11":"tag-kubernetes","12":"tag-pipeline"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/10889","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/81"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=10889"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/10889\/revisions"}],"predecessor-version":[{"id":10890,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/10889\/revisions\/10890"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=10889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=10889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=10889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}