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
€10.99 €16.99
Paperback
€20.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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
Estimated delivery fee Deliver to Malta

Premium delivery 7 - 10 business days

€32.95
(Includes tracking information)

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 Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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
Estimated delivery fee Deliver to Malta

Premium delivery 7 - 10 business days

€32.95
(Includes tracking information)

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
€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

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela