Skip to main content

Commands

k run nginx --image nginx --env="APP_COLOR=green" --labels="webapp-color=true"
k describe pod newpods-nplrh
k cluster-info
k get nodes
k get pods -A -o wide|yaml|json|name --show-labels --selector app=x > pod-definition.yaml
k get pod podname

Exec into pod

k exec my-pod -- df -h | grep secret
k exec -it my-pod -- /bin/sh # Most images lack bash

Kubectl Config

k config get-clusters
k config view # --minify --raw
k config current-context
k config get-contexts
k config use-context name
k config set-context --current --namespace app-space
k config set-contexts docker-desktop --namespace=my-namespace # to modify context not to make it current

# Modify existing context or create one
k config set-context my-context --cluster=my-cluster --user=my-user --namespace=my-namespace

Generate YAML

kubectl run nginx --image=nginx --dry-run=client -o yaml > deployment.yaml
kubectl get deployment my-deployment -o yaml > existing-deployment.yaml

Add / Edit Resources

kubectl create -f ./manifest.json # Ensure creating new resources
kubectl apply -f ./deployment.yaml # 3 way merge, keep manual changes
k edit rs new-replica-set
k replace [--force] -f file.yaml # could result downtime
kubectl apply -f ./deployment.yaml --server-side # More reliable, slower, better if multiple actors are in work

Logs

docker logs -f containerId
k logs -f podName [containerName]

Create Namespace

kubectl create namespace test-123 --dry-run -o yaml

Events

kubectl get event -n ns
kubectl get events --sort-by='.lastTimestamp'