Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selector does not work for service. #535

Closed
J-Siu opened this issue Sep 23, 2018 · 4 comments
Closed

Selector does not work for service. #535

J-Siu opened this issue Sep 23, 2018 · 4 comments
Labels
area/kubectl kind/bug Categorizes issue or PR as related to a bug. priority/P1 sig/cli Categorizes an issue or PR as relevant to SIG CLI.

Comments

@J-Siu
Copy link

J-Siu commented Sep 23, 2018

Is this a request for help? (If yes, you should use our troubleshooting guide and community support channels, see http://kubernetes.io/docs/troubleshooting/.):

What keywords did you search in Kubernetes issues before filing this one? (If you have found any duplicates, you should instead reply there.):


Is this a BUG REPORT or FEATURE REQUEST? (choose one): BUG REPORT

Kubernetes version (use kubectl version):

Client Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.3", GitCommit:"a4529464e4629c21224b3d52edfe0ea91b072862", GitTreeState:"clean", BuildDate:"2018-09-09T18:02:47Z", GoVersion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.3", GitCommit:"a4529464e4629c21224b3d52edfe0ea91b072862", GitTreeState:"clean", BuildDate:"2018-09-09T17:53:03Z", GoVersion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}

Environment:

apt-get update && apt-get install -y apt-transport-https curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y kubelet kubeadm kubectl
apt-mark hold kubelet kubeadm kubectl
  • Others:

What happened:

Selector does not work for service.

What you expected to happen:

Selector should work for service.

How to reproduce it (as minimally and precisely as possible):

  1. Deploy ghost:
kubectl run ghost --image=ghost --port 2368 --expose=true
  1. Verify service:
$ kubectl get svc -o wide
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE       SELECTOR
ghost        NodePort    10.100.72.171   <none>        2368:30538/TCP   1d        run=ghost
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP          1d        <none>
  1. Try with selector:
$ kubectl get svc -o wide -l run=ghost
No resources found.

$ kubectl delete svc -l run=ghost
No resources found

Anything else we need to know:

It work if I select k8s-app

$ kubectl get svc -o wide --all-namespaces
NAMESPACE     NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE       SELECTOR
default       ghost           NodePort    10.100.72.171   <none>        2368:30538/TCP   1d        run=ghost
default       kubernetes      ClusterIP   10.96.0.1       <none>        443/TCP          1d        <none>
kube-system   kube-dns        ClusterIP   10.96.0.10      <none>        53/UDP,53/TCP    1d        k8s-app=kube-dns
kube-system   tiller-deploy   ClusterIP   10.104.224.90   <none>        44134/TCP        9h        app=helm,name=tiller

$ kubectl get svc -o wide --all-namespaces -l k8s-app=kube-dns
NAMESPACE     NAME       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)         AGE       SELECTOR
kube-system   kube-dns   ClusterIP   10.96.0.10   <none>        53/UDP,53/TCP   1d        k8s-app=kube-dns

$ kubectl get svc -o wide --all-namespaces -l run=ghost
No resources found.
@seans3
Copy link
Contributor

seans3 commented Sep 26, 2018

/kind bug
/sig cli
/area kubectl
/priority P1

@k8s-ci-robot k8s-ci-robot added kind/bug Categorizes issue or PR as related to a bug. sig/cli Categorizes an issue or PR as relevant to SIG CLI. area/kubectl priority/P1 labels Sep 26, 2018
@m1kola
Copy link
Member

m1kola commented Oct 6, 2018

I can reproduce it. It's wired. When I add a new label to the service, I can select the service using this label:

kubectl label svc ghost app=something

kubectl get svc -o wide
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE       SELECTOR
ghost        ClusterIP   10.100.76.186   <none>        2368/TCP   14m       run=ghost
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP    54d       <none>

kubectl get svc -o wide -l app=something
NAME      TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE       SELECTOR
ghost     ClusterIP   10.100.76.186   <none>        2368/TCP   14m       run=ghost

but run=ghost still doesn't return any results

kubectl get svc -o wide -l run=ghost
No resources found.

I'll look, if I can find the cause and if it.

@m1kola
Copy link
Member

m1kola commented Oct 6, 2018

I think, it's not a bug. I just realised that the steps to reproduce do not have a step where we mark a service with a label. I didn't manage to find evidence that it's expected from kubectl run to implicitly add labels to pods.

