A few main commands to interact with clusters:
kubectl create secret generic <name> --from-literal=key=valCopied!, kubectl create configmap <name> --from-literal=key=valCopied!kubectl get podsCopied!, kubectl get servicesCopied!, kubectl get deploymentsCopied!kubectl describe pod <pod-name>Copied! (shows detailed events and state)kubectl logs <pod-name> -fCopied! (follow logs)kubectl exec -it <pod-name> -- /bin/bashCopied!kubectl apply -f <filename>.yamlCopied!kubectl delete -f <filename>.yamlCopied! or kubectl delete pod <pod-name>Copied!kubectl config get-contextsCopied!, kubectl config use-context <name>Copied!,
kubectl get pods -n <namespace>Copied!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.
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"
Used for non-sensitive configuration data.
yamlapiVersion: v1 kind: ConfigMap metadata: name: app-config data: APP_COLOR: 'blue' API_URL: 'https://api.example.com'
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
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
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
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
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
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