0% found this document useful (0 votes)
18 views59 pages

Advanced Kubernetes Cluster Setup Guide

Uploaded by

wolfcosmic5879
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views59 pages

Advanced Kubernetes Cluster Setup Guide

Uploaded by

wolfcosmic5879
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DevOps Project Setup Guide

i want to do advance project in devops tools like Ansible ,


Puppet , Docker , Kubernetes , Helm , Podman , Aqua
Security , Prometheus , Grafana

Self-Healing & Auto-Scaling Kubernetes Cluster with Advanced


Security & Observability
Goal: Build a highly resilient and secure Kubernetes cluster that
can automatically scale based on demand and self-heal from
failures, with comprehensive monitoring and logging.
Key Tools & Advanced Concepts:

Provisioning the Kubernetes cluster and all its supporting


infrastructure (node groups, load balancers, managed
databases).
Implementing Terraform import to manage existing resources
or refactor infrastructure.
Using null_resource and remote-exec for complex initial cluster
setup if cloud provider doesn't offer it directly.
Ansible:
Used for initial setup of bastion hosts or specific hardening of
cluster nodes before they join the cluster.
Dynamic Inventory sourcing node information.
Kubernetes:
Configuring Cluster Autoscaler and Horizontal Pod Autoscaler
(HPA) for dynamic scaling.
Implementing Vertical Pod Autoscaler (VPA) for resource
optimization.
Designing and deploying Custom Resource Definitions (CRDs)
for specific operational tasks (e.g., a backup operator).
Advanced Network Policies for isolation.
Implementing Pod Security Standards (PSS) or custom
Admission Controllers for security policy enforcement.
Storage Classes with dynamic provisioning and snapshots.
Helm:
Deploying all critical cluster services (Prometheus, Grafana,
Service Mesh, logging agents, security agents) using
production-grade Helm charts.
Developing a custom Helm chart for a demo application that
showcases auto-scaling.

1/59
Podman:
Run utility containers on the host machines (e.g., a log shipper
or security agent outside of Kubernetes but managed by
Ansible) using rootless Podman.
Experiment with podman generate kube to convert Podman
definitions to Kubernetes manifests.
Aqua Security:
Deep integration of Aqua CSP for runtime threat detection and
prevention within the cluster.
Scanning all deployed images for vulnerabilities and
compliance deviations.
Setting up forensics and incident response with Aqua.
Prometheus & Grafana:
Comprehensive cluster monitoring (node health, pod resources,
API server metrics).
Thanos/Prometheus Federation for long-term storage and
global view.
Advanced Alertmanager configurations with multiple receivers
and routing.
Rich Grafana dashboards for cluster health, application
performance, and security events.
Integrate with Loki (for logs) and Tempo (for traces) for full
observability.

help me built this project

That’s an excellent advanced DevOps project idea — ambitious, very relevant in the real world, and
a great showcase of skills across the entire DevOps stack. Let's break down the phases and
structure of this project to make it actionable.

🔧 Project Title:
"Self-Healing & Auto-Scaling Kubernetes Cluster with Advanced Security & Observability"

🧭 High-Level Architecture Overview


2/59
Components:

Terraform: Provision infrastructure (Cloud infra + Kubernetes).

Ansible: Bootstrap machines, install baseline tools, secure the nodes.

Kubernetes: Cluster setup, autoscalers, CRDs, policies.

Helm: Deploy services like monitoring, logging, mesh.

Podman: Host-level utility containers.

Aqua Security: Security scanning, runtime protection.

Prometheus + Grafana + Loki + Tempo: Observability stack.

📌 Phase 1: Infrastructure Provisioning (Terraform +


Ansible)

✅ Goals:
Provision Kubernetes cluster and infra on a cloud provider (e.g., AWS, GCP, or Azure).

Optional: use Terraform import if you already have resources.

Set up VPC, subnets, bastion hosts, node groups, load balancer.

🔧 Tools:
Terraform

Modules: vpc , eks/gke , rds , alb .

null_resource with remote-exec for custom setups.

Ansible:

Use dynamic inventory from cloud provider.

Secure nodes: disable password logins, configure auditd, sysctl hardening.

📌 Phase 2: Kubernetes Setup


✅ Goals:
3/59
Configure cluster autoscaler (CA), HPA, VPA.

Enable dynamic volume provisioning.

Setup PSS, network policies, admission controllers.

Use CRDs for custom ops (like backup or health monitors).

🔧 Features to Implement:
Horizontal & Vertical Pod Autoscalers.

Self-healing deployments with readiness & liveness probes.

Network Policies (Calico or Cilium).

PodSecurity policies or OPA Gatekeeper.

Snapshot-enabled storage classes.

📌 Phase 3: Service Deployment via Helm


✅ Goals:
Deploy all essential services via Helm charts.

Build a custom Helm chart for a demo app (autoscaling app).

🧱 Deploy via Helm:


Prometheus Operator

Grafana

Fluent Bit / Loki (logs)

Tempo (traces)

Nginx ingress controller

Aqua Security agents

📌 Phase 4: Podman Integration

4/59
✅ Goals:
Use Podman to run out-of-cluster tools like log shippers, vulnerability scanners.

Use podman generate kube to convert to K8s manifests (bonus).

🔧 Use Cases:
Rootless security agent

Local logging service

Run rsyslog / auditd side container

📌 Phase 5: Aqua Security Setup


✅ Goals:
Deploy Aqua CSP (Community Edition or Enterprise).

Connect Aqua scanners to CI/CD pipeline.

Enable runtime protection, network firewalling, and image scanning.

🛡️ Integrations:
Kubernetes Admission Webhook

Aqua Enforcer on each node

CI/CD Integration (e.g., GitHub Actions + Aqua Scanner)

📌 Phase 6: Monitoring, Logging, Alerting (Prometheus +


Grafana + Loki + Tempo)

✅ Goals:
Deploy observability stack via Helm:

Prometheus + Thanos

