Skip to content

Instantly share code, notes, and snippets.

@jjangga0214
Last active December 29, 2019 06:16
Show Gist options
  • Save jjangga0214/db9552935fa57c0b3cd00acc7f2488ba to your computer and use it in GitHub Desktop.
Save jjangga0214/db9552935fa57c0b3cd00acc7f2488ba to your computer and use it in GitHub Desktop.

kubectl

minikube

# alias mk=minikube
minikube start
minikube stop
minikube delete

basic

k create namespace <name>
kaf <path> -n <namespace>
kdelf <path> -n <namespace>
kg{p|s|ns..} -n <namespace>
kd{p|s|ns..} -n <namespace>
--selector app=demo
-l app=demo
k port-forward <pod> <local>:<pod> -n <namespace>

gen

# generate resource manifests
kubectl run demo --image=<image> --dry-run -o yaml
# export existing resources
kubectl get deployments <deployment> -o yaml -n <namespace>
k diff -f <path>

management

# log pod
k logs --tail 20 <pod> -n <namespace>
# get shell from pod's container
kubectl exec -it [-c <container>] <pod> /bin/sh
kubectl run -n <namespace> busybox --image=busybox:1.28 --rm -it --restart=Never /bin/ash

It can be particularly useful to view the Kubernetes API server’s logs; for example, if you have RBAC permission errors, they’ll show up here. If you have access to your master nodes,you can find the kube-apiserver Pod in the kube-system namespace and use kubectl logs to see its output.

If you’re using a managed service like GKE, where the master nodes are not visible toyou, check your provider’s documentation to see how to find the control plane logs(for example, on GKE they’ll be visible in the Stackdriver Logs Viewer).

For more sophisticated log watching, you may want to use a dedicated tool like Stern instead

Cloud Native Devops with Kubernetes p.120

context

kubectx <context>
kubectx -
kubectx <NEW_NAME>=<context>
kubectx -d <context>

kubens <namespace>
kubens -
# get current context
k config current-context
# list contexts
k config get-contexts
# create or update context
k config set-context <context> --cluster <cluster> --namespace <namespace>
# switch context 
k config use-context <context>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment