edoc Knowledge Base
Breadcrumbs

How can I assign node ports to Kubernetes services?

Component

Kubernetes, MicroK8s

Version

1.19

Created on

Last modified on

No Workflow Applied

Review status

No Workflow Applied

KB article number

1213530227

Summary

In this article, you will learn how to publish Kubernetes services (e.g. MySQL database from edoc automate or edoc workplace) via NodePort. Publishing via NodePort gives the database or service a port that can be reached from outside the Kubernetes environment. In this way, you can, for example, populate master data tables in edoc automate or access the databases in support cases.

Important requirements

  • edoc platform (On-Premises)

  • SSH access to the server with sudo permission

Solution

To configure NodePort for a service, you need the following information:

  • $NAME: Name of the new service.

  • $NAMESPACE: Name of the Kubernetes namespace(master, test, staging, default).

  • $SELECTOR: Selector: (run=).

  • $NODEPORT: external destination port.

  • $PORT: internal destination port (port in the Kubernetes container).

The following command lists all services in all namespaces including ports and selector: (run=).

kubectl get services -o wide -A

Sample edition (abridged):

NAMESPACE     NAME                              TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                   AGE    SELECTOR
master        workflow-session-socket-service   ClusterIP   10.152.183.248   <none>        80/TCP                    708d   run=workflow-session-socket-container
master        workflow-session-socket           ClusterIP   10.152.183.26    <none>        80/TCP                    708d   run=workflow-session-socket-container
master        workflow-service                  ClusterIP   10.152.183.227   <none>        80/TCP                    708d   run=workflow-container
master        workflow-redis                    ClusterIP   10.152.183.18    <none>        6379/TCP                  708d   run=workflow-redis-container
master        workflow-mysql                    ClusterIP   10.152.183.163   <none>        3306/TCP                  708d   run=workflow-mysql-container
master        workflow  

All the required information is available to you after executing the command. Next, use a template to create a new service that makes the port available externally.

Replace the variables with the values you received from the previous command:

apiVersion: v1
kind: Service
metadata:
  name: $NAME
  namespace: $KUBERNETES_NAMESPACE
spec:
  type: NodePort
  selector:
    run: $SELECTOR
  ports:
    - port: $PORT
      nodePort: $NODEPORT

Save the file on the server, e.g. in the home directory of the current user:

/home/$USER/$SERVICENAME.yaml

Use the following command to activate the service with Kubernetes:

kubectl apply -f /home/$USER/$SERVICENAME.yaml

The service should now be available externally under the defined port. Use the following command to check whether the service is available:

kubectl get services -o wide -A

The new service is displayed in the list.

Example: Publishing the MySQL database of edoc workplace

To publish the MySQL database of edoc workplace in the master environment under port 31111, you can use the following file:

YAML
apiVersion: v1
kind: Service
metadata:
  name: workflow-mysql-extern
  namespace: master
spec:
  type: NodePort
  selector:
    run: workflow-mysql-container
  ports:
    - port: 3306
      nodePort: 31111
kubectl apply -f /home/$USER/workflow-mysql-extern.yaml

After applying the Service definition, the NodePort type is available. You can check the availability using the following output of the services:

kubectl get services -o wide -A
NAMESPACE     NAME                              TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                   AGE    SELECTOR
master        workflow-mysql-extern             NodePort    10.152.183.215   <none>        3306:31111/TCP            18h    run=workflow-mysql-container

Port 31111 is now available from the outside and makes port 3306 available to the workflow-mysql-container.