Grafana dashboards

5/59
Loki (logs), Tempo (traces)

Enable full visibility into:

Cluster state (node, pod, API server metrics)

Application performance (response time, error rate)

Security Events (via Aqua)

📊 Grafana Dashboards:
Node health

Pod resource usage

HPA/VPA behavior

Network policies and denied connections

Image scanning results from Aqua

📌 Phase 7: Advanced Features & Automation


Self-healing logic: Liveness/readiness, probes, priorityClasses, PodDisruptionBudgets.

Automated failover (DB/Storage).

Setup Alertmanager with multiple routes (Slack, email, PagerDuty).

Run end-to-end chaos experiments (using Litmus or Chaos Mesh).

Include GitOps via ArgoCD or Flux for CD.

📘 Deliverables (For Resume, GitHub, Demos)


📁 Full source code (infra + Helm charts + manifests)
📊 Screenshots of:
Grafana dashboards

Aqua Security alerts

Autoscaler in action

6/59
📹 Short walkthrough video (~3 mins)
📖 [Link] with:
Architecture Diagram

Tooling stack

Setup instructions

Real-world use cases

🚀 Suggested Stack (AWS Example)


Cloud: AWS (EKS)

Provisioning: Terraform

Config Management: Ansible

Container Runtime: containerd + Podman

CI/CD: GitHub Actions + ArgoCD

Security: Aqua CSP + PSS + OPA

Monitoring: Prometheus + Grafana + Loki + Tempo

Helm: For deploying all services

📅 Suggested Timeline (6 Weeks)


Week Tasks

1 Terraform infra setup (EKS, VPC, IAM, Bastion, RDS)

2 Ansible node hardening + Kubernetes setup

3 Helm: Monitoring stack, Ingress, HPA/VPA

4 Aqua Security integration

5 Logging & Tracing setup (Loki, Tempo)

7/59
Week Tasks

6 Demo app + Helm + CI/CD + Final polish (README, videos)

✅ Next Steps
1. Tell me your cloud provider (AWS, GCP, etc.)

2. Do you want hands-on lab scripts or full code templates?

3. Would you like me to generate the architecture diagram?

Let’s start building step-by-step — I can help you generate Terraform files, Ansible roles, Helm
charts, dashboards, Aqua configuration, etc.

no cost project don't use aws or cost demand tool

Perfect — we’ll adapt this to a cost-free, local-first solution using open-source tools only, and
without cloud provider lock-in like AWS/GCP/Azure.

Here’s how we’ll revise your "Self-Healing & Auto-Scaling Kubernetes Cluster with Advanced
Security & Observability" project to make it zero-cost while keeping it production-grade and fully
aligned with DevOps best practices.

🛠️ Revised Tech Stack (No-Cost Version)


Component Tool (Free & Open Source)

Cluster Provisioning kubeadm on local VMs / bare-metal

VMs / Infra Vagrant + VirtualBox / libvirt

Config Management Ansible

Container Runtime containerd or Podman

Orchestration Kubernetes via kubeadm

8/59
Component Tool (Free & Open Source)

Autoscaling HPA, VPA (via Kubernetes APIs)

Monitoring Prometheus , Grafana , Node Exporter , kube-state-metrics

Logging Loki , Fluent Bit , Grafana

Tracing Tempo

Security Trivy , Aqua TeamScanner (open-source version) , Kubernetes PSP/PSS

CI/CD GitHub Actions (free) or Drone CI (self-hosted)

Helm Used for deploying services

Backup (Optional) Velero (open-source Kubernetes backup tool)

🔧 Architecture (Zero-Cost Setup)


Local Setup using:

Vagrant to spin up 3-5 VMs on VirtualBox

Kubeadm to install Kubernetes cluster

Ansible for provisioning and hardening

Self-Hosted Kubernetes Cluster on your laptop or a spare PC

Offline/Low-Bandwidth Friendly

✅ Key Features To Implement


Area Key Implementation

Self-Healing Liveness & Readiness Probes, ReplicaSets, kured for reboot automation

Auto-Scaling HPA with Prometheus Adapter, VPA

Observability Prometheus + Grafana + Loki + Tempo

9/59
Area Key Implementation

Security PodSecurity Standards (PSS), Trivy image scans, Admission Controllers

App Deployment Custom Helm chart for a sample app

Podman Run agents/log tools on host nodes with Podman

GitOps (Bonus) ArgoCD (self-hosted) to automate deployments

🧱 Project Folder Structure


graphql

devops-k8s-advanced/
├── terraform/ # (Optional, for libvirt provisioning)
├── vagrant/ # Vagrant setup to bring up K8s nodes
├── ansible/ # Playbooks for provisioning and hardening
├── kubeadm/ # Kubeadm configs and scripts
├── helm-charts/
│ ├── monitoring/ # Prometheus, Grafana, Loki, etc.
│ └── demo-app/ # Custom chart with HPA, probes
├── podman/ # Host containers configs
├── security/
│ ├── trivy-scan/
│ └── admission-controllers/
├── dashboards/ # Prebuilt Grafana dashboards
├── manifests/ # K8s YAMLs not covered by Helm
└── docs/ # README, architecture diagrams, etc.

🗓️ Project Timeline (Local Dev, No Cost)


Week Goals

1 Setup Vagrant VMs, install Kubernetes with kubeadm

2 Ansible for OS hardening, container runtime (Podman), ssh access

10/59
Week Goals

3 Deploy Prometheus + Grafana + Loki + Tempo using Helm

4 Build & Deploy demo app with autoscaling + probes

5 Setup security scanning (Trivy) + network policies + PSS

6 Build dashboards, alerts, finalize documentation and GitHub repo

🛠️ First Steps
✅ Step 1: Prepare Local Environment
Install:

VirtualBox

Vagrant

Ansible

kubectl

Helm

Podman

Kubeadm

✅ Step 2: Do You Want a Starter Kit?


I can provide:

