kubectl apply doesn't update service selector label

10/26/2018

right now I have a service

kubectl get svc ray-test-svc

apiVersion: v1
items:
- apiVersion: v1
  kind: Service
  metadata:
    annotations:
   ....
    labels:
      app: ray-test-app
      service: ray-test-svc
    name: ray-test-svc
    namespace: ray-test-ns
  spec:
    ports:
    - nodePort: 30198
      port: 80
      protocol: TCP
      targetPort: 8000
    selector:
      app: ray-test-app
      service: ray-test-svc
      version: v2
    type: LoadBalancer

After I edit my yaml file deleting the version: v2 in selector and do

kubectl apply -f ray-test-svc.yaml

the version:v2 is still in selector!

Here is my yaml file

kind: Service
apiVersion: v1
metadata:
  name: ray-test-svc
  annotations:
    ....
  labels:
    app: ray-test-app
    service: ray-test-svc
spec:
  selector:
    app: ray-test-app
    service: ray-test-svc
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 8000

I check the log by using -v=9 seeing that kubectl uses PATCH to do the update. Is this a bug in kubeApi or is there any way to just delete partial labels? Thanks!!

-- ComplexRay
kubectl
kubernetes
kubernetes-apiserver
restful-url

1 Answer

10/26/2018

It looks like misconfiguration: You didn't set the namespace in second YAML, and you apply it by command kubectl apply -f ray-test-svc.yaml. It will not update the old service, it will create a new one in namespace default. You can run command kubectl apply -f ray-test-svc.yaml -n ray-test-ns and it will update you service. Also you can add namespace: ray-test-ns to second YAML.

-- Nick Rak
Source: StackOverflow