Kubernetes & Docker/궁금한 것

쿠버네티스 YAML 파일 작성 시 실행 순서에 대해서

정현수 2020. 7. 17. 08:49
반응형

※주의

이 글은 제가 혼자 공부하면서 여기저기 찾아보고 정리하는 곳이라서

글의 퀄리티나 내용상 맞지 않는 부분들이 있습니다.

공식문서가 더욱 더 큰 도움이 될 수 있습니다!

궁금점

YAML 하나의 파일안에 여러 개의 오브젝트를 생성했을 때,

실행 순서는 위에서부터 아래로 생성이 될까?


행동

apiVersion: apps/v1 
kind: Deployment                        # deployment 
metadata: 
  name: jenkins                         # deployment의 이름 = jenkins 
  labels: 
    app: MyApp                          # deployment의 레이블 = MyApp 
spec: 
  replicas: 1                           # 최대 생성 pod 개수 
  selector: 
    matchLabels:                         
      app: MyApp                        # MyApp이라는 레이블을 가진 container와 연결해준다. 
  template: 
    metadata: 
      labels: 
        app: MyApp                      # pod tempate의 레이블 명세 
    spec: 
      containers: 
      - name: jenkins                   # jenkins 컨테이너 명세 
        image: jenkins/jenkins:lts      # jenkins 이미지 이름 명시 
        ports: 
        - containerPort: 8080           # container에서의 port번호 
          name: jenkins                 # port의 이름 = Service와 connect 
--- 
apiVersion: apps/v1 
kind: Deployment 
metadata: 
  name: nexus 
  labels: 
    app: MyApp 
spec: 
  replicas: 1 
  selector: 
    matchLabels: 
      app: MyApp 
  template: 
    metadata: 
      labels: 
        app: MyApp                       
    spec: 
      containers: 
      - name: nexus
        image: sonatype/nexus3:latest
        ports:
        - containerPort: 8081
          name: nexus
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat
  labels:
    app: MyApp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: MyApp
  template:
    metadata:
      labels:
        app: MyApp
    spec:
      containers:
      - name: tomcat
        image: tomcat
        ports:
        - containerPort: 8080
          name: tomcat
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mariadb
  labels:
    app: MyApp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: MyApp
  template:
    metadata:
      labels:
        app: MyApp
    spec:
      containers:
      - name: mariadb
        image: mariadb
        volumeMounts:
        - name: test-volume
          mountPath: /myvolume
        ports:
        - containerPort: 3306
          name: mariadb
        env:                            #환경변수 설정
        - name: MYSQL_ROOT_PASSWORD     #root_password 
          value: admin                  # = admin
      volumes:
      - name: test-volume
        hostPath:
          path: /mounttest-minikube
          type: Directory
---
apiVersion: v1
kind: Service
metadata:
  name: service
spec:
  type: NodePort
  ports:
    - name: jenkins
      protocol: TCP                     #기본 프로토콜 : TCP 
      port: 8080                        #Service 자신의 포트
      targetPort: jenkins               #Pod 내 컨테이너 포트 (deployment의 port name과 연결)
      nodePort: 30001
    - name: nexus
      protocol: TCP
      port: 8081
      targetPort: nexus
      nodePort: 30001
    - name: tomcat
      protocol: TCP
      port: 8082
      targetPort: tomcat
      nodePort: 30003
    - name: mariadb
      protocol: TCP
      port: 3306
      targetPort: mariadb
      nodePort: 30004
  selector:
    app: MyApp

 

현재

Jenkins deployment - Nexus deployment - Tomcat deployement - Mariadb deployment - Service

순으로 되어있습니다.

 

실행을 시키면

 

위에서부터 순차적으로 실행이 되는 것을 볼 수 있습니다.

순서를 바꿔도 똑같이 순차적으로 실행이 될까요?

 

apiVersion: v1
kind: Service
metadata:
  name: service
spec:
  type: NodePort
  ports:
    - name: jenkins
      protocol: TCP                     #기본 프로토콜 : TCP 
      port: 8080                        #Service 자신의 포트
      targetPort: jenkins               #Pod 내 컨테이너 포트 (deployment의 port name과 연결)
      nodePort: 30001
    - name: nexus
      protocol: TCP
      port: 8081
      targetPort: nexus
      nodePort: 30001
    - name: tomcat
      protocol: TCP
      port: 8082
      targetPort: tomcat
      nodePort: 30003
    - name: mariadb
      protocol: TCP
      port: 3306
      targetPort: mariadb
      nodePort: 30004
  selector:
    app: MyApp
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mariadb
  labels:
    app: MyApp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: MyApp
  template:
    metadata:
      labels:
        app: MyApp
    spec:
      containers:
      - name: mariadb
        image: mariadb
        volumeMounts:
        - name: test-volume
          mountPath: /myvolume
        ports:
        - containerPort: 3306
          name: mariadb
        env:                            #환경변수 설정
        - name: MYSQL_ROOT_PASSWORD     #root_password 
          value: admin                  # = admin
      volumes:
      - name: test-volume
        hostPath:
          path: /mounttest-minikube
          type: Directory
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat
  labels:
    app: MyApp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: MyApp
  template:
    metadata:
      labels:
        app: MyApp
    spec:
      containers:
      - name: tomcat
        image: tomcat
        ports:
        - containerPort: 8080
          name: tomcat
---
apiVersion: apps/v1 
kind: Deployment 
metadata: 
  name: nexus 
  labels: 
    app: MyApp 
spec: 
  replicas: 1 
  selector: 
    matchLabels: 
      app: MyApp 
  template: 
    metadata: 
      labels: 
        app: MyApp                       
    spec: 
      containers: 
      - name: nexus
        image: sonatype/nexus3:latest
        ports:
        - containerPort: 8081
          name: nexus
---
apiVersion: apps/v1 
kind: Deployment                        # deployment 
metadata: 
  name: jenkins                         # deployment의 이름 = jenkins 
  labels: 
    app: MyApp                          # deployment의 레이블 = MyApp 
spec: 
  replicas: 1                           # 최대 생성 pod 개수 
  selector: 
    matchLabels:                         
      app: MyApp                        # MyApp이라는 레이블을 가진 container와 연결해준다. 
  template: 
    metadata: 
      labels: 
        app: MyApp                      # pod tempate의 레이블 명세 
    spec: 
      containers: 
      - name: jenkins                   # jenkins 컨테이너 명세 
        image: jenkins/jenkins:lts      # jenkins 이미지 이름 명시 
        ports: 
        - containerPort: 8080           # container에서의 port번호 
          name: jenkins                 # port의 이름 = Service와 connect 

이번에는 거꾸로 순서를 바꿔서

Service - Mariadb deployment - Tomcat deployment - Nexus deployment - Jenkins deployment

순서로 실행을 해보았습니다.

 


결과

kubectl apply 명령어는 yaml 파일에 적힌 오브젝트들의 순서대로 생성이 됩니다!

반응형