Skip to content

Instantly share code, notes, and snippets.

@alanbchristie
Created May 27, 2026 11:14
Show Gist options
  • Select an option

  • Save alanbchristie/88dda74d1bb65fe1969b7801ea30e7ab to your computer and use it in GitHub Desktop.

Select an option

Save alanbchristie/88dda74d1bb65fe1969b7801ea30e7ab to your computer and use it in GitHub Desktop.
K8s port forward (to neo4j graph)
# Given a the command-line utility kubectl,
# a suitable KUBECONFIG file, and the graph password,
# we can port-forward the bolt interface of the deployed graph to our local machine.
#
# You will need: -
#
# - kubectl
# - A KUBECONFIG file
# - The graph password
#
# You can then connect to the Graph's bolt service using port forwarding,
# which connects a remote port (in our cluster) to your local machine.
# Given a KUBECONFIG file (e.g. my-kubeconfig.yaml) this command will
# connect your machine to the graph: -
kubectl port-forward service/graph 7687 -n graph-x --kubeconfig my-kubeconfig.yaml
# This should confirm the connection with something like...
Forwarding from 127.0.0.1:7687 -> 7687
Forwarding from [::1]:7687 -> 7687
# The kubectl command will block so you'll need a 2nd terminal.
#
# In another terminal, from a Python environment,
# you can then install the neo4j package, and run cypher queries
# against the graph. Here I'm counting the number of F2 nodes in the graph: -
python -m venv venv
source venv/bin/activate
pip install neo4j-driver==4.4.11
python
>>> from neo4j import GraphDatabase
>>> GRAPH_PASSWORD='SECRET'
>>> auth=('neo4j', GRAPH_PASSWORD)
>>> driver = GraphDatabase.driver("bolt://localhost:7687", auth=auth)
>>> with driver.session() as session:
records = session.run('match (n:F2) return count (n);')
for record in records:
print(record)
<Record count (n)=1432327818>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment