DEV Community

RAUL TAPARA YANGALI
RAUL TAPARA YANGALI

Posted on

πŸš€ StackOpsys: Part 3-Automating Kubernetes Infrastructure on Proxmox with Packer, Terraform andΒ Ansible

🧱 What’s included in this phase?

  • βœ… Automated Kubernetes cluster installation with kubeadm
  • βœ… Master and worker nodes configuration with containerd runtime
  • βœ… Network modules preparation for Istio
  • βœ… SSH key management and passwordless authentication
  • βœ… Cluster verification and kubeconfig setup
  • βœ… Task automation with go-task for streamlined operations

πŸ“¦ Technologies:

  • πŸ€– Ansible 2.9+
  • ☸️ Kubernetes (kubeadm)
  • 🐳 Containerd Runtime
  • ⚑ Task (go-task) for automation 🐧 Ubuntu Server 24.04 LTS
  • πŸ”§ Python 3.13

🌐 Setting Up StackOpsys Overview

πŸ“ Project Structure Overview

ansible/
β”œβ”€β”€ ansible.cfg              # Ansible configuration
β”œβ”€β”€ site.yaml               # Main playbook
β”œβ”€β”€ Taskfile.yml            # Automated tasks with Task
β”œβ”€β”€ inventory/
β”‚   β”œβ”€β”€ hosts.ini           # Host inventory
β”‚   └── group_vars/
β”‚       └── all.yaml        # Global variables
β”œβ”€β”€ roles/
β”‚   β”œβ”€β”€ prepare-nodes/      # Node preparation
β”‚   β”œβ”€β”€ configure-master-node/  # Master configuration
β”‚   β”œβ”€β”€ configure-worker-node/  # Worker configuration
β”‚   β”œβ”€β”€ kubeconfig/         # Kubeconfig setup
β”‚   └── reset-vm/           # Cluster reset (optional)
β”œβ”€β”€ collections/
β”‚   └── requirements.yaml   # Required Ansible collections
└── output/                 # Logs and output files
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Detailed Workflow:

βš™οΈ Configuration Examples

Inventory Configuration

[k8s_master]
cluster-kubeadm-k8s-master-01 ansible_host=192.168.100.220

[k8s_worker]
cluster-kubeadm-k8s-worker-01 ansible_host=192.168.100.223
cluster-kubeadm-k8s-worker-02 ansible_host=192.168.100.224
Enter fullscreen mode Exit fullscreen mode

Global Variables

os: "linux"
arch: "amd64"
ansible_user: <user>
ansible_ssh_private_key_file: ~/.ssh/ansible
ansible_become: true
Enter fullscreen mode Exit fullscreen mode

Cluster Architecture

  • Master Node: 1 node (192.168.100.220)
  • Worker Nodes: 2 nodes (192.168.100.223, 192.168.100.224)
  • Runtime: Containerd
  • Network: Pre-configured for Istio

πŸš€ Usage Steps

1. Prerequisites Setup

# Install Task (MacOS)
brew install go-task/tap/go-task

# Install Ansible collections
ansible-galaxy collection install -r collections/requirements.yaml

# Setup SSH keys
ssh-copy-id -i ~/.ssh/ansible.pub rtaparay@192.168.100.220
ssh-copy-id -i ~/.ssh/ansible.pub rtaparay@192.168.100.223
ssh-copy-id -i ~/.ssh/ansible.pub rtaparay@192.168.100.224
Enter fullscreen mode Exit fullscreen mode

2. Testing (Dry Run)

# Test complete configuration
task test:all

# Test master only
task test:site:master

# Test workers only
task test:site:worker
Enter fullscreen mode Exit fullscreen mode

3. Full Deployment

# Execute complete configuration
task apply:all

# Or step by step
task apply:site
task apply:kubeconfig
Enter fullscreen mode Exit fullscreen mode

4. Alternative: Direct Ansible

# Connectivity verification
ansible all -i inventory/hosts.ini -m ping

# Full deployment
ansible-playbook site.yaml -i inventory/hosts.ini
Enter fullscreen mode Exit fullscreen mode

πŸ”§ Ansible Roles Breakdown:

prepare-nodes - Disables swap permanently - Configures kernel
modules (overlay, br_netfilter) - Installs and configures containerd -
Prepares modules for Istio/Kubeflow - Installs kubeadm, kubelet, and
kubectl

configure-master-node - Initializes Kubernetes cluster with
kubeadm - Configures cluster networking - Generates join tokens for
worker nodes

configure-worker-node - Joins worker nodes to the cluster -
Configures kubelet on worker nodes

kubeconfig - Copies kubeconfig from master node - Sets up cluster
access configuration


πŸ“Š Outputs & Verification

Generated Files

output/
β”œβ”€β”€ ansible-2025-01-XX_XX-XX-XX.log  # Execution logs
└── kubeconfig                        # Kubernetes config
Enter fullscreen mode Exit fullscreen mode

Cluster Verification Commands

# Copy kubeconfig locally
scp rtaparay@192.168.100.220:~/.kube/config ~/.kube/config

# Verify cluster status
kubectl get nodes
kubectl get pods -A
kubectl cluster-info
Enter fullscreen mode Exit fullscreen mode

Expected Output

NAME                            STATUS   ROLES           AGE   VERSION
cluster-kubeadm-k8s-master-01   Ready    control-plane   5m    v1.28.x
cluster-kubeadm-k8s-worker-01   Ready    <none>          3m    v1.28.x
cluster-kubeadm-k8s-worker-02   Ready    <none>          3m    v1.28.x
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Task Automation Features

Available Tasks

  • task init - Initialize log directories
  • task test:all - Run all tests (dry-run)
  • task apply:all - Full cluster deployment
  • task apply:site - Main playbook execution
  • task apply:kubeconfig - Kubeconfig setup only

Automated Logging

  • Timestamped log files
  • Structured output directory
  • Verbose execution tracking

πŸ› οΈ Troubleshooting & Reset

Common Issues

# SSH connectivity test
ansible all -i inventory/hosts.ini -m ping

# Manual swap disable
sudo swapoff -a
sudo sed -i '/ swap / s/^/#/' /etc/fstab

# Verbose execution
ansible-playbook site.yaml -i inventory/hosts.ini -vvv
Enter fullscreen mode Exit fullscreen mode

Cluster Reset (Optional)

# Uncomment reset-vm role in site.yaml
ansible-playbook site.yaml -i inventory/hosts.ini --tags reset
Enter fullscreen mode Exit fullscreen mode

βœ… What’s next?

With your Kubernetes cluster now fully automated:

  • πŸ”Ή Deploy CNI plugins(Calico)
  • πŸ”Ή Configure service mesh (Istio)

🀝 Complete StackOpsys Journey

Part 1: πŸ—οΈ Custom VM templates with Packer

Part 2: ☁️ Infrastructure provisioning with Terraform

Part 3: πŸ€– Cluster automation with Ansible

πŸ“ Full project available at: πŸ‘‰ github.com/rtaparay/stackopsys


Top comments (0)