All HowTo's Kubernetes & Docker Linux Ubuntu, Mint & Debian Linux

Kubernetes (MicroK8s) Part 3 – Exposing Applications on Ubuntu 20.04

This article continues from Part 2 – Replica Sets and Scaling.

Our objective in this article is to get an application exposed to the wider network on an IP address of the host and a port of our choice. The IP address will be “10.0.0.210” and our port of choice will be “30036”.

We’re going to expose a dotNet application which, according to its documentation, listens on port “8080”.

Put the following content into a new file called “dotnet-rs.yaml”:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: dotnet-hello-rs # This is what our Service will point to. 
spec:
  replicas: 5 # This is the number of instances we want for this app.
  selector:
    matchLabels:
      app: dotnet-hello-rs
  template:
    metadata:
      labels:
        app: dotnet-hello-rs
    spec:
      containers:
      - name: dotnet-hello-rs
        image: appsvctest/dotnetcore # This is our image to execute. 
        ports:
        - containerPort: 8080 # Doco for this image says it's listening on this port. 

Put the following content into a new file called “dotnet-service.yaml”:

apiVersion: v1
kind: Service
metadata: 
  name: dotnet-hello-service
spec:
  selector: 
    app: dotnet-hello-service
  type: NodePort
  ports: 
    - name: http
      protocol: TCP
      nodePort: 30036 # The port our networked devices will look on for the application. 
      port: 9090 
      targetPort: 8080 # The port of the target ReplicaSet. 
  selector:
    app: dotnet-hello-rs # This is the name or the target ReplicaSet. 

Now start the ReplicaSet and Service objects.

sudo microk8s kubectl apply -f dotnet-rs.yaml
sudo microk8s kubectl apply -f dotnet-service.yaml

Validate our ReplicaSet:

sudo microk8s kubectl get rs -o wide
NAME              DESIRED   CURRENT   READY   AGE   CONTAINERS        IMAGES                  SELECTOR
dotnet-hello-rs   5         5         5       15m   dotnet-hello-rs   appsvctest/dotnetcore   app=dotnet-hello-rs

Validate our Service:

sudo microk8s kubectl get service -o wide
NAME                   TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE   SELECTOR
kubernetes             ClusterIP   10.152.183.1           443/TCP          32h   
dotnet-hello-service   NodePort    10.152.183.9           9090:30036/TCP   11m   app=dotnet-hello-rs

Ensure your firewall allows port “30036” through.

sudo ufw allow 30036

From the Kubernetes host, test that you can get to the Pod:

curl -v 10.152.183.9:8080

Now test from a networked machine (some other machine on your LAN):

curl -v 10.0.0.210:30036

You should get the output of the dotNet application.

Leave a Reply

Your email address will not be published. Required fields are marked *