2

Following this guestbook tutorial.

Near the end they sort of gloss over how to create a LoadBalancer or assign NodePorts to the service. They reference this doc that describes the different types and what they do, but don't really go into detail about how to apply them to a service. Is there a way to update the running service, creating and attaching a load balancer to the specified ports?

Using Amazon Web Services to host our cluster, it supports load balancer great at creation of a service, but I'm not sure how to modify the service.

EDIT:

Trying this resulted in an error:

$ kubectl expose service frontend --port=80 --type=LoadBalancer
Error from server: service "frontend" already exists

030
  • 5,731
  • 12
  • 61
  • 107
four43
  • 2,575
  • 2
  • 14
  • 17
  • So services aren't that hard to delete and recreate. Maybe that's more along the lines of k8s design, just delete it and recreate it. I was thinking if I took the service down, it would kill my pods, but that's the responsibility of the ReplicationControllers. I'm new to all this lingo. Original question still stands, for those interested. – four43 Oct 12 '15 at 19:09

3 Answers3

4

It's pretty confusing, but apparently kubectl expose can only create a new service, not update an existing one. It says that when running on an existing service, you should pass a --name flag to specify the desired name of the new service to avoid the conflict you were seeing.

To update a service in place, you should be able to use either kubectl patch or kubectl update. It's probably not the optimal approach, but I personally tend to do this sort of thing by running kubectl get svc svc-name -o yaml > svc.yaml, updating svc.yaml as desired (to have type: LoadBalancer in this case), then kubectl update -f svc.yaml.

Alex Robinson
  • 311
  • 1
  • 4
1

I'm not sure if this is suitable to your case, but a more radical and simpler approach would be to delete the service (but not the deployment):

kubectl delete service 'service-name'

and re-expose the deployment as LoadBalancer:

kubectl expose deployment/'deployment-name' --type="LoadBalancer"
Slipeer
  • 3,255
  • 2
  • 18
  • 32
  • A service is not a continer nor pod. LoadBalancer is a service. A pod won't down when a service is deleted. Got it. Thanks! – kujiy Mar 15 '18 at 10:23
0

You can do it either:

  • interactively by kubectl edit <SERVICE_NAME> and modify type to "LoadBalancer" in the editor
  • in one command by kubectl patch <SERVICE_NAME> -p '{"spec":{"type":"LoadBalancer"}}'
Ivan
  • 101
  • 2