Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Kubernetes - A Complete DevOps Cookbook
  • Table Of Contents Toc
Kubernetes - A Complete DevOps Cookbook

Kubernetes - A Complete DevOps Cookbook

By : Karslioglu
4.5 (4)
close
close
Kubernetes - A Complete DevOps Cookbook

Kubernetes - A Complete DevOps Cookbook

4.5 (4)
By: Karslioglu

Overview of this book

Kubernetes is a popular open source orchestration platform for managing containers in a cluster environment. With this Kubernetes cookbook, you’ll learn how to implement Kubernetes using a recipe-based approach. The book will prepare you to create highly available Kubernetes clusters on multiple clouds such as Amazon Web Services (AWS), Google Cloud Platform (GCP), Azure, Alibaba, and on-premises data centers. Starting with recipes for installing and configuring Kubernetes instances, you’ll discover how to work with Kubernetes clients, services, and key metadata. You’ll then learn how to build continuous integration/continuous delivery (CI/CD) pipelines for your applications, and understand various methods to manage containers. As you advance, you’ll delve into Kubernetes' integration with Docker and Jenkins, and even perform a batch process and configure data volumes. You’ll get to grips with methods for scaling, security, monitoring, logging, and troubleshooting. Additionally, this book will take you through the latest updates in Kubernetes, including volume snapshots, creating high availability clusters with kops, running workload operators, new inclusions around kubectl and more. By the end of this book, you’ll have developed the skills required to implement Kubernetes in production and manage containers proficiently.
Table of Contents (12 chapters)
close
close

Deploying and managing the life cycle of Jenkins X

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:

  1. 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.
  2. 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:

  1. Create a Kubernetes cluster with GKE using the jx command with the gke parameter as follows:
$ jx create cluster gke --skip-login
  1. Select your Google Cloud project; in our example, devopscookbook.
  2. Select us-central1-a when asked to pick a Google Cloud zone.
  3. Select Static Jenkins Server and Jenkinsfiles as the installation type.
  4. Enter your GitHub username:
Creating a local Git user for GitHub server
? GitHub username:
  1. 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:
  1. By default, Jenkins X will set the ingress rules to use the magic DNS nip.io domain:
? Domain [? for help] (your_IP.nip.io)
  1. Enter Yes to the following question:
? Do you wish to use GitHub as the pipelines Git server: (Y/n)
  1. Select the GitHub organization where you want to create the environment repository; in our case, k8devopscookbook.
  1. 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:

  1. 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
  1. 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:

  1. Get the existing Kubernetes clusters by listing the contexts:
$ jx context
  1. 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:

  1. Validate that your cluster is compliant. These tests will typically take an hour:
jx compliance run
  1. Check the status. This command will return a Compliance tests completed message only after tests are completed:
$ jx compliance status
Compliance tests completed.
  1. 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:

  1. 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
  1. Remove the Git files from the cloned directory. This will remove the Git history from the directory:
$ cd hello-world & sudo rm -r .git/
  1. 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:

  1. 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
  1. 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:

  1. 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
  1. 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

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Kubernetes - A Complete DevOps Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon