[AWS RDS] AWS RDS์— ๊ณ ์ • IP ๋ฐœ๊ธ‰๋ฐ›๊ธฐ (NLB, nginx๋ฅผ ์ด์šฉํ•œ proxy server ๊ตฌ์„ฑ)

2022. 5. 28. 01:09ใ†AWS

โ˜„๏ธ ๋ฌธ์ œ

ํ†ต์‹  ๊ตฌ์กฐ ์ƒ์˜ ๋ฌธ์ œ๋กœ IP๋กœ๋งŒ ํ†ต์‹ ์ด ๊ฐ€๋Šฅํ•œ ์ƒํ™ฉ์—์„œ RDS์™€ ํ†ต์‹ ์„ ํ•ด์•ผ ํ•œ๋‹ค.

RDS์˜ ์—”๋“œํฌ์ธํŠธ๋Š” ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•˜๊ณ  RDS ์ธ์Šคํ„ด์Šค์˜ IP๋Š” ๋ณ€๋™ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์–ด ๊ณ ์ •์ ์ธ IP๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

 

 

๐ŸŒŽ ํ™˜๊ฒฝ

  • AWS RDS
    • Engine : SQL Server Standard Edition
    • ๋‹ค์ค‘ AZ (Multi-AZ)
    • RDS ์ธ์Šคํ„ด์Šค์—๋Š” ๊ณ ์ • IP ์ฃผ์†Œ๋‚˜ ํƒ„๋ ฅ์  IP ์ฃผ์†Œ๋ฅผ ํ• ๋‹นํ•  ์ˆ˜ ์—†๋‹ค.

 


๐Ÿ”ซ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์ด ์žˆ๊ฒ ์ง€๋งŒ,

์‹ ๊ทœ ํ”„๋ก์‹œ ์„œ๋ฒ„๋ฅผ ๊ตฌ์„ฑํ•˜์—ฌ nginx๋ฅผ ์ด์šฉํ•œ ํฌ์›Œ๋”ฉ์œผ๋กœ RDS ์—”๋“œํฌ์ธํŠธ ํ†ต์‹ ์„ ํ•˜๊ณ , ํ”„๋ก์‹œ ์„œ๋ฒ„ ์•ž๋‹จ์— NLB๋ฅผ ๊ตฌ์„ฑํ•˜์—ฌ NLB์˜ ๊ณ ์ • IP๋ฅผ ์ด์šฉํ•˜๋„๋ก ํ•ด๋ณด์ž. (NLB์˜ ํƒ€๊ฒŸ์œผ๋กœ๋Š” RDS ์„ค์ •์ด ๋ถˆ๊ฐ€๋Šฅํ•˜์—ฌ proxy server ์šฉ๋„์˜ nginx๋ฅผ ์ด์šฉํ•ด๋ณด์ž)

To-Be ์•„ํ‚คํ…์ฒ˜

 

1. EC2 ๊ตฌ์„ฑ

  • ๊ณ ๊ฐ€์šฉ์„ฑ์„ ์œ„ํ•ด proxy server ์šฉ๋„์˜ EC2 node 2๊ฐœ ๊ตฌ์„ฑ

 

2. nginx ์„ค์น˜

# nginx ์„ค์น˜ 
sudo yum install nginx

 

3. nginx ์„ค์ •

  • RDS๋กœ ์ธ์ž…๋˜๋Š” TCP ํŠธ๋ž˜ํ”ฝ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด stream block์„ ์ด์šฉํ•˜์—ฌ ์„ค์ •ํ•œ๋‹ค.
# nginx์—์„œ stream ์ง€์‹œ์ž๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์„ค์น˜ 
sudo yum install nginx-mod-stream
# /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

stream {
    upstream db {
        server { RDS-endpoint }:1433;
    }
    server {
        listen 1433;
        proxy_pass db;
    }
}
# ๋ณ€๊ฒฝ๋œ ์„ค์ •ํŒŒ์ผ(nginx.conf)์„ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด nginx ์‹œ์ž‘
sudo nginx -s reload
sudo systemctl restart nginx

# nginx ์„œ๋น„์Šค๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ๊ตฌ๋™๋˜๋Š”์ง€ ํ™•์ธ
sudo systemctl status nginx

