Kubectl by Marcos Benevides

:ID: ee377537-0c8d-48c9-a181-19a2cff8c4a7

Commands

  kubectl cluster-info

Apply & Create

  kubectl create ...
  kubectl apply -f pod.yml

Delete

Deleting a pod won’t prevent it from being recreated.

  kubectl delete pod [pod-name]

you need to delete it’s deployment.

  kubectl delete deployment [deployment-name]

Describe

  kubectl describe pods <...>

Get

  kubectl get all
  kubectl get pods --watch
  kubectl get pods -o wide
  # Lists deployments and their labels
  kubectl get deployment --show-labels
  # Get deployments with a specific label
  kubectl get deployment -l app=nginx

Port-forward

  kubectl port-forward [pod-name] [external-port]:[internal-port]
  # For instance...
  kubectl port-forward [pod-name] 8080:80

YAML

Pod

  apiVersion: v1
  kind: Pod
  metadata:
    name: nginx
    labels:
      app: nginx
      ref: stable
  spec:
    containers:
    - name: nginx
      image: nginx:alpine
      ports:
      - containerPort: 80

Pod Healthchecks

  apiVersion: v1
  kind: Pod
  metadata:
    name: nginx
    labels:
      app: nginx
      ref: stable
  spec:
    containers:
    - name: nginx
      image: nginx:alpine
      ports:
      - containerPort: 80
      livenessProbe:
        httpGet:
          path: /index.html
          port: 80
        initialDelaySeconds: 15
        timeoutSeconds: 2
        periodSeconds: 5
        failureThreshold: 1
      readinessProbe:
        httpGet:
          path: /index.html
          port: 80
        initialDelaySeconds: 5
        periodSeconds: 10
        failureThreshold: 1
      resources:
        limits:
          memory: "128Mi" # 128 Mb
          cpu: "200m"     # 200 milicpu, i.e, 0.2 cpu or 20% cpu

Links to this note