📦 A ready-to-use GitHub repository scaffold


📜 Vagrantfile to bring up 3 nodes
🔧 Ansible roles to install containerd , kubeadm, and Podman

🎯 Custom Helm chart for an HPA-enabled app

Let me know if you'd like:

11/59
Local cluster with 3 nodes (control plane + 2 workers)?

Integration of Podman on hosts for logging/monitoring agents?

Full monitoring stack YAML/Helm to deploy?

Once confirmed, I’ll begin generating the setup files.

use local server not vm

Perfect — using a bare-metal local server (instead of VMs) keeps things simple and efficient,
especially if you have control over the hardware.

We’ll adapt the project to install and run everything directly on your physical server (e.g., a
personal PC, home lab machine, or any Linux server). This avoids virtualization overhead and aligns
with your no-cost goal.

✅ Updated Plan: Bare-Metal Kubernetes Cluster with DevOps


Toolchain

🖥️ Assumptions:
You have one or more Linux machines (even 1 is enough to start).

OS: Ubuntu 20.04 / 22.04 LTS (or similar Debian-based distro).

Root or sudo access.

At least 4 cores + 8 GB RAM for good performance.

🔧 Project Overview (Bare-Metal Setup)


Layer Tool Notes

OS Provisioning Manually (or via Ansible ) Harden Linux, install dependencies

Container containerd + Podman containerd for K8s, Podman for host tools
Runtime

12/59
Layer Tool Notes

Kubernetes kubeadm Best for single-node or multi-node bare metal


setup

Observability Prometheus , Grafana , Loki , All deployed via Helm


Tempo

Auto-Scaling HPA + VPA + Metrics Adapter Scale based on CPU/memory

Security Trivy, PodSecurity Standards Scan images, enforce policies

CI/CD GitHub Actions + Helm GitOps optional

App Deployment Helm + Custom Chart HPA, probes, etc.

🧱 Folder Structure (Local Server Project)


perl

devops-k8s-local/
├── ansible/ # Hardening scripts (SSH, Podman, audit)
├── kubeadm/ # Setup scripts for K8s init and join
├── helm-charts/
│ ├── monitoring/ # Prometheus, Grafana, etc.
│ └── demo-app/ # App with HPA, probes, VPA
├── podman/ # Podman-managed log/security containers
├── security/ # Trivy scans, policies, admission control
├── manifests/ # NetworkPolicies, PSS, CRDs
├── dashboards/ # Grafana dashboards
└── docs/ # Architecture, guides, screenshots

🧭 Setup Roadmap (No VMs, No Cloud)


1. Prepare the Host OS
Install dependencies:

13/59
bash

sudo apt update && sudo apt install -y \


curl wget apt-transport-https gnupg lsb-release software-properties-common

Disable swap (required by K8s):

bash

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

Enable IP forwarding and bridge networking:

bash

cat <<EOF | sudo tee /etc/modules-load.d/[Link]


br_netfilter
EOF

cat <<EOF | sudo tee /etc/sysctl.d/[Link]


[Link]-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF

sudo sysctl --system

2. Install containerd

bash

sudo apt install -y containerd


sudo mkdir -p /etc/containerd
containerd config default | sudo tee /etc/containerd/[Link]
sudo systemctl restart containerd

3. Install Kubernetes ( kubeadm , kubelet , kubectl )

14/59
bash

sudo apt update && sudo apt install -y apt-transport-https ca-certificates curl

sudo curl -fsSLo /usr/share/keyrings/[Link] \


[Link]

echo "deb [signed-by=/usr/share/keyrings/[Link]] \


[Link] kubernetes-xenial main" | \
sudo tee /etc/apt/[Link].d/[Link]

sudo apt update


sudo apt install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

4. Initialize Kubernetes Cluster (Single Node)


bash

sudo kubeadm init --pod-network-cidr=[Link]/16

Set up your kubeconfig:

bash

mkdir -p $HOME/.kube
sudo cp /etc/kubernetes/[Link] $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

5. Install a CNI (e.g., Flannel)


bash

kubectl apply -f
[Link]

15/59
6. Helm Setup
bash

curl [Link] | bash

Use Helm to deploy:

Prometheus stack

Grafana

Loki

Tempo

Your custom HPA app

7. Podman on Host
Install Podman:

bash

sudo apt install -y podman

Use Podman to run log shippers or monitoring agents outside the Kubernetes cluster (useful for
bare-metal).

Example:

bash

podman run -d --name log-agent -v /var/log:/logs my-custom-agent:latest

8. Security Tools
Trivy:

bash

16/59
curl -sfL [Link]
| sudo sh -s -- -b /usr/local/bin

Scan images:

bash

trivy image nginx:latest

9. Deploy Demo App (with HPA, Helm)


Use Helm to package and deploy a containerized app with:

CPU-based autoscaling (HPA)

Vertical Pod Autoscaler

Readiness + Liveness probes

Resource limits/requests

10. Monitoring & Observability Stack


Deploy via Helm:

bash

helm repo add prometheus-community [Link]


helm install kube-prometheus prometheus-community/kube-prometheus-stack

Integrate:

Loki for logs

Tempo for traces

Create Grafana dashboards

17/59
📘 Final Output
You’ll have a fully working self-healing, auto-scaling, secure Kubernetes cluster running on a bare-
metal server with:

Trivy security scans

Monitoring dashboards

Autoscaling demo app

Host-level Podman tools

Helm-managed deployments

✅ Ready for Scaffold?


Would you like me to generate a starter project for you with:

Kubeadm init script

Ansible hardening playbook

Helm charts (Prometheus, Loki, demo app)

Trivy scan examples

Custom Grafana dashboards

Just say “Generate project scaffold”, and I’ll start putting it together!

step 1 in detail (tool , command , code )

Great! Let's go step-by-step in Step 1: Prepare the Host OS — assuming you're setting up a bare-
metal Kubernetes cluster on a single Linux server (Ubuntu 22.04 LTS recommended).

