Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expose existing and deployed Kubernetes service via LoadBalancer

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?

like image 849
chetan Avatar asked Aug 16 '16 00:08

chetan


2 Answers

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

like image 154
Anirudh Ramanathan Avatar answered Oct 06 '22 19:10

Anirudh Ramanathan


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.

like image 34
Ferrandinand Avatar answered Oct 06 '22 18:10

Ferrandinand