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:
kubectl exec -it <pod-name> -n <namespace> -- /bin/sh
b) Managing Resources
Apply a configuration file:
kubectl apply -f <file.yaml>
Delete a resource:
kubectl delete pod <pod-name> -n <namespace>
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:
kubectl get svc <service-name> -n <namespace>
Deploy an Ingress resource to route traffic:
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: example-ingress namespace: default spec: rules: - host: example.com http: paths: - path: / pathType: Prefix backend: service: name: example-service port: number: 80
Apply the ingress configuration:
kubectl apply -f ingress.yaml
3. Port Forwarding
Port forwarding allows direct access to Kubernetes services running inside the cluster.
kubectl port-forward svc/<service-name> 8080:80 -n <namespace>Then, access it locally via:
http://localhost:80804. Accessing Kubernetes API Directly
The Kubernetes API allows programmatic access to resources.
Retrieve cluster API endpoint:
kubectl cluster-info
Use curl to interact with the API:
curl -k -H "Authorization: Bearer $(kubectl get secret <secret-name> -o jsonpath='{.data.token}' | base64 --decode)" <api-endpoint>
5. Using a Bastion Host
For private clusters, a bastion/jump host may be required:
ssh -L 6443:<api-server-ip>:6443 user@<bastion-host>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: Ensure kubectl 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.