๐ก Kubernetes kubectl Cheatsheet: Pod Operations & Debugging โ Executive Summary
Practical kubectl cheat sheet covering pod management, cluster troubleshooting, resource scaling, and production debugging commands for DevOps engineers.
References
- Reference document
- source code
Commands
kubectl โhelp
bash
kubectl controls the Kubernetes cluster manager.
Find more information at: https://kubernetes.io/docs/reference/kubectl/
Basic Commands (Beginner):
create Create a resource from a file or from stdin
expose Take a replication controller, service, deployment or pod and expose it as a new Kubernetes service
run Run a particular image on the cluster
set Set specific features on objects
Basic Commands (Intermediate):
explain Get documentation for a resource
get Display one or many resources
edit Edit a resource on the server
delete Delete resources by file names, stdin, resources and names, or by resources and label selector
Deploy Commands:
rollout Manage the rollout of a resource
scale Set a new size for a deployment, replica set, or replication controller
autoscale Auto-scale a deployment, replica set, stateful set, or replication controller
Cluster Management Commands:
certificate Modify certificate resources.
cluster-info Display cluster information
top Display resource (CPU/memory) usage
cordon Mark node as unschedulable
uncordon Mark node as schedulable
drain Drain node in preparation for maintenance
taint Update the taints on one or more nodes
Troubleshooting and Debugging Commands:
describe Show details of a specific resource or group of resources
logs Print the logs for a container in a pod
attach Attach to a running container
exec Execute a command in a container
port-forward Forward one or more local ports to a pod
proxy Run a proxy to the Kubernetes API server
cp Copy files and directories to and from containers
auth Inspect authorization
debug Create debugging sessions for troubleshooting workloads and nodes
Advanced Commands:
diff Diff the live version against a would-be applied version
apply Apply a configuration to a resource by file name or stdin
patch Update fields of a resource
replace Replace a resource by file name or stdin
wait Experimental: Wait for a specific condition on one or many resources
kustomize Build a kustomization target from a directory or URL.
Settings Commands:
label Update the labels on a resource
annotate Update the annotations on a resource
completion Output shell completion code for the specified shell (bash, zsh, fish, or powershell)
Other Commands:
alpha Commands for features in alpha
api-resources Print the supported API resources on the server
api-versions Print the supported API versions on the server, in the form of "group/version"
config Modify kubeconfig files
plugin Provides utilities for interacting with plugins
version Print the client and server version information
Usage:
kubectl [flags] [options]
Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).
Basic Commands (Beginner)
create
bash
# create
# ํ์ผ์ ์ด์ฉํ์ฌ configmap create
kubectl create configmap ${cm} -n ${namespace} --from-file=${filePath}
# namespace
kubectl create ns ${namespace}
# deployment
kubectl create deployment ${deployment} --image=$image --replicas=$rscnt
# role (--resource=apiGroup(rs.apps),resource(pods),subresource(pods/status)
kubectl create role ${role} -n ${namespace} --verb=get,list,watch --resource=pods,pods/status
kubectl create role $role --verb=get --resource=pods --resource-name=$pod1
--resource-name=$pod2
# rolebinding
# USERS(Grouped), ClusterRole
kubectl create rolebinding $rolebinding --clusterrole=admin --user=user1 --user=user2 --group=group1
# SA, ROLE
kubectl create rolebinding $rolebinding --role=admin --serviceaccount=monitoring:sa-dev
# secret
kubectl create secret tls $secret --cert=$crtfile --key=$keyfile
kubectl create secret generic $secret --from-literal=$key1=$value1 --from-literal=$key2=$value2
kubectl create secret docker-registry $secret --docker-server=$service_endpoint --docker-username=$user --docker-password=$password
# cronjob ํ
์คํธ๋ฅผ ์ํ ์ผํ์ฑ job ์์ฑ
kubectl create job $jobname --from=cronjob/$cronjobname
expose
bash
# create svc by application resources
#pod (po)
#service (svc)
#replicationcontroller (rc)
#deployment (deploy)
#replicaset (rs)
#ClusterIP๋ก ์์ฑ
kubectl expose $resourcetype $resourcename --port=$svcport --target-port=$appPort
#NodePort๋ก ์์ฑ (์๋์ง์ )
kubectl expose $resourcetype $resourcename --port=$svcport --target-port=$appPort --type=NodePort
run
bash
# ๋จ์ผ ๋ฐ์ด๋๋ฆฌ์ด๋ฏธ์ง ๋ฌ๋ ์ ์ง
kubectl run $POD --image=$IMAGE --command -- sleep infinity
# create pods
## ์ด๋ฏธ์ง ํ
์คํธ์ฉ ๋ช
๋ น (timeout)
kubectl run $POD --image=$IMAGE --restart=Never --command -- sleep 3600
# ์ด๋ฏธ์ง๋ง ๋ช
์ํ์ฌ ํ๋ ๋ฌ๋
#kubectl run nginx --image=nginx
kubectl run $POD --image=$IMAGE
#ํ๋๋ฅผ ๋ฌ๋ํ์ง ์๊ณ yaml๋ก ์ถ๋ ฅ
kubectl run $POD --image=$IMAGE --dry-run=client -o yaml > $sample_yaml
#ํ๋๋ฅผ ๋ฌ๋ํ์ง ์๊ณ yaml๋ก ์ถ๋ ฅ, command ์
๋ ฅ(๋ชจ๋ ์ต์
์ค ๋ง์ง๋ง์ ๊ธฐ์
)
kubectl run $POD --image=$IMAGE --dry-run=client -o yaml --command -- ${commands} > $sample_yaml
set
bash
# setting subresource
# change image for deployments
kubectl set image deploy/$deployments ${container}=${image}
Basic Commands (Intermediate)
explain
bash
#๋ฆฌ์์ค, ๋ด๋ถ ํ๋์ ๋ชฉ๋ก, ์ค๋ช
์ ํ์ธํ๊ธฐ ์ํ ๋ช
๋ น์ด, jsonPath ๊ธฐ์ค์ผ๋ก ์ฐพ๋๋ค.
#๋ฆฌ์์ค์ ์ค๋ช
์ ์ ์ธํ๊ณ ํ์ ํ๋ ์ ์ฒด๋ฅผ ํธ๋ฆฌ๊ตฌ์กฐ๋ก ๋์ด
kubectl explain $resource --recursive
#๋ฆฌ์์ค์ lv1 ํ๋,description ๋ชฉ๋ก ๋์ด
kubectl explain $resource
#๋ฆฌ์์ค์ ํด๋น ํ๋ ๋ด์์๋ lv2 ํ๋, description ๋ชฉ๋ก ๋์ด
kubectl explain ${resource}.${field}
get
bash
# ๋ชฉ๋ก ์กฐํ
kubectl get ${api-resource}
# ๋ค์์คํ์ด์ค ๋ช
์ํ์ฌ ์กฐํ
kubectl get ${api-resource} -n ${namespace}
# ๋ชจ๋ ๋ค์์คํ์ด์ค ์กฐํ
kubectl get ${api-resource} -A
kubectl get ${api-resource} --all-namespaces
# ๋ผ๋ฒจ ์กฐํ
kubectl get ${api-resource} --show-labels
# ํ์ ์ง์
kubectl get ${api-resource} -o wide
kubectl get ${api-resource} -o yaml
kubectl get ${api-resource} -o json
# ์ปค์คํ
์ปฌ๋ผ
kubectl get ${api-resource} -o custom-columns="$columnName":${jsonpath}
#kubectl get pods -o custom-columns="Image":.spec.containers[*].image
# ๋ผ๋ฒจ ์
๋ ํฐ
kubectl get ${api-resource} -l ${label}=${value}
kubectl get ${api-resource} -l ${label}
# AND
kubectl get ${api-resource} -l ${label}=${value},${label}=${value},${label}=${value}
# OR
kubectl get ${api-resource} -l 'app in (${label}=${value},${label}=${value},${label}=${value})'
kubectl get ${api-resource} --selector ${label}=${value}
kubectl get ${api-resource} --selector ${label}
# field ์
๋ ํฐ
#kubectl get ${api-resource} --field-selector=status.phase=Failed
kubectl get ${api-resource} --field-selector=${field}=${value}
# ์ํ๋ manifest/status ๊ฐ ์ป๊ธฐ : pv๋ช
kubectl get pvc -n grafana-loki data-portal-grafana-loki-ingester-0 -o jsonpath={.spec.volumeName}
# ์๋ฅผ ํ์ฉํ์ฌ pv ๊ฒ์
kubectl describe pv `kubectl get pvc -n grafana-loki data-portal-grafana-loki-ingester-0 -o jsonpath={.spec.volumeName}`
# ์๋ฅผ ํ์ฉํ์ฌ pv ์ค์ ๊ฒฝ๋ก ์ถ๋ ฅ
echo `kubectl get pv \`kubectl get pvc -n grafana-loki data-portal-grafana-loki-ingester-0 -o jsonpath={.spec.volumeName}\` -o jsonpath={.spec.nfs.path}`
edit
bash
# ๋ฌ๋์ค์ธ resource์์ manifest.yaml ์์ (์ฃผ๋ก deployments ๋ฑ ์ปจํธ๋กค๋ฌ๋ configmap ๋ฑ์ ๊ตฌ์ฑ์์)
kubectl edit $resourcetype $resourcename
delete
bash
# field-selector
kubectl -n ${namespace} delete pods --field-selector=status.phase=Failed
# label-selector
kubectl -n ${namespace} delete pods -l k8s-app=kube-dns
Deploy Commands
rollout
bash
# deployment restart
kubectl rollout restart deployment -n ${ ๋ค์์คํ์ด์ค } ${ Deployment1 Deployment2 ... }
kubectl rollout restart deamonset
kubectl rollout restart statefulset
scale
bash
##Set a new size for a deployment, rs, rc, statefulSet
#replicas ๋ณ๊ฒฝ
kubectl scale --replicas=$cnt rs $rs
autoscale
Cluster Management Commands
certificate
bash
#csr ์น์ธ
kubectl certificate approve $csr
#Pending์ธ csr์ ์ฐพ์ Approved๋ก ๋ณ๊ฒฝ
kubectl certificate approve $(kubectl get csr | grep Pending | awk '{print $1}' | sed -z 's/[\n\r]/ /g')
cluster-info
bash
# apiserver ์ฃผ์ ํ์ธ
kubectl cluster-info
# Service IP Range ํ์ธ
kubectl cluster-info dump | grep -m 1 service-cluster-ip-range
#10.233.0.0/18
# Pod IP Range ํ์ธ
kubectl cluster-info dump | grep -m 1 cluster-cidr
#10.233.64.0/18
top
sql
# show top of cluster member nodes
kubectl top node
# show top of namspace member pods
kubectl top pod -n {namespace}
cordon
bash
# node๋ฅผ ์ค์ผ์ค๋งํ ์ ์๋๋ก ํ๊ธฐ
kubectl cordon ${ NodeName }
uncordon
bash
# node๋ฅผ cordon ํด์
kubectl uncordon ${ NodeName }
drain
bash
# ์ ์ง ๋ณด์๋ฅผ ์ํด์ node๋ฅผ ์ค๋น ์ํ๋ก ๋น์
kubectl drain $node
taint
bash
#taint ์ถ๊ฐ
kubectl taint nodes ${node} ${key}=${value}:${taint-effect}
# taint-effect:
## NoSchedule: NoSchedule ์ดํํธ๊ฐ ์๋ ๋ฌด์๋์ง ์์ ํ
์ธํธ๊ฐ ํ๋ ์ด์ ์์ผ๋ฉด ์ฟ ๋ฒ๋คํฐ์ค๋ ํด๋น ๋
ธ๋์ ํ๋๋ฅผ ์ค์ผ์คํ์ง ์๋๋ค.
## PreferNoSchedule: NoSchedule ์ดํํธ๊ฐ ์๋ ๋ฌด์๋์ง ์์ ํ
์ธํธ๊ฐ ์์ง๋ง PreferNoSchedule ์ดํํธ๊ฐ ์๋ ๋ฌด์๋์ง ์์ ํ
์ธํธ๊ฐ ํ๋ ์ด์ ์์ผ๋ฉด ์ฟ ๋ฒ๋คํฐ์ค๋ ํ๋๋ฅผ ๋
ธ๋์ ์ค์ผ์ฅดํ์ง ์์ผ๋ ค๊ณ ์๋ ํ๋ค
## NoExecute: NoExecute ์ดํํธ๊ฐ ์๋ ๋ฌด์๋์ง ์์ ํ
์ธํธ๊ฐ ํ๋ ์ด์ ์์ผ๋ฉด ํ๋๊ฐ ๋
ธ๋์์ ์ถ์ถ๋๊ณ (๋
ธ๋์์ ์ด๋ฏธ ์คํ ์ค์ธ ๊ฒฝ์ฐ), ๋
ธ๋์์ ์ค์ผ์ค๋์ง ์๋๋ค(์์ง ์คํ๋์ง ์์ ๊ฒฝ์ฐ).
#taint ์ ๊ฑฐ
kubectl taint nodes ${node} ${key}-
kubectl taint nodes ${node} ${key}:${taint-effect}-
kubectl taint nodes ${node} ${key}=${value}:${taint-effect}-
- Use of stain
bash
# pipeline-agent node
kubectl taint nodes $node dedicated=pipeline-agent:NoSchedule
kubectl label nodes $node dedicated=pipeline-agent
# in template.spec
spec:
...
tolerations:
- key: "dedicated"
operator: "Equal"
value: "pipeline-agent"
effect: "NoSchedule"
nodeSelector:
dedicated: pipeline-agent
Troubleshooting and Debugging Commands
describe
bash
# find trouble or status on events
kubectl describe $resourcetype $resourcename
logs
bash
# ๋จ์ผ ์ปจํ
์ด๋ ๋ก๊ทธ (follow)
kubectl logs -n ${namespace} ${pod} ${container} -f
# [flags]
#kubectl logs ๋ช
๋ น์ด ์ฌ์ฉ์ ๋ชจ๋ ์ปจํ
์ด๋๋ฅผ log stream์ผ๋ก ์ฌ์์
--all-containers
#kubectl logs ๋ช
๋ น์ด ์ฌ์ฉ์ 5๊ฐ ์ด๊ณผ์ log streams ์ฌ์ฉ์
--max-log-requests=$num
attach
exec
bash
# container ๋ด๋ถ ์ ์ : ์
kubectl exec -it -n ${ ๋ค์์คํ์ด์ค } ${ Pod ๋ช
} -- bash
kubectl exec -it -n ${ ๋ค์์คํ์ด์ค } ${ Pod ๋ช
} -- sh
# container command ์คํ
kubectl exec -n ${ ๋ค์์คํ์ด์ค } ${ Pod ๋ช
} -- $commands
port-forward
bash
Examples:
# Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in the pod
kubectl port-forward pod/mypod 5000 6000
# Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in a pod selected by the
deployment
kubectl port-forward deployment/mydeployment 5000 6000
# Listen on port 8443 locally, forwarding to the targetPort of the service's port named "https" in a pod selected by
the service
kubectl port-forward service/myservice 8443:https
# Listen on port 8888 locally, forwarding to 5000 in the pod
kubectl port-forward pod/mypod 8888:5000
# Listen on port 8888 on all addresses, forwarding to 5000 in the pod
kubectl port-forward --address 0.0.0.0 pod/mypod 8888:5000
# Listen on port 8888 on localhost and selected IP, forwarding to 5000 in the pod
kubectl port-forward --address localhost,10.19.21.23 pod/mypod 8888:5000
# Listen on a random port locally, forwarding to 5000 in the pod
kubectl port-forward pod/mypod :5000
# ์ธ๊ทธ๋ ์ค ์์ด ์ธ๋ถ ๋
ธ์ถ (์์)
kubectl port-forward --address 0.0.0.0 svc/$service 9090:9090 &
proxy
bash
kubectl proxy #kubeapiserver local proxy 8001
cp
bash
# pod ์ปจํ
์ด๋ ๋ด๋ถ์ ํ์ผ ์ธ๋ถ๋ก ๋ณต์ฌ
kubectl cp portal-grafana-9fdc9976c-fc9bq:/opt/bitnami/grafana/conf/grafana.ini ./grafana.ini
>>>
# ์๋ฌ๊ฐ ๋์ง๋ง ํ์ผ์ ์ ์์ ์ผ๋ก ๊ฐ์ ธ์์ง.
Removing leading `/' from member names
<<<
# pod์์ pod์ผ๋ก ํ์ผ ๋ณต์ฌ๋ ์ง์ํ์ง ์์
auth
bash
#sa - $sa๊ฐ $target_ns์ secrets์ get ๊ถํ์ด ์๋์ง ํ์ธ
kubectl auth can-i get secrets --as=system:serviceaccount:${sa_ns}:${sa} -n ${target_ns}
#user
kubectl auth can-i get pods --as=${user}
debug
Advanced Commands
diff
bash
# yaml์ ์ฌ์ฉํ์ฌ resource create or update
kubectl apply -f ${yaml}
# ๋ช
๋ น์ด๋ก ๋๋ฏธํ์ผ ์์ฒญ
kubectl apply -f - <<EOF
EOF
# kustomize dir๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐฐํฌ
kubectl apply -k ${dir}
- Standard output upon execution
bash
cat <<EOF | tee /dev/tty | kubeectl apply -f -
EOF
patch
bash
## jsonPath๋ฅผ ์ฌ์ฉํ์ฌ ๋ฆฌ์์ค ๊ตฌ์ฑ์์๋ฅผ ์
๋ฐ์ดํธ
# ๋
ธ๋๋ฅผ ๋ถ๋ถ์ ์ผ๋ก ์
๋ฐ์ดํธ
kubectl patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}'
# ์ปจํ
์ด๋์ ์ด๋ฏธ์ง๋ฅผ ์
๋ฐ์ดํธ. ๋ณํฉ(merge) ํค์ด๋ฏ๋ก, spec.containers[*].name์ด ํ์
kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'
# ์์น ๋ฐฐ์ด์ ์ด์ฉํ json ํจ์น๋ฅผ ์ฌ์ฉํ์ฌ, ์ปจํ
์ด๋์ ์ด๋ฏธ์ง๋ฅผ ์
๋ฐ์ดํธ
kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]'
# ์์น ๋ฐฐ์ด์ ์ด์ฉํ json ํจ์น๋ฅผ ์ฌ์ฉํ์ฌ livenessProbe ๋ํ๋ก์ด๋จผํธ ๋นํ์ฑํ
kubectl patch deployment valid-deployment --type json -p='[{"op": "remove", "path": "/spec/template/spec/containers/0/livenessProbe"}]'
# ์ด๋ฏธ ๋ง๋ค์ด์ง reclaimPolicy=Delete(์ญ์ )์ธ pv๋ฅผ Retain(๋ณด์กด)์ผ๋ก ๋ณ๊ฒฝ
kubectl patch pv $pv -p '{"spec":{"persistentVolumeReclaimPolicy":"Retain"}}'
# ์์น ๋ฐฐ์ด์ ์ ์์ ์ถ๊ฐ
kubectl patch sa default --type='json' -p='[{"op": "add", "path": "/secrets/1", "value": {"name": "whatever" } }]'
# ๋ํ๋ก์ด๋จผํธ์ scale ์๋ธ๋ฆฌ์์ค๋ฅผ ํจ์นํ์ฌ ๋ ํ๋ฆฌ์นด ์ ์
๋ฐ์ดํธ
kubectl patch deployment nginx-deployment --subresource='scale' --type='merge' -p '{"spec":{"replicas":2}}'
- Remove all Terminating ns
bash
#metadata
for ns in $(kubectl get ns --no-headers | awk '/Terminating/ {print $1}'); do
echo "Force deleting namespace: $ns"
kubectl patch ns $ns --type='merge' -p='{"metadata": {"finalizers": []}}'
done
replace
bash
# ์ญ์ ํ yaml๋ก ์์ฑ
kubectl replace --force -f $yaml
wait
kustomize
Settings Commands
label
bash
# Update pod 'foo' with the label 'unhealthy' and the value 'true'
kubectl label pods foo unhealthy=true
# Update pod 'foo' with the label 'status' and the value 'unhealthy', overwriting any existing value
kubectl label --overwrite pods foo status=unhealthy
# Update all pods in the namespace
kubectl label pods --all status=unhealthy
# Update a pod identified by the type and name in "pod.json"
kubectl label -f pod.json status=unhealthy
# Update pod 'foo' only if the resource is unchanged from version 1
kubectl label pods foo status=unhealthy --resource-version=1
# Update pod 'foo' by removing a label named 'bar' if it exists
# Does not require the --overwrite flag
kubectl label pods foo bar-
annotate
completion
bash
## bash
cat <<EOF >> ~/.bashrc
source <(kubectl completion bash)
# source <(kubeadm completion bash)
alias k=kubectl
complete -F __start_kubectl k
EOF
source ~/.bashrc
## zsh
cat <<EOF >> ~/.zshrc
source <(kubectl completion zsh)
alias k=kubectl
if type kubectl &>/dev/null; then
source <(kubectl completion zsh)
compdef k=kubectl
fi
EOF
source ~/.zshrc
Other Commands
bash
alpha Commands for features in alpha
api-resources Print the supported API resources on the server
api-versions Print the supported API versions on the server, in the form of "group/version"
config Modify kubeconfig files
plugin Provides utilities for interacting with plugins
version Print the client and server version information
alpha
bash
#Commands for features in alpha
api-resource
bash
kubectl api-resources
kubectl api-resources --namespaced=true
#role, rolebinding
kubectl api-resources --namespaced=false
#clusterrole, clusterrolebinding
api-versions
bash
# Print the supported API versions on the server, in the form of "group/version"
config
bash
# kubeconfig ์กฐํ
kubectl config view
## [Context]
# context ์กฐํ
kubectl config get-contexts
# context ์ญ์
kubectl config delete-context ${context}
# context rename
kubectl config rename-context ${asis-context} ${tobe-context}
# context ์ถ๊ฐ
kubectl config set-context ${new-context} --cluster=${new-cluster} --user=${user}
# current-context ๋ณ๊ฒฝ
kubectl config use-context ${cluster}
# current-context ์กฐํ
kubectl config current-context
# current-context default namespace ๋ณ๊ฒฝ
kubectl config set-context --current --namespace={ ๋ค์์คํ์ด์ค }
## [Cluster]
# cluster ์กฐํ
kubectl config get-clusters
## [User]
# ์ฌ์ฉ์ ์กฐํ
kubectl config get-users
## [Credential]
kubectl config set-credentials ${service-account} --token=${service-account-token}
kubectl config scraped
bash
current-context -- Display the current-context
delete-cluster -- Delete the specified cluster from the kubeconfig
delete-context -- Delete the specified context from the kubeconfig
delete-user -- Delete the specified user from the kubeconfig
get-clusters -- Display clusters defined in the kubeconfig
get-contexts -- Describe one or many contexts
get-users -- Display users defined in the kubeconfig
rename-context -- Rename a context from the kubeconfig file
set -- Set an individual value in a kubeconfig file
set-cluster -- Set a cluster entry in kubeconfig
set-context -- Set a context entry in kubeconfig
set-credentials -- Set a user entry in kubeconfig
unset -- Unset an individual value in a kubeconfig file
use-context -- Set the current-context in a kubeconfig file
view -- Display merged kubeconfig settings or a specified kubeconfig file
plugin
bash
## Provides utilities for interacting with plugins
version
bash
## version ํ์ธ
kubectl version
flags
bash
## selector
#filed-selector
--filed-selector
#label-selector
-l
#namespaces
-n
#all-namespaces
-A
#kubectl logs ๋ช
๋ น์ด ์ฌ์ฉ์ ๋ชจ๋ ์ปจํ
์ด๋๋ฅผ log stream์ผ๋ก ์ฌ์์
--all-containers
#kubectl logs ๋ช
๋ น์ด ์ฌ์ฉ์ 5๊ฐ ์ด๊ณผ์ log streams ์ฌ์ฉ์
--max-log-requests=$num
## global
#rollout history์ ๊ธฐ๋ก (Deployments, StatefulSet, DaemonSet)
--records
## view
#ํ
์ด๋ธ๋ทฐ์ ํค๋ํ ์ ๊ฑฐ
--no-headers
#๋ผ๋ฒจ ์กฐํ
--show-labels
-o, โoutput
bash
-o custom-columns
-o custom-columns-file
-o go-template
-o go-template-file
#json ์ถ๋ ฅ
-o json
#json ์ผ๋ถ ์ถ๋ ฅ
-o=jsonpath='{@}'
-o=jsonpath='{.items[0]}'
-o=jsonpath='{.items[0].metadata.name}'
-o=jsonpath="{.items[*]['metadata.name', 'status.capacity']}"
-o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.startTime}{"\n"}{end}'
-o=jsonpath="{.data.alertmanager-overview\.json}"
-o jsonpath-as-json
-o jsonpath-file
#์ด๋ฆ๋ง ์ถ๋ ฅ
-o name
-o template
-o templatefile
#์์ธ์ ๋ณด ํฌํจ
-o wide
#yaml ์ถ๋ ฅ
-o yaml
features
Upgrading ~/.bashrc
-
Completion:
- completion
-
Autocomplete wrapper
bash
_k_wrap() {
local resource=()
local args=()
# ---- ์ต์
ํ์ฑ ----
while [[ $# -gt 0 ]]; do
case "$1" in
--resource)
shift
IFS=' ' read -r -a resource <<< "$1"
shift
;;
--resource=*)
IFS=' ' read -r -a resource <<< "${1#*=}"
shift
;;
--)
shift
args=("$@")
break
;;
*)
echo "unknown option: $1" >&2
return 1
;;
esac
done
# ---- args ๋ด์์ "--" ๋ถ๋ฆฌ ----
local pre=()
local post=()
local seen_ddash=0
for arg in "${args[@]}"; do
if [[ "$arg" == "--" && $seen_ddash -eq 0 ]]; then
seen_ddash=1
continue
fi
if (( seen_ddash )); then
post+=("$arg")
else
pre+=("$arg")
fi
done
# ---- kubectl ์คํ ----
if (( seen_ddash )) && ((${#post[@]} > 0)); then
kubectl "${pre[@]}" "${resource[@]}" -- "${post[@]}"
else
kubectl "${pre[@]}" "${resource[@]}"
fi
}
- Implementing autocompletion
bash
export dev_kubeconfig="/workspace/cmp-helm/_gitops/.tmp/.dev.kubeconfig"
export stg_kubeconfig="/workspace/cmp-helm/_gitops/.tmp/.stg.kubeconfig"
function kd() {
_k_wrap --resource="--kubeconfig $dev_kubeconfig" -- "$@"
}
function ks() {
_k_wrap --resource="--kubeconfig $stg_kubeconfig" -- "$@"
}
function k_argow() {
_k_wrap --resource="-n deploy-system deploy/argo-workflows-server" -- "$@"
}
function k_vault() {
_k_wrap --resource="-n portal-system sts/vault" -- "$@"
}
verbose flag (Client Version: v1.31.0)
bash
# Config loaded from file, apiserver ์์ฒญ๋ณธ๋ฌธ ํ์ธ
k get pods --v=6
# Req Header, Res Status ํ์ธ
k get pods --v=7
# Res Header, Res Body(Truncated) ํ์ธ
k get pods --v=8
# Res Body(Full) ํ์ธ
k get pods --v=9
# ์ข ๋ ์์ธํ ์ ๋ณด (returning cashed discovery info ๋ฑ)
k get pods --v=10
Special query
Backup k8s resources in an immediately executable format
bash
#for 1 resource
specified=(-n management-system deploy/cert-manager)
kubectl get ${specified[@]} -o yaml | \
yq 'del(.metadata.uid,
.metadata.resourceVersion,
.metadata.creationTimestamp,
.metadata.managedFields,
.metadata.annotations."kubectl.kubernetes.io/last-applied-configuration",
.metadata.generation,
.status)'
#for list
specified=(-n management-system deploy)
kubectl get ${specified[@]} -o yaml | \
yq 'del(.items[].metadata.uid,
.items[].metadata.resourceVersion,
.items[].metadata.creationTimestamp,
.items[].metadata.managedFields,
.items[].metadata.annotations."kubectl.kubernetes.io/last-applied-configuration",
.items[].metadata.generation,
.items[].status)'
tls file
bash
kubectl get secret -n cicd harbor.fredric.playground.cld-tls -o jsonpath="{.data.ca\.crt}" | base64 -d > ca.pem
kubectl get secret -n cicd harbor.fredric.playground.cld-tls -o jsonpath="{.data.tls\.crt}" | base64 -d > cert.pem
kubectl get secret -n cicd harbor.fredric.playground.cld-tls -o jsonpath="{.data.tls\.key}" | base64 -d > key.pem
get-jsonpath Samples
bash
# Pod ๋ด์ ๋ชจ๋ ์ปจํ
์ด๋์ ์ปค๋งจ๋ ์กฐํ
kubectl get pods $pod -n $namespace -o jsonpath='{.spec.containers[*].command}'
View all containers
bash
#container ์กฐํ ํ ์ค๋ณต counting
kubectl get pods --all-namespaces -o jsonpath="{.items[*].spec.containers[*].image}" |\
tr -s '[[:space:]]' '\n' |\
sort |\
uniq -c
# custom columns
kubectl get pods -A -o=custom-columns="Image:{.spec.containers[*].image}"
Check Default ServiceAccount Token
bash
kubectl get secret $(kubectl get sa default -ojsonpath="{.secrets[0].name}") -ojsonpath="{.data.token}" | base64 -d
Check resource quota
bash
kubectl get po -o custom-columns="Name:metadata.name,CPU-Requet:spec.containers[].resources.requests.cpu,CPU-limit:spec.containers[].resources.limits.cpu,MEM-Request:spec.containers[].resources.requests.memory,MEM-limit:spec.containers[].resources.limits.memory"
Export NodePort
bash
export NODE_PORT=$(kubectl get --namespace jenkins-Enterprise Cloud -o jsonpath="{.spec.ports[0].nodePort}" services jenkins)
export NODE_IP=$(kubectl get nodes --namespace jenkins-Enterprise Cloud -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT/login
Check CIDR
bash
# Service IP Range ํ์ธ
kubectl cluster-info dump | grep -m 1 service-cluster-ip-range
#10.233.0.0/18
# Pod IP Range ํ์ธ
kubectl cluster-info dump | grep -m 1 cluster-cidr
#10.233.64.0/18
api-resource, search by API group
bash
factor=istio
kubectl get $(kubectl api-resources -o name | grep $factor | sed -z 's/[\n\r]/,/g' | sed 's/.$//g') -A
Check the entire Ingress for /etc/hosts input
bash
kubectl get ing -A --no-headers | awk '
{
addr = $5
host = $4
hosts[addr] = (hosts[addr] ? hosts[addr] " " host : host)
}
END {
for (a in hosts) {
print a, hosts[a]
}
}' | sort
Update
Force deletion of non-delete resources
bash
resource_type=pv
resource_name=browser-file
NS=portal
kubectl get -n $NS $resource_type $resource_name -o name | xargs -I {} kubectl patch {} -n $NS -p '{"metadata":{"finalizers":[]}}' --type=merge
Find and Delete
bash
# field-selector
kubectl -n ${namespace} delete pods --field-selector=status.phase=Failed
# label-selector
kubectl -n ${namespace} delete pods -l k8s-app=kube-dns
Kubeconfig
Generate token without Service Account period limit
bash
#kubectl create clusterrole cluster-admin-role --verb=* --resource=*
#kubectl edit clusterrole cluster-admin-role #apigroup=*๋ก ๋ณ๊ฒฝ
ns=deploy-system
sa=deploy-admin
kubectl apply -f - <<EOF
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: ${sa}-role
rules:
- apiGroups:
- '*'
resources:
- '*'
verbs:
- '*'
EOF
kubectl create sa $sa -n $ns
kubectl create clusterrolebinding ${sa}-rolebinding --clusterrole ${sa}-role --serviceaccount ${ns}:${sa}
kubectl apply -f - <<EOF
apiVersion: v1
kind: Secret
metadata:
name: ${sa}-token
namespace: $ns
annotations:
kubernetes.io/service-account.name: $sa
type: kubernetes.io/service-account-token
EOF
Create SA with Shell
bash
#!/bin/bash
# `./generate_kubeconfig.sh dev 10.0.X.X mypasswd 2222 > .my.kubeconfig`
ns=default
sa=cmp-admin
# SSH ๋ช
๋ น์ ๋จ ํ๋ฒ ํธ์ถํ๋ ๋ฒ์ ์ด ๊ฐ์ฅ ์์ ์ ์
remote_script=$(cat <<EOF
# 1. ClusterRole ์์ฑ
kubectl apply -f - <<'E1'
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: ${sa}-role
rules:
- apiGroups:
- '*'
resources:
- '*'
verbs:
- '*'
E1
# 2. ServiceAccount ์์ฑ
kubectl create sa ${sa} -n ${ns} --dry-run=client -o yaml | kubectl apply -f -
# 3. ClusterRoleBinding ์์ฑ
kubectl create clusterrolebinding ${sa}-rolebinding \
--clusterrole ${sa}-role \
--serviceaccount ${ns}:${sa} \
--dry-run=client -o yaml | kubectl apply -f -
# 4. Token Secret ์์ฑ
kubectl apply -f - <<'E2'
apiVersion: v1
kind: Secret
metadata:
name: ${sa}-token
namespace: ${ns}
annotations:
kubernetes.io/service-account.name: ${sa}
type: kubernetes.io/service-account-token
E2
EOF
)
remote_script2=$(cat <<EOF
# 5. token ๊ฐ ์ถ๋ ฅ
kubectl get secret ${sa}-token -n ${ns} -o jsonpath="{.data.token}"
EOF
)
printf "%s" "$remote_script" | /bin/bash -s > /dev/tty
token_b64=$(printf "%s" "$remote_script2" | /bin/bash -s)
printf "%s" "$token_b64" | base64 -d
Register kubeconfig with CLI
- insecure
bash
context=
cluster_endpoint=
sa_token=
kubectl config set-cluster $context \
--server=$cluster_endpoint \
--insecure-skip-tls-verify=true
kubectl config set-credentials $context \
--token=
kubectl config set-context my-prod-context \
--cluster=prod-cluster \
--user=prod-user \
--namespace=production
Create Kubeconfig with SSH
bash
#!/bin/bash
# `./generate_kubeconfig.sh dev 10.0.X.X mypasswd 2222 > .my.kubeconfig`
contextName=$1
hostname=$2
ssh_port=${3:-22}
dest_ip=$(dig $hostname +short)
endpoint="https://$dest_ip:6443"
ns=default
sa=tester
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# ์๊ฒฉ ์คํฌ๋ฆฝํธ ์ ์
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
remote_script=$(cat <<EOF
# 1. ClusterRole ์์ฑ
kubectl apply -f - <<'E1'
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: ${sa}-role
rules:
- apiGroups:
- '*'
resources:
- '*'
verbs:
- '*'
E1
# 2. ServiceAccount ์์ฑ
kubectl create sa ${sa} -n ${ns} --dry-run=client -o yaml | kubectl apply -f -
# 3. ClusterRoleBinding ์์ฑ
kubectl create clusterrolebinding ${sa}-rolebinding \
--clusterrole ${sa}-role \
--serviceaccount ${ns}:${sa} \
--dry-run=client -o yaml | kubectl apply -f -
# 4. Token Secret ์์ฑ
kubectl apply -f - <<'E2'
apiVersion: v1
kind: Secret
metadata:
name: ${sa}-token
namespace: ${ns}
annotations:
kubernetes.io/service-account.name: ${sa}
type: kubernetes.io/service-account-token
E2
EOF
)
remote_script2=$(cat <<EOF
# 5. token ๊ฐ ์ถ๋ ฅ
kubectl get secret ${sa}-token -n ${ns} -o jsonpath="{.data.token}"
EOF
)
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# ์๊ฒฉ์์ ์คํฌ๋ฆฝํธ ์คํ ๋ฐ token ๊ฐ์ ธ์ค๊ธฐ
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
printf "%s" "$remote_script" | ssh -p$ssh_port root@$hostname "/bin/bash -s" > /dev/tty
token_b64=$(printf "%s" "$remote_script2" | ssh -p$ssh_port root@$hostname "/bin/bash -s")
# Base64 โ RAW token
serviceToken=$(printf "%s" "$token_b64" | base64 -d)
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# ๋ก์ปฌ kubeconfig ์์ฑ
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
cat <<EOF
apiVersion: v1
kind: Config
preferences: {}
current-context: ${contextName}
clusters:
- name: ${contextName}
cluster:
server: ${endpoint}
insecure-skip-tls-verify: true
contexts:
- name: ${contextName}
context:
cluster: ${contextName}
user: ${contextName}
users:
- name: ${contextName}
user:
token: ${serviceToken}
EOF
krew
Installation
shell
# krew: plugin-manager ์ค์น
# Krew ์ค์น
(
set -x; cd "$(mktemp -d)" &&
OS="$(uname | tr '[:upper:]' '[:lower:]')" &&
ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')" &&
KREW="krew-${OS}_${ARCH}" &&
curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/${KREW}.tar.gz" &&
tar zxvf "${KREW}.tar.gz" &&
./"${KREW}" install krew
)
# .bashrc or .zshrc์ ์ถ๊ฐ
sudo bash -c "echo 'export PATH=\"\${KREW_ROOT:-\$HOME/.krew}/bin:\$PATH\"'" >> /etc/profile
# shell ์ฌ์์
source /etc/profile
ns
bash
# ns ์ค์น: ๋ํดํธ ๋ค์์คํ์ด์ค ๋ณ๊ฒฝ
kubectl krew install ns
kubectl ns { ๋ค์์คํ์ด์ค }
images
shell
# ์ด๋ฏธ์ง์ ๋ณด
krew install images
kubectl images -A
Automation Support
Node IP lookup
bash
# ํ์ฌ ๋
ธ๋ ๋จ์ผ
k get nodes $(hostname) -o jsonpath='{.status.addresses}' | \
jq -r '.[] | select(.type == "InternalIP") | .address'
# all-members
k get nodes -o jsonpath='{.items[*].status.addresses}' | \
jq -r '.[] | select(.type == "InternalIP") | .address'
# control-plain
k get nodes -l node-role.kubernetes.io/control-plane \
-o jsonpath='{.items[*].status.addresses}' | \
jq -r '.[] | select(.type == "InternalIP") | .address'
# worker
k get nodes --selector='!node-role.kubernetes.io/control-plane,!node-role.kubernetes.io/master' \
-o jsonpath='{.items[*].status.addresses}' | \
jq -r '.[] | select(.type == "InternalIP") | .address'
ETC
Expose kube-apiserver swagger
bash
kubectl get --raw /openapi/v2 > k8s-openapi-v2.json
kubectl apply -f - << EOF
apiVersion: v1
kind: Pod
metadata:
name: kube-swagger
labels:
app: kube-swagger
spec:
containers:
- name: kube-swagger
image: swaggerapi/swagger-ui
ports:
- containerPort: 8080
volumeMounts:
- name: swagger-volume
mountPath: /app/swagger.json
readOnly: true
volumes:
- name: swagger-volume
hostPath:
path: $(pwd)/k8s-openapi-v2.jso
type: File
EOF
kubectl expose pod kube-swagger --type=NodePort
Expose kube-apiserver swagger
bash
kubectl get --raw /openapi/v2 > k8s-openapi-v2.json
kubectl apply -f - << EOF
apiVersion: v1
kind: Pod
metadata:
name: kube-swagger
labels:
app: kube-swagger
spec:
containers:
- name: kube-swagger
image: swaggerapi/swagger-ui
ports:
- containerPort: 8080
volumeMounts:
- name: swagger-volume
mountPath: /app/swagger.json
readOnly: true
volumes:
- name: swagger-volume
hostPath:
path: $(pwd)/k8s-openapi-v2.jso
type: File
EOF
kubectl expose pod kube-swagger --type=NodePort
yaml creation sample (problem solution)
bash
#a: pod
kubectl run nginx-pod --image=nginx:alpine --dry-run=client -o yaml> a.yaml
#b: pod
kubectl run redis --image=redis:alpine -l tier=db --dry-run=client -o yaml > b.yaml
#c: service
kubectl create svc clusterip redis-service --tcp=6379 --dry-run=client -o yaml > c.yaml
#d: deploy
kubectl create deploy webapp --image=kodekloud/webapp-color -r 3 -o yaml --dry-run=client > d.yaml
#e: pod
kubectl run custom-nginx --image=nginx --port=8080 --dry-run=client -o yaml > e.yaml
#f: ns, deploy
kubectl create ns dev-ns --dry-run=client -o yaml > f.yaml
echo "---" >> f.yaml
kubectl create deploy redis-deploy -n dev-ns --image=redis -r 2 --dry-run=client -o yaml >> f.yaml
#g: pod, service
kubectl run httpd --image=httpd:alpine --port=80 -l app=httpd --dry-run=client -o yaml > g.yaml
echo "---" >> g.yaml
kubectl create svc clusterip httpd --tcp=80:80 --dry-run=client -o yaml >> g.yaml
#g2: expose=true ์ต์
์ผ๋ก ์๋ ๋
ธ์ถ(svc ์ ์)
kubectl run httpd --image=httpd:alpine --port=80 --expose=true --dry-run=client -o yaml > g2.yaml
# Create a one-time job for cronjob testing
bash
kubectl create job $jobname --from=cronjob/$cronjobname








Leave a Reply