Single Node Kubernetes for Nginx & PHP-FPM with K3s Tested in Ubuntu 22.04 and Rocky Linux 8.5 RAM 2GB+
K3s is a highly available, certified Kubernetes distribution designed for production workloads in unattended, resource-constrained, remote locations or inside IoT appliances
-
Clone this repository:
git clone https://github.com/tobing/single-node-kubernetes.git
local_persistentvolume.yaml : To define volume in the local host (/tmp) link
*_deployment.yaml : To define kubernetes Deployment object link
*_service.yaml : To define kubernetes Service link
*_hpa.yaml : To define kubernetes Horizontal Pod Autoscale link
nginx_configMap.yaml : To define config related to app (nginx.conf) link
-
Install K3s using
curl -sfL https://get.k3s.io | sh -
-
Check K3s installation
kubectl get node
. Take note the node name and paste it to the local_persistentvolume.yaml sectionnodeAffinity.values
-
Traefik service installed and running automatically after K3s installation completed. Because Traefik & Nginx service using port 80, we need to DISABLE it first
-
Check Traefik using port 80
kubectl get svc -n=kube-system
-
Stop K3s service
service k3s stop
-
Modify
/etc/systemd/system/k3s.service
by adding--disable=traefik \
in the end of ExecStartExecStart=/usr/local/bin/k3s \ server \ --disable=traefik \
-
Reload for service config change
systemctl daemon-reload
-
Start K3s service
service k3s start
-
Check Traefik service not running
kubectl get svc -n=kube-system
Or DELETE the service
kubectl --namespace kube-system delete svc traefik
(not recommended) -
-
Go to inside
single-node-kubernetes
directory and runkubectl apply -f .
-
Verify all running properly
kubectl get pods,deploy,svc,pv,pvc,hpa
-
If something wrong check with
kubectl describe <NAME>
, example- For pod :
kubectl describe pod/php-776fc877d8-2q2fq
- For service :
kubectl describe service/nginx
- For pod :
-
Take note the EXTERNAL-IP for
service/nginx
. The TYPE is LoadBalancer -
Open a web browser and go to http://EXTERNAL-IP. The page should show "403 Forbidden"
-
Because the persistent volume location in /tmp we need to create a test file
vi /tmp/test.php
and type code below then save<?php echo gethostname(); ?>
-
Back to the web browser and open http://EXTERNAL-IP/test.php. Reload the page several times and the hostname will changed. This mean requests served by different pod
-
To test Horizontal Pod AutoScale (hpa) need to open 2 shell session
-
First shell to monitor the pods run command
watch -n1 kubectl get pods
-
Second shell from same or different server to run ab apache benchmark (change -n value accordingly)
ab -n 100000 -c 1000 http://EXTERNAL-IP/test.php/
After the ab apache benchmark running for some time, you will see the pods replica increasing in the first shell
-
-
You can modify
-
*hpa.yaml in the sections
minReplicas
,maxReplicas
,averageUtilization
-
*deployment.yaml in the sections
resources
After modified run
kubectl apply -f .
then test again and verify -
-
Show pods, deployments, services, persistent volumes, persistent volume claims, horizontal pod autoscale
kubectl get pods,deploy,svc,pv,pvc,hpa
-
Show pods, deployments, services, persistent volumes, persistent volume claims, horizontal pod autoscale in all namespaces
kubectl get pods,deploy,svc,pv,pvc,hpa --all-namespaces -o wide
-
Delete pods, deployments, services, persistent volumes, persistent volume claims, horizontal pod autoscale all at once (Don't delete service/kubernetes)
kubectl delete pod/php-776fc877d8-gp2fm service/php
(use NAME fromkubectl get pods,deploy,svc,pv,pvc,hpa
)