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
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
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.