Kubectl Cheat sheet
Page icon

Kubectl Cheat sheet

Imperative commands to manage Pods;

Description
CMD
PODs PODs PODs
PODs PODs PODs PODs PODs PODs PODs PODs PODs
Create Pod
k run <podname> --image=<image_name>
Expose a pod as a service
k expose pod <podname> --port=<port> --target-port=<target-port> --name=<service-name>
To get pod logs
k logs <pod-name>
Execute a command
k exec -it <pod-name> -- <command>
Copy Files from a Pod
k cp <pod-name>:<source-path> <detination-path>
Copy Files to a Pod
k cp <source-path> <pod-name>:<destination-path>
Delete a Pod
k delete pod <pod-name>
Create a Pod template
k run <pod-name> --image=<container-image> --dry-run=client -oyaml > pod.yaml
Get the Pod tempate
k get pod <pod-name> -oyaml > pod.yaml
Top the pod
k top <pod-name>
Pod assigned to node checl
k get pod -o wide
Describe a Pod
k describe pod <pod-name>
Port-forward a pod
k port-forward pod/<pod-name>
Example
k run --restart=Never --image=busybox static-busybox --dry-run=client -oyaml --command -- sleep 1000 > /etc/kubernetes/manifests/static-busybox.yaml

Imperative commands to manage Workloadss & Scheduling;

Description
CMD
Deployment Deployment
Deployment Deployment Deployment Deployment Deployment Deployment
Create a Deployment
k create deployment <deploy-name> --image=<image-name>
Scale a Deployment
k scale deployment <deploy-name> --replicas=<n°-replicas>
Upade the image of a deploy
k set image deployment <deploy-name> <container-name>=<container-image>
Rolling Restart a Deployment
k rollout restart deployment <deploy-name>
Pause a deployment
k rollout pause deployment <deploy-name>
Resume a deployment
k rollout resume deployment <deploy-name>
Rollback to a previous version
k rollout undo deployment <deploy-name>
Check Deploy status
k rollout status deployment <deploy-name>
Exposing a Deployment/svc
k expose deployment <deploy-name> --type=<service-name> --port=<service-port> --target-port=<cport>
Revision a deployment
k rollout undo deployment <deploy-name> --to-revision=<revision-number>
Delete a deployment
k delete deployment <deploy-name>
Edit a deployment
k edit deployment <deploy-name>
Create deployment template
k create deploy <d-name> --image=<image> --replicas=<n°> --dry-run=client -oyaml > deploy.yml
Describe a deploy
k describe deployment <deploy-name>
Services Services
Services Services Services Services Services
Createa a Service
k create service <service-type> <service-name> --tcp=<port>:<target-port>
Edit a Service
k edit service <service-name>
Delete a Service
k delete service <service-name>
Get Service Labels Info
k get service <service-name> -ojsonpath='{.spec.ports[0].targetPort}'
Port Forward svc
k port-forward service <service-name> 8080:80
Patch Service
k patch service <kube-dns -n kube-system -p '{"spec":{"selector":{"k8s-app":"kube-dns"}}}'
Ingress Resource
k create ingress <ing-name> --class=<ing-class> --rule=<path>=<s-name>:<s-port> --annotaion=<k>=<v>
Get svc EPs
k get endpoints <svc>
Get NetPolicy
k get netpol
Example
k create service nodeport web-servoce --tcp=80:80 --node-port=30080
ConfigMaps ConfigMaps
ConfigMaps ConfigMaps ConfigMaps ConfigMaps ConfigMaps ConfigMaps
Create a ConfigMap (literal)
k create configmap <configmap-name> --from-literal=<key>=<value>
Create a CM from file
k create configmap <configmap-name> --from-file=<path/to/file>
To create from Multiple Files
k create configmap <configmap-name> --from-file=<path/to/dir>
Create a CM from env var
k create configmap <configmap-name> --from-env-file=<path/to/dir>
Edit a CM
k edit configmap <cm-name>
Delete a CM
k delete configmap <cm-name>
Secrets Secrets
Secrets Secrets Secrets Secrets Secrets Secrets
Create a Secret from literal
k create secret generic <secret-name> --from-literal=<key>=<value>
Create a Secret from file
k create secret generic <secret-name> --from-file=<path/to/file>
Create Secret from *files
k create secret generic <secret-name> --from-file=<path/to/dir>
Edit a Secret
k edit secret <secret-name>
Delete a Secret
k delete secret <secret-name>
Secrets Secrets
Secrets Secrets Secrets Secrets Secrets Secrets