✅ STEP 1: Prepare the Host OS


(Ubuntu/Linux Bare-Metal Server)

18/59
🛠️ Tools Needed in This Step:
Tool Purpose

curl / wget Downloading software/scripts

apt Installing packages

sudo Elevated system access

modprobe / sysctl Kernel module/network tuning

swapoff Disable memory swap for K8s

⚙️ 1. Update & Upgrade the System


bash

sudo apt update && sudo apt upgrade -y

This ensures all your packages are up-to-date and secure.

🧱 2. Install Required Base Packages


bash

sudo apt install -y \


curl \
wget \
apt-transport-https \
gnupg \
lsb-release \
ca-certificates \
software-properties-common

These are foundational tools for downloading and managing repositories.

19/59
🚫 3. Disable Swap (Kubernetes requires swap off)
bash

# Turn off swap immediately


sudo swapoff -a

# Make the change permanent


sudo sed -i '/ swap / s/^/#/' /etc/fstab

You can verify it's off with:

bash

free -h

You should see 0 under Swap .

🔁 4. Load Required Kernel Modules


bash

cat <<EOF | sudo tee /etc/modules-load.d/[Link]


br_netfilter
EOF

sudo modprobe br_netfilter

🌐 5. Enable Required sysctl Settings (network and bridge


forwarding)
bash

cat <<EOF | sudo tee /etc/sysctl.d/[Link]


[Link]-nf-call-ip6tables = 1
[Link]-nf-call-iptables = 1
net.ipv4.ip_forward = 1

20/59
EOF

# Apply the changes


sudo sysctl --system

✅ 6. Verify Prerequisites (Post-Check)


Check that modules and sysctl settings are active:

bash

lsmod | grep br_netfilter


sysctl [Link]-nf-call-iptables
sysctl net.ipv4.ip_forward

📋 Summary of Step 1
You’ve now:

Ensured your system is updated

Disabled swap (Kubernetes requirement)

Loaded br_netfilter for Kubernetes networking

Enabled necessary kernel parameters

Installed all core dependencies

✅ Next Step: Install the container runtime ( containerd ) in Step 2.


Would you like to proceed with Step 2: Installing containerd, or do you want a bash script to
automate all of Step 1?

21/59
step 2 in detail (tool , command , code )

Excellent — let’s continue with Step 2: Installing the container runtime, specifically containerd ,
which is the recommended runtime for Kubernetes.

✅ STEP 2: Install containerd (Container


Runtime)

📦 Tools You’ll Use


Tool Purpose

containerd Core container runtime used by Kubernetes

runc OCI-compliant low-level container runtime

crictl CLI tool to test containerd runtime interface

systemctl To start and enable containerd service

🔧 Why Use containerd?


Kubernetes no longer supports Docker directly as a runtime. Instead, it uses container runtimes like
containerd , which is lightweight, CNCF-approved, and highly stable.

🚀 Step-by-Step Installation

✅ 1. Install containerd and dependencies


22/59
bash

sudo apt update


sudo apt install -y containerd

Verify the version:

bash

containerd --version

You should see something like:


containerd [Link] 1.6.x+...

⚙️ 2. Generate the default configuration


This creates a config file so you can modify runtime behavior if needed (for example, for cgroup
setup):

bash

sudo mkdir -p /etc/containerd


containerd config default | sudo tee /etc/containerd/[Link] > /dev/null

🔁 3. Set the correct cgroup driver (for systemd compatibility)


Edit the config file:

bash

sudo nano /etc/containerd/[Link]

Find this section (under


[plugins."[Link]".[Link]] ):

toml

SystemdCgroup = false

23/59
Change it to:

toml

SystemdCgroup = true

Then save and exit ( Ctrl+O , Enter , Ctrl+X ).

This aligns the container runtime with the systemd cgroup driver, which is what kubelet will use
later.

▶️ 4. Restart & Enable containerd


bash

sudo systemctl restart containerd


sudo systemctl enable containerd

🧪 5. (Optional) Install crictl to test containerd


crictl is a CLI tool for testing Kubernetes container runtimes (via the CRI API):

bash

VERSION="v1.30.0"
curl -LO [Link]
tools/releases/download/${VERSION}/crictl-${VERSION}-[Link]
sudo tar -C /usr/local/bin -xzf crictl-${VERSION}-[Link]
rm crictl-${VERSION}-[Link]

Then test:

bash

crictl info

You should get back runtime info in JSON format.

24/59
📋 Summary of Step 2
✅ You now have:
Installed containerd

Set it up to work with systemd cgroups (required by Kubernetes)

(Optionally) Installed crictl for containerd debugging

✅ Next Step: Step 3 is installing Kubernetes components ( kubeadm , kubelet , kubectl ).

Would you like to proceed to Step 3 in detail, or would you like a bash script to automate Step 2?

step 3 in detail (tool , command , code )

Great! You're making excellent progress. Let's move to Step 3: Installing Kubernetes core
components on your bare-metal local server.

✅ STEP 3: Install kubeadm , kubelet , and


kubectl
These are the 3 essential Kubernetes components:

Tool Purpose

kubeadm Bootstraps a Kubernetes cluster (init/join)

kubelet Agent running on each node, managing containers

kubectl Command-line tool to interact with the cluster

25/59
🧱 Prerequisites
Make sure you've already completed:

Step 1 (OS Prep: swap off, networking, dependencies)

Step 2 (Installed and configured containerd )

📦 Step-by-Step Installation
✅ 1. Add Kubernetes apt repository
bash

sudo curl -fsSLo /usr/share/keyrings/[Link] \


[Link]

echo "deb [signed-by=/usr/share/keyrings/[Link]] \


[Link] kubernetes-xenial main" | \
sudo tee /etc/apt/[Link].d/[Link]

✅ 2. Install the components


bash

sudo apt update


sudo apt install -y kubelet kubeadm kubectl

🔒 3. Hold the versions


You don’t want these critical components to be auto-upgraded by apt :

bash

sudo apt-mark hold kubelet kubeadm kubectl

26/59
🔁 4. Enable kubelet to start on boot
bash

sudo systemctl enable kubelet

kubelet will fail to start now — that’s normal because the cluster hasn’t been bootstrapped yet. It

will automatically reconnect once we run kubeadm init in Step 4.

🧪 5. Verify installation
Check versions:

bash

kubelet --version
kubeadm version
kubectl version --client

You should see version outputs like:

nginx

Kubernetes v1.30.0

If you see these, installation is successful.

🧰 Optional (Check systemctl logs)


You can inspect kubelet status with:

bash

sudo systemctl status kubelet

It will likely show "inactive" or "failed" — don't worry. It becomes active after kubeadm init .

27/59
📋 Summary of Step 3
✅ You now have:
Added the official Kubernetes apt repo

Installed kubelet , kubeadm , and kubectl

Held their versions to prevent unintended upgrades

Verified the installation

✅ Ready for Step 4?


Next Step: Step 4: Initialize the Kubernetes Cluster using kubeadm

Would you like me to walk you through Step 4 in detail, or generate a bash script that automates
all of Step 3?

step 4 in detail (tool , command , code )

Awesome! Now that you've installed kubeadm , kubelet , and kubectl , let's initialize your
Kubernetes cluster locally using kubeadm .

✅ STEP 4: Initialize the Kubernetes Cluster


(Single-Node Bare-Metal)

🧰 Tools Used in This Step


Tool Purpose

kubeadm Initializes and bootstraps the K8s control plane

28/59
Tool Purpose

kubectl CLI to manage the cluster

kubelet System service that manages pods/containers

flannel Container Network Interface (CNI) for pod networking

🚧 System Check Before You Begin


✅ Ensure:
containerd is running:

bash

sudo systemctl status containerd

swap is off:

bash

free -h

br_netfilter is enabled and net.ipv4.ip_forward = 1 is active.

🛠️ 1. Initialize the Kubernetes Cluster


Run this to create a single-node cluster:

bash

sudo kubeadm init --pod-network-cidr=[Link]/16

This CIDR is required by Flannel (you can use others like Calico later with different CIDRs).

🔍 What This Command Does:


Initializes the control plane (API server, scheduler, controller-manager)

29/59
Configures etcd (key-value store for cluster state)

Prepares the kubeconfig file

Sets up certificates and bootstrap tokens

✅ 2. Save the Join Command Output


After running kubeadm init , you'll see a line like this:

bash

kubeadm join [Link]:6443 --token abcdef.1234567890abcdef \


--discovery-token-ca-cert-hash sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

📌 Save this join command — you’ll need it if you want to add more nodes later.

📂 3. Configure kubectl Access for Your User


By default, only root can use kubectl . To allow your regular user:

bash

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/[Link] $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

✅ Test:
bash

kubectl get nodes

You should see your master/control-plane node in NotReady status — that’s expected until
networking is configured.

🌐 4. Install a Pod Network (CNI) — Flannel


30/59
To allow pods to communicate, install a CNI plugin. Here’s Flannel (simple and reliable):

bash

kubectl apply -f
[Link]

✅ Check nodes again in ~30 seconds:


bash

kubectl get nodes

Status should now be Ready.

🧪 5. Allow Pod Scheduling on the Master Node (for single-


node clusters only)
By default, master/control-plane nodes are tainted and won’t run workloads.

To allow scheduling pods on your single node:

bash

kubectl taint nodes --all [Link]/control-plane-

✅ Now you can deploy apps to this node directly.

📋 Summary of Step 4
You now have:

✅ A fully bootstrapped Kubernetes control plane


✅ kubectl access from your local user

✅ Flannel CNI for networking


✅ Ready node to run pods (even on master)

31/59
✅ What’s Next?
Step 5: Install Helm & Deploy Monitoring Stack (Prometheus + Grafana)
Or
Step 6: Deploy a Demo App with HPA/Autoscaling

Would you like to proceed with Step 5: Helm Setup, or should I generate a bash script that
automates Step 4?

step 5 in detail (tool , command , code )

Great! With your Kubernetes cluster initialized, it’s time to move to:

✅ STEP 5: Install Helm & Deploy


Monitoring Stack (Prometheus + Grafana)
Helm is the "package manager" for Kubernetes. You’ll use it to deploy complex applications (like
Prometheus, Grafana, Loki, etc.) easily.

🧰 Tools Used in This Step


Tool Purpose

helm Kubernetes package manager (like apt/npm)

kubectl Interact with the Kubernetes API

prometheus & grafana Monitoring stack via Helm Charts

⚙️ Part A: Install Helm

32/59
✅ 1. Download & Install Helm
bash

curl [Link] | bash

Confirm installation:

bash

helm version

You should see something like:

bash

[Link]{Version:"v3.14.x", ...}

🧱 Part B: Add Helm Repositories

bash

helm repo add prometheus-community [Link]


helm repo add grafana [Link]
helm repo update

📦 Part C: Deploy Prometheus + Grafana

✅ 1. Create a dedicated namespace

33/59
bash

kubectl create namespace monitoring

✅ 2. Install Prometheus
bash

helm install kube-prometheus-stack prometheus-community/kube-prometheus-stack \


--namespace monitoring

This includes:

Prometheus Operator

Prometheus

Grafana

Alertmanager

Node Exporter

Kube State Metrics

⏳ Wait a minute or two, then check:


bash

kubectl get pods -n monitoring

They should be in Running state.

🧪 3. Access Grafana Dashboard


Port-forward the Grafana service:

bash

kubectl port-forward svc/kube-prometheus-stack-grafana -n monitoring 3000:80

Then open [Link]

34/59
Default username: admin

Password: Get it with:

bash

kubectl get secret --namespace monitoring kube-prometheus-stack-grafana -o jsonpath="


