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

Posted on Leave a comment

Create and use ConfigMap in Kubernetes With Diagram

Create ConfigMap from YAML file

Create and use configmap from single values

kubectl create configmap cm1 --from-literal=myval1=10000

That would create a configmap with a key myval1 = 10000

Of course, you can use multiple --from-literal blocks in a single command.

Use a single value from configmap in pod yaml

Create and use multiple environment variables from file

If you plan to use multiple environment values, prepare a file my-env-vars.whatever like this:

infantry=10000
bowman=2000
generals=100

Then, you can create a configmap like this:

kubectl create configmap cm3 --from-env-file=./my-env-vars.whatever

Then you can refer to all the variables as environment variables in pod by creating a YAM definition like this

Import configmap values as volume

For example, I have a configmap named cm3 like this:

In a pod definition, I mount the configmap as a volume at has mountPath as /etc/config2

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  name: pod3-player
spec:
  volumes:
  - name: local-config
    configMap:
      name: cm3
  containers:
  - image: nginx
    name: pod3-player
    volumeMounts:
    - name: local-config
      mountPath: /etc/config2

Now, if I view the content of the folder /etc/config2 inside the container in the pod, I can see a list of files which names are the keys in the configmap:

kubectl exec -it pod3-player -- ls /etc/config2

And if I view the content of a file, for example, health, I will get the value

kubectl exec -it pod3-player -- cat /etc/config2/health