» Post Updates
- 2023-03-22: Added bootstrap manifest.
- 2022-09-21: Added ephemeral debug containers.
- 2022-02-23: Added contexts and Service FQDN.
- 2022-01-24: Added logs with
stern
section. - 2021-12-01: Added Wireshark section.
- 2021-10-09: Added Kubescape section.
- 2021-04-21: Replaced
ctr
withcrictl
for finding the PID.
» Switching Contexts
Credits to Sarasa Gunawardhana
List all contexts:
|
|
Get current context:
|
|
Switch context:
|
|
» Service FQDN
Inside the cluster it’s <SERVICE>.<NAMESPACE>.svc.cluster.local
.
» Get logs from all pods of a specific namespace
Using stern:
|
|
» Start a fresh container
Handy for some quick testing.
Run a new container and get to the shell. As soon as you exit the container it will be removed from the cluster:
|
|
Run a pod but won’t get you into the shell immediately. Will stay until it crashes or you delete the pod:
|
|
Will deploy a container which will stay until you delete the deployment:
|
|
» Bootstrap manifest
When you want to bootstrap a manifest file, add --dry-run=client --output=yaml
to the corresponding kubectl create
command.
For example:
|
|
or
|
|
Unfortunately, the output contains more fields then required, such as an empty status
object for the deployment.
» Ephemeral debug containers
Creating a debug container (documentation) :
|
|
This might raise the following warning and the debug container won’t open the shell:
|
|
Adjust the securityContext
of the deployment to allow running as root (warning: this will recreate the pod (the stuff you are looking for might get lost) and should in general only be done for debugging reasons on non-prod environments)
|
|
» Run Wireshark on the pods network
This requires tcpdump
in the container and wireshark
on the host which is running the kubectl
command.
When tcpdump
can not be installed in the container, you can try using an ephemeral debug container (not yet tested by me).
|
|
For example:
|
|
or:
|
|
» Enter container namespace from cluster node
You can enter the namespace of your running containers from the cluster node which is running the container, thus, you first have to login/ssh into the cluster node.
One use case for doing this might be capturing traffic (e.g. with tcpdump
) of the given container.
Depending on the container runtime of your cluster, you might need the Docker
or the containerd
approach.
While the runtime specific sections show you an alternative approch of listing the container id, you can also get this value with good old kubectl
:
|
|
» Using Docker
Get the container ID (alternative):
|
|
Get the pid:
|
|
Enter the namespace:
Adjust nsenter
with the namespaces you need. For example, when you want to capture the network traffic, use --net
:
|
|
» Using crictl
Get the container ID (alternative):
|
|
Get the pid:
|
|
This will give you an output similar to the one below where the first entry (4921) represents the pid we are looking for.
|
|
Enter the namespace (again, adjust the namespaces as required):
|
|
» Run Security Checks
Test if there are any security flaws with your cluster.
» kube-bench
Start the pod which will run the checks:
|
|
Wait a moment and then check the logs:
|
|
» kube-hunter
» Outside the cluster
This will give you a limited view from the outside of the cluster.
|
|
For a more detailed analysis, run the container inside the cluster.
» Inside the cluster
Run the pod:
|
|
Wait a moment and then check the logs:
|
|
» kubescape
Kubescape is another tool which scans the cluster for security risks. The checks are based on the Kubernetes hardening guideline from the NSA and CISA.
First, install the client on your local machine as described here. Then run the scan, for example:
|
|
» Other useful project and links
- kubedump, a tool for dumping manifests from your Kubernetes clusters.
- Liveness Probes are Dangerous
- GOMAXPROCS and GOMEMLIMIT in Kubernetes