3 min read

K3s expose service

To install single node k3s cluster is pretty straightforward using k3s provided install script is enough. If you need simple cluster for local development or local testing see the article below.

Install k3s kubernetes cluster
What is k3s? K3s is a lightweight and certified Kubernetes distribution built by the Rancher. It’s currently in the sandbox projects category at the CNCF. K3s is a production-grade distribution of Kubernetes which is in nature lightweight and the foremost reason for building it was the need to use…

Install k3s one-liner

To install the K3s, on the new VM instance, you can simply run the script in the terminal:

curl -sfL https://get.k3s.io | sh -

After the installation and initial setup process (which can take a few minutes), you can access the k3s cluster using the kube config file located at the /etc/rancher/k3s/k3s.yaml.

Expose k3s service

apiVersion: apps/v1
kind: Deployment
  name: nginx-deployment
      app.kubernetes.io/name: nginx
  replicas: 1
        app.kubernetes.io/name: nginx
      - name: nginx
        image: nginx:1.14.2
        - containerPort: 80
apiVersion: v1
kind: Service
  name: my-service
    app.kubernetes.io/name: nginx
    - protocol: TCP
      port: 8080
      nodePort: 30272
      targetPort: 80
  type: LoadBalancer

Nginx and LoadBalancer service which will expose the Nginx to the internet

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       <none>        443/TCP        14m
my-service   LoadBalancer      8080:30272/TCP   25s

After waiting period my-service will converge the EXTERNAL-IP from pending -> 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>
<title>Welcome to nginx!</title>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
<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>

External machine ping

The Nginx responded from the Kubernetes cluster to our request. The next step should be to install an ingress controller to handle multiple routing on the application level.

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.

How to configure Traefik on k3s?
Traffic engineering with Traefik on k3s distribution of KubernetesTraefik is one of the most popular ingress controllers on Kubernetes. Traefik v2 brought some major changes in the usage of the controller itself. It brought the approach of heavy usage of Custom Resources on Kubernetes to provide rec…

This demo is a simple demonstration of how to expose the Kubernetes cluster workload to the outside of the cluster.