
Rapid and reliable delivery of applications is an important aspect of software development that ensures the timely execution of solutions in production environments. Jenkins is a popular open-source automation service that enables Continuous Integration (CI) and Continuous Deployment (CD) in software development. It plays a crucial role in automating the application building, testing, and deployment processes to streamline the software development lifecycle.
Implementing a CI/CD pipeline with Jenkins and Kubernetes offers many software development and delivery advantages. These include streamlining development processes, enhancing collaboration, and speed up the delivery of high-quality applications. In this guide, you will deploy a Node.js containerized application to a Vultr Kubernetes Engine (VKE) cluster using the Jenkins CI/CD Pipeline.
Before you start, you need to:
3 nodes.4 GB RAM and 2 vCPUs to use as the management server.To avoid deployment time errors in your cluster due to container registry, generate a Kubernetes Secret resource from the registry control panel and deploy it to your cluster.
Install the Java OpenJDK 11 dependecy package.
Download the Jenkins repository key to your server.
Add Jenkins repository to APT repository sources.
Update the server repository index.
Install Jenkins on the server.
View the Jenkins status to verify that it's successfully installed and running.
Output:
Add the Jenkins user to the Docker users group to enable communications with the Docker daemon.
Allow the default Jenkins port 8080 through the default UFW firewall to enable access to the web interface.
View the default Jenkins administrator password.
Copy the generated password similar to the output below:
Access the Jenkins port 8080 using your Server IP Address to access the Jenkins dashboard in a web browser such as Google Chrome.
Enter the default password you copied earlier and click Continue to start the Jenkins Set Up process.
Click Install suggested plugins to start installing all necessary Jenkins packages on your server.
Enter a new Jenkins administrator username and password. Then, click Save and Continue to access the instance configuration page.
Keep your Server IP address structure in the Jenkins URL field, and click Save and Finish to save the access URL structure.
Click Start using Jenkins to finish the configuration process and access the Jenkins dashboard.
To integrate Jenkins with your VKE cluster, GitHub, and the Vultr container registry, install all necessary plugins to enable all pipeline resources. Follow the below steps to install all required Docker, GitHub, and Kubernetes plugins within your Jenkins dashboard.
Access your Jenkins dashboard.
Navigate to Manage Jenkins on the main navigation menu.
Click Manage Plugins to access the plugins page.
Click Available Plugins, and enter the keyword Docker in the search field.
Select Docker, Docker Pipeline, and docker-build-step from the plugin search results.
Enter Kubernetes in the search field, and select Kubernetes, Kubernetes Credentials, Kubernetes CLI, and Kubernetes Pipeline from the plugin results.
Click Install to start the installation process.
Scroll and check the Restart Jenkins when installation is complete and no jobs are running option on the installation progress page to automatically restart Jenkins.
A GitHub repository webhook triggers Jenkins to automatically start the build process whenever push and pull events occur in the linked repository. This integration facilitates Continuous Integration and Delivery (CI/CD) workflows. Follow the steps below to create a new GitHub repository webhook to integrate with your Jenkins dashboard.
Log in to your GitHub account.
Click the + sign in the top right corner and select New repository to set up a new repository.
Enter your desired repository name, for example, node-application. Then, choose your desired visibility type, and click Create repository to publish the new repository.
Navigate to the repository page, and click Settings on the top navigation bar.
Click Webhooks on the left navigation menu.
Click Add webhook to set up a new webhook.
Click Add webhook to apply your new repository webhook.
To communicate with your GitHub repository, the Vultr Container Registry, and your VKE cluster, set up new credentials within your Jenkins dashboard to store the access details for each platform as described in the steps below.
Navigate to your Jenkins dashboard and click Manage Jenkins.
Click Credentials from the list of available options to set up a new profile.
Click the (global) Domains value and select Add credentials from the list of options.
Keep Username with password as the credential type and enter your Vultr Container Registry username and password in the respective fields.
Enter vcr-credentials in the ID field and click Create to save the new credential.
When successful, click Add credentials to set up a new GitHub credential.
Keep Username with password selected as the credential type, and enter your GitHub username, and password in the respective fields.
Enter github-credential in the ID field and click Create to save the new credential.
When successful, click Add credentials to set up a new GitHub credential. To set up the Kubernetes credential, click Add credentials.
Click the Kind drop-down and select Secret file from the list of options.
Click the Choose File button in the File section, browse, and upload your VKE cluster YAML file.
Enter kubeconfig in the ID field and click Create to save the new credential.
Clone your GitHub node-application repository you created earlier.
Switch to the new repository directory.
Create a new package.json file to define the application dependencies.
Add the following configurations to the file.
Save and close the file.
Create a new app.js file to define the application structure.
Add the following contents to the file.
Save and close the file.
The above Node.js application code sets up a basic server that listens on port 8080 and responds with Node Hello World Application! when accessed.
Create a new Dockerfile to define the application image structure.
Add the following configurations to the file.
Save and close the file.
In this section, create the necessary Kubernetes resources that pull your Node.js application container image from the Vultr Container Registry and deploy it to your cluster. Each time Jenkins starts the build process, the resource files run again to update your cluster.
Create a deployment.yml file.
Add the following configurations to the file. Replace example-user with your actual GitHub username.
Save and close the file.
The above deployment configuration defines your application replicas, Docker image, and port to expose the application within the cluster.
Create a new Service resource file service.yml to expose the Node.js application outside the Kubernetes cluster.
Add the following contents to the file.
Save and close the file.
A Jenkinsfile file defines a Jenkins Pipeline for your CI/CD processes. It includes the entire build process steps from pulling the source code to deploying the application to your cluster. Follow the steps in this section to set up a new Jenkinsfile that includes all build processes to prepare the Node.js application for deployment in your cluster.
Create a new Jenkinsfile.
Add the following configurations to the file. Replace https://sjc.vultrcr.com/jenkinsregistry with your actual Vultr Container Registry URL.
Save and close the file.
Below is what the above pipeline executes:
agent any: Specifies that the pipeline can run on any available agent (Jenkins node).stage("Checkout code"): Checks out the source code from the version control system (specified by scm), and enables the pipeline to access the application code.stage("Build image"): Builds a new Docker image of the Node.js applicatiom tagged with a version identifier based on the Jenkins build number (${env.BUILD_ID}).stage("Push image"): Pushes the application Docker image to your defined registry sjc.vultrcr.com/jenkinsregistry using the vcr-credentials variable.stage("Deploy to Vultr Kubernetes"): Updates the Kubernetes deployment configuration deployment.yml to use the new Docker image tagged with the latest Jenkins build number. Then, it applies the updated deployment and service configurations to your cluster using kubectl.sed: Replaces all occurrences of the previous image tag node-app:latest with the new image tag node-app:${env.BUILD_ID} in the deployment.yml file.withKubeConfig: Specifies the Kubernetes configuration kubeconfig to use in the pipeline processes. The sh command runs kubectl to deploy the deployment and service configurations to your VKE cluster.![NOTE] If you applied different ID values in your Jenkins dashboard, replace
vcr-credentialswith your Vultr Container Registry credential, andkubeconfigwith your actual cluster credential ID.
All files, including your Node.js application, Dockerfile, Jenkinsfile, and Kubernetes resources, are ready for deployment. Configure Git to commit and push all files from your management machine to the GitHub repository as described in the steps below.
Configure a global username to associate with your Git operations. Replace Example User with your actual username.
Configure a global email. Replace user@example.com with your active email address.
Initialize the Git repository.
Add your GitHub repository as the remote repository to push changes.
Stage all files in your working directory.
Commit the staged changes with a descriptive commit message of your choice. For example, Initial Commit.
Push the changes to your GitHub repository.
When prompted, enter your GitHub username and personal access token to successfully push changes to your repository.
When successful, visit your GitHub repository and verify that the new files are available.
At this point, you have Jenkins integrated with the Kubernetes cluster. Now, you will create a Jenkins pipeline to deploy your application. Follow the steps below to create a new pipeline.
Access your Jenkins dashboard and click New Item.
Enter a name for your project. For example myproject.
Select Multibranch Pipeline, and click OK to save changes.
Apply the following changes on the project configuration page:
Repository HTTPS URL field.
Click Save to apply your new project configuration.
Monitor the new Jenkins scan progress and verify that the scan log includes a FINISHED SUCCESS result.
Navigate to the pipeline project Status page.
Verify that a green checkmark is available and the Last Success field includes a time value that represents a successful build process.
Click the main value to view the pipeline processes page and verify that all processes are successful.
Access your server terminal session to verify the new deployment in your cluster.
Output:
As displayed in the above output, the Node.js application deployment is available in your cluster.
View the cluster services and verify that a new LoadBalancer service is available.
Output:
Keep note of the Node.js application LoadBalancer EXTERNAL-IP value to use when accessing your Node.js application.
Access your Node.js application using the Load Balancer IP on port 8080. Replace the example IP 172.16.10.10 with your actual IP Address.
Verify that the Node Hello World Application prompt displays in your browser session.
Switch to your node-application directory.
Change the Node Hello World Application message in the app.js file using the sed command to the Node Hello World Application Updated.
Stage changes in your local repository.
Commit new changes.
Push the changes to your GitHub repository.
Wait for at least 2 minutes for Jenkins to trigger the pipeline, rebuild the Node.js container image, and deploy it to your Kubernetes cluster.
When the pipeline execution process is successful. Visit your cluster Load Balancer IP to view the updated application.
You have implemented a CI/CD pipeline with Jenkins on a Vultr Kubernetes Engine (VKE) cluster. By setting up a CI/CD pipeline with Jenkins and Kubernetes, you can generate an efficient, reliable, and scalable software delivery cycle. This integration automates the software deployment process and allows you to focus on innovation and collaboration. For more information, visit the official Jenkins documentation.
0 Comments
Be the first to comment and share your perspective with the community.