--dry-run: 리소스 즉시 생성
--dry-run=client: 커맨드 테스트 -> 리소스가 생성되지 않음.
-o yaml: 리소스 정의를 YAML 파일 형식으로 생성해준다.
$ kubectl run nginx --image=nginx --dry-run=client -o yaml > nginx-pod.yaml
=> 리다이렉션으로 nginx-pod.yaml 파일을 생성할 수 있고, 해당 파일을 수정 및 리소스를 추가하여 업데이트 할 수 있다.
예제:
# POD
Create an NGINX Pod
$ kubectl run nginx --image=nginx
Generate POD Manifest YAML file (-o yaml). Don't create it(--dry-run)
$ kubectl run nginx --image=nginx --dry-run=client -o yaml
# Deployment
Create a deployment
$ kubectl create deployment --image=nginx nginx
Generate Deployment YAML file (-o yaml). Don't create it(--dry-run)
$ kubectl create deployment --image=nginx nginx --dry-run=client -o yaml
Generate Deployment with 4 Replicas
$ kubectl create deployment --image=nginx nginx --replicas=4
=
$ kubectl scale deployment nginx --replicas=4
Another way to do this is to save the YAML definition to a file and modify
$ kubectl create deployment nginx --image=nginx --dry-run=client -o yaml > nginx-deployment.yaml
=> 이렇게 yaml 파일을 리다이렉션으로 생성하게되면 create를 하기 전에 replicas 등 다른 리소스 필드를 수정 할 수 있다.
# Service
Create a Service named redis-service of type ClusterIP to expose pod redis on port 6379
$ kubectl expose pod redis --port=6379 --name=redis-service --dry-run=client -o yaml
=> pod의 라벨을 자동으로 사용한다.
or
$ kubectl create service clusterip redis --tcp=6379:6379 --dry-run=client -o yaml
=> => pod의 라벨을 자동으로 사용하지 않는다. 대신에 selector를 app=redis로 인식한다.
Create a Service named nginx of type NodePort to expose pod nginx's port 80 on port 30080 on the nodes:
$ kubectl expose pod nginx --port=80 --name=nginx-service --type=NodePort --dry-run=client -o yaml
=> pod의 라벨을 자동으로 사용한다.
or
$ kubectl create service nodeport nginx --tcp=80:80 --node-port=30080 --dry-run=client -o yaml
참고자료
https://kubernetes.io/docs/reference/kubectl/
Command line tool (kubectl)
Production-Grade Container Orchestration
kubernetes.io
'Devops > k8s' 카테고리의 다른 글
[k8s] imperative command (0) | 2023.11.09 |
---|---|
kubernetes 커맨드 shortcuts (0) | 2023.11.09 |
[CKAD] rewrite-target 옵션 (0) | 2023.11.02 |
[CKAD/Lab] Imperative commands (0) | 2023.09.25 |
[CKAD/Lab] - namespace (0) | 2023.09.24 |