Imperative commands for Installation and Maintaince;

Description
CMD
NODES NODES
NODES NODES NODES NODES NODES NODES
Check Cluster Info
k cluster-info
View Nodes
k get nodes
Top Nodes
k top nodes
Details about nodes
k describe node <node-name>
Check k8s version
k version
Drain a Node
k drain <node-name>
Uncordon a node
k uncordon <node-name>
Cordon a node
k cordon <node-name>
Using JsonPath 4 cc
k get nodes -o=custom-columns=NODE:.metadata.name,CPU:.status.capacity.cpu
Using JsonPath 2 sort
k get nodes --sort-by=.status.capacity.cpu
Using JsonPath 2 loop
k get nodes -ojsonpath='{range .items[*]} {.metadata.name} {"\t"} {.status.capacity.cpu} {"\n"} {end}'
Determine context used by user aws-user in kconfig file
k config view --kubeconfig=/root/my-kube-config -ojsonpath="{.contexts[?(@.context.user=='aws-user')].name}" > /opt/outputs/aws-context-name
CONFIG CONFIG
CONFIG CONFIG CONFIG CONFIG CONFIG CONFIG
Get contexts
k config get-contexts
Switch contexts
k config use-context <context-name>
Create context
k config set-context <context-name> --cluster=<cluster-name> --user=<user> --namespace=<ns>
Rename Context
k config rename-context <oldname> <newname>
Delete context
k config delete-context <context-name>
View current context
k config current-contexts
Modify Context
k config modify-context <context-name> --cluster=<cluster> --user=<user> --namespace=<nns>
Create a context with D/cluster
k config use-context <context-name> --cluster=<new-cluster-name>
Create a context with a D/ns
k config use-context <context-name> --namespace=<ns>
View cluster config
k config view --minify --raw -ojsonpath="{.clusters[?'@.name=='<cluster-name>].cluster}"
ETCD ETCD ETCD
ETCD ETCD ETCD ETCD ETCD ETCD ETCD ETCD
ETCD BACKUP
ETCDCTL_API=3 etcdctl --endpoints=https://127.0.0.1:2379 --cacert=/etc/kubernetes/pki/etcd/ca.crt --cert=/etc/kubernetes/pki/etcd/server.crt --key=/etc/kubernetes/pki/etcd/server.key snapshot save /opt/snapshot.db
ETCD RESTORE
etcdctl snapshot restore /opt/snapshot.db --data-dir=/var/etcd/new

Imperative commands to manage Security;

Description
CMD
Create a ServiceAccount
k create sa <sa-name>
Create a Role
k create role <role-name> --resource=<resource> --verb=<verb> --namespace=<ns>
Create a RoleBinding
k create rolebinding <rb-name> --role=<role-name> --serviceaccount=<ns:sa-name>
Create a ClusterRole
k create clusterrole <cr-name> --resource=<resource> --verb=<verb>
Create a ClusterRoleBinding
k create clusterrolebinding <crb-name> --clusterrole=<cr-name> --serviceaccount=<ns:sa-name>
Get a SA
k get sa <sa-name> -n <ns>
Get a Role
k get role <r-name> -n <ns>
Get a ClusterRole
k get clusterrole <cr-name> -n <ns>
Example
k create role my-role --resource=pods --verb=get --namespace=my-ns