Visualizing K8s service interactions
An old peeve of mine was to be able to see the interactions among services in a kubernetes cluster nee namespace. I stumbled across Otterize and their wonderful network-mapper tool. It will give you a list of all the services and what they call. It will even create a PNG map for you. Alas a huge PNG file with itty-bitty letters in the legends. Whereas I am thankful for this tool, I wanted something sexier.
I remember some time ago I had used mermaid files to create SVG graphs. I took another look into otterize's output and it seems to me that it was screaming to become a mermaid. So I helped it out. This small script will take Otterize's output , convert it to mermaid format ( a clumsy one to be frank), and proceeds to create an SVG file. You can use a web browser to view your SVG file.
#!/bin/bash
#
# script to convert otterize output to mermaid and then svg
# Angelos Karageorgiou - Jan 2 2024
#
TOSKIP="^skipme"
namespace=$(kubectl config view --minify -o jsonpath='{..namespace}')
(
echo "flowchart LR"
otterize -n $namespace network-mapper list | while read line
do
# beginning of a section
found=$(echo "$line" | grep 'calls:$')
if [[ $? -eq 0 ]]
then
source=$(echo $line | sed -e "s/ in namespace $namespace calls://g")
continue
fi
# the service that is called
dest=$(echo $line | sed -e 's/^-//g' -e "s/ in namespace $namespace//g")
echo "$source --> $dest" | grep -v $TOSKIP
done
) > /tmp/$namespace.maid
# generate the svg now
mmdc -i /tmp/$namespace.maid -o /tmp/$namespace.svg
ls /tmp/$namespace*
It has a couple of dependencies
领英推荐
npm install @mermaid-js/mermaid-cli
sudo apt install puppeteer chromium-browser
Happy new year everybody!