Kubernetes kubectl Sheet: Pods & Cluster Ops

๐Ÿ’ก 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

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

This post is part of Coupang Partners and AliExpress affiliate activities, and a certain amount of commission is received accordingly.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *