Kubernetes Management
Essential kubectl commands, k9s terminal UI, and YAML manifest examples for managing Kubernetes resources including Pods, Deployments, Services, ConfigMaps, Secrets, Jobs, and CronJobs.
A few main commands to interact with clusters:
- Create resources:
kubectl create secret generic <name> --from-literal=key=valCopied,kubectl create configmap <name> --from-literal=key=valCopied - Get resources:
kubectl get podsCopied,kubectl get servicesCopied,kubectl get deploymentsCopied - Describe resources:
kubectl describe pod <pod-name>Copied(shows detailed events and state) - Logs:
kubectl logs <pod-name> -fCopied(follow logs) - Execute command:
kubectl exec -it <pod-name> -- /bin/bashCopied - Apply manifest:
kubectl apply -f <filename>.yamlCopied - Delete resource:
kubectl delete -f <filename>.yamlCopiedorkubectl delete pod <pod-name>Copied - Context/Namespace:
kubectl config get-contextsCopied,kubectl config use-context <name>Copied,kubectl get pods -n <namespace>Copied
k9s: Terminal UI for Kubernetes
For a more productive experience, I highly recommend using k9s. It provides a terminal-based UI to manage your clusters, view logs, shell into pods, and more, without typing long kubectl commands.
Manifest Examples
Secret
Used to store sensitive data like passwords or tokens.
yamlapiVersion: v1 kind: Secret metadata: name: my-secret type: Opaque data: username: YWRtaW4= # base64 for "admin" password: bXlwYXNzd29yZA== # base64 for "mypassword"
ConfigMap
Used for non-sensitive configuration data.
yamlapiVersion: v1 kind: ConfigMap metadata: name: app-config data: APP_COLOR: 'blue' API_URL: 'https://api.example.com'
Pod
The smallest deployable unit. Usually managed by Deployments.
yamlapiVersion: v1 kind: Pod metadata: name: nginx-pod labels: app: nginx spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80
Deployment
Manages a set of identical Pods, providing declarative updates and scaling.
yamlapiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.21 ports: - containerPort: 80
Service (ClusterIP)
Exposes the Pods on an internal IP within the cluster.
yamlapiVersion: v1 kind: Service metadata: name: nginx-internal spec: selector: app: nginx ports: - protocol: TCP port: 80 targetPort: 80 type: ClusterIP
Service (LoadBalancer)
Exposes the Service externally using a cloud provider's load balancer.
yamlapiVersion: v1 kind: Service metadata: name: nginx-external spec: selector: app: nginx ports: - protocol: TCP port: 80 targetPort: 80 type: LoadBalancer
Job
Creates one or more Pods and ensures that a specified number of them successfully terminate.
yamlapiVersion: batch/v1 kind: Job metadata: name: hello-job spec: template: spec: containers: - name: hello image: busybox command: ['echo', 'Hello Kubernetes Job!'] restartPolicy: Never backoffLimit: 4
CronJob
Runs Jobs on a time-based schedule.
yamlapiVersion: batch/v1 kind: CronJob metadata: name: hello-cronjob spec: schedule: '*/1 * * * *' jobTemplate: spec: template: spec: containers: - name: hello image: busybox command: ['echo', 'Hello from CronJob!'] restartPolicy: OnFailure