Skip to content

99.2. Kubectl Cheat Sheet

Daniel Trolezi edited this page Sep 16, 2024 · 3 revisions

Commands

Cluster info

kubectl cluster-info

List all pods from default namespace

kubectl get pods

List all namespaces

kubectl get pods --all-namespaces

List pods from namespace

kubectl get pods -n <namespace>

List all services namespaces

kubectl get service --all-namespaces

Run shell from inside the pod

kubectl exec -it <pod> sh

or

kubectl exec <pod> -- sh

Run curl from inside the pod

kubectl exec -it <pod> -- curl -v google.com

Check pod's IP

kubectl get pod <pod> -o custom-columns=NAME:metadata.name,IP:status.podIP

Create new namespace

kubectl create namespace <namespace>

Apply the deployment manifest

kubectl apply -f deploymemt.yaml

Example of deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: eks-sample-linux-deployment
  namespace: eks-sample-app
  labels:
    app: eks-sample-linux-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: eks-sample-linux-app
  template:
    metadata:
      labels:
        app: eks-sample-linux-app
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/arch
                operator: In
                values:
                - amd64
                - arm64
      containers:
      - name: nginx
        image: public.ecr.aws/nginx/nginx:1.23
        ports:
        - name: http
          containerPort: 80
        imagePullPolicy: IfNotPresent
      nodeSelector:
        kubernetes.io/os: linux

List all deployments

kubectl get deployments -n <namespace>

Delete deployment

kubectl delete -n <namespace> deployment <deployment>

Delete pod

kubectl delete <pod> -n <namespace>

Reduce number of CoreDNS replicas:

kubectl scale deployment coredns -n kube-system --replicas=1

Debug & Logs

Additional Packages

  • kubectx: Tool that can switch between kubectl contexts easily and create aliases

References