{.[Link]-password}" | base64 --decode

📈 4. Optional: Enable Node & Pod Dashboards


Once in Grafana:

Go to “Dashboards”

Explore: Node Exporter Full, Pod Resource Usage, Kubelet Metrics

Grafana and Prometheus will now collect metrics like:

Node CPU/RAM/disk

Pod/container health

Cluster-wide resource usage

🧹 5. Verify Everything is Working


bash

kubectl get svc -n monitoring


kubectl get pods -n monitoring
kubectl get endpoints -n monitoring

✅ All services should be up and running.

📋 Summary of Step 5
You have:

35/59
✅ Installed Helm
✅ Deployed Prometheus, Grafana, and supporting services via Helm
✅ Accessed Grafana Dashboard with cluster observability

🚀 What’s Next?
Step 6: Deploy a Demo Application + Horizontal Pod Autoscaler

Or: Step 7: Install Aqua Security & Pod Security Policies

Would you like to continue with the demo app + HPA, or jump to advanced security setup with
Aqua & PSPs?

step 6 in detail (tool , command , code )

Awesome! Let's move forward to:

✅ STEP 6: Deploy a Demo Application +


Enable Horizontal Pod Autoscaling (HPA)
This step demonstrates auto-scaling in action using:

A simple demo app (CPU-intensive web app)

Kubernetes HPA (Horizontal Pod Autoscaler)

Metrics Server (feeds CPU/memory metrics to HPA)

🧰 Tools Used in This Step


Tool Purpose

kubectl Cluster interaction

36/59
Tool Purpose

metrics-server Collects resource metrics for HPA

HPA Scales pods automatically based on metrics

Demo app A sample CPU-intensive web app

⚙️ Part A: Install metrics-server

✅ 1. Apply official deployment


bash

kubectl apply -f [Link]


server/releases/latest/download/[Link]

🧪 2. Verify installation
bash

kubectl get deployment metrics-server -n kube-system


kubectl top nodes
kubectl top pods

✅ If you see CPU/memory values — it's working.

📦 Part B: Deploy the Demo App (CPU-bound Web App)

✅ 1. Create a namespace
bash

37/59
kubectl create namespace demo

✅ 2. Deploy the app


yaml

# [Link]
apiVersion: apps/v1
kind: Deployment
metadata:
name: cpu-demo
namespace: demo
spec:
replicas: 1
selector:
matchLabels:
app: cpu-demo
template:
metadata:
labels:
app: cpu-demo
spec:
containers:
- name: cpu-demo
image: vish/stress
args:
- -cpus
- "2"
resources:
requests:
cpu: 100m
limits:
cpu: 500m
---
apiVersion: v1
kind: Service
metadata:
name: cpu-demo
namespace: demo

38/59
spec:
selector:
app: cpu-demo
ports:
- protocol: TCP
port: 80
targetPort: 8080

Apply it:

bash

kubectl apply -f [Link]

🔄 Part C: Create the HPA Resource

bash

kubectl autoscale deployment cpu-demo \


--cpu-percent=50 \
--min=1 \
--max=5 \
-n demo

This tells Kubernetes:

If CPU usage > 50%, scale up

Keep between 1 and 5 replicas

📊 Part D: Generate CPU Load (to trigger scaling)


Create a pod that runs stress tool to hit the CPU:

bash

39/59
kubectl run -n demo load-generator --image=busybox --restart=Never \
-- /bin/sh -c "while true; do wget -q -O- [Link]
done"

🔍 Part E: Monitor HPA Behavior


bash

watch -n 2 kubectl get hpa -n demo

You'll see CPU increase and replicas scale up:

bash

NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS


cpu-demo Deployment/cpu-demo 120% / 50% 1 5 3

🧹 Cleanup (Optional)
bash

kubectl delete ns demo

📋 Summary of Step 6
✅ You now have:
Installed metrics-server

Deployed a sample CPU-heavy app

Enabled autoscaling with kubectl autoscale

Observed dynamic pod scaling

40/59
🎯 Next Steps:
Step 7: Deploy Vertical Pod Autoscaler (VPA)

Or: Step 8: Install Aqua Security for Runtime Protection

Would you like to go into Step 7: VPA or dive into Step 8: Aqua Security integration?

step 7 in detail (tool , command , code )

Excellent! Let’s now implement:

✅ STEP 7: Deploy Vertical Pod Autoscaler


(VPA) in Kubernetes
While Horizontal Pod Autoscaler (HPA) adjusts the number of pods based on CPU/memory, VPA
automatically adjusts CPU and memory requests/limits of containers themselves, based on actual
usage. It's great for right-sizing your pods.

🧰 Tools Used in This Step


Tool Purpose

VPA Kubernetes controller to optimize pod resources

kubectl Cluster interaction

⚙️ Part A: Install VPA Components


Official GitHub: Vertical Pod Autoscaler

41/59
✅ 1. Apply VPA manifests
bash

kubectl apply -f
[Link]
[Link]
kubectl apply -f
[Link]
[Link]

⏳ Wait a few seconds and verify:


bash

kubectl get pods -n kube-system | grep vpa

You should see:

vpa-admission-controller

vpa-recommender

vpa-updater

✅ All should be Running .

🧱 Part B: Deploy a VPA-enabled App


We’ll reuse the CPU-bound app, but without HPA.

✅ 1. Create a namespace
bash

kubectl create ns vpa-demo

42/59
✅ 2. Deploy a pod with poor resource settings
yaml

# [Link]
apiVersion: apps/v1
kind: Deployment
metadata:
name: vpa-demo
namespace: vpa-demo
spec:
replicas: 1
selector:
matchLabels:
app: vpa-demo
template:
metadata:
labels:
app: vpa-demo
spec:
containers:
- name: stress
image: vish/stress
args:
- -cpus
- "1"
resources:
requests:
cpu: "50m"
limits:
cpu: "100m"

Apply it:

bash

kubectl apply -f [Link]

