Introduction
Continuous delivery (CD) is a software engineering approach in which teams produce software in short cycles, ensuring that the software can be reliably released at any time. The goal is to build, test, and release software faster and more frequently. Kubernetes and Jenkins X are powerful tools that facilitate this process. This tutorial will guide you through the steps of using Kubernetes with Jenkins X for continuous delivery.
What is Kubernetes?
Kubernetes is an open-source platform designed to automate deploying, scaling, and operating application containers. Originally developed by Google, Kubernetes provides a robust, flexible framework for managing containerized applications across different types of infrastructures.
What is Jenkins X?
Jenkins X is an open-source project that leverages Jenkins and Kubernetes to provide automated CI/CD (Continuous Integration/Continuous Delivery) for cloud-native applications. Jenkins X simplifies the process of creating and managing Kubernetes environments and pipelines, making it easier for developers to deliver applications in a consistent and reliable manner.
Prerequisites
Before diving into the tutorial, ensure you have the following:
- Basic understanding of Kubernetes and Jenkins: Familiarity with Kubernetes concepts such as Pods, Services, and Deployments, as well as Jenkins pipelines and jobs.
- Installed tools:
- Kubernetes CLI (kubectl)
- Jenkins X CLI (jx)
- Docker
- Helm
- A Kubernetes cluster: You can use Minikube for local development or a managed Kubernetes service like GKE, EKS, or AKS for production environments.
Step 1: Setting Up Your Environment
1.1 Install Kubernetes and Kubectl
First, ensure that Kubernetes and kubectl are installed. You can follow the official Kubernetes documentation for installation instructions based on your operating system.
1.2 Install Docker
Docker is required for building container images. Download and install Docker from the official Docker website.
1.3 Install Jenkins X CLI
The Jenkins X CLI (jx) is used to interact with Jenkins X and manage your Kubernetes cluster. Install it using the following commands:
brew install jenkins-x/jx/jx
Code language: Shell Session (shell)
For Linux:
curl -L https://github.com/jenkins-x/jx/releases/download/v2.1.150/jx-linux-amd64.tar.gz | tar xzv
mv jx /usr/local/bin
Code language: Shell Session (shell)
1.4 Install Helm
Helm is a package manager for Kubernetes. Install it using the following command:
For macOS:
brew install helm
Code language: Shell Session (shell)
For Linux:
curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
Code language: Shell Session (shell)
Step 2: Creating a Kubernetes Cluster
For this tutorial, we will use Minikube to create a local Kubernetes cluster. If you’re using a managed Kubernetes service, you can skip this step.
2.1 Install Minikube
Install Minikube using the following commands:
For macOS:
brew install minikube
Code language: Haml (haml)
For Linux:
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
Code language: Shell Session (shell)
2.2 Start Minikube
Start Minikube with the following command:
minikube start --memory=4096 --cpus=4
Code language: Shell Session (shell)
2.3 Configure kubectl
Configure kubectl to use the Minikube context:
kubectl config use-context minikube
Code language: Shell Session (shell)
Step 3: Installing Jenkins X
With your Kubernetes cluster up and running, you can now install Jenkins X.
3.1 Boot Jenkins X
Jenkins X uses the jx boot
command to set up your Jenkins X environment. Run the following command to start the installation process:
jx boot
Code language: Shell Session (shell)
Follow the prompts to configure your installation. You’ll need to provide information such as your GitHub credentials and the repository where Jenkins X will store its configuration.
3.2 Verify Installation
Verify that Jenkins X is installed correctly by running:
jx status
This command should show the status of your Jenkins X installation, including the URL of the Jenkins X console.
Step 4: Creating a New Project
4.1 Create a New Quickstart Project
Jenkins X provides quickstart templates for creating new projects. Run the following command to create a new quickstart project:
jx create quickstart
Code language: Shell Session (shell)
Select a language and framework from the list of available quickstarts. Jenkins X will create a new repository based on the selected template and set up the CI/CD pipeline.
4.2 Clone the Repository
Clone the newly created repository to your local machine:
git clone https://github.com/<your-github-username>/<your-repository>.git
cd <your-repository>
Code language: Shell Session (shell)
Step 5: Configuring the Pipeline
Jenkins X uses Tekton for defining and running CI/CD pipelines. The pipeline configuration is stored in the jenkins-x.yml
file.
5.1 Define the Pipeline
Open the jenkins-x.yml
file and define the stages of your pipeline. A basic pipeline might look like this:
buildPack: go
pipelineConfig:
pipelines:
release:
pipeline:
stages:
- name: build
steps:
- name: build
sh: make build
- name: test
steps:
- name: test
sh: make test
- name: release
steps:
- name: release
sh: make release
Code language: YAML (yaml)
5.2 Customize the Pipeline
Customize the pipeline to fit your project’s requirements. For example, you can add steps for linting, security scanning, and deploying to different environments.
Step 6: Deploying Your Application
6.1 Create Environments
Jenkins X manages different environments such as staging
and production
using GitOps. Create these environments using the following commands:
jx create env --name staging
jx create env --name production
Code language: Shell Session (shell)
6.2 Promote to Staging
To deploy your application to the staging environment, run the following command:
jx promote --version 0.0.1 --env staging
Code language: Shell Session (shell)
This command will trigger the promotion pipeline, which builds, tests, and deploys the application to the staging environment.
6.3 Promote to Production
Once your application is tested in staging, you can promote it to production:
jx promote --version 0.0.1 --env production
Code language: Shell Session (shell)
Step 7: Monitoring and Observability
7.1 Install Prometheus and Grafana
Jenkins X integrates with Prometheus and Grafana for monitoring and observability. Install them using the following commands:
jx add app prometheus
jx add app grafana
Code language: Shell Session (shell)
7.2 Access Grafana Dashboard
Access the Grafana dashboard to visualize metrics and monitor your application:
jx open grafana
Code language: Shell Session (shell)
Step 8: Managing Secrets
8.1 Install External Secrets
Jenkins X uses External Secrets for managing secrets. Install it using the following command:
jx add app external-secrets
Code language: Shell Session (shell)
8.2 Create Secrets
Create secrets using Kubernetes secrets and External Secrets. For example:
kubectl create secret generic my-secret --from-literal=username=my-user --from-literal=password=my-password
Code language: Shell Session (shell)
Step 9: Advanced Pipeline Configuration
9.1 Parallel Steps
To run steps in parallel, define them in the jenkins-x.yml
file:
pipelineConfig:
pipelines:
release:
pipeline:
stages:
- name: build
parallel:
- name: build-go
sh: make build-go
- name: build-js
sh: make build-js
Code language: YAML (yaml)
9.2 Conditional Steps
Run steps conditionally based on certain criteria:
pipelineConfig:
pipelines:
release:
pipeline:
stages:
- name: deploy
steps:
- name: deploy
sh: make deploy
when:
branch:
include:
- main
Code language: YAML (yaml)
9.3 Approval Gates
Add approval gates to your pipeline for manual approval before proceeding to the next stage:
pipelineConfig:
pipelines:
release:
pipeline:
stages:
- name: approve
steps:
- name: approval
input:
name: Approval
message: "Approve the deployment to production?"
Code language: YAML (yaml)
Step 10: Cleaning Up
After you are done experimenting with Jenkins X, clean up your resources to avoid unnecessary costs.
10.1 Delete Environments
Delete the staging and production environments:
jx delete env staging
jx delete env production
Code language: Shell Session (shell)
10.2 Uninstall Jenkins X
Uninstall Jenkins X from your Kubernetes cluster:
jx uninstall
Code language: Shell Session (shell)
10.3 Delete Kubernetes Cluster
If you are using Minikube, stop and delete the cluster:
minikube stop
minikube delete
Code language: Shell Session (shell)
Conclusion
In this tutorial, we covered how to use Kubernetes with Jenkins X for continuous delivery. We walked through setting up the environment, creating a Kubernetes cluster, installing Jenkins X, creating and configuring a project, deploying applications, and managing secrets. Additionally, we explored advanced pipeline configurations and monitoring with Prometheus and Grafana.