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

Arrow left icon
Profile Icon Rimantas Mocevicius
Arrow right icon
Free Trial
Full star icon Full star icon Full star icon Full star icon Half star icon 4.3 (6 Ratings)
Paperback Nov 2020 136 pages 1st Edition
eBook
S$20.98 S$29.99
Paperback
S$36.99
Subscription
Free Trial
Arrow left icon
Profile Icon Rimantas Mocevicius
Arrow right icon
Free Trial
Full star icon Full star icon Full star icon Full star icon Half star icon 4.3 (6 Ratings)
Paperback Nov 2020 136 pages 1st Edition
eBook
S$20.98 S$29.99
Paperback
S$36.99
Subscription
Free Trial
eBook
S$20.98 S$29.99
Paperback
S$36.99
Subscription
Free Trial

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781800561878
Concepts :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.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
$199.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 S$6 each
Feature tick icon Exclusive print discounts
$279.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 S$6 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total S$ 219.97
Mastering Kubernetes
S$107.99
kubectl: Command-Line Kubernetes in a Nutshell
S$36.99
Kubernetes and Docker - An Enterprise Guide
S$74.99
Total S$ 219.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

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.