Professional Documents
Culture Documents
Kubectl - Kubernetes Command Center - The Kubernetes Workshop
Kubectl - Kubernetes Command Center - The Kubernetes Workshop
Kubectl - Kubernetes Command Center - The Kubernetes Workshop
3. kubectl – Kubernetes
Command Center
Overview
Introduction
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 1/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 3/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 4/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 5/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
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.
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 6/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 7/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 9/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
Note
cat $HOME/.kube/config
You should get an output similar to the
following:
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 11/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
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.
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 13/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 14/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
k get no
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 15/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
kubectl cluster-info
You should see an output similar to the
following:
curl
https://raw.githubusercontent.com/
PacktWorkshops/Kubernetes-
Workshop/master/Chapter03/Activity
03.01/sample-application.yaml --
output sample-application.yaml
Now, you can deploy the sample-
application.yaml file using the
following command:
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 17/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
deployment.apps/redis-back created
service/redis-back created
deployment.apps/melonvote-front
created
service/melonvote-front created
Now that you have deployed the provided
application, if you try any of the commands
shown later in this section, you will see the
various objects, events, and so on related to this
application. Note that your output may not
exactly match the images shown here:
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 18/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 19/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
k get svc
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 21/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
https://kubernetes.io/docs/reference/kubectl/overview/#outpu
options.
Note
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.
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 24/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 25/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 26/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
ports:
- containerPort: 80
In this YAML definition, the replicas field
defines the number of replica pods in this
Deployment.
3. Use the following command to apply the
configuration you've defined in the YAML
manifest file:
kubectl apply -f sample-
deployment.yaml
The sample output will look as follows:
kubectl apply -f sample-
deployment.yaml
4. Use the following command to check the
Deployments that currently exist in the
default namespace:
kubectl get deployments
The output will look as follows:
NAME READY UP-TO-DATE AVAILABLE
AGE
aci-helloworld 1/1 1 1 27d
kubeserve 3/3 3 3 26m
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 28/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 29/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 30/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 31/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 32/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 33/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 34/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 35/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 36/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
curl
https://raw.githubusercontent.com/
PacktWorkshops/Kubernetes-
Workshop/master/Chapter03/Activity
03.01/sample-application.yaml --
output sample-application.yaml
This manifest file defines all the different objects
that are required to run the application, as well
as the application itself.
Note
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 38/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
Note
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.
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 39/40
10/16/22, 9:10 AM 3. kubectl – Kubernetes Command Center | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_03_Final_SMP_ePub.xhtml 40/40