A bit more details below. @J-Siu I hope it will explain why you are experiencing this.


When you run:

kubectl run ghost --image=ghost --port 2368 --expose=true

It creates a service and a pod (and a deployment, but it's not important for this issue). Important thing is - It does not assign new labels to the service implicitly. The selector you see in the output of kubectl get svc -o wide is the selector that your service uses to find related pods.

Try run kubectl get svc ghost -oyaml and you will see something like this:

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: 2018-10-06T10:07:07Z
  name: ghost
  namespace: default
  resourceVersion: "110979"
  selfLink: /api/v1/namespaces/default/services/ghost
  uid: 94d84ee9-c94f-11e8-9ae1-0800274eddd9
spec:
  clusterIP: 10.106.181.96
  ports:
  - port: 2368
    protocol: TCP
    targetPort: 2368
  selector:
    run: ghost
# ^^^^^^^^^^^^
# This is what `kubectl get svc -o wide` prints. Resource itself doesn't have any labels
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}

(see the comment in the yaml file)

In order to be able to select the service by a label, you need to add a new label to it explicitly. Like kubectl label svc ghost app=something.

It works with the k8s-app=kube-dns, because the kube-dns service has this label. See:

kubectl get svc -n kube-system kube-dns -oyaml
apiVersion: v1
kind: Service
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"addonmanager.kubernetes.io/mode":"Reconcile","k8s-app":"kube-dns","kubernetes.io/name":"KubeDNS"},"name":"kube-dns","namespace":"kube-system"},"spec":{"clusterIP":"10.96.0.10","ports":[{"name":"dns","port":53,"protocol":"UDP"},{"name":"dns-tcp","port":53,"protocol":"TCP"}],"selector":{"k8s-app":"kube-dns"}}}
  creationTimestamp: 2018-08-12T19:54:21Z
  labels:
    addonmanager.kubernetes.io/mode: Reconcile
    k8s-app: kube-dns
    kubernetes.io/name: KubeDNS
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# This is what `kubectl` uses to find resources
  name: kube-dns
  namespace: kube-system
  resourceVersion: "16567"
  selfLink: /api/v1/namespaces/kube-system/services/kube-dns
  uid: 815573bc-9e69-11e8-a6b0-0800274eddd9
spec:
  clusterIP: 10.96.0.10
  ports:
  - name: dns
    port: 53
    protocol: UDP
    targetPort: 53
  - name: dns-tcp
    port: 53
    protocol: TCP
    targetPort: 53
  selector:
    k8s-app: kube-dns
# ^^^^^^^^^^^^^^^^^^^^
# This is what services uses to discover related pods
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}

(also see the comments in the yaml file)

So kube-dns just uses the same labels to mark services, pods and deployments. That's why you can see results when you run

kubectl get svc -o wide --all-namespaces -l k8s-app=kube-dns

@J-Siu
Copy link
Author

J-Siu commented Oct 6, 2018

@m1kola

Arrrrr. I understand what you saying after I went through it a few times. As I am new to kubernetes and confusing between labels/selectors and also the -l option. But after some search and try with --show-labels, it becomes clear:

$ kubectl get svc -o wide --all-namespaces --show-labels
NAMESPACE     NAME            TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE       SELECTOR               LABELS
default       ghost           ClusterIP      10.97.101.35    <none>        2368/TCP         13d       run=ghost              <none>
default       hn              LoadBalancer   10.96.66.191    <pending>     8080:30673/TCP   12d       run=hn                 run=hn
default       kubernetes      ClusterIP      10.96.0.1       <none>        443/TCP          14d       <none>                 component=apiserver,provider=kubernetes
kube-system   kube-dns        ClusterIP      10.96.0.10      <none>        53/UDP,53/TCP    14d       k8s-app=kube-dns       k8s-app=kube-dns,kubernetes.io/cluster-service=true,kubernetes.io/name=KubeDNS
kube-system   tiller-deploy   ClusterIP      10.104.224.90   <none>        44134/TCP        13d       app=helm,name=tiller   app=helm,name=tiller

Thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/kubectl kind/bug Categorizes issue or PR as related to a bug. priority/P1 sig/cli Categorizes an issue or PR as relevant to SIG CLI.
Projects
None yet
Development

No branches or pull requests

4 participants