K3s expose service

There are various ways to expose traffic to the outside on the Kubernetes. In this tutorial we will show you a way to expose the LoadBalancer service and allow traffic to the pods running in the cluster.

Expose k3s service

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app.kubernetes.io/name: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  selector:
    app.kubernetes.io/name: nginx
  ports:
    - protocol: TCP
      port: 8080
      nodePort: 30272
      targetPort: 80
  type: LoadBalancer
ℹ️
The nodePort is defined to use port 30272 which is in fact the port K3s configured in our NAT to forward to nginx service.

Applying the manifest and inspect the final state.

$ kubectl apply -f manifest.yaml
$ kubect get pods
NAME                               READY   STATUS    RESTARTS   AGE
nginx-deployment-579b58dcd-pt2c9   1/1     Running   0          6s
$ kubectl get svc
NAME         TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP      10.43.0.1       <none>        443/TCP        14m
my-service   LoadBalancer   10.43.249.240   10.8.0.2      8080:30272/TCP   25s

After waiting period service nginx will converge the EXTERNAL-IP from pending -> 10.8.0.2 in this case. The LoadBalancer EXTERNAL-IP can vary from case to case.

Testing the outside connectivity

$ PUBLIC_IP="localhost" 
$ curl http://localhost:8080/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

External machine ping

The Nginx from the Kubernetes cluster responded to the request. The next step should be to install an ingress controller to handle multiple routings at the application level. Since LoadBalancer uses EXTERNAL_IP to allow traffic inside the cluster. IP addresses are costly resources. Exposing the ingress controller via LoadBalancer service is the most often approach to limit the costs.

You could use Traefik or the Nginx. Bind the LoadBalancer service to the ingress controller to the ingress controller and you can host multiple applications on your Kubernetes cluster which are accessible from the Internet.

To learn more about this approach read the article below.

K3s Traefik expose service
By installing K3s, Traefik comes preinstalled in the cluster. You can test that as soon as you install the K3s cluster and hit the curl localhost you will get a response 404 not found. That means that Traefik is already installed. If that is not the case you can follow