I have deployed a Service into a kubernetes cluster and it looks like so:
$ kubectl get svc my-service
NAME CLUSTER_IP EXTERNAL_IP PORT(S) SELECTOR AGE
my-service 192.168.202.23 <none> 8080/TCP name=my-service 38d
The spec part of YAML config looks like so:
"spec": {
"ports": [
{
"name": "http-port",
"protocol": "TCP",
"port": 8080,
"targetPort": 8080
}
],
"selector": {
"name": "my-service"
},
"clusterIP": "192.168.202.23",
"type": "ClusterIP",
"sessionAffinity": "None"
},
"status": {
"loadBalancer": {}
}
}
Now, I want to expose this service to be externally accessible using LoadBalancer. Using kubectl expose service gives an error like so:
$ kubectl expose service my-service --type="LoadBalancer"
Error from server: services "my-service" already exists
Is it not possible to 'edit' existing deployed service and make it externally accessible?
The type of the service that you have created is ClusterIP
which is not visible outside the cluster. If you edit the service and change the type
field to either NodePort
, or LoadBalancer
, it would expose it.
Documentation on what those service types are and what they mean is at: http://kubernetes.io/docs/user-guide/services/#publishing-services---service-types
In addition to Anirudh answer(right answer)... take into account the following.
Valid values for the ServiceType field are:
ClusterIP: use a cluster-internal IP only - this is the default and is discussed above. Choosing this value means that you want this service to be reachable only from inside of the cluster.
NodePort: on top of having a cluster-internal IP, expose the service on a port on each node of the cluster (the same port on each node). You’ll be able to contact the service on any :NodePort address. This means that you will forward the node port to the container port which is exposed.Problem, those ports should be externally reachable on every node in the cluster.
LoadBalancer: on top of having a cluster-internal IP and exposing service on a NodePort also, ask the cloud provider for a load balancer which forwards to the Service exposed as a :NodePort for each Node LoadBalancer type creates an external load balancer on the cloud provider.
Support for external load balancers varies by provider as does the implementation. GCE and AWS are supported (not sure if there is other cloud provider support by now) but if you want to install it in your own infrastructure you will need to install a HAPROXY or ngnix container(or similar) to balance your traffic, this feature doesn't suit to you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With