Skip to content

tobing/single-node-kubernetes

Repository files navigation

Single Node Kubernetes for Nginx & PHP-FPM

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

Installation

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

  2. Install K3s using curl -sfL https://get.k3s.io | sh -

  3. Check K3s installation kubectl get node. Take note the node name and paste it to the local_persistentvolume.yaml section nodeAffinity.values

    image

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

      ExecStart=/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)

     

  5. Go to inside single-node-kubernetes directory and run kubectl apply -f .

  6. Verify all running properly kubectl get pods,deploy,svc,pv,pvc,hpa

    image

  7. If something wrong check with kubectl describe <NAME>, example

    • For pod : kubectl describe pod/php-776fc877d8-2q2fq
    • For service : kubectl describe service/nginx
  8. Take note the EXTERNAL-IP for service/nginx. The TYPE is LoadBalancer

  9. Open a web browser and go to http://EXTERNAL-IP. The page should show "403 Forbidden"

  10. 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();
    ?>
    
  11. 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

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

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

kubectl command options

  • 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 from kubectl get pods,deploy,svc,pv,pvc,hpa)

Firewall Exceptions for K3s

image

About

Single Node Kubernetes for Nginx & PHP-FPM with K3s

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published