How do I un-expose (undo expose) service?

2/6/2018

In Kubernetes, it is possible to make a service running in cluster externally accessible by running kubectl expose deployment. Why deployment as opposed to service is beyond my simpleton's comprehension. That aside, I would like to also be able to undo this operation afterwards. Think of a scenario, where I need to get access to the service that normally is only accessible inside the cluster for debugging purposes and then to restore original situation.

Is there any way of doing this short of deleting the deployment and creating it afresh?


PS. Actually deleting service and deployment doesn't help. Re-creating service and deployment with the same name will result in service being exposed.

-- wvxvw
kubernetes
service

1 Answer

2/6/2018

Assuming you have a deployment called hello-world, and do a kubectl expose as follows:

kubectl expose deployment hello-world --type=ClusterIP --name=my-service

this will create a service called my-service, which makes your deployment accessible for debugging, as you described.

To display information about the Service:

kubectl get services my-service

To delete this service when you are done debugging:

kubectl delete service my-service

Now your deployment is un-exposed.

-- Lindsay Landry
Source: StackOverflow