Jenkins X is an open source solution that offers software developers pipeline automation, built-in GitOps, CI, automated testing, and CD, known as CI/CD, in Kubernetes. Jenkins X is highly focused on accelerating software delivery at a large scale using the Kubernetes ecosystem.
In this section, we will focus on Jenkins X recipes and create a Kubernetes cluster with CI/CD capabilities on your cloud provider.
Getting ready
In the following recipes, you will learn how to create a static Jenkins Server to deploy Kubernetes clusters with pipeline automation and automated CI/CD with GitOps promotion and preview environments.
This recipe requires kubectl and Helm. For this recipe, we will use GKE (short for Google Kubernetes Engine), therefore the gcloud CLI tool needs to be installed as well. You also need to have a proper GitHub organization and GitHub account created.
How to do it...
This section is further divided into the following subsections to ease the process:
- Installing the Jenkins X CLI
- Creating a Jenkins X Kubernetes cluster
- Verifying Jenkins X components
- Switching Kubernetes clusters
- Validating cluster conformance
Installing the Jenkins X CLI
The Jenkins X CLI jx is used along with your preferred cloud provider CLI to orchestrate the Deployment of the Kubernetes cluster. Jenkins X supports Azure, AWS, GCP (short for Google Cloud Platform), IBM Cloud, Oracle Cloud, Minikube, Minishift, and OpenShift as the provider for the Deployment. For this recipe, we will use GKE. See the Jenkins X documentation for other vendor instructions.
Let's perform the following steps to install Jenkins X CLI tool:
- Visit the JX release site (https://github.com/jenkins-x/jx/releases) and note the latest release version. At the time of writing, the latest release was v2.0.905.
- Update the release version in the following command. Download and install the latest version of the Jenkins X CLI:
$ curl -L https://github.com/jenkins-x/jx/releases/download/v2.0.905/jx-linux-amd64.tar.gz | tar xzv
$ sudo mv jx /usr/local/bin
Now you have the Jenkins X CLI installed, you can move on to the next recipe.
Creating a Jenkins X Kubernetes cluster
You may prefer other cloud vendors or on-premises deployment. For this recipe, we will use GKE. See the Jenkins X documentation for other vendor instructions.
Let's perform the following steps to create your first Jenkins X Kubernetes cluster using jx:
- Create a Kubernetes cluster with GKE using the jx command with the gke parameter as follows:
$ jx create cluster gke --skip-login
- Select your Google Cloud project; in our example, devopscookbook.
- Select us-central1-a when asked to pick a Google Cloud zone.
- Select Static Jenkins Server and Jenkinsfiles as the installation type.
- Enter your GitHub username:
Creating a local Git user for GitHub server
? GitHub username:
- Enter your GitHub API token. Go to the GitHub Token page at https://github.com/settings/tokens/new?scopes=repo,read:user,read:org,user:email,write:repo_hook,delete_repo to get your API token:
Please click this URL and generate a token
https://github.com/settings/tokens/new?scopes=repo,read:user,read:org,user:email,write:repo_hook,delete_repo
Then COPY the token and enter it following:
? API Token:
- By default, Jenkins X will set the ingress rules to use the magic DNS nip.io domain:
? Domain [? for help] (your_IP.nip.io)
- Enter Yes to the following question:
? Do you wish to use GitHub as the pipelines Git server: (Y/n)
- Select the GitHub organization where you want to create the environment repository; in our case, k8devopscookbook.
- You will see a message similar to the following when your Deployment is successful:
Jenkins X installation completed successfully
********************************************************
NOTE: Your admin password is: your_password
********************************************************
...
Context "gke_devopscookbook_us-central1-a_slayersunset" modified.
NAME HOSTS ADDRESS PORTS AGE
chartmuseum chartmuseum.jx.your_IP.nip.io your_IP 80 7m43s
docker-registry docker-registry.jx.your_IP.nip.io your_IP 80 7m43s
jenkins jenkins.jx.your_IP.nip.io your_IP 80 7m43s
nexus nexus.jx.your_IP.nip.io your_IP 80 7m43s
You can also find your admin password in the preceding output.
Verifying Jenkins X components
Let's perform the following steps to verify that all Jenkins X components are running as expected:
- Confirm that all pods are running. All pods in the jx namespace should be in a running state:
$ kubectl get pods -n jx
NAME READY STATUS RESTARTS AGE
jenkins-956c58866-pz5vl 1/1 Running 0 11m
jenkins-x-chartmuseum-75d45b6d7f-5bckh 1/1 Running 0 11m
jenkins-x-controllerrole-bd4d7b5c6-sdkbg 1/1 Running 0 11m
jenkins-x-controllerteam-7bdd76dfb6-hh6c8 1/1 Running 0 11m
jenkins-x-controllerworkflow-7545997d4b-hlvhm 1/1 Running 0 11m
jenkins-x-docker-registry-6d555974c7-sngm7 1/1 Running 0 11m
jenkins-x-heapster-7777b7d7d8-4xgb2 2/2 Running 0 11m
jenkins-x-nexus-6ccd45c57c-btzjr 1/1 Running 0 11m
maven-brcfq 2/2 Running 0 63s
maven-qz0lc 2/2 Running 0 3m
maven-vqw9l 2/2 Running 0 32s
- Get the list of Jenkins X service URLs that we will need to connect. You will have a list of jenkins, chartmuseum, docker-registry, and nexus URLs similar to the following:
$ jx get urls
NAME URL
jenkins http://jenkins.jx.your_IP.nip.io
jenkins-x-chartmuseum http://chartmuseum.your_IP.nip.io
jenkins-x-docker-registry http://docker-registry.jx.your_IP.nip.io
nexus http://nexus.jx.your_IP.nip.io
Now you can connect to the Jenkins UI by visiting the first URL from the preceding output of the jx get urls command.
Switching Kubernetes clusters
Let's perform the following steps to switch between the Kubernetes clusters that you have access to using Jenkins X:
- Get the existing Kubernetes clusters by listing the contexts:
$ jx context
- Select the cluster you would like to use. In our case, we switch to the gke_devopscookbook cluster that we created using Jenkins X:
Change Kubernetes context: [Use arrows to move, space to select, type to filter]
> gke_devopscookbook_us-central1-a_slayersunset
eks_devopscookbook_us-west
openshift_cluster
Now you know how to switch context using the Jenkins X CLI.
Validating cluster conformance
If you switch between the existing Kubernetes clusters, it is suggested that you validate the cluster configuration before you run your pipelines. Let's perform the following steps:
- Validate that your cluster is compliant. These tests will typically take an hour:
jx compliance run
- Check the status. This command will return a Compliance tests completed message only after tests are completed:
$ jx compliance status
Compliance tests completed.
- Review the results. If your cluster is compliant all executed test results should be displayed as PASSED:
$ jx compliance results
Now you know how to check cluster conformance results.
How it works...
The Creating a Jenkins X Kubernetes cluster recipe showed you how to provision the Kubernetes cluster for pipeline automation and automated CI/CD.
In the Creating a Jenkins X Kubernetes cluster recipe, in step 1, we created the cluster using the Jenkins X CLI. By default, Jenkins X uses n1-standard-2 as the machine type on GKE and creates a cluster with a minimum of three and a maximum of five nodes settings. Remember that you could also use an existing Kubernetes cluster instead of creating a new one. Most of the settings will be saved and remembered next time you run the create cluster command.
Jenkins X deploys a couple of services, including Jenkins, a private Docker registry, a private Helm repository ChartMuseum, Monocular to manage Helm charts, and a Maven and npm repository called Nexus.
As you will find in your repository after the installation, Jenkins X creates two Git repositories, one for your staging environment and one for production. Jenkins X uses the GitOps approach to promote code from one repo to another through Git pull requests (PRs). Therefore each repo contains a Jenkins pipeline to handle promotions.
In the Creating a Jenkins X Kubernetes cluster recipe, in step 7, Jenkins X uses the magic DNS service and converts the IP address of your GKE cluster into a DNS discoverable hostname using a service via nip.io. If you have your own domain and the DNS is configured to point to your cluster, you can update the settings later using the jx upgrade ingress --cluster command.
Later, in step 10, you will get the default password assigned to your admin user. You will be asked to change this password when you first connect to the Jenkins UI via the URL provided in this step.
There's more…
It is also useful to have knowledge of the following information:
- Importing an application
- Upgrading Jenkins X
- Deleting a Jenkins X Kubernetes cluster
Importing an application
Let's perform the following steps to import an existing application into the Jenkins X environment:
- Clone or use an existing application. As an example, we will create a clone of the hello-world example:
$ mkdir import && cd import
$ git clone https://github.com/k8sdevopscookbook/hello-world.git
- Remove the Git files from the cloned directory. This will remove the Git history from the directory:
$ cd hello-world & sudo rm -r .git/
- Run the following command in the folder to import the source code into Jenkins X:
$ jx import
Upgrading a Jenkins X application
Let's perform the following steps to upgrade a Jenkins X application and its components:
- First, upgrade the jx CLI. This command will upgrade the application if there is a new version available in the remote repository:
$ jx upgrade cli
- Once you are on the latest CLI, upgrade the platform using the following command. The new jx CLI command will upgrade the platform components if a new version exists:
$ jx upgrade platform
Deleting a Jenkins X Kubernetes cluster
Removing managed Kubernetes clusters may be tricky, especially if you are not the one who created them. Since we used GKE to create them, it is faster to use the gcloud CLI tool to delete them. Let's perform the following steps to delete the Kubernetes cluster we created with Jenkins X:
- Use your cloud provider's instructions to delete the Kubernetes cluster. In our case, we used GKE for the recipe. First, list the clusters:
$ gcloud container clusters list
NAME LOCATION MASTER_VERSION MASTER_IP MACHINE_TYPE NODE_VERSION NUM_NODES STATUS
clustername us-central1-a 1.12.8-gke.10 your_IP n1-standard-2 1.12.8-gke.10 3 RUNNING
- Delete the cluster using the clustername from the output of step 1:
$ gcloud container clusters delete <clustername>
Now you have learned how to use Jenkins X to create your cluster. This knowledge has prepared you for Chapter 3, Building CI/CD Pipelines, where you will continue to use the environment and learn to import an application as a pipeline in Jenkins X.
See also
- Introduction to Jenkins: https://jenkins.io/blog/2018/03/19/introducing-jenkins-x/
- Jenkins X repository and binaries: https://github.com/jenkins-x/jx
- Jenkins X tutorials: https://jenkins-x.io/tutorials/
- Jenkins X getting started instructions: https://jenkins-x.io/getting-started/install-on-cluster/
- Jenkins X CLI commands and an explanation of how to use them: https://jenkins-x.io/commands/jx/