Kubernetes Cheat Sheet

What is Kubernetes? Kubernetes, also known as K8s, is an open-source system for automating deployment, scaling, and management of containerized applications.

It groups containers that make up an application into logical units for easy management and discovery. Kubernetes builds upon 15 years of experience of running production workloads at Google, combined with best-of-breed ideas and practices from the community.

What is kubectl? Kubectl is a command line tool used to run commands against Kubernetes clusters. It does this by authenticating with the Master Node of your cluster and making API calls to do a variety of management actions.

The format of a kubectl command looks like this:

kubectl [command] [type] [name] [flags]
  • [command]: specifies the action you want to perform like create, delete, get, apply

  • [type]: any Kubernetes resource, whether automatically provided by Kubernetes (like a service or a pod) or created by you with a Custom Resource Definition

  • [name]: the name you have given the resource — if you omit the name, kubectl will return every resource specified by the type

  • [flags]: specify any additional global or command specific options such as the output format

What is kubeconfig ? kubeconfig is a configuration file which is used by kubectl In order to access your Kubernetes cluster. The default kubectl configuration file is located at ~/.kube/config and is referred to as the kubeconfig file.

kubeconfig files organize information about clusters, users, namespaces, and authentication mechanisms. The kubectl command uses these files to find the information it needs to choose a cluster and communicate with it.

The loading order follows these rules:

  1. If the --kubeconfig flag is set, then only the given file is loaded. The flag may only be set once and no merging takes place.

  2. If the $KUBECONFIG environment variable is set, then it is parsed as a list of filesystem paths according to the normal path delimiting rules for your system.

  3. Otherwise, the ${HOME}/.kube/config file is used and no merging takes place.

If you see a message similar to the following, kubectl is not configured correctly or is not able to connect to a Kubernetes cluster.

Cluster Management

The context consist of the clustername and namespace that the current user connects to.

ConfigMaps

Shortcode=cm

Daemonsets

Shortcode = ds

Deployments

Shortcode = deploy

If you use kubectl create to create a deployment, it will automatically get a lable with the name app=<NameofDeployment> .

Label plays an essential role in the monitoring that the kubernetes deployment is doing, label is used to make sure that the suffcient amount of pods are available.

Events

Shortcode = ev

Ingress

Shortcode=ing

Labels

Labels play an essential role in the kubernetes.

Usually labels are applied automatically, or we add them trough the yaml files.

Logs

Container doesn't have stout, to see what's happening within a container use logs:

For logs we also recommend using a tool developed by Johan Haleby called Kubetail. This is a bash script that will allow you to get logs from multiple pods simultaneously. You can learn more about it at its Github repository.

Manifest Files

Another option for modifying objects is through Manifest Files. Using this method is highly recommend. It is done by using yaml files with all the necessary options for objects configured. Also it is recommended to store your yaml files in a git repository, so you can track changes and streamline changes.

Incase of error while using apply or replace, use delete and then create combination.

Namespaces

Shortcode = ns

The optional kubectx package can be used to make switching between namespaces easier, it contains kubectx to switch between context, and kubens to switch between Namespaces. If multiple clusters are available to a kubernetes client, switching context is relevant. If multiple namespaces exist within a cluster, switching namespaces is relevant.

Nodes

Shortcode = no.

Pods

Shortcode = po

kubectl exec only works on pods!

Persistent Volume

Shortcode=pv

Persistent Volume Claim

shortcode=pvc

Replication Controllers

Shortcode = rc

ReplicaSets

Shortcode = rs

Secrets

Services

Shortcode = svc

Select service type using --type . it could be ClusterIP, NodePort, LoadBalancer or ExternalName. Default is 'ClusterIP'.

Services are using Labels, So it is very important for a Service that a Label is present, if you try to expose something that doesn't have a lablel, you can use a Service on top of that.

Service Accounts

Shortcode = sa

StatefulSet

Shortcode = sts

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:

--dry-run you can generate the yaml file using kubectl command, with out creating that object:

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

Create vs Apply :kubectl create can be used to create new resources while kubectl apply inserts or updates resources while maintaining any manual changes made like scaling pods.

--field-selectorlet you select Kubernetes resources based on the value of one or more resource fields. This kubectl command selects all Pods for which the value of the status.phase field is Running:

You can use the =, ==, and != operators with field selectors (= and == mean the same thing).

--watch or -w - watch for changes:

--record Add the current command as an annotation to the resource. The recorded change is useful for future introspection. For example, to see the commands executed in each Deployment revision:

-h for getting help:

explain command is always handy:

good luck!

.

.

.

Collected by Payam Borosan

Last updated

Was this helpful?