spec: selector "exists" does not find pod

4/3/2018

I have the following service where I want to select a pod from a label that exists.

But it does not work, do you know why?

Note: mypod: exists in the followin:

apiVersion: v1
kind: Service
metadata:
  name: myservice
  labels:
    run: myservice
spec:
  ports:
  - port: 8080
    targetPort: 80
  selector:
    mypod: exists
  type: LoadBalancer

The pod that I am trying to select is listed kubectl get pods --show-labels:

NAME                                         READY     STATUS    RESTARTS   AGE       LABELS
mypod-5bf4c474c8-l6rr2                1/1       Running   0          32m       mypod=49c8b466-2fdd-4ea6-b799-bc60c259a8ec

I don't have controle over the uuid and I know there will be only one pod - that is why I am trying to use exists.

Also if you know, what happens if there is more than one?

But most important, why does it not find the pod with exists?

-- Chris G.
kubernetes

2 Answers

4/3/2018

In addition to @Michael Hausenblas answer.

Kubernetes is using 2 types of labels selectors - Set-based and Equality-based.

Because the Service object is old and was created before Set-based requirements were released, you cannot use rules like if label 'myapp' exists, you should use equals like if label 'myapp' = 'abc'.

So, if your CI/CD is changing the value of myapp label, you can add some additional one with a static value, like appname: myapp and use it in the Service selectors. There are no other ways to do it.

-- Anton Kostenko
Source: StackOverflow

4/3/2018

As you can see from your kubectl command output, the value of the label with the key mypod is in fact 49c8b466-2fdd-4ea6-b799-bc60c259a8ec and not exists. So I suppose your CI/CD pipeline overwrites this? Anyways, a simple key-only query like kubectl get po -l=mypod should work.

-- Michael Hausenblas
Source: StackOverflow