Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
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

By Rimantas Mocevicius
€16.99
Book Nov 2020 136 pages 1st Edition
eBook
€16.99
Print
€20.99
Subscription
€14.99 Monthly
eBook
€16.99
Print
€20.99
Subscription
€14.99 Monthly

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
Buy Now
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.

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 : Nov 20, 2020
Length 136 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781800561878
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
Buy Now

Product Details


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

Table of Contents

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

Customer reviews

Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
Top Reviews
No reviews found
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.