Posted on Leave a comment

Basic setups for working with Kubernetes

When working with kubernetes (maybe in a CKA exam), you work with terminal most of the time. Configuring the environment properly is vital to save you time. Here are things I do first when entering a new environment (Kubernetes Playground | Katacoda, for example).

Enable bash completion

Honestly, I don’t use this much. However, occasionally, it saves me some precious time so here you go:

echo 'source <(kubectl completion bash)' >>~/.bashrc
kubectl completion bash >/etc/bash_completion.d/kubectl

In addition, I would love to have k as an alias of kubectl:

echo 'alias k=kubectl' >>~/.bashrc
echo 'complete -F __start_kubectl k' >>~/.bashrc

Now I can type k get p and press tab twice, there are some nice suggestions:

Configure VIM

You have some choices when it comes to text editor in the terminal. For me personally, I use Vim. There are some basic configurations I need to set on every new environment:

  • Tab is 2
  • No tab, only spaces
  • Cursor column (super helpful when working with indentation)

So, here is my settings for VIM

set tabstop=2
set number
set shiftwidth=2
set expandtab
set cursorcolumn
set paste
set autoindent
set smartindent
colorscheme desert

With all that set, I’m ready to work on any tasks 😉

Quick copy/paste for all of those

You can copy all these commands and paste directly to your terminal to get similar effects:

echo 'source <(kubectl completion bash)' >>~/.bashrc
kubectl completion bash >/etc/bash_completion.d/kubectl
echo 'alias k=kubectl' >>~/.bashrc
echo 'complete -F __start_kubectl k' >>~/.bashrc
echo "set tabstop=2 number shiftwidth=2 expandtab cursorcolumn paste autoindent smartindent" > ~/.vimrc
echo "colorscheme desert" >> ~/.vimrc
Posted on Leave a comment

Affinity and Antiaffinity in Kubernetes simplified

Studying Affinity and AntiAffinity can be challenging due to the length of the official documentation. This post is not intended to replace the official docs but to provide a bird’s-eye view of the structure.

Some takeaways:

  • affinity is a sub section of pod’s spec
  • The diagram as follow:
pod affinity simple diagram
pod affinity simple diagram

  • The full diagram as follow:
pod affinity diagram full size
Pod affinity full diagram

Please ignore the root node (green). I don’t know how to remove it in Xmind.

The diagram looks complicated. However, if you look closely, there is a lot of duplication. For example, the block with key, operator, values appears many times.

The operator can be one of the following values:

key and values are string and array of string accordingly.

You can always refer to the official documentation here for examples.

Posted on Leave a comment

Kubernetes labels cheatsheet

Labels are key, value pairs that help identify a particular object (pods/deployment…)

Working with labels and pod

Create a pod with labels

 # Create pod
 kubectl run my-pod1 --image=nginx --labels="type=webserver,image-name=nginx"
 
 # Get all labels of a pod
  kubectl get pod my-pod1 --show-labels
  
  # Output
NAME      READY   STATUS    RESTARTS   AGE   LABELS
my-pod1   1/1     Running   0          44s   image-name=nginx,type=webserver
 

Add labels to running pods

# add a label
kubectl label pod my-pod1 opensource=true

#  add multiple labels
kubectl label pod my-pod1 a=1 b=2

# Get the labels
 kubectl get pod my-pod1 --show-labels
 
 # Output
NAME      READY   STATUS    RESTARTS   AGE     LABELS
my-pod1   1/1     Running   0          3m25s   a=1,b=2,image-name=nginx,opensource=true,type=webserve

Update an existing label

# Update current label (previously, a=1)
kubectl label pod my-pod1 a=2 --overwrite=true

# Get pod labels
kubectl get pod my-pod1 --show-labels

# Output
NAME      READY   STATUS    RESTARTS   AGE     LABELS
my-pod1   1/1     Running   0          4m56s   a=2,b=2,image-name=nginx,opensource=true,type=webserver

If you prefer, setting labels in YAML file is possible under metadata section.

Select a pod based on its label

Let’s create a few more pods with various labels

 kubectl run my-pod2 --image=nginx --labels="type=webserver,duty=backup,ver=1.0.0"

Now, we can use --selector to select pods:

kubectl get pods --selector="type=webserver"

# Output
NAME      READY   STATUS    RESTARTS   AGE
my-pod1   1/1     Running   0          9m49s
my-pod2   1/1     Running   0          33s

 kubectl get pods --selector="duty=backup"
 
 # Output, only my-pod2 is shown since only this pod has that label
NAME      READY   STATUS    RESTARTS   AGE
my-pod2   1/1     Running   0          87s

We can omit the value (after the = sign) to get the objects that have that label, regardless of the values

kubectl get pods --selector="a"

# Output
NAME      READY   STATUS    RESTARTS   AGE
my-pod1   1/1     Running   0          12m

kubectl get pods --selector="type"

#Output
NAME             READY   STATUS    RESTARTS   AGE
my-pod1          1/1     Running   0          13m
my-pod2          1/1     Running   0          4m17s
pod-with-label   1/1     Running   0          106m

Finally, we can select objects that have value exists in a possible set. First of all, get all pods and show their labels

# First get all pods and show their labels:
kubectl get pods --show-labels

# Output
NAME             READY   STATUS                       RESTARTS   AGE     LABELS
my-pod1          1/1     Running                      0          15m     a=2,b=2,image-name=nginx,opensource=true,type=webserver
my-pod2          1/1     Running                      0          6m43s   duty=backup,type=webserver,ver=1.0.0
my-pod3          1/1     Running                      0          9s      duty=backup,type=proxy,ver=1.0.0
nginx            1/1     Running                      0          33h     run=nginx
pod-with-label   1/1     Running                      0          108m    image=new-nginx,type=server
pod2             0/1     CreateContainerConfigError   0          5d15h   <none>
pod2-player      1/1     Running                      3          5d9h    <none>
pod3-player      1/1     Running                      3          5d8h    <none>
pvpod1           1/1     Running                      1          2d9h    <none>

As you can see, my-pod1, my-pod2, my-pod3 all have label type. If we want to select the ones with type webserver or server, we can run the following

kubectl get pods --selector="type in (webserver,server)"

# Output
NAME             READY   STATUS    RESTARTS   AGE
my-pod1          1/1     Running   0          18m
my-pod2          1/1     Running   0          9m11s
pod-with-label   1/1     Running   0          111m