Kubernetes commands cheat sheet

Dhruv | Jul 26, 2022

Here are some quick kubernetes commands that are helpful for me. This list keeps on growing.

General

Kubectl Version

kubectl version

Deploy a container

kubectl create deploy DEPLOYMENT_NAME --image=IAMGE_NAME:IMAGE_VERSION

# Example
kubectl create deploy nginx --image=nginx:1.17.10

Apply a configuration

kubectl apply -f FILE.yaml

Expose Service

kubectl expose deployment DEPLOYMENT_NAME --port PORT --type EXPOSURE_TYPE

# Example
kubectl expose deployment nginx --port 80 --type LoadBalancer

Change Replicas

kubectl scale deployment DEPLOYMENT_NAME --replicas REPLICA_COUNT

# Example
kubectl scale deployment nginx --replicas 3

Pods

List Pods

kubectl get pods

Get Pending Pods

kubectl get pods --field-selector=status.phase=Pending

Describe Pod

kubectl describe pod POD_NAME

Get System pods

kubectl -n kube-system get pods

Get pod definition in a YAML file

kubectl get pod POD_NAME -o yaml > FILE_NAME.yaml

Get pods with selector

kubectl get pods --selector app=App1

Get pods with multiple selectors

kubectl get pods --selector app=App1,tier=frontend

Create a pod

kubectl run POD_NAME --image=IMAGE

Update pod with resource requests

apiVersion: v1
kind: Pod
metadata:
  name: bee
spec:
  containers:
  - image: nginx
    name: bee
  resources:
    requests:
      memory: "1Gi"
      cpu: 1

Create pod with command & arguments

apiVersion: v1 
kind: Pod 
metadata:
  name: ubuntu-sleeper-2 
spec:
  containers:
  - name: ubuntu
    image: ubuntu
		command: ["sleep"]
    args: ["5000"]

Executing commands in pod

kubectl exec -it pod-name -- /bin/bash

Mounting Shared Volumes

apiVersion: v1
kind: Pod
metadata:
  name: app
  namespace: elastic-stack
  labels:
    name: app
spec:
  containers:
  - name: app
    image: kodekloud/event-simulator
    volumeMounts:
    - mountPath: /log
      name: log-volume

  - name: sidecar
    image: kodekloud/filebeat-configured
    volumeMounts:
    - mountPath: /var/log/event-simulator/
      name: log-volume

  volumes:
  - name: log-volume
    hostPath:
      # directory location on host
      path: /var/log/webapp
      # this field is optional
      type: DirectoryOrCreate

initContainers

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: busybox:1.28
    command: ['sh', '-c', 'echo The app is running! && sleep 3600']
  initContainers:
  - name: init-myservice
    image: busybox
    command: ['sh', '-c', 'git clone <some-repository-that-will-be-used-by-application> ; done;']

Multiple InitCOntainers

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: busybox:1.28
    command: ['sh', '-c', 'echo The app is running! && sleep 3600']
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2; done;']
  - name: init-mydb
    image: busybox:1.28
    command: ['sh', '-c', 'until nslookup mydb; do echo waiting for mydb; sleep 2; done;']

Environment Variable

apiVersion: v1 
kind: Pod 
metadata:
  name: ubuntu-sleeper-2 
spec:
  containers:
  - name: ubuntu
    image: ubuntu
		env: 
			- name: x
				value: y
			- name: a
				value: b

Taints and Toleration

Apply taint on node

kubectl taint nodes NODE_NAME KEY=VALUE:EFFECT

Create Pod with toleration

Yaml File

apiVersion: v1
kind: Pod
metadata:
  name: bee
spec:
  containers:
  - image: nginx
    name: bee
  tolerations:
  - key: spray
    value: mortein
    effect: NoSchedule
    operator: Equal

Apply it

kubectl apply -f FILE_NAME.yaml

Remove Taint from node

kubectl taint nodes NODE_NAME KEY:EFFECT-

Label a node

kubectl label nodes NODE_NAME LABEL_KEY=LABEL_VALUE

Rollouts

Revision and rollout history

kubectl rollout history deploymeny/deployment-name

Rollout status

kubectl rollout status deployment/mydeployment-name

Undo rollout

kubectl rollout undo deployment/mydeployment-name

Deployments

Edit deployment

kubectl edit deployment deployment-name

ConfigMaps

Creating ConfigMaps (Imperative)

We are creating a configmap by the name app-config with a key-value pair APP_COLOR and blue

kubectl create configmap \
	app-config --from-literal=APP_COLOR=blue

Add multiple key-value pairs:

kubectl create configmap \
	app-config --from-literal=APP_COLOR=blue
							--from-literal=APP_MODE=prod

This can become complicated.

Therefore, using file:

kubectl create configmap \
	app-config --from-file=app_config.properties

Creating ConfigMaps (Declarative)

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config (name of config file)
data:
	APP_COLOR: blue
	APP_MODE: prod
kubectl create -f ABOVE_FILE_NAME

Get configmaps

kubectl get configmaps

Injecting ConfigMap into Pod

apiVersion: v1
kind: Pod
metadata:
  name: bee
spec:
  containers:
  - image: nginx
    name: bee
		ports:
		 - containerPort: 8080
		envFrom:
		 - configMapRef:
					name: app-config

Secrets

Imperative way to create secrets

kubectl create secret generic \
	<secret-name> --from-literal=<key>=<value>

Imperative way to create secrets

apiVersion: v1
kind: Secret
metadata:
  name: app-config (name of config file)
data:
	DB_HOST: bX1zxWw=

These are base64 encoded values

echo -n 'mysql' | base64

Decoding value

echo -n "bX1zWw=" | base64 --decode

Injecting Secrets into Pod

apiVersion: v1
kind: Pod
metadata:
  name: bee
spec:
  containers:
  - image: nginx
    name: bee
		ports:
		 - containerPort: 8080
		envFrom:
		 - secretRef:
					name: app-config

Other ways to inject secrets

Single Environment variable

env:
	- name: DB_PASSWORD
	  valueFrom:
			secretKeyRef:
				name: app-secret
				key: DB_Password

Volume

volumes:
- name: app-secret-volume
	secret:
		secretName: app-secret

Get secrets

kubectl get secrets