Recently I needed to test the full HTTP stack between a Kubernetes cluster's member nodes and an external Internet routing setup, and so I wanted to quickly install K3s (which includes Traefik by default, and load balances through ports 80 and 443 on all nodes), then get a quick 'hello world' web page up, so I could see if the traffic was routing properly all the way from the external host through to a running container exposed via Traefik Ingress.
Here's how I set up a basic 'Hello World' web page on my K3s cluster:
First, I created an HTML file to be stored as a ConfigMap. Create a file named index.html
with the following contents:
<html>
<head>
<title>Hello World!</title>
</head>
<body>Hello World!</body>
</html>
Create a ConfigMap with the HTML from the file you just created:
$ kubectl create configmap hello-world --from-file index.html
Save the following to Kubernetes resource definitions into a file named hello-world.yml
:
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: hello-world
annotations:
kubernetes.io/ingress.class: "traefik"
spec:
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: hello-world
port:
number: 80
---
apiVersion: v1
kind: Service
metadata:
name: hello-world
spec:
ports:
- port: 80
protocol: TCP
selector:
app: hello-world
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world-nginx
spec:
selector:
matchLabels:
app: hello-world
replicas: 3
template:
metadata:
labels:
app: hello-world
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
volumeMounts:
- name: hello-world-volume
mountPath: /usr/share/nginx/html
volumes:
- name: hello-world-volume
configMap:
name: hello-world
Then deploy the Nginx container deployment, Service, and Traefik Ingress resources with:
$ kubectl apply -f hello-world.yml
After a few seconds, you should be able to access port 80 on any member nodes (assuming networking is working), and get back:
$ curl localhost:80
<html>
<head>
<title>Hello World!</title>
</head>
<body>Hello World!</body>
</html>
And in my case, I could test out the external routing and make sure that same response was making it through. Yay!
Comments
Lets see the same thing with metallb rather than traefik for the load balancer !
Running the latest k3s on Debian 11, I don't get hello-world from localhost:80, I have to use
curl localhost:80/hello-world.html
I'm not sure what the issue is, I'll report back once I understand.
Please disregard my comment, I didn't name the file index.html
doesn't work.
Finally something I can test my k3s install with. I followed your k3s-ansible steps from the turing pi video series but my setup was not powerful for the cluster-monitoring. Tried to find some things to do next but man this space is complex. Very glad to have found this and it is working as advertised. Now I can edit the deployment yaml and test some stuff out. Thank you!
Nice. I installed k3s on VBox VM Centos, and then what... this is the hello-world I was looking to see it do something.
I can see the output from my Windows using he VM's static IP, also from within the VM using localhost
Please explain
1. How does the Windows access work?
2. Please explain the Kubernetes fil, hello-world.yml
3. where did the configmap get created ? I can't find, but it is there, i.e. via
kubectl get configmaps -v6 hello_world -o yaml
4 How do I delete it everything related to the above example? e.g. so I can start over and play from scratch.
2.
Thanks! The first beginners example that worked out of the box. Thanks for getting me started!
Hello Jeff Geerling,
Many thanks for this cluster validation steps..
In the last step you do a curl localhost:80 to acces the page, which does not work.
I need to explicitly port forward the service and then it works , can you please tell me what I am missing.