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.
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.
1. Here is an example of the NodePort type service configuration:
kind: Service apiVersion: v1 metadata: name: nginx1 namespace: test labels: run: nginx spec: type: NodePort selector: run: nginx ports: - port: 80 targetPort: 80
2. 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:
ports: - port: 80 targetPort: 80 nodePort: 30984
Otherwise, the obtained port should be exposed from the Jelastic 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 Jelastic to expose a port and redirect requests to the NodePort service.