Kubernetes Training Lab

In this tutorial, you’ll set up a Kubernetes training environment using Teabar, perfect for workshops, onboarding new team members, or self-paced learning.

Time to complete: 30-45 minutes

Prerequisites:

  • Teabar account
  • Basic understanding of Kubernetes concepts

What You’ll Build

A complete Kubernetes training lab with:

  1. Isolated Kubernetes clusters for each trainee
  2. Pre-configured sample applications
  3. Guided exercises with validation
  4. Automatic cleanup after training

Step 1: Create the Training Blueprint

Create a blueprint that provisions a Kubernetes cluster with training materials:

# blueprints/k8s-training.yaml
name: k8s-training
version: "1.0"
description: Kubernetes training environment with sample applications

parameters:
  trainee_name:
    type: string
    description: Name of the trainee
  skill_level:
    type: string
    default: beginner
    enum: [beginner, intermediate, advanced]

components:
  kubernetes:
    type: kubernetes
    version: "1.28"
    nodes:
      control_plane: 1
      workers: 2
    resources:
      cpu: 4
      memory: 8G

  registry:
    image: registry:2
    ports:
      - 5000:5000

  training-app:
    image: teabar/k8s-training-app:latest
    environment:
      TRAINEE_NAME: ${trainee_name}
      SKILL_LEVEL: ${skill_level}

hooks:
  post_create:
    - name: Setup training materials
      command: |
        kubectl apply -f /training/manifests/
        kubectl create namespace trainee-workspace
        kubectl config set-context --current --namespace=trainee-workspace

expose:
  - component: kubernetes
    type: kubeconfig
  - component: training-app
    port: 8080
    public: true

Step 2: Create Training Exercises

Create exercise manifests that trainees will work with:

# training/exercises/01-pods/exercise.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: exercise-01-pods
  namespace: training
data:
  instructions.md: |
    # Exercise 1: Working with Pods
    
    ## Objectives
    - Create a simple Pod
    - Inspect Pod status and logs
    - Delete a Pod
    
    ## Tasks
    
    ### Task 1: Create a Pod
    Create a Pod named `my-first-pod` using the `nginx:alpine` image.
    
    ```bash
    kubectl run my-first-pod --image=nginx:alpine
    ```
    
    ### Task 2: Verify the Pod
    Check that your Pod is running:
    
    ```bash
    kubectl get pods
    kubectl describe pod my-first-pod
    ```
    
    ### Task 3: View Pod Logs
    ```bash
    kubectl logs my-first-pod
    ```
    
    ### Task 4: Clean Up
    ```bash
    kubectl delete pod my-first-pod
    ```
    
  validation.sh: |
    #!/bin/bash
    # Validation script for Exercise 1
    
    echo "Checking for my-first-pod..."
    if kubectl get pod my-first-pod &>/dev/null; then
      echo "✓ Pod exists"
      
      STATUS=$(kubectl get pod my-first-pod -o jsonpath='{.status.phase}')
      if [ "$STATUS" == "Running" ]; then
        echo "✓ Pod is running"
        exit 0
      else
        echo "✗ Pod is not running (status: $STATUS)"
        exit 1
      fi
    else
      echo "✗ Pod not found"
      exit 1
    fi

Step 3: Set Up Batch Training

Create a script to provision multiple training environments:

#!/bin/bash
# provision-training.sh

TRAINEES=(
  "alice"
  "bob"
  "charlie"
  "diana"
)

SKILL_LEVEL="${1:-beginner}"

echo "Provisioning $SKILL_LEVEL training environments for ${#TRAINEES[@]} trainees..."

for trainee in "${TRAINEES[@]}"; do
  echo "Creating environment for $trainee..."
  
  teabar env create "k8s-training-$trainee" 
    --blueprint k8s-training 
    --var trainee_name="$trainee" 
    --var skill_level="$SKILL_LEVEL" 
    --async
done

echo "Waiting for all environments to be ready..."
for trainee in "${TRAINEES[@]}"; do
  teabar env wait "k8s-training-$trainee" --healthy --timeout 10m
done

echo "All environments ready!"
echo ""
echo "Environment URLs:"
for trainee in "${TRAINEES[@]}"; do
  URL=$(teabar env info "k8s-training-$trainee" --output json | jq -r '.url')
  echo "  $trainee: $URL"
done

Step 4: Create Training Dashboard

Build a simple dashboard to track trainee progress:

# blueprints/training-dashboard.yaml
name: training-dashboard
version: "1.0"

components:
  dashboard:
    image: teabar/training-dashboard:latest
    ports:
      - 3000:3000
    environment:
      TRAINING_PREFIX: k8s-training-
    # Dashboard authenticates via OIDC - no API keys needed
    
expose:
  - component: dashboard
    port: 3000
    public: true

Step 5: Distribute Access

Option A: Kubeconfig Files

