kubectl - How to edit service spec type to LoadBalancer via command line?

7/27/2018

I have a k8s service of type clusterIP.. i need to change the below configuration via CLI

  1. the http port to https port
  2. the port number
  3. the type to Load Balancer

Is there a way to do it..?

-- Rajkumar Purushothaman
kubectl
kubernetes

2 Answers

7/27/2018

You can't remove the existing port, but you can add the HTTPs port and also change the type using kubectl patch

Example:

kubectl patch svc <my_service> -p '{"spec": {"ports": [{"port": 443,"targetPort": 443,"name": "https"},{"port": 80,"targetPort": 80,"name": "http"}],"type": "LoadBalancer"}}'

If you don't want to create JSON on the command line, create a yaml file like so:

ports:
  - port: 443
    targetPort: 443
    name: "https"
  - port: 80
    targetPort: 80
    name: "http"
  type: LoadBalancer

And then do:

kubectl patch svc <my_service> --patch "$(cat patch.yaml)"
-- jaxxstorm
Source: StackOverflow

7/27/2018

kubectl edit svc <service_name> -n <namespace>

i - to edit the service

ESC, :wq - update your service

Use kubectl patch svc <service_name> -p '{"spec": ....}' if you don't want the prompt.

-- AvnishSingh
Source: StackOverflow