ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

๐Ÿ“Œ NestJS + Kubernetes & ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ๋ฐฐํฌ: ์‹ค์ „ ๊ฐ€์ด๋“œ

1. ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ์•„ํ‚คํ…์ฒ˜(MSA) ๊ฐœ๋…๊ณผ NestJS ์ ์šฉ


๐Ÿ”น ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๋ž€?

๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ์•„ํ‚คํ…์ฒ˜(Microservices Architecture, MSA)๋Š” ํ•˜๋‚˜์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋…๋ฆฝ์ ์ธ ์„œ๋น„์Šค๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ์šด์˜ํ•˜๋Š” ์•„ํ‚คํ…์ฒ˜ ํŒจํ„ด์ž…๋‹ˆ๋‹ค.
NestJS๋Š” ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ํŒจํ„ด์„ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ง€์›ํ•˜๋ฉฐ, ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ๊ฐ„ ํ†ต์‹ , API Gateway, ๋ฉ”์‹œ์ง€ ํ(RabbitMQ, Kafka)์™€์˜ ํ†ตํ•ฉ ๋“ฑ์„ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


๐Ÿ”น Monolithic vs. Microservices ๋น„๊ต

์•„ํ‚คํ…์ฒ˜ ํŠน์ง• ์žฅ์  ๋‹จ์ 

Monolithic (๋ชจ๋†€๋ฆฌ์‹) ํ•˜๋‚˜์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์œผ๋กœ ๊ตฌ์„ฑ ๋ฐฐํฌ์™€ ๊ฐœ๋ฐœ์ด ๊ฐ„๋‹จ ํ™•์žฅ์„ฑ ๋ถ€์กฑ, ์œ ์ง€๋ณด์ˆ˜ ์–ด๋ ค์›€
Microservices (๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค) ๋…๋ฆฝ์ ์ธ ์„œ๋น„์Šค๋กœ ๊ตฌ์„ฑ ํ™•์žฅ์„ฑ ๋›ฐ์–ด๋‚˜๊ณ  ์œ ์ง€๋ณด์ˆ˜ ์šฉ์ด ์„œ๋น„์Šค ๊ฐ„ ํ†ต์‹  ๋ณต์žก

๐Ÿ’ก ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๋Š” ์„œ๋น„์Šค๋ณ„ ๋…๋ฆฝ์ ์ธ ํ™•์žฅ ๋ฐ ๋ฐฐํฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜์—ฌ, ๋Œ€๊ทœ๋ชจ ํŠธ๋ž˜ํ”ฝ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์„œ๋น„์Šค์— ์œ ๋ฆฌํ•ฉ๋‹ˆ๋‹ค.


๐Ÿ”น NestJS์—์„œ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ํŒจํ„ด ์ ์šฉ

๋ฐ˜์‘ํ˜•

โœ… NestJS์—์„œ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๋ฅผ ์ง€์›ํ•˜๋Š” ๋ฐฉ์‹

  1. NestJS์˜ Microservices Module ํ™œ์šฉ
  2. gRPC ๊ธฐ๋ฐ˜ ํ†ต์‹  ์ง€์›
  3. Kafka, RabbitMQ์™€ ๊ฐ™์€ ๋ฉ”์‹œ์ง€ ํ ์—ฐ๋™ ๊ฐ€๋Šฅ
  4. API Gateway๋ฅผ ํ†ตํ•œ ์—”๋“œํฌ์ธํŠธ ํ†ตํ•ฉ ๊ด€๋ฆฌ

๐Ÿ”น NestJS ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ๊ธฐ๋ณธ ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ

nestjs-microservices/
โ”œโ”€โ”€ apps/
โ”‚   โ”œโ”€โ”€ api-gateway/  # API Gateway
โ”‚   โ”œโ”€โ”€ user-service/  # ์‚ฌ์šฉ์ž ์„œ๋น„์Šค
โ”‚   โ”œโ”€โ”€ order-service/ # ์ฃผ๋ฌธ ์„œ๋น„์Šค
โ”œโ”€โ”€ libs/
โ”‚   โ”œโ”€โ”€ common/        # ๊ณตํ†ต ์œ ํ‹ธ๋ฆฌํ‹ฐ ๋ชจ๋“ˆ
โ”‚   โ”œโ”€โ”€ database/      # ๊ณตํ†ต ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ชจ๋“ˆ

๐Ÿ’ก ๊ฐ ์„œ๋น„์Šค๋Š” ๋…๋ฆฝ์ ์œผ๋กœ ์‹คํ–‰๋˜๋ฉฐ, API Gateway๋ฅผ ํ†ตํ•ด ์—”๋“œํฌ์ธํŠธ๋ฅผ ํ†ตํ•ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


๐Ÿ”น NestJS ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ๊ธฐ๋ณธ ๊ตฌํ˜„ (TCP ํ†ต์‹  ์˜ˆ์ œ)

โœ… 1) nestjs ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

nest new nestjs-microservices
cd nestjs-microservices

โœ… 2) ์‚ฌ์šฉ์ž ์„œ๋น„์Šค ์ƒ์„ฑ (user-service)

nest g app user-service

๐Ÿ“‚ apps/user-service/main.ts

import { NestFactory } from '@nestjs/core';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { UserModule } from './user.module';