Generate and distribute kubeconfig files:

#!/bin/bash
# generate-configs.sh

OUTPUT_DIR="./trainee-configs"
mkdir -p "$OUTPUT_DIR"

for trainee in "${TRAINEES[@]}"; do
  echo "Generating config for $trainee..."
  
  teabar env kubeconfig "k8s-training-$trainee" 
    --output "$OUTPUT_DIR/$trainee-kubeconfig.yaml"
  
  # Generate instructions file
  URL=$(teabar env info "k8s-training-$trainee" --output json | jq -r '.url')
  
  cat > "$OUTPUT_DIR/$trainee-instructions.txt" << INSTRUCTIONS
Kubernetes Training - Welcome!

Hi $trainee,

Your training environment is ready!

Getting Started:
1. Download your kubeconfig file (attached)
2. Set up kubectl:
   export KUBECONFIG=~/Downloads/$trainee-kubeconfig.yaml
3. Verify connection:
   kubectl cluster-info
4. Access the training portal: $URL

Support:
If you encounter issues, reach out in #k8s-training Slack channel.
INSTRUCTIONS
done

echo "Configs generated in $OUTPUT_DIR"

Option B: Web Terminal Access

Provide browser-based terminal access:

# Add to blueprint
components:
  terminal:
    image: wettyoss/wetty:latest
    ports:
      - 3001:3000
    environment:
      SSHHOST: kubernetes
      SSHPORT: 22
      SSHUSER: trainee

expose:
  - component: terminal
    port: 3001
    public: true
    path: /terminal

Step 6: Create Exercise Progression

Beginner Track

# training/tracks/beginner.yaml
track: beginner
exercises:
  - id: 01-pods
    title: Working with Pods
    duration: 15m
    
  - id: 02-deployments
    title: Creating Deployments
    duration: 20m
    
  - id: 03-services
    title: Exposing with Services
    duration: 20m
    
  - id: 04-configmaps
    title: Configuration with ConfigMaps
    duration: 15m
    
  - id: 05-volumes
    title: Persistent Storage
    duration: 25m

Validation Webhook

Create a service that validates exercise completion:

// validation-service/index.js
const express = require('express');
const { execSync } = require('child_process');

const app = express();
app.use(express.json());

app.post('/validate/:exercise', async (req, res) => {
  const { exercise } = req.params;
  const { trainee } = req.body;
  
  try {
    // Set kubeconfig for trainee's environment
    process.env.KUBECONFIG = `/configs/${trainee}-kubeconfig.yaml`;
    
    // Run validation script
    const result = execSync(
      `bash /exercises/${exercise}/validation.sh`,
      { encoding: 'utf-8' }
    );
    
    res.json({
      success: true,
      exercise,
      trainee,
      output: result
    });
  } catch (error) {
    res.json({
      success: false,
      exercise,
      trainee,
      error: error.message
    });
  }
});

app.listen(3000);

Step 7: Monitor Progress

Track training progress across all environments:

# Check all training environments
teabar env list --filter "k8s-training-*" --format table

# View specific trainee's activity
teabar activity --env k8s-training-alice --since 2h

# Check resource usage across all training envs
teabar metrics summary --filter "k8s-training-*"

Step 8: Cleanup

After training is complete:

#!/bin/bash
# cleanup-training.sh

echo "Cleaning up training environments..."

# List all training environments
ENVS=$(teabar env list --filter "k8s-training-*" --output json | jq -r '.[].name')

for env in $ENVS; do
  echo "Deleting $env..."
  teabar env delete "$env" --yes
done

echo "Cleanup complete!"

Or set automatic cleanup:

# teabar.yaml
cleanup:
  # Auto-delete training environments after 8 hours
  environments:
    - pattern: "k8s-training-*"
      max_lifetime: 8h
      notify_before: 30m

Advanced: Self-Service Training Portal

Create a self-service portal where trainees can provision their own environments:

# blueprints/training-portal.yaml
name: training-portal
version: "1.0"

components:
  portal:
    image: teabar/self-service-portal:latest
    ports:
      - 3000:3000
    environment:
      ALLOWED_BLUEPRINTS: k8s-training
      MAX_ENVIRONMENTS_PER_USER: 1
      ENVIRONMENT_LIFETIME: 4h
      # Portal uses OIDC for authentication - users log in with their Keycloak credentials
      
expose:
  - component: portal
    port: 3000
    public: true

Cost Management

Set budgets for training:

# Set daily budget for training environments
teabar budget set 
  --filter "k8s-training-*" 
  --daily 50 
  --alert-at 80%

# View training costs
teabar costs show --filter "k8s-training-*" --group-by env

Summary

You’ve created a complete Kubernetes training infrastructure that:

  • Provisions isolated clusters for each trainee
  • Includes guided exercises with validation
  • Supports progress tracking
  • Automatically cleans up resources

Next Steps

ende