Skip to content

Instantly share code, notes, and snippets.

@spyesx
Last active October 29, 2019 09:16
Show Gist options
  • Save spyesx/4cd3d0d912b2cab267a7679d448bc086 to your computer and use it in GitHub Desktop.
Save spyesx/4cd3d0d912b2cab267a7679d448bc086 to your computer and use it in GitHub Desktop.
kubectl cheat sheet

Add a kubectl context

# copy cluster's certificate to a file
vi cluster-certificate.txt

# Set cluster
kubectl config set-cluster <CLUSTER_NAME> --server=https://37.187.1.138:6443 --certificate-authority=cluster-certificate.txt --embed-certs=true

# Set credentials
kubectl config set-credentials <USER_NAME> --token=<TOKEN>

# Set context
kubectl config set-context <KUBECTL_CONTEXT_NAME> --cluster=<CLUSTER_NAME> --user=<USER_NAME> --namespace=<NAMESPACE>

# Use context
kubectl config use-context <KUBECTL_CONTEXT_NAME>

Clean up a namespace

# Delete a config map
kubectl get configmaps | awk '{print $1}' | grep -v 'NAME' | xargs kubectl delete configmap

# Real "get all"
kubectl get -n <NAMESPACE> configmaps,daemonsets,deployments,endpoints,ingresses,jobs,persistentvolumeclaims,pods,podtemplates,replicasets,services,statefulsets

# Delete ALL
kubectl get -n <NAMESPACE> configmaps,daemonsets,deployments,endpoints,ingresses,jobs,persistentvolumeclaims,pods,podtemplates,replicasets,services,statefulsets,secrets | awk '{print $1}' | grep -v "NAME" | grep -v "secret/default-token" | xargs kubectl delete

Stop/Start a deployment

kubectl get deployment

# Stop
kubectl scale deployment.apps/<DEPLOYMENT_NAME> --replicas 0

# Start
kubectl scale deployment.apps/<DEPLOYMENT_NAME> --replicas 1

Sometime you just want to restart a container into a pod.

This will send a SIGTERM signal to process 1, which is the main process running in the container. All other processes will be children of process 1, and will be terminated after process 1 exits.

Note: It will not solve your problem. This is only a quick fix.

kubectl exec -it <POD_NAME> -c <CONTAINER_NAME> -- /bin/sh -c "kill 1"

Fix rewrite problems with PHPMyAdmin

I don't know why but sometimes, helm upgrade doesn't upgrade these annotations so I have to do it manualy.

kubectl annotate --overwrite ingress <INGRESS_NAME> "nginx.ingress.kubernetes.io/rewrite-target"-
kubectl annotate --overwrite ingress <INGRESS_NAME> "ingress.kubernetes.io/rewrite-target"-

Other cheat sheets

Tools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment