The hard way · Part 6

Check it, then tear it down

Evidence in, evidence out.

Status

The scripts are checked (flags, local control plane run, AWS --dry-run) but have not yet been run end to end on AWS. The outputs below are real where marked; elsewhere the page says what to look for.

Prove that every piece works, then delete everything and prove that, too.

1Create the files

Same folder. Open each file below and use its copy button: it creates the file for you.

$ cd ~/k8s-hard-way
scripts/97-smoke-test.sh (42 lines)
$ cat > scripts/97-smoke-test.sh <<'SCRIPT_EOF'
#!/usr/bin/env bash
# 97 - does it work? Seven checks, each one proving a piece of the cluster. Read what each
# prints; do not just look for the final line. Needs the tunnel and CoreDNS.
source "$(dirname "$0")/../lib.sh"
NS=smoke; IMG_WEB=public.ecr.aws/docker/library/nginx:1.27-alpine; IMG_BOX=public.ecr.aws/docker/library/busybox:1.36
kc delete namespace $NS --ignore-not-found >/dev/null; kc create namespace $NS >/dev/null
trap 'kc delete namespace $NS --wait=false >/dev/null 2>&1; kc delete secret hw-secret --ignore-not-found >/dev/null 2>&1' EXIT

log "1. both workers registered and Ready"
kc get nodes -o wide

log "2. Secrets are encrypted in etcd (look for the k8s:enc:aescbc:v1:key1 prefix)"
kc create secret generic hw-secret --from-literal=password=s3cret >/dev/null
kssh server 'sudo etcdctl get /registry/secrets/default/hw-secret | head -c 80' | cat -v | head -2; echo

log "3. a Deployment spreads Pods over both workers"
kc -n $NS create deployment web --image=$IMG_WEB --replicas=2 >/dev/null
kc -n $NS rollout status deployment/web --timeout=180s
kc -n $NS get pods -o wide

log "4. a Pod on one node reaches a Pod on the other (the routes from 95-routes.sh)"
kc -n $NS run server-pod --image=$IMG_WEB --overrides='{"spec":{"nodeName":"node-0"}}' >/dev/null
kc -n $NS run client-pod --image=$IMG_BOX --overrides='{"spec":{"nodeName":"node-1"}}' -- sleep 3600 >/dev/null
kc -n $NS wait --for=condition=Ready pod/server-pod pod/client-pod --timeout=180s >/dev/null
IP=$(kc -n $NS get pod server-pod -o jsonpath='{.status.podIP}')
echo "server-pod is $IP on node-0; client-pod on node-1 fetches it:"
kc -n $NS exec client-pod -- wget -qO- -T 5 "http://$IP" | head -4

log "5. a Service and cluster DNS"
kc -n $NS expose deployment web --port=80 >/dev/null
kc -n $NS exec client-pod -- nslookup web.$NS.svc.cluster.local | tail -3
kc -n $NS exec client-pod -- wget -qO- -T 5 http://web | head -2

log "6. logs and exec work (the API server calling kubelets: the RBAC rule from 96)"
kc -n $NS logs server-pod | tail -2
kc -n $NS exec server-pod -- hostname

log "7. a NodePort answers on a node"
kc -n $NS expose deployment web --name web-np --type=NodePort --port=80 >/dev/null
NP=$(kc -n $NS get svc web-np -o jsonpath='{.spec.ports[0].nodePort}')
kssh node-0 "curl -s -m 5 localhost:$NP | head -4"
log "all checks ran. Read them above; each one should show a real answer."
SCRIPT_EOF
scripts/99-teardown.sh (57 lines)
$ cat > scripts/99-teardown.sh <<'SCRIPT_EOF'
#!/usr/bin/env bash
# 99 - delete EVERYTHING this guide created, and prove it is gone.
#
# Order matters: AWS will not delete a VPC that still has things in it, so we go from the
# inside out: instances, then routes and gateways, then the group, subnet and VPC, then IAM.
# Everything is found by the Project tag as well as by saved IDs, so it works even if
# .state was lost. It deletes only what carries Project=$PROJECT.
source "$(dirname "$0")/../lib.sh"
confirm_account
[ -x "$(dirname "$0")/96-kubectl.sh" ] && "$(dirname "$0")/96-kubectl.sh" stop || true

tagq() { echo "Name=tag:Project,Values=$PROJECT"; }

log "instances"
IDS=$(aws ec2 describe-instances --filters "$(tagq)" Name=instance-state-name,Values=pending,running,stopping,stopped \
      --query 'Reservations[].Instances[].InstanceId' --output text)
if [ -n "$IDS" ]; then aws ec2 terminate-instances --instance-ids $IDS >/dev/null; aws ec2 wait instance-terminated --instance-ids $IDS; fi

log "key pair"
aws ec2 delete-key-pair --key-name "$PROJECT" 2>/dev/null || true

log "VPC contents"
for VPC in $(aws ec2 describe-vpcs --filters "$(tagq)" --query 'Vpcs[].VpcId' --output text); do
  for A in $(aws ec2 describe-route-tables --filters Name=vpc-id,Values="$VPC" \
            --query 'RouteTables[].Associations[?!Main].RouteTableAssociationId' --output text); do
    aws ec2 disassociate-route-table --association-id "$A"
  done
  for R in $(aws ec2 describe-route-tables --filters Name=vpc-id,Values="$VPC" "$(tagq)" --query 'RouteTables[].RouteTableId' --output text); do
    aws ec2 delete-route-table --route-table-id "$R"
  done
  for G in $(aws ec2 describe-internet-gateways --filters Name=attachment.vpc-id,Values="$VPC" --query 'InternetGateways[].InternetGatewayId' --output text); do
    aws ec2 detach-internet-gateway --internet-gateway-id "$G" --vpc-id "$VPC"; aws ec2 delete-internet-gateway --internet-gateway-id "$G"
  done
  for S in $(aws ec2 describe-subnets --filters Name=vpc-id,Values="$VPC" --query 'Subnets[].SubnetId' --output text); do
    aws ec2 delete-subnet --subnet-id "$S"
  done
  for G in $(aws ec2 describe-security-groups --filters Name=vpc-id,Values="$VPC" "$(tagq)" --query 'SecurityGroups[].GroupId' --output text); do
    aws ec2 delete-security-group --group-id "$G"
  done
  aws ec2 delete-vpc --vpc-id "$VPC"; log "deleted $VPC"
done

log "IAM"
R="$PROJECT-ssm"
aws iam remove-role-from-instance-profile --instance-profile-name "$R" --role-name "$R" 2>/dev/null || true
aws iam delete-instance-profile --instance-profile-name "$R" 2>/dev/null || true
aws iam detach-role-policy --role-name "$R" --policy-arn arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore 2>/dev/null || true
aws iam delete-role --role-name "$R" 2>/dev/null || true

log "local state"
rm -rf "$STATE"/ids.env "$STATE"/ssh "$STATE"/ssh_config "$STATE"/known_hosts

log "proof: anything still tagged Project=$PROJECT?"
LEFT=$(aws resourcegroupstaggingapi get-resources --tag-filters "Key=Project,Values=$PROJECT" \
       --query 'ResourceTagMappingList[].ResourceARN' --output text)
[ -z "$LEFT" ] && log "nothing left. The bill stops here." || { warn "still tagged (some take a minute to vanish):"; echo "$LEFT"; }
aws iam get-role --role-name "$R" >/dev/null 2>&1 && warn "role $R still exists" || log "role gone"
SCRIPT_EOF
scripts/all.sh (15 lines)
$ cat > scripts/all.sh <<'SCRIPT_EOF'
#!/usr/bin/env bash
# Run the whole guide, start to finish, after one confirmation. Handy once you have read the
# individual scripts. Stops at the first failure; re-run a single script to retry a step.
set -euo pipefail
cd "$(dirname "$0")"
source ../lib.sh
confirm_account
export ASSUME_YES=yes
for s in 00-prereqs 10-network 20-compute 40-certs 50-configs 60-push-files 70-etcd 80-control-plane 90-workers 95-routes; do
  log "################ $s"; "./$s.sh"
done
./96-kubectl.sh start
./98-coredns.sh
./97-smoke-test.sh
log "cluster is up. Tear it down with scripts/99-teardown.sh when you are done."
SCRIPT_EOF
$ chmod +x scripts/*.sh remote/*.sh 2>/dev/null; true

2Run it

$ scripts/97-smoke-test.sh
The seven checks
  • Nodes: both workers registered and Ready.
  • Encryption: a Secret in etcd starts with k8s:enc:aescbc:v1:key1:, not the text s3cret.
  • Scheduling: a 2-replica Deployment lands on both workers.
  • Pod network: a Pod on node-1 fetches a page from a Pod on node-0. This is the check for the routes.
  • Services and DNS: nslookup and wget a Service by name.
  • API to kubelet: logs and exec work. This is the RBAC rule.
  • NodePort: a node answers on its NodePort. This is kube-proxy.

Read each section as it prints. A passing run shows a real answer for every check, not just a last line.

3Tear it down

$ scripts/99-teardown.sh        # asks to confirm, deletes, then checks
...
==> proof: anything still tagged Project=k8s-hard-way?
==> nothing left. The bill stops here.
What teardown does
  • Terminates the instances, deletes the key pair, then works from the inside out: routes, gateway, subnet, security group, VPC, then IAM.
  • It finds things by the Project tag as well as saved IDs, so it works even if you lost .state.
  • The last step asks the tagging API whether anything is still tagged, and checks the IAM role is gone. That is the proof.
Remember

Build it, check it with evidence, and delete it with proof. A cluster you cannot tear down cleanly is a cost you cannot see.

Dig deeper with your AI

Guess first, then click for the TL;DR. Ask your AI for the full answer, and check it on your cluster or with kubectl explain.

  • What would a production cluster do differently?

    Three control plane nodes in three zones, a three-member etcd, a load balancer in front of the API, a real CNI, automatic certificate rotation, and managed upgrades.

  • What is the difference between this and kubeadm?

    kubeadm automates exactly these steps: certificates, kubeconfigs, static Pods for the control plane, and joining workers. Doing it by hand first shows you what it does.

  • What would you add next?

    A second and third control plane node with a real etcd cluster, then a real CNI plugin, then an upgrade from 1.37 to the next version by hand.

Inspired by Kubernetes The Hard Way. Text and scripts: CC BY-NC-SA 4.0.