Kubernetes
Viewing and finding resources {.cols-2}
Nodes
kubectl get no # Display all node informationkubectl get no -o wide # Show more information about all nodeskubectl describe no # Display node detailskubectl get no -o yaml # Display node details in yaml formatkubectl get node --selector=[label_name] # Filter the node with the specified labelkubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type="ExternalIP")].address}'# Output the field information defined by the jsonpath expressionkubectl top node [node_name] # Display node (CPU/memory/storage) usage
Resource name: nodes, abbreviation: no
Pods
kubectl get po # Display all container group informationkubectl get po -o widekubectl describe pokubectl get po --show-labels # View the labels of the container groupkubectl get po -l app=nginxkubectl get po -o yamlkubectl get pod [pod_name] -o yaml --exportkubectl get pod [pod_name] -o yaml --export > nameoffile.yaml# Export container group information to yaml file in yaml formatkubectl get pods --field-selector status.phase=Running# Use the field selector to filter out container group information
Resource name: pods, abbreviation: po
Namespaces
kubectl get nskubectl get ns -o yamlkubectl describe ns
Resource name: namespaces, abbreviation: ns
Deployments
kubectl get deploykubectl describe deploykubectl get deploy -o widekubectl get deploy -o yaml
Resource name: deployments, abbreviation: deploy
Services
kubectl get svckubectl describe svckubectl get svc -o widekubectl get svc -o yamlkubectl get svc --show-labels
Resource name: services, abbreviation: svc
Daemon Sets
kubectl get dskubectl describe ds --all-namespaceskubectl describe ds [daemonset_name] -n [namespace_name]kubectl get ds [ds_name] -n [ns_name] -o yaml
Resource name: daemonsets, abbreviation: ds
Events
kubectl get eventskubectl get events -n kube-systemkubectl get events -w
Resource name: events, abbreviation: ev
Logs
kubectl logs [pod_name]kubectl logs --since=1h [pod_name]kubectl logs --tail=20 [pod_name]kubectl logs -f -c [container_name] [pod_name]kubectl logs [pod_name] > pod.log
Service Accounts
kubectl get sakubectl get sa -o yamlkubectl get serviceaccounts default -o yaml >./sa.yamlkubectl replace serviceaccount default -f ./sa.yaml
Resource name: serviceaccounts, abbreviation: ev
Replica Sets
kubectl get rskubectl describe rskubectl get rs -o widekubectl get rs -o yaml
Resource name: replicasets, abbreviation: rs
Roles
kubectl get roles --all-namespaceskubectl get roles --all-namespaces -o yaml
Secrets
kubectl get secretskubectl get secrets --all-namespaceskubectl get secrets -o yaml
Config maps
Resource name: configmaps, abbreviation: cm
kubectl get cmkubectl get cm --all-namespaceskubectl get cm --all-namespaces -o yaml
Ingresses
Resource name: ingresses, abbreviation: ing
kubectl get ingkubectl get ing --all-namespaces
Persistent Volumes
Resource name: persistentvolumes, abbreviation: pv
kubectl get pvkubectl describe pv
Persistent volume declaration
Resource name: persistentvolumeclaims, abbreviation: pvc
kubectl get pvckubectl describe pvc
storage class
Resource name: storageclasses, Abbreviation: sc
kubectl get sckubectl get sc -o yaml
Multiple resources
kubectl get svc, pokubectl get deploy, nokubectl get allkubectl get all --all-namespaces
Updating resources
Taint
kubectl taint [node_name] [taint_name]
Label
kubectl label [node_name] disktype=ssdkubectl label [pod_name] env=prod
Maintain/Schedulable
kubectl cordon [node_name] # node maintenancekubectl uncordon [node_name] # node is schedulable
clear
kubectl drain [node_name] # empty the node
Node/Pod {.row-span-2}
kubectl delete node [node_name]kubectl delete pod [pod_name]kubectl edit node [node_name]kubectl edit pod [pod_name]
Stateless/Namespaced {.row-span-2}
kubectl edit deploy [deploy_name]kubectl delete deploy [deploy_name]kubectl expose deploy [deploy_name] --port=80 --type=NodePortkubectl scale deploy [deploy_name] --replicas=5kubectl delete nskubectl edit ns [ns_name]
Service
kubectl edit svc [svc_name]kubectl delete svc [svc_name]
Daemon set
kubectl edit ds [ds_name] -n kube-systemkubectl delete ds [ds_name]
Service account
kubectl edit sa [sa_name]kubectl delete sa [sa_name]
Notes
kubectl annotate po [pod_name] [annotation]kubectl annotateno [node_name]
Create resources
Create pod
kubectl create -f [name_of_file]kubectl apply -f [name_of_file]kubectl run [pod_name] --image=nginx --restart=Neverkubectl run [pod_name] --generator=run-pod/v1 --image=nginxkubectl run [pod_name] --image=nginx --restart=Never
Create Service
kubectl create svc nodeport [svc_name] --tcp=8080:80
Create a stateless application
kubectl create -f [name_of_file]kubectl apply -f [name_of_file]kubectl create deploy [deploy_name] --image=nginx
interaction
kubectl run [pod_name] --image=busybox --rm -it --restart=Never --sh
Output YAML
kubectl create deploy [deploy_name] --image=nginx --dry-run -o yaml > deploy.yamlkubectl get po [pod_name] -o yaml --export > pod.yaml
Help
kubectl -hkubectl create -hkubectl run -hkubectl explain deploy.spec
Miscellaneous
APIs
kubectl get --raw /apis/metrics.k8s.io/
Information
kubectl configkubectl cluster-infokubectl get componentstatus
Also See
- Kubernetes Official Documentation (kubernetes.io)