
Welcome to the exciting world of Kubernetes! π In this guide, weβll show you how to build a scalable infrastructure using K3s on Raspberry Pi, perfect for hosting a WordPress site. Weβll cover everything from setup to scaling and monitoring, all using lightweight Raspberry Pis.
π― Objectives Our mission is simple: create a powerful, scalable infrastructure that effortlessly handles WordPress deployment. Ready to roll up your sleeves? Letβs dive in!
π οΈ What Youβll Need
Hardware:
- 5 Raspberry Pis (or just 1 if youβre starting small)
- Router & Switch
- SD Cards & Ethernet Cables
- Power Supply for each Pi
- External Drive for backups
Software:
- Win32 Disk Imager for disk setup
- K3s for ARM architecture
- Raspberry Pi OS Lite 64-bit
Knowledge:
- Basics of Linux, Kubernetes, and Networking π§
π¦ Step 1: Setting Up Raspberry Pis
- Prepare Your Hardware:
- Power up your Raspberry Pis and connect them to the network.
- Install the OS:
- Use Raspberry Pi Imager to flash Raspberry Pi OS Lite (64-bit) onto SD cards.
- Insert the SD card, power on the Pi, and complete the initial setup.
- Network Configuration:
- Edit network settings on each Pi to assign static IPs. This ensures stable communication between nodes.
- Example:
sudo nano /etc/network/interfacesauto eth0 iface eth0inet staticaddress 192.168.1.2netmask 255.255.255.0gateway 192.168.1.1 - Save and restart the network service.
- Enable SSH Access:
- SSH allows remote access to each Pi. Activate it with:
sudo raspi-config
- SSH allows remote access to each Pi. Activate it with:
ποΈ Step 2: Master Node Configuration
- Install K3s on Master Node:
- Log into the master Pi and install K3s with:
curl -sfL https://get.k3s.io | sh -
- Log into the master Pi and install K3s with:
- Configure Master Node Components:
- Export the kubeconfig file:
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml - Install Helm (Kubernetes package manager):
sudo snap install helm --classic helm init
- Export the kubeconfig file:
π Step 3: Setting Up Worker Nodes
- Install K3s on Worker Nodes:
- SSH into each worker Pi and install K3s, joining them to the cluster:
curl -sfL https://get.k3s.io | K3S_URL=https://<master-ip>:6443 K3S_TOKEN=<node-token> sh -
- SSH into each worker Pi and install K3s, joining them to the cluster:
- Verify Node Status:
- On the master node, check if all nodes are ready:
sudo k3s kubectl get nodes
- On the master node, check if all nodes are ready:
π Step 4: Network Configuration
- Set Static IPs:
- Consistent IPs are key for smooth operation. Set these in the
/etc/network/interfacesfile.
- Consistent IPs are key for smooth operation. Set these in the
- Troubleshoot Connectivity:
- If issues arise, use ping tests between nodes and ensure SSH works.
- Command:
ping <other-node-ip>
ποΈ Step 5: Persistent Volume Setup
- Create Persistent Volumes (PVs):
- Define storage needs in a YAML file:
apiVersion: v1 kind:PersistentVolume metadata:name: pv-localspec: capacity:storage: 1GiaccessModes: - ReadWriteOncehostPath: path: "/mnt/data" - Apply with:
sudo k3s kubectl apply -f pv.yaml
- Define storage needs in a YAML file:
- Test PVs:
- Deploy a test pod to ensure data persistence.
π Step 6: Deploy WordPress on Kubernetes
- Create Deployment YAML:
- Example WordPress YAML:
apiVersion: apps/v1 kind:Deployment metadata:name: wordpressspec:replicas: 1selector:matchLabels:app: wordpresstemplate: metadata:labels:app: wordpressspec:containers:- name: wordpressimage: wordpress:latestenv:- name: WORDPRESS_DB_HOSTvalue: mysql-service- name: WORDPRESS_DB_PASSWORDvalueFrom: secretKeyRef:name: mysql-secretkey: password---apiVersion: v1kind: Service metadata:name: wordpress-servicespec:selector:app: wordpress ports:- protocol:TCP port: 80targetPort: 80type: LoadBalancer- Access WordPress:
- Open your browser and navigate to the external IP assigned to the WordPress service.
π Step 7: Scaling WordPress Deployment
- Enable Autoscaling:
- Configure autoscaling with a Horizontal Pod Autoscaler (HPA):
apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metadata: name: wordpress-hpa spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: wordpress minReplicas: 1 maxReplicas: 5 metrics: - type: Resource resource: name: cpu targetAverageUtilization: 80
- Configure autoscaling with a Horizontal Pod Autoscaler (HPA):
- Test Scalability:
- Use load testing tools to simulate traffic and monitor scaling:
ab -n 10000 -c 10 http://<wordpress-service-ip>/
- Use load testing tools to simulate traffic and monitor scaling:
π Step 8: Monitoring with Prometheus and Grafana
- Deploy Prometheus:
- Collect metrics to monitor your clusterβs performance.
- Set Up Grafana:
- Visualize metrics using Grafana dashboards. Import Kubernetes dashboards for detailed insights.
π Conclusion
Youβve successfully deployed a scalable WordPress site using Kubernetes on Raspberry Pi! This guide is just the beginning. Keep exploring and tweaking your setup to match your needs. π
Happy clustering! π
π Useful Resources: