Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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..?

like image 998
Rajkumar Purushothaman Avatar asked Jul 27 '18 13:07

Rajkumar Purushothaman


People also ask

What is service type LoadBalancer?

The LoadBalancer type is an extension of the NodePort type. So a Service of type LoadBalancer has a cluster IP address and one or more nodePort values.

How LoadBalancer service works in Kubernetes?

This kind of algorithm works by monitoring changes in response latency as the load adjusts based on server capacity. The Kubernetes load balancer sends connections to the first server in the pool until it is at capacity, and then sends new connections to the next available server.


3 Answers

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)"
like image 165
jaxxstorm Avatar answered Sep 19 '22 16:09

jaxxstorm


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.

like image 26
AvnishSingh Avatar answered Sep 18 '22 16:09

AvnishSingh


The original solution does not works on powershell. Here is the following we need to do in order to make it work.

kubectl patch

Powershell:

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

Notice the \ for powershell use case.

Bash:

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

like image 38
Chandra Prakash Ajmera Avatar answered Sep 18 '22 16:09

Chandra Prakash Ajmera