본문 바로가기

컨테이너/Kubernetes

[쿠버네티스] (6) 서비스 & microservice

Connect a Frontend to a Backend Using Services

This task shows how to create a frontend and a backend microservice. The backend microservice is a hello greeter. The frontend exposes the backend using nginx and a Kubernetes Service object.

Objectives

  • Create and run a sample hello backend microservice using a Deployment object.
  • Use a Service object to send traffic to the backend microservice's multiple replicas.
  • Create and run a nginx frontend microservice, also using a Deployment object.
  • Configure the frontend microservice to send traffic to the backend microservice.
  • Use a Service object of type=LoadBalancer to expose the frontend microservice outside the cluster.

마이크로 서비스를 간단하게 구현해보자.

전 시간에 배운 내용을 참고해서, 백엔드 POD 와 프론트엔드 POD를 각기 다른 서비스에 배포 해본후에 서로 통신을 하여 

데이터를 받아서 출력하는데 까지 목표로 한다.

서비스타입은 LoadBalancer로 클러스터 밖에서 들어오는 request를 분산하여 본다.

 

 

 

총 4개의 Yaml 파일을 실행하여

backend - deployment

backend- service

frontend- deployment

frontend - service 

 

총 4개의 오브젝트를 생성할것이다.

 

이중 backend는 80 포트로 요청이 왔을때 {"message":"Hello"} 라고 출력해주는 POD이고

frontend는 curl http://{{EXTERNAL-IP}}} 로 request가 오게되면 backend POD로 요청을 전달해주는 역할을 한다.


Backend-deployment.yaml

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend
spec:
  selector:
    matchLabels:
      app: hello
      tier: backend
      track: stable
  replicas: 3
  template:
    metadata:
      labels:
        app: hello
        tier: backend
        track: stable
    spec:
      containers:
        - name: hello
          image: "gcr.io/google-samples/node-hello:1.0"
          ports:
            - name: http
              containerPort: 80

Backend-Service.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: hello
spec:
  selector:
    app: hello
    tier: backend
  ports:
  - protocol: TCP
    port: 80
    targetPort: http

Frontend-Deployment.yaml

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend
spec:
  selector:
    matchLabels:
      app: hello
      tier: frontend
      track: stable
  replicas: 1
  template:
    metadata:
      labels:
        app: hello
        tier: frontend
        track: stable
    spec:
      containers:
        - name: nginx
          image: "gcr.io/google-samples/hello-frontend:1.0"
          lifecycle:
            preStop:
              exec:
                command: ["/usr/sbin/nginx","-s","quit"]

Frontend-service.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: frontend
spec:
  selector:
    app: hello
    tier: frontend
  ports:
  - protocol: "TCP"
    port: 80
    targetPort: 80
  type: LoadBalancer
~