Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
The Kubernetes Workshop

You're reading from  The Kubernetes Workshop

Product type Book
Published in Sep 2020
Publisher Packt
ISBN-13 9781838820756
Pages 780 pages
Edition 1st Edition
Languages
Authors (6):
Zachary Arnold Zachary Arnold
Profile icon Zachary Arnold
Sahil Dua Sahil Dua
Profile icon Sahil Dua
Wei Huang Wei Huang
Profile icon Wei Huang
Faisal Masood Faisal Masood
Profile icon Faisal Masood
Mélony Qin Mélony Qin
Profile icon Mélony Qin
Mohammed Abu Taleb Mohammed Abu Taleb
Profile icon Mohammed Abu Taleb
View More author details

Table of Contents (20) Chapters

Preface
1. Introduction to Kubernetes and Containers 2. An Overview of Kubernetes 3. kubectl – Kubernetes Command Center 4. How to Communicate with Kubernetes (API Server) 5. Pods 6. Labels and Annotations 7. Kubernetes Controllers 8. Service Discovery 9. Storing and Reading Data on Disk 10. ConfigMaps and Secrets 11. Build Your Own HA Cluster 12. Your Application and HA 13. Runtime and Network Security in Kubernetes 14. Running Stateful Components in Kubernetes 15. Monitoring and Autoscaling in Kubernetes 16. Kubernetes Admission Controllers 17. Advanced Scheduling in Kubernetes 18. Upgrading Your Cluster without Downtime 19. Custom Resource Definitions in Kubernetes

3. kubectl – Kubernetes Command Center

Overview

In this chapter, we will demystify some common kubectl commands and see how we can use kubectl to control our Kubernetes cluster. We will begin this chapter by taking a brief look at what the end-to-end process looks like when using kubectl commands to communicate with a Kubernetes cluster. Then, we will set up a few shortcuts and autocompletion for the Bash terminal. We will begin with the basics of using kubectl by learning how to create, delete, and manage Kubernetes objects. We will learn about the two approaches to managing resources in Kubernetes - declarative and imperative - with exercises. By the end of this chapter, you will also have learned how to update a live application running on your Kubernetes cluster in real-time using kubectl.

Introduction

In Chapter 1, Introduction to Kubernetes and Containers, we saw that Kubernetes is a portable and highly extensible open-source container orchestration tool. It provides very powerful capabilities that can be used to manage containerized workloads at scale. In the previous chapter, you got the big picture of how the different components of Kubernetes work together to achieve the desired goals. We also demonstrated some basic usage of kubectl in Chapter 2, An Overview of Kubernetes. In this chapter, we will take a closer look at this utility and look at how we can make use of its potential.

To reiterate, kubectl is a command-line utility for interacting with Kubernetes clusters and performing various operations. There are two ways to use kubectl while managing your cluster - imperative management, which focuses on commands rather than the YAML manifests to achieve the desired state, and declarative management, which focuses on creating and updating YAML manifest files...

How kubectl Communicates with Kubernetes

As we saw in the previous chapter, the API server manages communications between the end-user and Kubernetes, and it also acts as an API gateway to the cluster. To achieve this, it implements the RESTful API over the HTTP and HTTPS protocols to perform CRUD operations to populate and modify Kubernetes API objects such as pods, services, and more based upon the instructions sent by a user via kubectl. These instructions can be in various forms. For example, to retrieve information for pods running in the cluster, we would use the kubectl get pods command, while to create a new pod, we would use the kubectl run command.

First, let's take a look at what happens behind the scenes when you run a kubectl command. Take a look at the following illustration, which provides an overview of the process, and then we will take a closer look at the different details of the process:

Figure 3.1: A representative flowchart for the...

Setting up Environments with Autocompletion and Shortcuts

In most Linux environments, you can set up autocompletion for kubectl commands before you start working with the instructions mentioned in this chapter. Learning how autocompletion and shortcuts work in Linux environments will be significantly helpful for those who are interested in getting certifications such as Certified Kubernetes Administrator (CKA) and Certified Kubernetes Application Developer (CKAD), which are conferred by the Linux Foundation. We'll learn how to set up autocompletion in the following exercise.

Exercise 3.01: Setting up Autocompletion

In this exercise, we will show you how to set up autocompletion and an alias for kubectl commands in Bash. This is a useful feature that will help you save time and avoid typos. Perform the following steps to complete this exercise:

  1. We will need the bash-completion package, so install it if it is not already installed. You can go to the GitHub repository...

Common kubectl Commands

As previously described, kubectl is a CLI tool that is used to communicate with the Kubernetes API server. kubectl has a lot of useful commands for working with Kubernetes. In this section, we're going to walk you through some commonly used kubectl commands and shortcuts that are used to manage Kubernetes objects.

Frequently Used kubectl Commands to Create, Manage, and Delete Kubernetes Objects

There are several simple kubectl commands that you will use almost all the time. In this section, we will take a look at some of the basic kubectl commands:

  • get <object>: You can use this command to get the list of the desired types of objects. Using all instead of specifying an object type will get the list of all kinds of objects. By default, this will get the list of specified object types in the default namespace. You can use the -n flag to get objects from a specific namespace; for example, kubectl get pod -n mynamespace.
  • describe <...

Populating Deployments in Kubernetes

As we mentioned earlier, Deployment is a convenient way to manage and update pods. Defining a Deployment in Kubernetes is an effective and efficient way to provide declarative updates for the application running in your cluster.

You can create a Deployment by using kubectl imperative commands or by using declarative YAML manifest files. In the following exercise, we're going to deploy an application (we will go with Nginx for this exercise) in Kubernetes and learn how to interact with Deployments using kubectl commands, as well as how to modify the YAML manifest file.

Exercise 3.02: Creating a Deployment

There are two ways to create a Deployment in Kubernetes – using the kubectl create/run command and creating a manifest file in YAML format and then using the kubectl apply command. We can achieve the same goal with those two options. Let's try both and then compare them:

  1. Create a Deployment using the following command...

Summary

This chapter demystified how kubectl allows us to control our Kubernetes cluster using API calls. First, we learned how to set up an environment for kubectl commands and looked at a number of shortcuts. Furthermore, we covered how to create, edit, and delete a Kubernetes object using kubectl commands and looked at a Deployment as an example. Finally, we deployed a real-life application and showed you how to edit a live Deployment. Every example in this chapter has been applied in a general context; however, we believe that the skills developed in this chapter can help you resolve specific problems that you might encounter in a professional environment.

In the next chapter, you'll explore the other side of this bridge and dive deeper into how the API server works. You will also take a closer look at REST API requests and how the API server deals with them.

lock icon The rest of the chapter is locked
You have been reading a chapter from
The Kubernetes Workshop
Published in: Sep 2020 Publisher: Packt ISBN-13: 9781838820756
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}