⚙️ Part C: Create a VPA Object

43/59
yaml

# [Link]
apiVersion: [Link]/v1
kind: VerticalPodAutoscaler
metadata:
name: vpa-demo
namespace: vpa-demo
spec:
targetRef:
apiVersion: "apps/v1"
kind: Deployment
name: vpa-demo
updatePolicy:
updateMode: "Auto" # Options: Off, Initial, Auto

Apply it:

bash

kubectl apply -f [Link]

🔁 Part D: Generate Load & Wait


To generate CPU stress:

bash

kubectl run -n vpa-demo load-gen --image=busybox --restart=Never \


-- /bin/sh -c "while true; do wget -q -O- [Link]
done"

Wait 5–10 minutes while VPA collects metrics and adjusts resources.

🔍 Part E: Check VPA Recommendations


bash

44/59
kubectl describe vpa vpa-demo -n vpa-demo

Look for:

yaml

Recommendations:
Container Recommendations:
Container Name: stress
Lower Bound:
Cpu: 75m
Target:
Cpu: 150m
Upper Bound:
Cpu: 300m

VPA will evict and recreate the pod with updated resource settings if updateMode: Auto .

🧹 Optional Cleanup
bash

kubectl delete ns vpa-demo

📋 Summary of Step 7
You have:

✅ Installed Vertical Pod Autoscaler


✅ Deployed a sample app with CPU load
✅ Observed resource request/limit tuning over time

✅ What’s Next?

45/59
Step 8: Implement Pod Security with Aqua Security or PSPs

Or: Step 9: Custom CRDs for backup operators or schedulers

Would you like to continue with Step 8: Aqua Security Runtime Protection next?

step 8 in detail (tool , command , code )

Here’s Step 8: Advanced Security Setup for your bare-metal Kubernetes cluster, including Trivy
vulnerability scanning, Pod Security Admission, and a runtime threat detection tool like Falco. This
will significantly strengthen your cluster’s security posture.

🔒 STEP 8: Security – Trivy + Pod Security Admission + Falco


🧰 Tools Involved
Tool Purpose

Trivy Lightweight vulnerability scanner for container images

Pod Security Admission (PSA) Native Kubernetes admission controller enforcing Pod Security Standards

Falco Runtime security monitoring using eBPF for threat detection

✅ A) Install Trivy (Image Scanning)


bash

curl -sfL [Link]


| sudo sh -s -- -b /usr/local/bin

Test scanning an image:

bash

46/59
trivy image nginx:latest

Look for vulnerabilities in the output.

✅ For CI pipelines (e.g., in GitHub Actions), you can run:


bash

trivy image --severity HIGH,CRITICAL my-app:latest

✅ B) Enforce Pod Security Admission (PSA)


PSA is built into Kubernetes v1.25+ and replaces PSPs [Link] +6 [Link] +2

[Link] +13 [Link] +2 [Link] .

1. Label your namespaces according to desired policy level:

bash

# Enforce 'baseline' policy on demo namespace


kubectl label namespace demo \
[Link]/enforce=baseline \
[Link]/audit=baseline \
[Link]/warn=baseline

2. Test policy enforcement:

Try deploying a privileged pod:

yaml

# [Link]
apiVersion: v1
kind: Pod
metadata:
name: privileged
namespace: demo
spec:
containers:
- name: ns

47/59
image: busybox
securityContext:
privileged: true
command: ["sleep", "3600"]

bash

kubectl apply -f [Link]

It should be rejected with a policy violation.

3. Audit logs:

bash

kubectl get events -n demo --field-selector reason=FailedCreate

PSA logs the violation, helping you detect and remediate insecure pod specs [Link] +2

[Link] [Link] [Link] +1 .

✅ C) Deploy Falco for Runtime Threat Detection


Falco is an open-source runtime security tool that detects suspicious behavior inside your nodes .

bash

# Add the Falco Helm repo


helm repo add falcosecurity [Link]
helm repo update

# Install Falco
helm install falco falcosecurity/falco \
--namespace falco --create-namespace

Check the installation:

bash

kubectl get pods -n falco


kubectl logs -n falco -l app=falco

48/59
Falco uses eBPF to inspect system calls and flags suspicious activity such as:

Unexpected container executions

Shells inside pods

Sensitive file access

You can forward these alerts to:

Console ( kubectl logs )

Files

Syslog/Fluent Bit to Grafana Loki

Falco gives near real-time runtime monitoring.

🧹 Summary of Step 8
✅ Trivy scans your images for vulnerabilities before deployment
✅ Pod Security Admission enforces secure pod specs via namespace labels
✅ Falco monitors running pods/nodes and alerts on threats

📋 ✅ After Completing Step 8


You’ll have:

1. Image-level vulnerabilities checked at build/deploy time

2. Pod-level policies enforced at creation

3. Runtime monitoring guarding against suspicious behavior

🔮 What’s Next?
Would you like to add OPA Gatekeeper or Kyverno for custom policy enforcement, or integrate
Aqua’s open‑source tools like kube-bench or kube-hunter next?

49/59
Sources

step 9 in detail (tool , command , code )

Great! Let’s move to:

✅ STEP 9: Create Custom CRDs &


Operators for Automation (e.g., Backup
Operator)
This step teaches you to build a Custom Resource Definition (CRD) and a simple Kubernetes
Operator that watches that CRD and performs automated tasks — in this case, a simulated backup
action.

🧰 Tools Involved
Tool Purpose

kubectl Managing Kubernetes resources

controller-runtime or client-go Building custom operators

CRD Define a new Kubernetes resource (e.g., Backup )

Operator (Python shell script / Go controller) Watches CRDs and executes tasks

We’ll keep it simple and write a basic Bash or Python-based Operator to respond to our CRD.

⚙️ Part A: Define the CRD for a “Backup” Object


yaml

