Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
kubectl: Command-Line Kubernetes in a Nutshell
kubectl: Command-Line Kubernetes in a Nutshell

kubectl: Command-Line Kubernetes in a Nutshell: Deploy, manage, and debug container workloads using the Kubernetes CLI

eBook
€16.99
Paperback
€20.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
Table of content icon View table of contents Preview book icon Preview Book

kubectl: Command-Line Kubernetes in a Nutshell

Chapter 1: Introducing and Installing kubectl

Kubernetes is an open source container orchestration system for managing containerized applications across multiple hosts in a cluster.

Kubernetes provides mechanisms for application deployment, scheduling, updating, maintenance, and scaling. A key feature of Kubernetes is that it actively manages containers to ensure that the state of the cluster always matches the user's expectations.

Kubernetes enables you to respond quickly to customer demand by scaling or rolling out new features. It also allows you to make full use of your hardware.

Kubernetes is the following:

  • Lean: Lightweight, simple, and accessible
  • Portable: Public, private, hybrid, and multi-cloud
  • Extensible: Modular, pluggable, hookable, composable, and toolable
  • Self-healing: Auto-placement, auto-restart, and auto-replication

Kubernetes builds on a decade and a half of experience at Google running production workloads at scale, combined with best-of-breed ideas and best practices from the community:

Figure 1.1 – A 10,000-foot view of Kubernetes' architecture

Figure 1.1 – A 10,000-foot view of Kubernetes' architecture

One of the ways to manage Kubernetes clusters is kubectl—Kubernetes' command-line tool for management, it is a tool for accessing a Kubernetes cluster that allows you to run different commands against Kubernetes clusters to deploy apps, manage nodes, troubleshoot deployments, and more.

In this chapter, we're going to cover the following main topics:

  • Introducing kubectl
  • Installing kubectl
  • kubectl commands

Technical requirements

To learn kubectl, you will need access to a Kubernetes cluster; it can be one of these cloud ones:

Alternatively, it can be a local one:

In this book, we are going to use Google Cloud's GKE Kubernetes cluster.

Introducing kubectl

You can use kubectl to deploy applications, inspect and manage them, check cluster resources, view logs, and more.

kubectl is a command-line tool that can run from your computer, in CI/CD pipelines, as part of the operating system, or as a Docker image. It is a very automation-friendly tool.

kubectl looks for a configuration file named .kube in the $HOME folder. In the .kube file, kubectl stores the cluster configurations needed to access a Kubernetes cluster. You can also set the KUBECONFIG environment variable or use the --kubeconfig flag to point to the kubeconfig file.

Installing kubectl

Let's take a look at how you can install kubectl on macOS, on Windows, and in CI/CD pipelines.

Installing on macOS

