How to specify selector for kubectl service creation?

6/22/2020

Is there a way to specify selector while creating service with kubectl directly like in command below ?

    ~ $ kubectl create service clusterip headless-svc --tcp=80:80 --clusterip='None' --selector="app=multitool" --dry-run -o yaml
Error: unknown flag: --selector

Obviously I can modify yaml file, e.g.

    ~ $ kubectl create service clusterip headless-svc --tcp=80:80 --clusterip='None' --dry-run -o yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: headless-svc
  name: headless-svc
spec:
  clusterIP: None
  ports:
  - name: 80-80
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: headless-svc
  type: ClusterIP
status:
  loadBalancer: {}

But Im curious if it possible to do during svc creation ?

Thank you !

-- Andy
kubernetes

1 Answer

6/22/2020

kubectl create service does not have an option to specify selector but you can use kubectl expose to create a service for deployment and specify selector

kubectl expose deployment nginx --port=80 --target-port=8000 --selector='app=multitool'
-- Arghya Sadhu
Source: StackOverflow