OpenTofu Lima Kubernetes

Fully automated Lima VM and Kubernetes cluster provisioning using OpenTofu Infrastructure as Code. This project creates a complete Kubernetes development environment with Ubuntu 24.04, Kubernetes 1.33.x, and pre-deployed sample applications.

Prerequisites

Quick Start

  1. Deploy everything with OpenTofu:

    tofu init
    tofu apply
  2. Access your ready cluster:

    lima kubectl get nodes
    lima kubectl get pods -A
  3. Access sample applications:

    # Add hostnames to /etc/hosts
    echo "127.0.0.1 nginx.local hello.local" | sudo tee -a /etc/hosts
    
    # Access applications
    curl http://nginx.local:8080
    curl http://hello.local:8080

That's it! OpenTofu automatically:

  • ✅ Creates Lima VM with Ubuntu 24.04
  • ✅ Installs Docker, kubectl, kind, helm
  • ✅ Provisions 4-node Kubernetes 1.33.1 cluster
  • ✅ Installs NGINX ingress controller
  • ✅ Deploys sample applications
  • ✅ Validates cluster readiness

Configuration

Variables

Customize your deployment by setting variables:

# Create terraform.tfvars
cat > terraform.tfvars << EOF
vm_name = "my-k8s-cluster"
cpus = 6
memory = "8GiB"
disk_size = "60GiB"
k8s_version = "v1.33.1"
arch = "aarch64"  # or "x86_64"
docker_enabled = true
EOF

Available Variables

Variable Description Default Type
vm_name Name of the Lima VM k8s-dev string
cpus Number of CPUs 4 number
memory Memory allocation 6GiB string
disk_size Disk size 50GiB string
k8s_version Kubernetes version v1.33.1 string
arch Architecture aarch64 string
docker_enabled Enable Docker true bool

Usage

VM Management

# Start VM
limactl start k8s-dev

# Stop VM  
limactl stop k8s-dev

# Access VM shell
limactl shell k8s-dev

# List VMs
limactl list

# Delete VM
limactl delete k8s-dev

Kubernetes Operations

# Use kubectl through Lima
lima kubectl get pods

# Apply example manifests
lima kubectl apply -f examples/manifests/

# Port forward
lima kubectl port-forward service/nginx-example-service 8080:80

# Access with ingress (add to /etc/hosts)
echo "127.0.0.1 nginx.local hello.local" | sudo tee -a /etc/hosts
curl http://nginx.local
curl http://hello.local

Docker Operations

# Build images in Lima VM
lima docker build -t myapp .

# Run containers
lima docker run -d --name myapp -p 8080:80 myapp

# Docker Compose
lima docker-compose up -d

Examples

Deploy Additional Applications

# Sample applications are automatically deployed during provisioning
# Check deployment status
lima kubectl get deployments,services,ingress -n development

# View application logs
lima kubectl logs -f deployment/nginx-example -n development
lima kubectl logs -f deployment/hello-world -n development

Access Applications

Applications are automatically deployed and accessible via ingress:

# Add hostnames (already included in OpenTofu output)
echo "127.0.0.1 nginx.local hello.local" | sudo tee -a /etc/hosts

# Access applications
curl http://nginx.local:8080    # Nginx example
curl http://hello.local:8080    # Hello World app

# Or open in browser
open http://nginx.local:8080
open http://hello.local:8080

Architecture

This fully automated setup provides:

  • Lima VM: Ubuntu 24.04 ARM64 VM with 4 CPUs, 6GB RAM, 50GB disk
  • Kind Cluster: 4-node Kubernetes 1.33.1 cluster (1 control-plane + 3 workers)
  • Ingress Controller: NGINX ingress with port forwarding (80→8080, 443→8443)
  • Metrics Server: Resource monitoring with kind compatibility
  • Development Namespace: Pre-configured with sample applications
  • Complete Toolchain: Docker, kubectl, kind v0.24.0, helm pre-installed
  • OpenTofu Integration: Full Infrastructure as Code automation

Troubleshooting

VM Issues

# Check VM status
limactl list

# View VM logs
limactl shell k8s-dev -- sudo journalctl -f

# Restart VM
limactl stop k8s-dev && limactl start k8s-dev

Kubernetes Issues

# Check cluster health
lima kubectl get nodes
lima kubectl get pods --all-namespaces

# View cluster events  
lima kubectl get events --sort-by='.lastTimestamp'

# Check ingress controller
lima kubectl get pods -n ingress-nginx

Complete Reset

# Destroy everything
tofu destroy

# Remove Lima VM manually if needed
limactl delete k8s-dev

Cleanup

To remove all resources:

tofu destroy

This will stop and delete the Lima VM and clean up all generated files.

Contributing

  1. Fork the repository
  2. Create your feature branch
  3. Test your changes
  4. Submit a pull request

License

MIT License - see LICENSE file for details.