Hands-On Lab
Hands-On Lab: Inspecting Pod Identity and Using Selectors
Let's put the concepts from Chapter 2 into action! In this lab, we'll build a Go tool that goes beyond simply listing Pods. We'll fetch specific details for a single Pod and learn how to leverage labels and selectors to find groups of related Pods – a core technique for interacting with Kubernetes networking components.
Objectives:
Develop a Go command-line tool that connects to your Kubernetes cluster using
kubeconfig
.Implement functionality to retrieve a specific Pod by name and namespace, outputting key details:
Pod IP (
status.podIP
)Node Name (
spec.nodeName
)Host IP (
status.hostIP
)Container Ports defined in the spec (
spec.containers[*].ports
)Pod Readiness Status (
status.conditions
->PodReady
)
Implement separate functionality (or a mode in the same tool) to list all Pods matching a given label selector (e.g.,
app=my-backend
). For each matching Pod, print its Name, Namespace, and IP.Utilize
k8serrors.IsNotFound
for graceful handling when a specific Pod is requested but doesn't exist.
Tasks:
Navigate to the Code: The complete Go source code for this lab is available in the companion GitHub repository. Locate the code specific to this chapter's exercises:
Congratulations! You've now built a Go tool capable of targeted Pod inspection and selection using labels. You can extract vital network identity information and find groups of Pods programmatically, skills that are essential for understanding and manipulating Kubernetes Service endpoints and Network Policies in later chapters.
Explore Further (Optional):
Modify the tool to accept multiple label selectors (e.g.,
--selector "app=X" --selector "tier=Y"
). Remember selectors are combined with AND logic.Enhance the output to show more details for Pods listed by selector (e.g., their readiness status).
Try implementing set-based selectors (
environment in (prod, staging)
).
Last updated
Was this helpful?