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/interfaces
auto eth0 iface eth0
inet static
address 192.168.1.2
netmask 255.255.255.0
gateway 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/interfaces
file.
- 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-local
spec: capacity:
storage: 1Gi
accessModes: - ReadWriteOnce
hostPath: 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: wordpress
spec:
replicas: 1
selector:
matchLabels:
app: wordpress
template: metadata:
labels:
app: wordpress
spec:
containers:
- name: wordpress
image: wordpress:latest
env:
- name: WORDPRESS_DB_HOST
value: mysql-service
- name: WORDPRESS_DB_PASSWORD
valueFrom: secretKeyRef:
name: mysql-secret
key: password
---
apiVersion: v1
kind: Service metadata:
name: wordpress-service
spec:
selector:
app: wordpress ports:
- protocol:
TCP port: 80
targetPort: 80
type: 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: