Create the various debug or test pod inside kubernetes cluster
Sometimes we requires a running kubernetes pods to debug or test out few things such as:
- Checking DB connection, data
- Verifying the network connectivity
- Testing internal or private endpoints(accessible within cluster only)
- Verifying the state of the kubernetes cluster
- Mounting the volumes to verify storage is working as expected
This article demonstrates quick and handy examples for how to create various debug or test pods inside the k8s cluster.
Fo example most of the times we need the following:
- create a debug pod and run curl or telnet or dig.
- create a pod with running postgres sql client.
- create a pod with running mssql client
- create a pod with running mongodb client
- create a pod with running redis-cli
1. create a debug pod and run curl or telnet or dig
This is a vey common thing for any k8s cluster admin to create a pod with running curl command and to check the db, network connectivity or test the internal endpoint which is accessible within the cluster only.
a. pod with alpine image
kubectl run -it --tty --rm debug --image=alpine --restart=Never -- sh -n <namespace>
It will exec into the pod with ssh. Next install curl with apk add curl
and we are good to go. Similarly you can install the telnet with apk update && apk add busybox-extras
, nslookup and dig with apk update && apk add bind-tools
It will create a ephemeral pod, as soon as you are done with your debug or test pressing control + D
will pods "debug" deleted
b. pod with ubuntu or debian image
kubectl run ubuntu --image=ubuntu:latest --command sleep 604800 -n <namespace>
It will create ubuntu pod into which you can exec with kubectl exec -it pods/ubuntu -n <namespace> -- sh
and run apt update && apt install curl dns-utils && telnet
to install curl, nslookup or dig and telnet respectively. As soon as you are done with your debug or test you can delete with kubectl delete pods/ubuntu —n <namespace>
2. create a pod with running postgres sql client
a. create a postgres client pod with kubectl run
kubectl run pgsql-client --rm --tty -i --restart='Never' --namespace dev --image docker.io/bitnami/postgresql --env="PGPASSWORD=<password>" --command -- psql --host <db-host> -U <user> -d <db-name> -p 5432
It will enter you into pssql-client console where you can execute the query against selected db or run other postgres-sql command
b. create a postgres client pod with deployment
Sometime there may be need to create full fledged k8s deployment this can be done through the below gist. You can also specify nodeName where you want to create the deployment and running for for postgres shell or client.
Click on view raw and runkubectl apply -f <gist raw file link> -n <namespace>
once deployment created you can exec into pod and run psql -U <username> -h <db-host> -p 5432 <db-name>
After your testing or debugging it can be deleted by kubectl delete -f <gist raw file link> -n <namespace>
3. create a pod with running mssql client
Apply following gist by clicking on view raw and run kubectl apply -f <gist raw file link> -n <namespace>
Once deployment created you can exec into pod and run sqlcmd -S <db-host>,1433 -U <username> -P <password> -d <db-name> -Q "SELECT * FROM <table_name>"
After your testing or debugging it can be deleted by kubectl delete -f <gist raw file link> -n <namespace>
If the above gist did not work — creating pod and exec in did not work then we can follow the instructions for microsoft documentation to install mssql-command line tools:
- Create a ubuntu pod
kubectl run ubuntu --image=ubuntu:latest --command sleep 604800 -n <namespace>
- Install the pre requisite tools into ubuntu contains
kubectl exec -it pods/ubuntu -n <namespace> - sh
apt update -y && apt install curl -y && apt install sudo -y
- Then run the instructions from the microsoft documentation for ubuntu
- Run the sql query on the exec container shell
sqlcmd -S <DB_HOST>,<DB_PORT e.g. 1433> -U <DB_USER> -P <password> -C -d messaging -Q "<sql query>";
4. create a pod with running mongodb client
Apply following gist by clicking on view raw and run kubectl apply -f <gist raw file link> -n <namespace>
Once deployment created you can exec into pod and run mongo --ssl
Don’t specify
--host <mongohost>:27010 --sslCAFile <ca-cert.pem> --username <username>. --paasword--ssl
option and --sslCAFile
if your mongo db server won’t have SSL enable. After your testing or debugging it can be deleted by kubectl delete -f <gist raw file link> -n <namespace>
5. create a pod with running redis-cli
Apply following gist by clicking on view raw and run kubectl apply -f <gist raw file link> -n <namespace>
Once deployment created you can exec into redis-cli pod and connect to remote redis server. After your work it can be deleted by kubectl delete -f <gist raw file link> -n <namespace>
Thanks for reading till end. Feel free to add your comment. I would be happy that if any one of the above could help you to test or debug your db connectivity, check data or network connectivity issue inside your kubernetes cluster 😅