How to Connect to Kubernetes (K8s) Resources
This document outlines various ways to connect to and interact with Kubernetes (K8s) resources. Kubernetes resources include pods, services, deployments, config maps, secrets, and more. Accessing these resources depends on the context, such as whether you are interacting from within the cluster, from an internal network, or externally.
Prerequisites
kubectl
installed and configured.Access to the Kubernetes cluster via
kubeconfig
.Permissions to interact with the required resources.
Optional: Helm installed for managing applications.
Ways to Access Kubernetes Resources
There are multiple methods to access Kubernetes resources, each suited for different use cases:
1. Using kubectl
kubectl
is the primary CLI tool for interacting with Kubernetes resources.
a) View Resources
List all pods:
kubectl get pods -n <namespace>
Describe a specific pod:
kubectl describe pod <pod-name> -n <namespace>
Get logs from a pod:
kubectl logs <pod-name> -n <namespace>
Execute commands inside a running pod:
b) Managing Resources
Apply a configuration file:
Delete a resource:
2. Using a LoadBalancer or Ingress Controller
For accessing services externally, you may need a LoadBalancer or an Ingress resource.
Get the external IP of a LoadBalancer service:
Deploy an Ingress resource to route traffic:
Apply the ingress configuration:
3. Port Forwarding
Port forwarding allows direct access to Kubernetes services running inside the cluster.
Then, access it locally via:
4. Accessing Kubernetes API Directly
The Kubernetes API allows programmatic access to resources.
Retrieve cluster API endpoint:
Use
curl
to interact with the API:
5. Using a Bastion Host
For private clusters, a bastion/jump host may be required:
Then, set kubectl
to use https://localhost:6443
.
Common Kubernetes Resources
Pods: The smallest deployable units.
Services: Expose applications running on pods.
Deployments: Manage replica sets and rollouts.
ConfigMaps & Secrets: Store configuration and sensitive information.
PersistentVolumes & PersistentVolumeClaims: Manage storage.
Namespaces: Isolate resources within a cluster.
Ingress: Route external traffic into the cluster.
Troubleshooting
kubectl
command fails: Ensurekubectl
is authenticated and has access to the cluster.Service not accessible: Check service type (ClusterIP, NodePort, LoadBalancer) and firewall rules.
Permission denied errors: Verify your role-based access control (RBAC) settings.