Hands-On Lab
Now it's time to put theory into practice! In this lab, we'll combine the concepts covered in this chapter to build our first functional Go program that interacts with your Kubernetes cluster.
Objectives:
Create a Go application that connects to your Kubernetes cluster using your local
kubeconfig
file (out-of-cluster authentication).Use the
client-go
clientset to list all Pods within a specific namespace (we'll usedefault
for simplicity, but you can modify this). For each Pod, print its Name and assigned Pod IP address.Extend the program to also list all Nodes in the cluster. For each Node, print its Name and its Internal and External IP addresses (if available).
Incorporate basic error handling to manage potential issues during connection or API calls.
Tasks:
Navigate to the Code: Find the complete Go source code for this lab within the companion GitHub repository. The code specifically for this chapter's lab is located at:
Congratulations! You've successfully built and run your first Go application that communicates directly with the Kubernetes API using client-go
. You fetched real-time information about Pods and Nodes, laying the foundation for the more complex networking interactions we'll explore in the coming chapters.
Explore Further (Optional):
Try modifying the code to list Pods in a different namespace (e.g.,
kube-system
). Remember you might need permissions for that namespace.Experiment with
metav1.ListOptions
to filter Pods by a label selector (you'll need Pods with labels first!). We'll cover selectors in more detail later.
Last updated
Was this helpful?