728x90
다음과 같은 파일을 생성한다.
student@Master:~$ cat mynginx.yaml
#mynginx.yaml
apiVersion: v1
kind: Pod
metadata:
name: mynginx
spec:
containers:
- name: mynginx
image: nginx
student@Master:~$
YAML 파일을 기반으로 컨테이너를 생성한다.
student@Master:~$ kubectl apply -f mynginx.yaml
pod/mynginx created
student@Master:~$ kubectl get pod
NAME READY STATUS RESTARTS AGE
mynginx 1/1 Running 0 34s
student@Master:~$ kubectl get pod mynginx -o yaml
apiVersion: v1
kind: Pod
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"mynginx","namespace":"default"},"spec":{"containers":[{"image":"nginx","name":"mynginx"}]}}
creationTimestamp: "2022-08-20T05:14:58Z"
managedFields:
- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:metadata:
f:annotations:
.: {}
f:kubectl.kubernetes.io/last-applied-configuration: {}
f:spec:
f:containers:
~~~ 생략
kubectl run 명령어와 마찬가지로 mynginx 라는 컨테이너가 생성되는 것을 확인할 수 있다. kubectl run은 명령형 스타일의 컨테이너 실행 명령어이고, kubectl apply는 선언형 스타일의 명령어이다.
apply 명령어의 장점은 로컬 파일 시스템에 위치한 YAML 정의서 뿐만 아니라, 인터넷 상에 위치한 YAML 정의 파일도 가져다가 사용할 수 있다는 장점이 있다.
student@Master:~$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/pods/simple-pod.yaml
pod/nginx created
student@Master:~$ kubectl get pod
NAME READY STATUS RESTARTS AGE
mynginx 1/1 Running 0 4m9s
nginx 1/1 Running 0 12s
student@Master:~$
student@Master:~$ kubectl delete -f https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/pods/simple-pod.yaml
pod "nginx" deleted
student@Master:~$
이번에는 mynginx.yaml 파일에 라벨 정보를 추가하고 이미지 주소를 수정한다.
student@Master:~$ cat mynginx.yaml
#mynginx.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
hello: world
name: mynginx
spec:
containers:
- name: mynginx
image: nginx:1.17.2
student@Master:~$
앞서와 동일하게 apply 명령어를 이용하여 선언형 명령을 내린다.
student@Master:~$ kubectl get pod
NAME READY STATUS RESTARTS AGE
mynginx 1/1 Running 0 10m
student@Master:~$ kubectl apply -f mynginx.yaml
pod/mynginx configured
student@Master:~$ kubectl get pod
NAME READY STATUS RESTARTS AGE
mynginx 1/1 Running 0 10m
student@Master:~$ kubectl get pod mynginx -o yaml
apiVersion: v1
kind: Pod
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"labels":{"hello":"world"},"name":"mynginx","namespace":"default"},"spec":{"containers":[{"image":"nginx:1.17.2","name":"mynginx"}]}}
creationTimestamp: "2022-08-20T05:14:58Z"
labels:
hello: world
managedFields:
- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:metadata:
f:annotations:
.: {}
~~~~ 생략
그러면 pod/mynginx configured 라는 메시지와 함께 새로 컨테이너가 생성되는 것이 아니라, 기존의 컨테이너의 설정값이 수정되었다. 마지막으로 다시 동일한 파일에 대해서 똑깥이 Apply 명령어를 내리면 어떻게 될까?
student@Master:~$ kubectl apply -f mynginx.yaml
pod/mynginx unchanged
student@Master:~$ kubectl get pod
NAME READY STATUS RESTARTS AGE
mynginx 1/1 Running 1 13m
student@Master:~$ kubectl get pod mynginx -o yaml
apiVersion: v1
kind: Pod
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"labels":{"hello":"world"},"name":"mynginx","namespace":"default"},"spec":{"containers":[{"image":"nginx:1.17.2","name":"mynginx"}]}}
creationTimestamp: "2022-08-20T05:14:58Z"
labels:
hello: world
managedFields:
- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:metadata:
f:annotations:
.: {}
f:kubectl.kubernetes.io/last-applied-configuration: {}
f:labels:
.: {}
f:hello: {
~~~ 생략
728x90
'DevOps > DockerKubernetes' 카테고리의 다른 글
| kubernetes를 사용하면 무엇을 할 수 있을까? (0) | 2022.09.03 |
|---|---|
| 리소스 별 Kubernetes 명령어 (0) | 2022.08.20 |
| 쿠버네티스 기본 명령어 (0) | 2022.08.16 |
| Kubernetes 설치 (0) | 2022.08.16 |
| Ubuntu에 Minikube 설치하기 (0) | 2022.08.16 |