Kubernetes Cluster: Exposing Services
While components of your application can communicate with each other by service names using the internal network, external connections require additional configurations.
Kubernetes supports three service types to establish an internal and external connections to application:
The ClusterIP service is the default K8s service. It makes application accessible by other applications within K8s cluster. No external access provided.
Simple ClusterIP service example:
The most basic way to establish an external connection to a service is to expose it via NodePort directly. As the name implies, this type of service opens a specific port on the nodes, any traffic sent to this port is forwarded to your service. By default, the nodePort for your service is selected randomly from the 30000-32767 range.
Note: This method has several downsides that should be considered when configuring the Kubernetes Cluster (one service per port, restricted range of ports, etc.). As a result, the NodePort service type can be used for the demo or other temporary applications. However, the production solutions usually require more complex configuration with ingresses and LoadBalancer service options. Follow our guide(s) to create verified configurations for your application and put in production:
Here is an example of the NodePort type service configuration:
If needed, a particular nodePort can be selected for your service. For example, the following code can be used to configure a redirect from the 30984 port:
In case public IP is attached to the Kubernetes worker nodes, no additional actions are required.
Otherwise, the obtained port should be exposed from the platform side. Navigate to the Kubernetes environment Settings > Endpoints and click Add. In the opened frame, provide the following data:
- Node - choose any worker node from the list
- Name - set any preferred endpoint name
- Private Port - provide the nodePort from the previous step
- Protocol - select the TCP option
Click Add to confirm. It may take up to a few minutes for the platform to expose a port and redirect requests to the NodePort service.
The service LoadBalancer type is the commonly used way to provide a service on the Internet. It requires the public IP attached to any worker node.
Keep in mind that with LoadBalancer type all traffic is directly forwarded to the service with no filtering, routing, etc. The port parameter is an incoming port on the Internet which the service maps to a targetPort on the application side.