Cheat Sheet for Kubernetes – Kubectl Command line document

This article contains a list of commonly used kubectl commands and flags which we can use as Cheat sheet for Kubernetes.

Introduction

Kubectl is the command line configuration tool for Kubernetes that communicates with a Kubernetes API server. Using Kubectl allows you to create, inspect, update, and delete Kubernetes objects. This cheatsheet will serve as a quick reference to make commands on many common Kubernetes components and resources.

Category Based Cheat Sheet

Lets see how we can use the full command for an object on things like pod(s) or the shortcode variation mentioned in parantheses in the heading of each section.

Cluster Management

Display endpoint information about the master and services in the cluster

Display the Kubernetes version running on the client and server

Fetching configuration of the cluster

To view available API resources

To list API version available

To list everything

Daemonsets

Shortcode = ds

List one or more daemonsets

Edit and update the definition of one or more daemonset

Delete a daemonset

Create a new daemonset

Manage the rollout of a daemonset

Display the detailed state of daemonsets within a namespace

Deployments

Shortcode = deploy

List one or more deployments

Display the detailed state of one or more deployments

Edit and update the definition of one or more deployment on the server

Create one a new deployment

Delete deployments

See the rollout status of a deployment

Events

Shortcode = ev

List recent events for all resources in the system

List Warnings only

List events but exclude Pod events

Pull events for a single node with a specific name

Filter out normal events from a list of events

Logs

Print the logs for a pod

Print the logs for the last hour for a pod

Get the most recent 20 lines of logs

Get logs from a service and optionally select which container

Print the logs for a pod and follow new logs

Print the logs for a container in a pod

Output the logs for a pod into a file named ‘pod.log’

View the logs for a previously failed pod

Manifest Files

Another option for modifying objects is through Manifest Files. We highly recommend using this method. It is done by using yaml files with all the necessary options for objects configured. We have our yaml files stored in a git repository, so we can track changes and streamline changes.

Apply a configuration to an object by filename or stdin. Overrides the existing configuration.

Create objects

Create objects in all manifest files in a directory

Create objects from a URL

Delete an object

Namespaces

Shortcode = ns

Create namespace

List one or more namespaces

Display the detailed state of one or more namespace

Delete a namespace

Edit and update the definition of a namespace

Display Resource (CPU/Memory/Storage) usage for a namespace

Nodes

Shortcode = no

Update the taints on one or more nodes

List one or more nodes

Delete a node or multiple nodes

Display Resource usage (CPU/Memory/Storage) for nodes

Resource allocation per node

Pods running on a node

Annotate a node

Mark a node as unschedulable

Mark node as schedulable

Drain a node in preparation for maintenance

Add or update the labels of one or more nodes

Pods

Shortcode = po

List one or more pods

Delete a pod

Display the detailed state of a pods

Create a pod

Execute a command against a container in a pod

Get interactive shell on a a single-container pod

Display Resource usage (CPU/Memory/Storage) for pods

Add or update the annotations of a pod

Add or update the label of a pod

Replication Controllers

Shortcode = rc

List the replication controllers

List the replication controllers by namespace

ReplicaSets

Shortcode = rs

List ReplicaSets

Display the detailed state of one or more ReplicaSets

Scale a ReplicaSet

Secrets

Create a secret

List secrets

List details about secrets

Delete a secret

Services

Shortcode = svc

List one or more services

Display the detailed state of a service

Expose a replication controller, service, deployment or pod as a new Kubernetes service

Edit and update the definition of one or more services

Service Accounts

Shortcode = sa

List service accounts

Display the detailed state of one or more service accounts

Replace a service account

Delete a service account

StatefulSet

Shortcode = sts

List StatefulSet

Delete StatefulSet only (not pods)

Common Options

In Kubectl you can specify optional flags with commands. Here are some of the most common and useful ones.

-o Output format. For example if you wanted to list all of the pods in ps output format with more information.

-n Shorthand for –namespace. For example, if you’d like to list all the Pods in a specific Namespace you would do this command:

-f Filename, directory, or URL to files to use to create a resource. For example when creating a pod using data in a file named newpod.json.

Conclusion

We hope that we have extensively covered most of the aspect of the Kubectl command that can be helpful for daily operations while handling the Kubernetes cluster. However if you need more details you can always refer the Official Kubernetes Documentation

Leave a comment