# ์„œ๋ฒ„๋ฅผ ์žฌ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ์—๋„ nginx ์ž๋™์œผ๋กœ ์„œ๋น„์Šคํ•˜๋„๋ก enable
sudo systemctl enable nginx
๐Ÿšจ ์ค‘์š” ๐Ÿšจ
Nginx์—์„œ RDS-endpoint๋กœ ํŠธ๋ž˜ํ”ฝ์„ ์ „๋‹ฌํ•˜๋„๋ก ์„ค์ •ํ•˜์˜€์ง€๋งŒ,
rds์˜ real ip๊ฐ€ ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒฝ์šฐ nginx๊ฐ€ ์ด๋ฅผ ์ธ์ง€ํ•˜์ง€ ๋ชปํ•˜๊ณ  ๋ณ€๊ฒฝ ์ „์˜ ip๋กœ ํŠธ๋ž˜ํ”ฝ์„ ์ „๋‹ฌํ•˜์—ฌ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋‹ค.

⇒ ์ถ”์ • ์›์ธ: nginx ๋‚ด๋ถ€์˜ DNS caching
ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•: โญ๏ธ resolver ์˜ต์…˜ ์ ์šฉ ํ•„์ˆ˜ โญ๏ธ
 ์ฐธ๊ณ  ๋งํฌ: NGINX Dynamic IP address upstream - DNS Cache issue | How to solve

 

 

 

3. RDS ๋ณด์•ˆ๊ทธ๋ฃน ์„ค์ •

  • RDS ๋ณด์•ˆ๊ทธ๋ฃน์˜ Inbound ๊ทœ์น™์— Proxy Server์— ๋Œ€ํ•ด 1433 ํฌํŠธ๋ฅผ ํ—ˆ์šฉํ•˜๋Š” ๊ทœ์น™์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

 

4. ํƒ€๊ฒŸ ๊ทธ๋ฃน ์ƒ์„ฑ

  • ๋Œ€์ƒ : ์ธ์Šคํ„ด์Šค (ํ”„๋ก์‹œ ์„œ๋ฒ„)
  • ํ”„๋กœํ† ์ฝœ : TCP
  • ํฌํŠธ : 1433 (proxy server๊ฐ€ 1433์„ listen ํ•˜๋„๋ก ์„ค์ •ํ–ˆ๊ธฐ ๋•Œ๋ฌธ)
  • ํƒ€๊ฒŸ์œผ๋กœ 1์—์„œ ๊ตฌ์„ฑํ•œ EC2 2๋Œ€๋ฅผ ์ถ”๊ฐ€.

 

5. NLB ๊ตฌ์„ฑ

  • ๊ต์ฐจ์˜์—ญ ๋กœ๋“œ๋ฐธ๋Ÿฐ์‹ฑ ํ™œ์„ฑํ™” (
  • 1433 ํฌํŠธ์— ๋Œ€ํ•ด ๋“ค์–ด์˜จ ์š”์ฒญ์„ 5์—์„œ ์ƒ์„ฑํ•œ ํƒ€๊ฒŸ ๊ทธ๋ฃน์œผ๋กœ ์ „๋‹ฌํ•˜๋Š” ๋ฆฌ์Šค๋„ˆ ์ถ”๊ฐ€

 

6. proxy server์˜ ๋ณด์•ˆ๊ทธ๋ฃน ์„ค์ •

  • 1์—์„œ ์ƒ์„ฑํ•œ EC2 ๋ณด์•ˆ๊ทธ๋ฃน์˜ inbound ๊ทœ์น™์— NLB์™€ Source๋กœ๋ถ€ํ„ฐ 1433 ํฌํŠธ๋ฅผ ํ—ˆ์šฉํ•˜๋Š” ๊ทœ์น™์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

 

 

๐Ÿ™Œ 6๋ฒˆ๊นŒ์ง€์˜ ์„ค์ •์„ ์™„๋ฃŒํ•˜๋ฉด ์†Œ์Šค์—์„œ NLB์˜ ๊ณ ์ • IP๋กœ 1433์œผ๋กœ RDS์™€ ํ†ต์‹  ๊ฐ€๋Šฅ~!


๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ

  1. Amazon RDS DB ์ธ์Šคํ„ด์Šค์— ํ• ๋‹น๋œ IP ์ฃผ์†Œ์— ๋Œ€ํ•ด ์•Œ์•„์•ผ ํ•  ์‚ฌํ•ญ์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?
  2. EC2 proxy to RDS for a static IP address
  3. Using a TCP proxy to connect to SQL Database over VPN