async function bootstrap() {
  const app = await NestFactory.createMicroservice<MicroserviceOptions>(
    UserModule,
    {
      transport: Transport.TCP,
      options: { host: '127.0.0.1', port: 3001 },
    },
  );
  await app.listen();
}

bootstrap();

โœ… 3) API Gateway ์ƒ์„ฑ ๋ฐ ์„ค์ •

nest g app api-gateway

๐Ÿ“‚ apps/api-gateway/main.ts

import { NestFactory } from '@nestjs/core';
import { ApiGatewayModule } from './api-gateway.module';

async function bootstrap() {
  const app = await NestFactory.create(ApiGatewayModule);
  await app.listen(3000);
}
bootstrap();

๐Ÿ“‚ apps/api-gateway/user.controller.ts

import { Controller, Get, Inject } from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices';

@Controller('users')
export class UserController {
  constructor(@Inject('USER_SERVICE') private readonly client: ClientProxy) {}

  @Get()
  async getUsers() {
    return this.client.send('get_users', {});
  }
}

๐Ÿ“‚ apps/user-service/user.controller.ts

import { Controller } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';

@Controller()
export class UserController {
  @MessagePattern('get_users')
  async getUsers() {
    return [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
  }
}

๐Ÿ”น ์‹คํ–‰ ๋ฐ ํ…Œ์ŠคํŠธ

โœ… 1) User Service ์‹คํ–‰

npm run start:user-service

์ถœ๋ ฅ ์˜ˆ์‹œ:

Listening on TCP: 127.0.0.1:3001

โœ… 2) API Gateway ์‹คํ–‰

npm run start:api-gateway

โœ… 3) API ์š”์ฒญ ํ…Œ์ŠคํŠธ

curl http://localhost:3000/users

์‘๋‹ต:

[
  { "id": 1, "name": "Alice" },
  { "id": 2, "name": "Bob" }
]

๐Ÿ’ก NestJS์˜ Microservices Module์„ ํ™œ์šฉํ•˜์—ฌ, TCP ๊ธฐ๋ฐ˜์˜ ๊ฐ„๋‹จํ•œ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๋ฅผ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


๐Ÿ”น ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ์ ์šฉ ์‹œ ๊ณ ๋ คํ•  ์ 

โœ” ์„œ๋น„์Šค ๊ฐ„์˜ ๋…๋ฆฝ์ ์ธ ๋ฐฐํฌ ๋ฐ ํ™•์žฅ ๊ฐ€๋Šฅ
โœ” ํ†ต์‹  ๋ฐฉ์‹ ์„ ํƒ์ด ์ค‘์š” (gRPC, Kafka, RabbitMQ ๋“ฑ)
โœ” ์„œ๋น„์Šค ๊ฐ„ ๋ฐ์ดํ„ฐ ๋™๊ธฐํ™” ๋ฐ ์ด๋ฒคํŠธ ๊ธฐ๋ฐ˜ ์„ค๊ณ„ ํ•„์š”
โœ” Kubernetes ๋ฐฐํฌ ์‹œ ์„œ๋น„์Šค ๊ฐ„ ๋„คํŠธ์›Œํฌ ์„ค์ • ๊ณ ๋ ค ํ•„์š”


๐Ÿ”น ๊ฒฐ๋ก : NestJS์—์„œ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ์•„ํ‚คํ…์ฒ˜ ์‹œ์ž‘ํ•˜๊ธฐ

โœ… NestJS๋Š” ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๋ฅผ ์‰ฝ๊ฒŒ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ
โœ… TCP ๊ธฐ๋ฐ˜์˜ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๋ฅผ ์„ค์ •ํ•˜๊ณ , API Gateway๋ฅผ ํ†ตํ•ด ์„œ๋น„์Šค ํ˜ธ์ถœ ๊ฐ€๋Šฅ
โœ… Kafka, RabbitMQ, gRPC ๋“ฑ ๋‹ค์–‘ํ•œ ํ†ต์‹  ๋ฐฉ์‹ ์ ์šฉ ๊ฐ€๋Šฅ
โœ… ๋‹ค์Œ ๊ธ€์—์„œ๋Š” NestJS ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๋ฅผ Docker๋กœ ์ปจํ…Œ์ด๋„ˆํ™”ํ•˜์—ฌ ๋ฐฐํฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค! ๐Ÿš€


๐Ÿ” ๋‹ค์Œ ๊ธ€ ์˜ˆ๊ณ : NestJS ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ์ปจํ…Œ์ด๋„ˆํ™” ๋ฐ Docker ๋ฐฐํฌ

 

โ€ป ์ด ํฌ์ŠคํŒ…์€ ์ฟ ํŒก ํŒŒํŠธ๋„ˆ์Šค ํ™œ๋™์˜ ์ผํ™˜์œผ๋กœ, ์ด์— ๋”ฐ๋ฅธ ์ผ์ •์•ก์˜ ์ˆ˜์ˆ˜๋ฃŒ๋ฅผ ์ œ๊ณต๋ฐ›์Šต๋‹ˆ๋‹ค.
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
ยซ   2025/04   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
๊ธ€ ๋ณด๊ด€ํ•จ
๋ฐ˜์‘ํ˜•