This guide explains how to set up, test, and visualize the system that integrates with Kubernetes, processes resources, and visualizes relationships between them as a graph.
The system connects to a Kubernetes cluster to:
- Fetch resources, such as Pods, Deployments, Services, and ConfigMaps.
- Construct relationships between resources (e.g., Deployments managing Pods, Services targeting Pods).
- Generate a graph representation as a versioned JSON output.
- Visualize the relationships using a graph tool.
Testing this system involves:
- Setting up a Kubernetes cluster.
- Running the system.
- Adding and modifying Kubernetes resources within the cluster.
- Visualizing and validating the graph.
- Python: 3.8+ installed.
- Install required Python dependencies with:
pip install -r requirements.txt- Install and start Minikube: Follow the official guide: Minikube Installation.
- Install kubectl: Follow the official guide: Kubectl Installation.
Run the following command to start the Minikube Kubernetes cluster:
minikube startVerify that the cluster is running:
kubectl get nodes- Navigate to your project’s directory.
- Run the main system script:
python main.pyWhat happens:
- The system sets up the Kubernetes API client and initializes its database.
- It fetches all existing Kubernetes resources and their relationships.
- The system generates a versioned graph file in the
graph_outputdirectory and monitors the cluster for any dynamic changes.
Manually add Kubernetes resources by creating and applying YAML files.
- Create a deployment to run an Nginx container.
- Save the YAML to a file (e.g.,
nginx-deployment.yaml) and apply it:
kubectl apply -f nginx-deployment.yaml- Write a configuration file as a ConfigMap.
- Apply it:
kubectl apply -f example-configmap.yaml- Create a service to expose a Deployment (or Pods).
- Apply it to the cluster:
kubectl apply -f nginx-service.yamlExpected Outcome:
The system’s watcher will detect the newly added resources, update the database, and generate an updated graph in the graph_output directory.
- Open the latest graph file in the
graph_outputdirectory (e.g.,graph_OUTPUT_<timestamp>.json). - Use a graph visualization script or external tools to render the graph.
Run the relationship_visualizer.py script:
python relationship_visualizer.pyWhat happens:
- The emitted graph file is read and visualized.
- Nodes represent Kubernetes resources (e.g., Pods, Deployments) with color coding by resource type.
- Edges represent the relationships (e.g., a Deployment controlling Pods).
- Modify or scale an existing resource to test dynamic behavior:
- Scale a Deployment:
kubectl scale deployment nginx-deployment --replicas=5- Modify or Add a ConfigMap: Create and apply:
kubectl apply -f another-configmap.yaml- Check the
graph_outputdirectory for the updated graph output. The relationships and newly added resources should now appear.
- A graph is generated containing all current Kubernetes resources and their relationships.
- New resources (e.g., deployments, services, ConfigMaps) are detected and added as nodes in the graph.
- Relationships between resources (e.g., Service → Pod) are updated.
- Changes to resources (e.g., scaling replicas or creating additional resources) are reflected in a new version of the graph output.
- Graph renders with:
- Nodes color-coded based on resource type (Pods, Deployments, Services, etc.).
- Edges representing the relationships.