Reader small image

You're reading from  The Kubernetes Workshop

Product typeBook
Published inSep 2020
PublisherPackt
ISBN-139781838820756
Edition1st Edition
Right arrow
Authors (6):
Zachary Arnold
Zachary Arnold
author image
Zachary Arnold

Zachary Arnold works as a software engineer at Ygrene Energy Fund. Zach has an experience of over 10 years in modern web development. He is an active contributor to the Open Source Kubernetes project in both SIG-Release and SIG-Docs currently focusing on security. He has been running clusters in production since Kubernetes 1.7 and has spoken at the previous 4 KubeCons. His passion areas in the project center on building highly stable Kubernetes cluster components and running workloads securely inside of Kubernetes.
Read more about Zachary Arnold

Sahil Dua
Sahil Dua
author image
Sahil Dua

Sahil Dua is a software engineer. He started using Kubernetes to run machine learning workloads. Currently, he is running various types of applications on Kubernetes. He shared his learnings as a keynote session at KubeCon Europe 2018. He is a passionate open source contributor and has contributed to some famous projects such as Git, pandas, hound, go-GitHub, and so on. He has been an open source community leader for over 2 years at DuckDuckGo.
Read more about Sahil Dua

Wei Huang
Wei Huang
author image
Wei Huang

Wei Huang: Wei works as a senior software engineer in IBM. He has over 10 years' experiences around database, data warehouse tooling, cloud, container, monitoring and devops. He started to use Kubernetes since 1.3, including extending Kubernetes LoadBalancer using CRD, networking, scheduling and monitoring. Now he is a core maintainer of Kubernetes SIG-Scheduling.
Read more about Wei Huang

Faisal Masood
Faisal Masood
author image
Faisal Masood

Faisal Masood is a cloud transformation architect at AWS. Faisal's focus is to assist customers in refining and executing strategic business goals. Faisal main interests are evolutionary architectures, software development, ML lifecycle, CD and IaC. Faisal has over two decades of experience in software architecture and development.
Read more about Faisal Masood

Mélony Qin
Mélony Qin
author image
Mélony Qin

Mélony Y. QIN, also known as CloudMelon, is the founder of CloudMelon Vis, a tech media and educational platform for technopreneurs in the cloud-native and serverless space, and a former product manager at Microsoft. With a passion for cloud-native technologies, OSS, DevOps, Kubernetes, serverless, data, and AI, Mélony has authored multiple books, including the Certified Kubernetes Administrator (CKA) Exam Guide, the Kubernetes Workshop, and Microsoft Azure Infrastructure, all published by Packt Publishing. Mélony is a member of the Association for Computing Machinery (ACM) and the Project Management Institute (PMI), leveraging her extensive experience with diverse cloud technologies to drive innovation in the cloud-native, serverless, and generative AI space. She runs the CloudMelonVis YouTube channel and Cloud-Native Innovators newsletter, read by professionals from top tech companies such as Microsoft, Google, Amazon, Dell, and Carrefour.
Read more about Mélony Qin

Mohammed Abu Taleb
Mohammed Abu Taleb
author image
Mohammed Abu Taleb

Mohammed Abu-Taleb works as a Technical Advisor at Microsoft. Working at Microsoft CSS team for troubleshooting complex issues and cases for premier customers that are using Azure Kubernetes Services (AKS). Prior that, Mohammed was a SME (subject matter expert) for the azure managed monitoring service (Azure Monitor) focusing on designing, deploying, and troubleshooting monitoring strategies for containers.
Read more about Mohammed Abu Taleb

View More author details
Right arrow

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 2020Publisher: PacktISBN-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.
undefined
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 AU $19.99/month. Cancel anytime

Authors (6)

author image
Zachary Arnold

Zachary Arnold works as a software engineer at Ygrene Energy Fund. Zach has an experience of over 10 years in modern web development. He is an active contributor to the Open Source Kubernetes project in both SIG-Release and SIG-Docs currently focusing on security. He has been running clusters in production since Kubernetes 1.7 and has spoken at the previous 4 KubeCons. His passion areas in the project center on building highly stable Kubernetes cluster components and running workloads securely inside of Kubernetes.
Read more about Zachary Arnold

author image
Sahil Dua

Sahil Dua is a software engineer. He started using Kubernetes to run machine learning workloads. Currently, he is running various types of applications on Kubernetes. He shared his learnings as a keynote session at KubeCon Europe 2018. He is a passionate open source contributor and has contributed to some famous projects such as Git, pandas, hound, go-GitHub, and so on. He has been an open source community leader for over 2 years at DuckDuckGo.
Read more about Sahil Dua

author image
Wei Huang

Wei Huang: Wei works as a senior software engineer in IBM. He has over 10 years' experiences around database, data warehouse tooling, cloud, container, monitoring and devops. He started to use Kubernetes since 1.3, including extending Kubernetes LoadBalancer using CRD, networking, scheduling and monitoring. Now he is a core maintainer of Kubernetes SIG-Scheduling.
Read more about Wei Huang

author image
Faisal Masood

Faisal Masood is a cloud transformation architect at AWS. Faisal's focus is to assist customers in refining and executing strategic business goals. Faisal main interests are evolutionary architectures, software development, ML lifecycle, CD and IaC. Faisal has over two decades of experience in software architecture and development.
Read more about Faisal Masood

author image
Mélony Qin

Mélony Y. QIN, also known as CloudMelon, is the founder of CloudMelon Vis, a tech media and educational platform for technopreneurs in the cloud-native and serverless space, and a former product manager at Microsoft. With a passion for cloud-native technologies, OSS, DevOps, Kubernetes, serverless, data, and AI, Mélony has authored multiple books, including the Certified Kubernetes Administrator (CKA) Exam Guide, the Kubernetes Workshop, and Microsoft Azure Infrastructure, all published by Packt Publishing. Mélony is a member of the Association for Computing Machinery (ACM) and the Project Management Institute (PMI), leveraging her extensive experience with diverse cloud technologies to drive innovation in the cloud-native, serverless, and generative AI space. She runs the CloudMelonVis YouTube channel and Cloud-Native Innovators newsletter, read by professionals from top tech companies such as Microsoft, Google, Amazon, Dell, and Carrefour.
Read more about Mélony Qin

author image
Mohammed Abu Taleb

Mohammed Abu-Taleb works as a Technical Advisor at Microsoft. Working at Microsoft CSS team for troubleshooting complex issues and cases for premier customers that are using Azure Kubernetes Services (AKS). Prior that, Mohammed was a SME (subject matter expert) for the azure managed monitoring service (Azure Monitor) focusing on designing, deploying, and troubleshooting monitoring strategies for containers.
Read more about Mohammed Abu Taleb