Access Services Outside the Cluster
4 minute read
Some of the microservices in the United Manufacturing Hub are exposed outside the cluster with a LoadBalancer service. A LoadBalancer is a service that exposes a set of Pods on the same network as the cluster, but not necessarily to the entire internet. The LoadBalancer service provides a single IP address that can be used to access the Pods.
Before you begin
You need to have a UMH cluster. If you do not already have a cluster, you can create one by following the Getting Started guide.
You also need to access the system where the cluster is running, either by logging into it or by using a remote shell.
Accessing the services
To get a list of available services and related ports you can run the following command from the instance:
sudo $(which kubectl) get svc -n united-manufacturing-hub --kubeconfig /etc/rancher/k3s/k3s.yaml
All of them are available from within the cluster. The ones of type LoadBalancer are also available from outside the cluster using the node IP and the port listed in the Ports column.
Use the port on the left side of the colon (:
) to connect to the service from
outside the cluster. For example, the database is available on port 5432
.
Services with LoadBalancer by default
The following services are exposed outside the cluster with a LoadBalancer service by default:
- Database at port 5432
- Kafka Console at port 8090
- Grafana at port 8080
- MQTT Broker at port 1883
- OPCUA Simulator at port 46010
- Node-RED at port 1880
To access Node-RED, you need to use the /nodered
path, for example
http://192.168.1.100:1880/nodered
.
Services with NodePort by default
The Kafka Broker uses the service type NodePort by default.
Follow these steps to access the Kafka Broker outside the cluster:
Access your instance via SSH
Execute this command to check the host port of the Kafka Broker:
sudo $(which kubectl) get svc united-manufacturing-hub-kafka-external -n united-manufacturing-hub --kubeconfig /etc/rancher/k3s/k3s.yaml
In the
PORT(S)
column, you should be able to see the port with9094:<host-port>/TCP
.To access the Kafka Broker, use
<instance-ip-address>:<host-port>
.
Services with ClusterIP
Some of the microservices in the United Manufacturing Hub are exposed via a ClusterIP service. That means that they are only accessible from within the cluster itself. There are two options for enabling access them from outside the cluster:
- Creating a LoadBalancer service: A LoadBalancer is a service that exposes a set of Pods on the same network as the cluster, but not necessarily to the entire internet.
- Port forwarding: You can just forward the port of a service to your local machine.
Port forwarding can be unstable, especially if the connection to the cluster is slow. If you are experiencing issues, try to create a LoadBalancer service instead.
Create a LoadBalancer service
Follow these steps to enable the LoadBalancer service for the corresponding microservice:
Execute the following command to list the services and note the name of the one you want to access.
sudo $(which kubectl) get svc -n united-manufacturing-hub --kubeconfig /etc/rancher/k3s/k3s.yaml
Start editing the service configuration by running this command:
sudo $(which kubectl) edit svc <service-name> -n united-manufacturing-hub --kubeconfig /etc/rancher/k3s/k3s.yaml
Find the
status.loadBalancer
section and update it to the following:status: loadBalancer: ingress: - ip: <external-ip>
Replace
<external-ip>
with the external IP address of the node.Go to the
spec.type
section and change the value fromClusterIP
toLoadBalancer
.After saving, your changes will be applied automatically and the service will be updated. Now, you can access the service at the configured address.
Port forwarding
Execute the following command to list the services and note the name of the one you want to port-forward and the internal port that it use.
sudo $(which kubectl) get svc -n united-manufacturing-hub --kubeconfig /etc/rancher/k3s/k3s.yaml
Run the following command to forward the port:
sudo $(which kubectl) port-forward service/<your-service> <local-port>:<remote-port> -n united-manufacturing-hub --kubeconfig /etc/rancher/k3s/k3s.yaml
Where
<local-port>
is the port on the host that you want to use, and<remote-port>
is the service port that you noted before. Usually, it’s good practice to pick a high number (greater than 30000) for the host port, in order to avoid conflicts.You should be able to see logs like:
Forwarding from 127.0.0.1:31922 -> 9121 Forwarding from [::1]:31922 -> 9121 Handling connection for 31922
You can now access the service using the IP address of the node and the port you choose.
Security considerations
MQTT broker
There are some security considerations to keep in mind when exposing the MQTT broker.
By default, the MQTT broker is configured to allow anonymous connections. This means that anyone can connect to the broker without providing any credentials. This is not recommended for production environments.
To secure the MQTT broker, you can configure it to require authentication. For that, you can either enable RBAC or set up HiveMQ PKI (recommended for production environments).
Troubleshooting
LoadBalancer service stuck in Pending state
If the LoadBalancer service is stuck in the Pending state, it probably means
that the host port is already in use. To fix this, edit the service and change
the section spec.ports.port
to a different port number.
What’s next
- See how to Expose Grafana to the Internet