본문 바로가기
DevOps/DockerKubernetes

kubectl 명령어

by 계영수 2022. 9. 15.
728x90

What is kubectl?

kubectl is a command line tool that allows you to interact with Kubernetes. Kubectl uses the Kubernetes API to communicate with the cluster and carry out your commands.

 

"You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs."

 

▶ kubectl get

Use kubectl get to list objects in the kubernetes cluster.

$ kubectl get <object type> <object name> -o <output> --sort-by <JSONpath> --selecto <selector>
  • -o : Set output format.
  • --sort-by : Sort output using a JSONPath expression
  • --selector : Filter results by label.

kubectl describe

You can get detailed information about Kubernetes objects using kubectl describe.

$ kubectl describe <object type> <object name>

▶ kubectl create

Use kubectl create to create objects.

Supply a YAML file with -f to create an object from a YAML descriptor stored in the file.

If you attempt to create an object that already exists, an error will occur.

$ kubectl create -f <file name>

kubectl apply

kubectl apply is similar to kubectl create. However, it you use kubectl apply on an object that already exists, it will modify the existing object, if possible.

$ kubectl apply -f <file name>

▶ kubectl delete

Use kubectl delete to delete objects from the cluster.

$ kubectl delete <object type> <object name>

▶ kubectl exec

kubectl exec can be used to run commands inside containers. Keep in mind that, in order for a command to succeed, the necessary software must exist within the container to run it.

$ kubectl exec <pod name> -c <container name> -- <command>

LAB

1. Pod 생성을 위한 yaml 파일 구성

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: busybox
    image: radial/busyboxplus:curl
    command: ['sh', '-c', 'while true; do sleep 3600; done']

2.

$ kubectl apply -f pod.yml
$ kubectl get api-resources
$ kubectl get pods
$ kubectl get pods my-pod
$ kubectl get pods -o wide
$ kubectl get pods -o json
$ kubectl get pods -o yaml
$ kubectl get pods -o wide --sort-by .spec.nodeName
$ kubectl get pods -n kube-system
$ kubectl get pods -n kube-system --selecot k8s-app=calico-node
$ kubectl create -f pod.yml (error)
$ kubectl exec mypod -c busybox -- echo "Hello, World!"
$ kubectl delete pod my-pod

728x90

'DevOps > DockerKubernetes' 카테고리의 다른 글

kubectl Tips  (0) 2022.09.16
Backup and Restore etcd  (0) 2022.09.15
YAML 공부하기  (0) 2022.09.04
Pod  (0) 2022.09.04
우분투 리눅스에서 도커 설치  (0) 2022.09.04