50/59
# [Link]
apiVersion: [Link]/v1
kind: CustomResourceDefinition
metadata:
name: [Link]
spec:
group: [Link]
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
schedule:
type: string
target:
type: string
scope: Namespaced
names:
plural: backups
singular: backup
kind: Backup
shortNames:
- bkp

✅ Apply it
bash

kubectl apply -f [Link]

📦 Part B: Create a Sample Backup Resource

51/59
yaml

# [Link]
apiVersion: [Link]/v1
kind: Backup
metadata:
name: db-backup
namespace: default
spec:
schedule: "*/10 * * * *"
target: "/data/postgres"

bash

kubectl apply -f [Link]

🧠 Part C: Write a Basic Operator (Python Watcher Example)


We'll use kubernetes Python client to watch the custom resource and simulate a backup action.

✅ 1. Install Kubernetes Python Client


bash

pip install kubernetes

✅ 2. Python Operator Script


python

# [Link]
from kubernetes import client, config, watch

config.load_kube_config()
crd_api = [Link]()
w = [Link]()

print("Starting backup operator...")

for event in [Link](crd_api.list_namespaced_custom_object,

52/59
group="[Link]",
version="v1",
namespace="default",
plural="backups"):
backup = event['object']
name = backup['metadata']['name']
target = backup['spec']['target']
schedule = backup['spec']['schedule']
etype = event['type']

if etype in ["ADDED", "MODIFIED"]:


print(f"[{etype}] Backup task '{name}' scheduled to backup {target} every
{schedule}")
# Simulated backup logic
with open(f"/tmp/{name}-[Link]", "a") as f:
[Link](f"Backup triggered for {target} on schedule {schedule}\n")

✅ 3. Run the operator


bash

python3 [Link]

Whenever a new Backup resource is created or modified, your operator logs a simulated backup.

🚀 Optional: Run Operator as a Pod


You can dockerize and run it as a Kubernetes deployment if desired.

🧹 Cleanup (Optional)
bash

kubectl delete -f [Link]


kubectl delete -f [Link]

53/59
✅ Step 9 Summary
You Did Description

📘 Created CRD Defined a new Kubernetes resource ( Backup )

🤖 Built Operator Wrote a Python watcher to automate based on CR

🛠 Simulated Action Logged the “backup” when CR was applied

🔮 What’s Next?
Would you like to:

Convert this into a Go-based real Operator using kubebuilder?

Use Helm + CRDs for more templated operator workflows?

Move on to Step 10: Advanced Observability Stack (Prometheus, Loki, Grafana, Tempo)?

Let me know!

step 10 in detail (tool , command , code )

Fantastic! Let's now complete Step 10: Advanced Observability Stack using open-source tools —
Prometheus, Grafana, Loki, and Tempo — all deployed via Helm on your bare-metal Kubernetes
cluster.

✅ STEP 10: Full Observability with


Prometheus, Grafana, Loki & Tempo

🧰 Tools & Purpose

54/59
Tool Purpose

Prometheus Metrics collection for nodes, pods, apps

Grafana Visualization and dashboards

Loki Logs aggregation from all pods

Tempo Distributed tracing system (Jaeger/OTel compatible)

Helm Easy deployment and management of complex stacks

🔧 Prerequisites
✅ Kubernetes cluster is running
✅ kubectl and helm are installed on your local machine
Install Helm if not already:

bash

curl [Link] | bash

📦 A) Add Helm Repos


bash

helm repo add prometheus-community [Link]


helm repo add grafana [Link]
helm repo update

📈 B) Deploy Prometheus
bash

55/59
helm install prometheus prometheus-community/kube-prometheus-stack \
--namespace monitoring --create-namespace

Wait for pods:

bash

kubectl get pods -n monitoring

Access Prometheus UI (NodePort):

bash

kubectl port-forward svc/prometheus-kube-prometheus-prometheus -n monitoring 9090

Visit: [Link]

📊 C) Deploy Grafana
Grafana is already bundled with the above Helm chart, but you can access it with:

bash

kubectl port-forward svc/prometheus-grafana -n monitoring 3000

Login:

User: admin

Password: Run this:

bash

kubectl get secret prometheus-grafana -n monitoring -o jsonpath="{.[Link]-


password}" | base64 -d

🎨 Use the prebuilt dashboards: Kubernetes / Node / Pod / etc.

📜 D) Deploy Loki for Log Aggregation


56/59
Install Loki:
bash

helm install loki grafana/loki-stack \


--namespace logging --create-namespace \
--set [Link]=true

Check pods:

bash

kubectl get pods -n logging

Promtail sends logs to Loki.

Configure Loki in Grafana


1. Go to Grafana > Settings > Data Sources > Add data source

2. Choose Loki

3. URL: [Link]

4. Save & Test

Then go to Explore tab to query logs:

logql

{job="kubernetes"}

⏱️ E) Deploy Tempo for Distributed Tracing


Install Tempo:
bash

helm install tempo grafana/tempo \

57/59
--namespace tracing --create-namespace

Add Tempo to Grafana:


1. Grafana > Data Sources > Add data source > Tempo

2. URL: [Link] (default cluster-internal service)

3. Save & Test

Then use Trace to Logs feature inside dashboards.

🧠 (Optional) Use OpenTelemetry SDKs


To generate traces:

python

from opentelemetry import trace


from [Link].trace_exporter import OTLPSpanExporter

Or inject OpenTelemetry sidecar containers into your pods.

📋 Summary
✅ You now have a production-grade observability stack:
Component URL Purpose

Prometheus [Link] Metrics

Grafana [Link] Dashboards

Loki Query via Grafana Explore Logs

Tempo Tracing backend Distributed tracing

58/59
🔮 Next Suggestions
Would you like to:

Integrate alerting via Alertmanager (Slack, Email, PagerDuty)?

Push logs to external storage like Elasticsearch?

Enable TLS + Auth for Prometheus/Grafana access?

Build custom dashboards per app/service?

Let me know and I’ll guide you!

59/59

You might also like