Add plugins to Kubectl with Krew and make graphs with Neo4J

If you work on Kubernetes you might be accustomed to kubectl. It is a powerful tool to manage Kubernetes. You might also have developer/administrator control planes that give you nice visuals of how your clusters work. Or you might not have that. If you do not have a way to visualize your cluster in this article I will show you how to visualize your cluster with a kubectl plugin.

Alright, first we need to update kubectl with a great plugin called Krew. Install it into your OS and shell of choice. Once installed you can run kubectl krew update to get the latest plugins. Likely you won’t have any to start.

Graphing Kubernetes Resources

Ok, now this is the fun part.

kubectl krew install graph

Once installed you can start creating graphs from a namespace of your Kubernetes cluster.

kubectl graph all -n mynamespace -o cypher 

What this will do is generate the graph cypher language that Neo4J understands to create a visualization. To visualize this information the easiest is to sign up for an AuraDB Neo4J database.

Once you sign up and create your free database you can add the username and password to your environment variables. I’ll skip it for now. If you must add -u and -p parameters to cypher shell. You will also want to install cypher shell depending on your OS there might be a variety of ways to do this.

kubectl graph all -n nynamespace -o cypher | cypher-shell neo4j+s://

Once you run this command, you can access the Neo4J browser to see the generated graph of your namespace. Login to the Neo4J browser and run the cypher command to return all nodes and relationships.

Match (N) return N

Kubernetes Namespace Graph

Use the cypher below to delete all the nodes and relationships from your graph to clean things up.