The easiest way to install kubectl on macOS is using the Homebrew package manager (https://brew.sh/):

  1. To install, run this:
    $ brew install kubectl
  2. To see the version you have installed, use this:
    $ kubectl version –client --short
    Client Version: v1.18.1

Installing on Windows

To install kubectl on Windows, you could use the simple command-line installer Scoop (https://scoop.sh/):

  1. To install, run this:
    $ scoop install kubectl
  2. To see the version you have installed, use this:
    $ kubectl version –client --short
    Client Version: v1.18.1
  3. Create the .kube directory in your home directory:
    $ mkdir %USERPROFILE%\.kube
  4. Navigate to the .kube directory:
    $ cd %USERPROFILE%\.kube
  5. Configure kubectl to use a remote Kubernetes cluster:
    $ New-Item config -type file

Installing on Linux

When you want to use kubectl on Linux, you have two options:

  • Use curl:
    $ curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
  • If your Linux system supports Docker images, use https://hub.docker.com/r/bitnami/kubectl/.

    Note

    Linux is a very common environment for CI/CD pipelines.

kubectl commands

To get a list of supported kubectl commands, run this:

$ kubectl --help

kubectl commands are grouped by category. Let's look at each category.

Basic commands

The following are basic kubectl commands:

  • create: Create a resource from a file or from stdin; for example, create a Kubernetes deployment from the file.
  • expose: Take a service, deployment, or pod and expose it as a new Kubernetes Service.
  • run: Run a particular image on the cluster.
  • set: Set specific features on objects—for example, set environment variables, update a Docker image in a pod template, and so on.
  • explain: Get the documentation of resources—for example, the documentation on deployments.
  • get: Display one or many resources. For example, you can get a list of running pods or the YAML output of a pod.
  • edit: Edit a resource—for example, edit a deployment.
  • delete: Delete resources by filenames, stdin, resources, and names, or by resources and label selectors.

Deploy commands

The following are kubectl deploy commands:

  • rollout: Manage the rollout of a resource.
  • scale: Set a new size for a deployment, ReplicaSet, or StatefulSet.
  • autoscale: Auto-scale a deployment, ReplicaSet, or StatefulSet.

Cluster management commands

The following are the kubectl cluster management commands:

  • certificate: Modify certificate resources.
  • cluster-info: Display cluster information.
  • top: Display resource (CPU/memory/storage) usage.
  • cordon: Mark a node as unschedulable.
  • uncordon: Mark a node as schedulable.
  • drain: Drain a node in preparation for maintenance.
  • taint: Update the taints on one or more nodes.

Troubleshooting and debugging commands

The following are the kubectl troubleshooting and debugging commands:

  • describe: Show the details of a specific resource or group of resources.
  • logs: Print the logs for a container in a pod.
  • attach: Attach to a running container.
  • exec: Execute a command in a container.
  • port-forward: Forward one or more local ports to a pod.
  • proxy: Run a proxy to the Kubernetes API server.
  • cp: Copy files and directories to and from containers.
  • auth: Inspect authorization.

Advanced commands

The following are the kubectl advanced commands:

  • diff: Show difference of live version against a would-be applied version.
  • apply: Apply a configuration to a resource by filename or stdin.
  • patch: Update the field(s) of a resource using a strategic merge patch.
  • replace: Replace a resource by filename or stdin.
  • wait: Wait for a specific condition on one or many resources.
  • convert: Convert config files between different API versions.
  • kustomize: Build a kustomization target from a directory or a remote URL.

Settings commands

The following are the settings commands in kubectl:

  • label: Update the labels on a resource.
  • annotate: Update the annotations on a resource.

Other commands

The following are several other commands used in kubectl:

  • alpha: Commands for features in alpha.
  • api-resources: Print the supported API resources on the server.
  • api-versions: Print the supported API versions on the server, in the form of group/version.
  • config: Modify kube-config files.
  • plugin: Provide utilities for interacting with plugins.
  • version: Print the client and server version information.

As you can see from the lists, commands are divided into different groups. We are going to learn about most but not all of these commands in the coming chapters.

At the time of writing, the kubectl version is 1.18; with more recent versions, the commands might have changed.

Summary

In this chapter, we have learned what kubectl is and how to install it on macOS, Windows, and CI/CD pipelines. We also checked out the different commands supported by kubectl and what they do.

In the next chapter, we will learn how to get information about Kubernetes clusters using kubectl.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Explore the Kubernetes command line for deploying applications, inspecting clusters, and viewing logs
  • Leverage kubectl for Kubernetes application management and container debugging
  • Apply your knowledge of Docker to learn kubectl equivalent commands for Docker subcommands

Description

The kubectl command line tool lets you control Kubernetes clusters to manage nodes in the cluster and perform all types of Kubernetes operations. This introductory guide will get you up to speed with kubectl in no time. The book is divided into four parts, touching base on the installation and providing a general overview of kubectl in the first part. The second part introduces you to managing Kubernetes clusters and working with nodes. In the third part, you’ll be taken through the different ways in which you can manage Kubernetes applications, covering how to create, update, delete, view, and debug applications. The last part of the book focuses on various Kubernetes plugins and commands. You’ll get to grips with using Kustomize and discover Helm, a Kubernetes package manager. In addition to this, you’ll explore how you can use equivalent Docker commands in kubectl. By the end of this book, you’ll have learned how to install and update an application on Kubernetes, view its logs, and inspect clusters effectively.

Who is this book for?

This book is for developers, system administrators, and anyone who wants to use the kubectl command-line tool to perform Kubernetes functionalities. A basic understanding of Kubernetes and Docker is required to get started with this book.

What you will learn

  • Get to grips with the basic kubectl commands
  • Delve into different cluster nodes and their resource usages
  • Understand the most essential features of kubectl
  • Discover how to patch Kubernetes deployments with Kustomize
  • Find out ways to extend kubectl tools with their own plugins
  • Explore how to use Helm as an advanced tool for deploying apps

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Nov 20, 2020
Length: 136 pages
Edition : 1st
Language : English
ISBN-13 : 9781800564572
Concepts :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning

Product Details

Publication date : Nov 20, 2020
Length: 136 pages
Edition : 1st
Language : English
ISBN-13 : 9781800564572
Concepts :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 122.97
Mastering Kubernetes
€59.99
kubectl: Command-Line Kubernetes in a Nutshell
€20.99
Kubernetes and Docker - An Enterprise Guide
€41.99
Total 122.97 Stars icon

Table of Contents

15 Chapters
Section 1: Getting Started with kubectl Chevron down icon Chevron up icon
Chapter 1: Introducing and Installing kubectl Chevron down icon Chevron up icon
Section 2: Kubernetes Cluster and Node Management Chevron down icon Chevron up icon
Chapter 2: Getting Information about a Cluster Chevron down icon Chevron up icon
Chapter 3: Working with Nodes Chevron down icon Chevron up icon
Section 3: Application Management Chevron down icon Chevron up icon
Chapter 4: Creating and Deploying Applications Chevron down icon Chevron up icon
Chapter 5: Updating and Deleting Applications Chevron down icon Chevron up icon
Chapter 6: Debugging an Application Chevron down icon Chevron up icon
Section 4: Extending kubectl Chevron down icon Chevron up icon
Chapter 7: Working with kubectl Plugins Chevron down icon Chevron up icon
Chapter 8: Introducing Kustomize for Kubernetes Chevron down icon Chevron up icon
Chapter 9: Introducing Helm for Kubernetes Chevron down icon Chevron up icon
Chapter 10: kubectl Best Practices and Docker Commands Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.3
(6 Ratings)
5 star 50%
4 star 33.3%
3 star 16.7%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Walter Lee Dec 01, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I highly recommend this new book by Rimantas Mocevicius. Finally a good book on the Swiss army knife, kubectl, for k8s with good examples. The first 6 chapters shows all about kubectl and how to do this and that with kubectl. Although we will often do declarative deployments. It is still important to know how to do things imperatively, e.g. k drain/cordon a problematic node, describe this and that k8s objects, k rollout undo/status/history, etc... It shows many real life kubectl on cloud examples with GCP GKE.I like Chapter 7 most because it shows how we can extend kubectl with plugins, e.g. how to use Krew to find and install plugins, then you can use "k really get all" plugin, k view-allocations, etc. It then teaches how to create your own simple plugin.Chapter 8 teaches Kustomize and how to use it to deploy with overlays.Chapter 9 is another favorite, Helm! Rimantas knows this very well because he is a co-author of Helm."Helm is the de facto Kubernetes package manager, and one of the best and easiest ways to install any kind of complex application on Kubernetes." (p. 93 of the book).He walks the readers step by step in Helm and how to use it to helm install, upgrade, rollback, template, lint, plugin, ... applications/deployments etc. For example, Rimantas shows us the proper way to change values of a helm chart (p.97). "To change values, it is recommended to use the override-values.yaml file ...Changing the default values.yaml file that comes with the chart is not recommended, as you might lose track of changes in the newer versions of the file.”! (It is a very good tip !)Ch.10 has some kubectl best practices and docker commands. I like the side by side equivalent of docker commands in kubectl. When the k8s API server is not responding, then these docker commands can help you troubleshoot more.Cons: there are a few syntax errors on p. 46, 53 and 70 when it should be double dashes " - -" but shown as single long dash "-" in the code block. Likely a formatting issue when published.
Amazon Verified review Amazon
Amazon Customer Mar 04, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Thank you Eldad! JFrog rules the world. This book brought me up to speed from novice to expert in kubernetes and container management. It's easy to read, understand and apply the knowledge to simple and complex DevOps solutions. I'm very happy I came across this book after going through many such books on the topic. Thank you again for excellent work!
Amazon Verified review Amazon
Rahul Agrawal Dec 05, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I always wanted to learn more about kubectl commands i am so happy that I came across this book by Packt, I will recommend this to all
Amazon Verified review Amazon
Dustin Dec 07, 2020
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
As other reviews have mentioned, this book does a great job of using kubectl to create and modify Kubernetes cluster resources. The official documentation can be overwhelming when new to Kubernetes. This book does a good job of aiding with order and structure to learn kubectl.This book also touches on kustomize and Helm, a nice surprise and not something often discussed while teaching kubectl.I would have enjoyed more detail on creating kubectl plugins. A more thorough chapter on creating plugins would make this book really stand out.
Amazon Verified review Amazon
jjuarez Dec 07, 2020
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
I like the completion of the look over the tool capabilities, I don’t like that the book is, may be, quite short
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.