Thursday, July 16, 2020

selectors and labels --kubernetes

We have deployed a number of PODs. They are labelled with 'tier', 'env' and 'bu'. How many PODs exist in the 'dev' environment? Use selectors to filter the output

master $ kubectl get pods --selector env=dev
NAME          READY   STATUS    RESTARTS   AGE
app-1-gn2vz   1/1     Running   0          2m18s
app-1-w74zc   1/1     Running   0          2m18s
app-1-xwrxd   1/1     Running   0          2m18s
db-1-24wcv    1/1     Running   0          2m17s
db-1-96lkn    1/1     Running   0          2m17s
db-1-l4nss    1/1     Running   0          2m17s
db-1-ntpbh    1/1     Running   0          2m17s
master $ kubectl get pods --selector env=dev | wc -l
8

How many PODs are in the 'finance' business unit ('bu')?

master $ kubectl get pods --selector bu=finance
NAME          READY   STATUS    RESTARTS   AGE
app-1-gn2vz   1/1     Running   0          4m47s
app-1-w74zc   1/1     Running   0          4m47s
app-1-xwrxd   1/1     Running   0          4m47s
app-1-zzxdf   1/1     Running   0          4m46s
auth          1/1     Running   0          4m46s
db-2-2lzpz    1/1     Running   0          4m46s
master $ kubectl get pods --selector bu=finance | wc -l
7


How many objects are in the 'prod' environment including PODs, ReplicaSets and any other objects?

master $ kubectl get all --selector env=prod
NAME              READY   STATUS    RESTARTS   AGE
pod/app-1-zzxdf   1/1     Running   0          6m44s
pod/app-2-dckt9   1/1     Running   0          6m45s
pod/auth          1/1     Running   0          6m44s
pod/db-2-2lzpz    1/1     Running   0          6m44s

NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
service/app-1   ClusterIP   10.103.148.63   <none>        3306/TCP   6m44s

NAME                    DESIRED   CURRENT   READY   AGE
replicaset.apps/app-2   1         1         1       6m45s
replicaset.apps/db-2    1         1         1       6m44s
master $


Identify the POD which is 'prod', part of 'finance' BU and is a 'frontend' tier?

master $ kubectl get pods --selector env=prod,bu=finance,tier=frontend
NAME          READY   STATUS    RESTARTS   AGE
app-1-zzxdf   1/1     Running   0          11m

A ReplicaSet definition file is given 'replicaset-definition-1.yaml'. Try to create the replicaset. There is an issue with the file. Try to fix it.

First of all if we try to create the replicaset with the existing definition file, it will give error

master $ kubectl create -f replicaset-definition-1.yaml
The ReplicaSet "replicaset-1" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"tier":"nginx"}: `selector` does not match template `labels`

Hence, we have to make some changes on the definition file itself ; Change the label name within the template section to match with that of general label name in the file i.e frontend.
vi replicaset-definition-1.yaml
Make necessary changes, save it and the run the below command again to create the replicaset successfully.

master $ kubectl create -f replicaset-definition-1.yaml
replicaset.apps/replicaset-1 created
master $ kubectl get pods
NAME                 READY   STATUS              RESTARTS   AGE
app-1-gn2vz          1/1     Running             0          16m
app-1-w74zc          1/1     Running             0          16m
app-1-xwrxd          1/1     Running             0          16m
app-1-zzxdf          1/1     Running             0          16m
app-2-dckt9          1/1     Running             0          16m
auth                 1/1     Running             0          16m
db-1-24wcv           1/1     Running             0          16m
db-1-96lkn           1/1     Running             0          16m
db-1-l4nss           1/1     Running             0          16m
db-1-ntpbh           1/1     Running             0          16m
db-2-2lzpz           1/1     Running             0          16m
replicaset-1-lpz6p   0/1     ContainerCreating   0          6s
master $ kubectl get pods
NAME                 READY   STATUS    RESTARTS   AGE
app-1-gn2vz          1/1     Running   0          16m
app-1-w74zc          1/1     Running   0          16m
app-1-xwrxd          1/1     Running   0          16m
app-1-zzxdf          1/1     Running   0          16m
app-2-dckt9          1/1     Running   0          16m
auth                 1/1     Running   0          16m
db-1-24wcv           1/1     Running   0          16m
db-1-96lkn           1/1     Running   0          16m
db-1-l4nss           1/1     Running   0          16m
db-1-ntpbh           1/1     Running   0          16m
db-2-2lzpz           1/1     Running   0          16m
replicaset-1-lpz6p   1/1     Running   0          11s
master $ kubectl get replicaset
NAME           DESIRED   CURRENT   READY   AGE
app-1          3         3         3       16m
app-2          1         1         1       16m
db-1           4         4         4       16m
db-2           1         1         1       16m
replicaset-1   2         2         2       20s

No comments:

Post a Comment