Skip to content

Instantly share code, notes, and snippets.

@CarlosA-TrueNorth
Forked from Alvarz/kubectl_cheatsheet.md
Created March 29, 2021 18:28
Show Gist options
  • Save CarlosA-TrueNorth/7ee543ebab72f94792f59ba830742a76 to your computer and use it in GitHub Desktop.
Save CarlosA-TrueNorth/7ee543ebab72f94792f59ba830742a76 to your computer and use it in GitHub Desktop.
Kubectl cheatsheet

Basics cheatsheet from kubernetes

https://kubernetes.io/docs/reference/kubectl/cheatsheet/

Namespaces

List namespaces

kubectl get namespace

Create namespace

kubectl create namespaces <insert-some-namespace-name>

also, it can be created with a yml

apiVersion: v1
kind: Namespace
metadata:
  name: <insert-namespace-name-here>
kubectl create -f ./my-namespace.yaml

Delete namespace

kubectl delete namespaces <insert-some-namespace-name>

Set namespace preference

kubectl config set-context --current --namespace=<insert-namespace-name-here>
# Validate it
kubectl config view --minify | grep namespace:

Get all Pods regardless namespace

kubectl get pods --all-namespaces

Services running

Get services

kubectl get svc -n <namespace>

Pods

Get Pods from namespace

kubectl get pods --namespace=<insert-namespace-name-here>

Get pods of current namespace

kubectl get pods --show-labels

get pods by namepsace

kubectl get pod -n <namespace>

secrets

kubectl get secrets -n <namespace>
kubectl get deployment -n <namespace>

port forwarding

kubectl port-forward svc/<service-name> <our-port>:<their-port> -n <namespace>

Ingress

getting ingress

kubectl get ingress -n <namespace> <ingress-name> -o yaml > ingress.yaml
kubectl apply -f <file-yaml> -n <namespace>

logs

attach terminal to a pod's log

kubectl logs -n <namespace> <pod-name> -f 

attach terminal to a pod's so that we can run commands inside it

kubectl -n <namespace> exec -it <pod-name> bash

cronjob

list cronjobs config

kubectl get cronjobs -n <nameslace>

review cron job

kubectl describe cronjob <name> -n <namespace>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment