logo
eng-flag

Kubernetes Cheatsheet

Table of Contents

  1. Kubernetes Installation
  2. Basic Kubernetes Commands
  3. Working with Pods
  4. Deployments
  5. Services
  6. ConfigMaps and Secrets
  7. Namespaces
  8. Persistent Volumes
  9. StatefulSets
  10. DaemonSets
  11. Jobs and CronJobs
  12. RBAC (Role-Based Access Control)
  13. Helm
  14. Monitoring and Logging
  15. Troubleshooting

Kubernetes Installation

Install Minikube (for local development)

curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube

Install kubectl

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

Start Minikube

minikube start

Basic Kubernetes Commands

Check cluster info

kubectl cluster-info

Get node information

kubectl get nodes

Get all resources in all namespaces

kubectl get all --all-namespaces

Get detailed information about a specific resource

kubectl describe <resource_type> <resource_name>

Example:

kubectl describe pod my-pod

Working with Pods

Create a pod from a YAML file

kubectl apply -f pod.yaml

List all pods

kubectl get pods

Get detailed information about a specific pod

kubectl describe pod <pod_name>

Delete a pod

kubectl delete pod <pod_name>

Execute a command in a running pod

kubectl exec -it <pod_name> -- <command>

Example:

kubectl exec -it my-pod -- /bin/bash

View pod logs

kubectl logs <pod_name>

Deployments

Create a deployment

kubectl create deployment <deployment_name> --image=<image_name>

List all deployments

kubectl get deployments

Update a deployment

kubectl set image deployment/<deployment_name> <container_name>=<new_image>

Scale a deployment

kubectl scale deployment <deployment_name> --replicas=<number_of_replicas>

Delete a deployment

kubectl delete deployment <deployment_name>

Services

Create a service

kubectl expose deployment <deployment_name> --type=<service_type> --port=<port>

List all services

kubectl get services

Get detailed information about a specific service

kubectl describe service <service_name>

Delete a service

kubectl delete service <service_name>

ConfigMaps and Secrets

Create a ConfigMap

kubectl create configmap <configmap_name> --from-file=<path_to_file>

Create a Secret

kubectl create secret generic <secret_name> --from-literal=<key>=<value>

List ConfigMaps

kubectl get configmaps

List Secrets

kubectl get secrets

Namespaces

Create a namespace

kubectl create namespace <namespace_name>

List all namespaces

kubectl get namespaces

Set the current namespace

kubectl config set-context --current --namespace=<namespace_name>

Persistent Volumes

Create a PersistentVolume

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: manual
  nfs:
    server: nfs-server.default.svc.cluster.local
    path: "/path/to/data"

Create a PersistentVolumeClaim

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: manual

List PersistentVolumes

kubectl get pv

List PersistentVolumeClaims

kubectl get pvc

StatefulSets

Create a StatefulSet

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  serviceName: "nginx"
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

List StatefulSets

kubectl get statefulsets

Scale a StatefulSet

kubectl scale statefulset <statefulset_name> --replicas=<number_of_replicas>

DaemonSets

Create a DaemonSet

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd-elasticsearch
  namespace: kube-system
  labels:
    k8s-app: fluentd-logging
spec:
  selector:
    matchLabels:
      name: fluentd-elasticsearch
  template:
    metadata:
      labels:
        name: fluentd-elasticsearch
    spec:
      containers:
      - name: fluentd-elasticsearch
        image: quay.io/fluentd_elasticsearch/fluentd:v2.5.2

List DaemonSets

kubectl get daemonsets

Jobs and CronJobs

Create a Job

apiVersion: batch/v1
kind: Job
metadata:
  name: pi
spec:
  template:
    spec:
      containers:
      - name: pi
        image: perl
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never
  backoffLimit: 4

Create a CronJob

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure

List Jobs

kubectl get jobs

List CronJobs

kubectl get cronjobs

RBAC (Role-Based Access Control)

Create a Role

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: default
  name: pod-reader
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "watch", "list"]

Create a RoleBinding

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: read-pods
  namespace: default
subjects:
- kind: User
  name: jane
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: pod-reader
  apiGroup: rbac.authorization.k8s.io

List Roles

kubectl get roles

List RoleBindings

kubectl get rolebindings

Helm

Install Helm

curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash

Add a Helm repository

helm repo add stable https://charts.helm.sh/stable

Search for a Helm chart

helm search repo <chart_name>

Install a Helm chart

helm install <release_name> <chart_name>

List installed Helm releases

helm list

Upgrade a Helm release

helm upgrade <release_name> <chart_name>

Uninstall a Helm release

helm uninstall <release_name>

Monitoring and Logging

Deploy Prometheus and Grafana using Helm

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm install prometheus prometheus-community/kube-prometheus-stack

Access Grafana dashboard

kubectl port-forward deployment/prometheus-grafana 3000

Deploy ELK stack for logging

kubectl apply -f https://raw.githubusercontent.com/elastic/cloud-on-k8s/master/config/samples/elasticsearch/elasticsearch.yaml
kubectl apply -f https://raw.githubusercontent.com/elastic/cloud-on-k8s/master/config/samples/kibana/kibana.yaml
kubectl apply -f https://raw.githubusercontent.com/elastic/cloud-on-k8s/master/config/samples/beats/filebeat_autodiscover.yaml

Troubleshooting

Check pod status

kubectl get pods
kubectl describe pod <pod_name>

View pod logs

kubectl logs <pod_name>
kubectl logs <pod_name> -c <container_name>  # For multi-container pods

Check node status

kubectl get nodes
kubectl describe node <node_name>

Check cluster events

kubectl get events --sort-by=.metadata.creationTimestamp

Check resource usage

kubectl top nodes
kubectl top pods

Debug with a temporary pod

kubectl run -it --rm debug --image=busybox --restart=Never -- /bin/sh

Port forward to a pod

kubectl port-forward <pod_name> <local_port>:<pod_port>

2024 © All rights reserved - buraxta.com