Rub1K
Rub1K

Reputation: 91

Add some labels to a deployment?

I'm very beginner with K8S and I've a question about the labels with kubernetes. On youtube video (In french here), I've seen that :

The man create three deploys with these commands and run the command kubectl get deployment then kubectl get deployment --show-labels :

kubectl run monnginx --image nginx --labels "env=prod,group=front"
kubectl run monnginx2 --image nginx --labels "env=dev,group=front"
kubectl run monnginx3 --image nginx --labels "env=prod,group=back"
root@kubmaster:# kubectl get deployments
NAME            READY        UP-TO-DATE AVAILABLE      AGE
monnginx         1/1             1          1          46s
monnginx2        1/1             1          1          22s
monnginx3        1/1             1          1          10s
      
root@kubmaster:# kubectl get deployments --show-labels
NAME            READY        UP-TO-DATE AVAILABLE      AGE   LABELS
monnginx         1/1             1          1          46s   env=prod,group=front
monnginx2        1/1             1          1          22s   env=dev,group=front 
monnginx3        1/1             1          1          10s   env=prod,group=back
      

Currently, if I try to do the same things :

root@kubermaster:~ kubectl run mynginx --image nginx --labels "env=prod,group=front"
pod/mynginx created

root@kubermaster:~ kubectl run mynginx2 --image nginx --labels "env=dev,group=front"
pod/mynginx2 created

root@kubermaster:~ kubectl run mynginx3 --image nginx --labels "env=dev,group=back"
pod/mynginx3 created

When I try the command kubectl get deployments --show-labels, the output is :

No resources found in default namespace.

But if I try kubectl get pods --show-labels, the output is :

NAME       READY   STATUS    RESTARTS   AGE     LABELS
mynginx    1/1     Running   0          2m39s   env=prod,group=front
mynginx2   1/1     Running   0          2m32s   env=dev,group=front
mynginx3   1/1     Running   0          2m25s   env=dev,group=back

If I follow every steps from the videos, there is a way to put some labels on deployments... But the command kubectl create deployment does not accept the flag --labels :

Error: unknown flag: --labels

There is someone to explain why I've this error and How put some label ?

Thanks a lot !

Upvotes: 9

Views: 24993

Answers (3)

Alvis Felix
Alvis Felix

Reputation: 49

As of 2022 you can use the following imperative command to create a pod with labels as:-

kubectl run POD_NAME --image IMAGE_NAME -l myapp:app

where, myapp:app is the label name.

Upvotes: 2

buderu
buderu

Reputation: 445

I think the problem is something different.

Until Kubernetes 1.17 the command kubectl run created a deployment.
Since Kubernetes 1.18 the command kubectl run creates a pod.

Release Notes of Kubernetes 1.18

kubectl run has removed the previously deprecated generators, along with flags 
unrelated to creating pods. kubectl run now only creates pods. See specific 
kubectl create subcommands to create objects other than pods. (#87077, 
@soltysh) [SIG Architecture, CLI and Testing]

Upvotes: 4

Kamol Hasan
Kamol Hasan

Reputation: 13546

Because $ kubectl create deployment doesn't support --labels flag. But you can use $ kubectl label to add labels to your deployment.

Examples:

# Update deployment 'my-deployment' with the label 'unhealthy' and the value 'true'.
$ kubectl label deployment my-deployment unhealthy=true
  
# Update deployment 'my-deployment' with the label 'status' and the value 'unhealthy', overwriting any existing value.
$ kubectl label --overwrite deployment my-deployment status=unhealthy

It works with other Kubernetes objects too.

Format: kubectl label [--overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 ... KEY_N=VAL_N

Upvotes: 16

Related Questions