Kubernetes The Fast Way

In this post, I'm going through Kubernetes commands trying to use the imperative approach as much as possible.
Below there is my personal cheat sheet for Kubernetes administration hands-on.

Enjoy!

Table of content

Before you start

I've seen a lot of guys using k shortcut (alias) for kubectl command.

This documentation page mentions k alias as well.

Install bash-completion on Ubuntu / Debian

sudo apt install bash-completion

Install bash-completion on macOS:

brew install bash-completion

Kubectl autocomplete

echo "source <(kubectl completion bash)" >> ~/.bashrc &&\
echo "alias k=kubectl" >> ~/.bashrc &&\
echo "complete -F __start_kubectl k" >> ~/.bashrc

Generate manifest yaml output

Working with kubectl I found one more convenient alias which saved me a lot of time.

alias kg="kubectl -o yaml --dry-run=client" # generate manifest yaml output, 'g' for generate

Then you can use it as follow:

Generate single pod manifest

kg run webserver-pod --image nginx

output:

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: webserver-pod
  name: webserver-pod
spec:
  containers:
  - image: nginx
    name: webserver-pod
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

Generate deployment manifest

kg create deploy webserver --image nginx --replicas 3

output:

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: webserver
  name: webserver
spec:
  replicas: 3
  selector:
    matchLabels:
      app: webserver
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: webserver
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}

Caution!

Although aliases might be super-efficient while working on your own machine it could be harmful when it comes to working in a managed environment where you are limited with default commands only.

It's up to you to stick with default or leverage shortcuts.

Frequently used commands

Run a single pod

with given name webserver and image nginx:

kubectl run webserver --image nginx # run a single pod
kubectl run webserver --image nginx -l env=dev,tier=frontend # run a single pod with labels
kubectl run webserver --image nginx --env "env=prod" --env "tier=frontend" # run a single pod with environment variables set

pod

*Image 1. single pod in default namespace*

Check pod status

Check if pod is created and running:

kubectl get po # this command list all pods in the current namespace

or (if you have a bunch of pods) get pod status by its name (webserver):

kubectl get po webserver # get pod by name webserver

Delete pod

kubectl delete po webserver # delete pod by name webserver

Create deployment

with given name websever-dep and image nginx:

kubectl create deploy webserver-dep --image nginx 

deployment.png

*Image 2. deployment with replicaset and single pod in default namespace*

Check deployment status

kubectl get deploy # this command list all deployment in the current namespace

Scale deployment

kubectl scale deploy webserver-dep --replicas 3 # scale up to 3

scale.png

*Image 3. deployment scaled up to 3*