Get service by label selector did not match the real label

Cluster information:

Kubernetes version:1.14.4
Cloud being used: bare-metal
Installation method: kubeadm
Host OS: CentOS7.6
CNI and version: Calico 3.3.6
CRI and version: Docker 18.09.5

I’v got a svc:

apiVersion: v1
kind: Service
metadata:
  labels:
    app: service-apollo-admin-server
  name: service-apollo-admin-server
  namespace: apollo
spec:
  ports:
  - nodePort: 31346
    port: 8090
    protocol: TCP
    targetPort: 8090
  selector:
    app: pod-apollo-admin-server
  sessionAffinity: ClientIP
  sessionAffinityConfig:
    clientIP:
      timeoutSeconds: 10800
  type: NodePort

The label of this svc is “app: service-apollo-admin-server”,
but the selector is “app: pod-apollo-admin-server”.


When performed

kubectl get svc -n apollo --selector=app=pod-apollo-admin-server

There are no resources found.

But

kubectl get svc -n apollo --selector=app=service-apollo-admin-server

gives a result


What I understand is with “–selector=app=pod-apollo-admin-server”, gives the right result.

Is that right?

And this is being tested on 1.21.12, still remain

Your service is labelled app: service-apollo-admin-server.

You asked kubectl to get service labelled app=pod-apollo-admin-server. There are none. Because that is not the label on the service.

Your service is looking for pods labelled app=pod-apollo-admin-server, but that’s not what you asked for here.

Hello thockin,

Is there a way to get service filtered by spec.selector?

Thank you

I’ve searched if my understanding is right, like this:

it says

kubectl get services \
--selector=${KEY-1}=${VALUE-1},${KEY-2}=${VALUE-2},...
--namespace=${NAMESPACE}

Where ${KEY} and ${VALUE} are the Pod’s label(s) key(s) and values(s)

The logic he describe is what I’m looking for, but instead, it’s still filtered by metadata.labels

I think that answer is wrong. There is not a way to get services by their selector, except to get them all and filter.

Thank you.
I’ll find another way around.