From 1b52f148abe46f9d02f6aa08dd513496d3195e9c Mon Sep 17 00:00:00 2001 From: seungheonlee Date: Sun, 31 Aug 2025 15:58:16 +0900 Subject: [PATCH 1/7] =?UTF-8?q?chore:=20mysql=5Freplication=5Fsetup=20?= =?UTF-8?q?=EC=84=9C=EB=B9=84=EC=8A=A4=EC=97=90=20setup=20=ED=94=84?= =?UTF-8?q?=EB=A1=9C=ED=95=84=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker-compose-db.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/docker-compose-db.yml b/docker-compose-db.yml index 6254cb6..103bcba 100644 --- a/docker-compose-db.yml +++ b/docker-compose-db.yml @@ -50,6 +50,7 @@ services: retries: 10 mysql_replication_setup: + profiles: [ setup ] image: mysql:8.0 container_name: mysql_replication_setup volumes: From bc1072ce0db82a451e3741dc8c757c65948f8ca5 Mon Sep 17 00:00:00 2001 From: seungheonlee Date: Sun, 31 Aug 2025 15:58:38 +0900 Subject: [PATCH 2/7] =?UTF-8?q?chore:=20elk-setup=20=EC=84=9C=EB=B9=84?= =?UTF-8?q?=EC=8A=A4=EC=97=90=20container=5Fname=20=EC=84=A4=EC=A0=95=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker-compose-elk.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/docker-compose-elk.yml b/docker-compose-elk.yml index 0e99b3f..6dedfa6 100644 --- a/docker-compose-elk.yml +++ b/docker-compose-elk.yml @@ -3,6 +3,7 @@ version: "3.8" services: setup: profiles: [ setup ] + container_name: rabbit-elk-setup init: true build: context: elk/setup/ From 7af927e6db27b7bd0e9d4d38faabca09e5cedb32 Mon Sep 17 00:00:00 2001 From: seungheonlee Date: Sun, 31 Aug 2025 16:13:16 +0900 Subject: [PATCH 3/7] =?UTF-8?q?chore:=20docker-compose=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=20=ED=8F=AC=EB=A7=B7=20=ED=86=B5=EC=9D=BC=20=EB=B0=8F?= =?UTF-8?q?=20=EA=B0=80=EB=8F=85=EC=84=B1=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 길이가 짧은 단순한 값의 경우 배열 형태로 변경하여 코드 일관성 및 가독성 향상 --- docker-compose-db.yml | 21 +++++++-------------- docker-compose-elk.yml | 13 +++---------- docker-compose.yml | 15 ++++----------- 3 files changed, 14 insertions(+), 35 deletions(-) diff --git a/docker-compose-db.yml b/docker-compose-db.yml index 103bcba..390434b 100644 --- a/docker-compose-db.yml +++ b/docker-compose-db.yml @@ -4,10 +4,8 @@ services: image: redis:alpine container_name: rabbit-redis hostname: redis - ports: - - "6379:6379" - networks: - - rabbit-db + ports: [ "6379:6379" ] + networks: [ rabbit-db ] mysql_master: container_name: rabbit-mysql-master @@ -16,13 +14,11 @@ services: MYSQL_DATABASE: rabbit MYSQL_ROOT_HOST: '%' MYSQL_ROOT_PASSWORD: 1234 - ports: - - "3306:3306" + ports: [ "3306:3306" ] volumes: - ./mysql/master-data-source.cnf:/etc/mysql/conf.d/my.cnf - ./mysql/init-master.sql:/docker-entrypoint-initdb.d/01-init-master.sql - networks: - - rabbit-db + networks: [ rabbit-db ] healthcheck: test: [ "CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p1234" ] timeout: 20s @@ -35,12 +31,10 @@ services: MYSQL_DATABASE: rabbit MYSQL_ROOT_HOST: '%' MYSQL_ROOT_PASSWORD: 1234 - ports: - - "3307:3306" + ports: [ "3307:3306" ] volumes: - ./mysql/replica-data-source.cnf:/etc/mysql/conf.d/my.cnf - networks: - - rabbit-db + networks: [ rabbit-db ] depends_on: mysql_master: condition: service_healthy @@ -56,8 +50,7 @@ services: volumes: - ./mysql/setup-replication.sh:/setup-replication.sh command: [ "/bin/bash", "/setup-replication.sh" ] - networks: - - rabbit-db + networks: [ rabbit-db ] depends_on: mysql_master: condition: service_healthy diff --git a/docker-compose-elk.yml b/docker-compose-elk.yml index 6dedfa6..c94c655 100644 --- a/docker-compose-elk.yml +++ b/docker-compose-elk.yml @@ -30,9 +30,7 @@ services: volumes: - ./elk/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml:ro,Z - elasticsearch:/usr/share/elasticsearch/data - ports: - - "9200:9200" - - "9300:9300" + ports: [ "9200:9200", "9300:9300" ] environment: ES_JAVA_OPTS: "-Xmx256m -Xms256m" ELASTIC_PASSWORD: ${ELASTIC_PASSWORD:-rabbit1234} @@ -48,11 +46,7 @@ services: volumes: - ./elk/logstash/config/logstash.yml:/usr/share/logstash/config/logstash.yml:ro,Z - ./elk/logstash/pipeline:/usr/share/logstash/pipeline:ro,Z - ports: - - "5044:5044" - - "50000:50000/tcp" - - "50000:50000/udp" - - "9600:9600" + ports: [ "5044:5044", "50000:50000/tcp", "50000:50000/udp", "9600:9600" ] environment: LS_JAVA_OPTS: "-Xmx256m -Xms256m" LOGSTASH_USERNAME: ${LOGSTASH_USERNAME:-rabbit_logstash} @@ -68,8 +62,7 @@ services: ELASTIC_VERSION: ${ELASTIC_VERSION:-8.10.2} volumes: - ./elk/kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml:ro,Z - ports: - - "5601:5601" + ports: [ "5601:5601" ] environment: KIBANA_SYSTEM_PASSWORD: ${KIBANA_SYSTEM_PASSWORD:-rabbit1234} networks: [ rabbit-elk ] diff --git a/docker-compose.yml b/docker-compose.yml index f2acc1b..23e6ec1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,29 +3,22 @@ services: rabbitmq: image: rabbitmq:3-management container_name: rabbit-rabbitmq - ports: - - "5672:5672" - - "15672:15672" - - "61613:61613" + ports: [ "5672:5672", "15672:15672", "61613:61613" ] command: > /bin/bash -c "rabbitmq-plugins enable --offline rabbitmq_management rabbitmq_stomp && rabbitmq-server" environment: RABBITMQ_DEFAULT_USER: guest RABBITMQ_DEFAULT_PASS: guest - networks: - - rabbit-default + networks: [ rabbit-default ] nginx: image: nginx:latest container_name: rabbit-nginx - ports: - - "80:80" - - "443:443" + ports: [ "80:80", "443:443" ] volumes: - ./nginx/config/nginx.conf:/etc/nginx/conf.d/default.conf - ./nginx/ssl:/etc/nginx/ssl - networks: - - rabbit-default + networks: [ rabbit-default ] networks: rabbit-default: From 2d3af4c82e56b5c2bf719fa9ff1fa41820cf214b Mon Sep 17 00:00:00 2001 From: seungheonlee Date: Sun, 31 Aug 2025 16:14:05 +0900 Subject: [PATCH 4/7] =?UTF-8?q?chore:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20sh=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- elk/setup/sh/tmp.sh | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 elk/setup/sh/tmp.sh diff --git a/elk/setup/sh/tmp.sh b/elk/setup/sh/tmp.sh deleted file mode 100644 index c6a5c1e..0000000 --- a/elk/setup/sh/tmp.sh +++ /dev/null @@ -1,24 +0,0 @@ -# -curl -u pitchain_logstash:pitchain_logstash_password -X GET "localhost:9200/_security/_authenticate?pretty" - -# pitchain_logstash 유저 생성 확인 -curl -u pitchain_logstash:pitchain_logstash_password http://elasticsearch:9200/ - -# logstash_writer Role 생성 확인 -curl -u elastic:pitchain_elasticsearch_password http://localhost:9200/_security/role/logstash_writer?pretty - -# 도커 컴포즈 세팅 -docker-compose --profile setup -f docker-compose-elk.yml up --build -d - -# es 삭제 -docker-compose -f docker-compose-elk.yml down -v # ES 컨테이너와 볼륨(설정) 삭제 -rm -rf ./elk/elasticsearch/data ─╯ -rm -rf ./elk/elasticsearch/nodes -docker volume rm pitchain_elasticsearchr - -docker-compose --env-file /home/ec2-user/app/.env --profile setup -f /home/ec2-user/app/docker-compose-elk.yml up --build -d -docker-compose --profile setup -f docker-compose-elk.yml up --build -d - - - -ssh -i /Users/seungheonlee/Desktop/IntelliJ/pitchain-server-key.pem ec2-user@ec2-43-201-79-150.ap-northeast-2.compute.amazonaws.com \ No newline at end of file From 5f4b322a1fe15fc214b05a6c5cad1823e2f80e55 Mon Sep 17 00:00:00 2001 From: seungheonlee Date: Sun, 31 Aug 2025 16:51:07 +0900 Subject: [PATCH 5/7] =?UTF-8?q?chore:=20mysql=5Freplication=5Fsetup=20?= =?UTF-8?q?=EC=BB=A8=ED=85=8C=EC=9D=B4=EB=84=88=20=EC=9D=B4=EB=A6=84=20?= =?UTF-8?q?=ED=8F=AC=EB=A7=B7=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker-compose-db.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose-db.yml b/docker-compose-db.yml index 390434b..e9c892d 100644 --- a/docker-compose-db.yml +++ b/docker-compose-db.yml @@ -46,7 +46,7 @@ services: mysql_replication_setup: profiles: [ setup ] image: mysql:8.0 - container_name: mysql_replication_setup + container_name: mysql-replication-setup volumes: - ./mysql/setup-replication.sh:/setup-replication.sh command: [ "/bin/bash", "/setup-replication.sh" ] From 4aef9f3a15dfaa40065d141aad5fc8ad9af47bef Mon Sep 17 00:00:00 2001 From: seungheonlee Date: Sun, 31 Aug 2025 16:53:31 +0900 Subject: [PATCH 6/7] =?UTF-8?q?chore:=20setup-replication.sh=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=20=EC=B6=9C=EB=A0=A5=20=EB=B0=8F=20=EA=B0=80=EB=8F=85?= =?UTF-8?q?=EC=84=B1=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mysql/setup-replication.sh | 117 +++++++++++++++++++------------------ 1 file changed, 59 insertions(+), 58 deletions(-) diff --git a/mysql/setup-replication.sh b/mysql/setup-replication.sh index 12bb831..e0ac17c 100755 --- a/mysql/setup-replication.sh +++ b/mysql/setup-replication.sh @@ -3,62 +3,70 @@ # MySQL Master-Replica 복제 설정 자동화 스크립트 set -e -echo "MySQL Master-Replica 복제 설정을 시작합니다..." - -# Master DB가 완전히 시작될 때까지 대기 -echo "Master DB 연결 대기 중..." +log() { + echo "$(date '+%Y-%m-%d %H:%M:%S') [+] $1" +} +sublog() { + echo "$(date '+%Y-%m-%d %H:%M:%S') ⠿ $1" +} +suberr() { + echo "$(date '+%Y-%m-%d %H:%M:%S') ❌ $1" >&2 +} + +log "MySQL Master-Replica 복제 설정을 시작합니다..." + +log "Master DB 연결 대기 중..." until mysql -h rabbit-mysql-master -u root -p1234 -e "SELECT 1" > /dev/null 2>&1; do - echo "Master DB 연결 대기 중..." - sleep 3 + sublog "Master DB 연결 대기 중..." + sleep 3 done +sublog "Master DB 연결됨" -# Replica DB가 완전히 시작될 때까지 대기 -echo "Replica DB 연결 대기 중..." +log "Replica DB 연결 대기 중..." until mysql -h rabbit-mysql-replica -u root -p1234 -e "SELECT 1" > /dev/null 2>&1; do - echo "Replica DB 연결 대기 중..." - sleep 3 + sublog "Replica DB 연결 대기 중..." + sleep 3 done +sublog "Replica DB 연결됨" -# 추가 안정화 대기 시간 -echo "DB 초기화 완료 대기 중..." +log "DB 초기화 완료 대기 중..." sleep 10 +sublog "DB 초기화 완료" -# Master DB에서 복제 사용자가 생성되었는지 확인 -echo "Master DB에서 복제 사용자 확인 중..." +log "Master DB에서 복제 사용자 확인 중..." REPLICA_USER_EXISTS=$(mysql -h rabbit-mysql-master -u root -p1234 -e "SELECT COUNT(*) FROM mysql.user WHERE user='replica';" 2>/dev/null | tail -n 1) if [ "$REPLICA_USER_EXISTS" -eq 0 ]; then - echo "복제 사용자가 존재하지 않습니다. 생성 중..." - mysql -h rabbit-mysql-master -u root -p1234 << EOF + sublog "복제 사용자가 존재하지 않습니다. 생성 중..." + mysql -h rabbit-mysql-master -u root -p1234 << EOF CREATE USER IF NOT EXISTS 'replica'@'%' IDENTIFIED WITH mysql_native_password BY '1234'; GRANT REPLICATION SLAVE ON *.* TO 'replica'@'%'; FLUSH PRIVILEGES; EOF - echo "복제 사용자가 생성되었습니다." + sublog "복제 사용자가 생성되었습니다." +else + sublog "복제 사용자가 이미 존재합니다." fi -# Master DB에서 바이너리 로그 상태 확인 -echo "Master DB에서 바이너리 로그 상태 확인 중..." +log "Master DB에서 바이너리 로그 상태 확인 중..." MASTER_STATUS=$(mysql -h rabbit-mysql-master -u root -p1234 -e "SHOW MASTER STATUS\G" 2>/dev/null) MASTER_FILE=$(echo "$MASTER_STATUS" | grep "File:" | awk '{print $2}') MASTER_POSITION=$(echo "$MASTER_STATUS" | grep "Position:" | awk '{print $2}') - -echo "Master File: $MASTER_FILE" -echo "Master Position: $MASTER_POSITION" +sublog "Master File: $MASTER_FILE" +sublog "Master Position: $MASTER_POSITION" if [ -z "$MASTER_FILE" ] || [ -z "$MASTER_POSITION" ]; then - echo "❌ Master 상태를 가져올 수 없습니다." - exit 1 + suberr "Master 상태를 가져올 수 없습니다." + exit 1 fi -# 기존 복제 설정 정리 -echo "기존 복제 설정 정리 중..." +log "기존 복제 설정 정리 중..." mysql -h rabbit-mysql-replica -u root -p1234 << EOF STOP SLAVE; RESET SLAVE ALL; EOF +sublog "기존 복제 설정 정리 완료" -# Replica DB에서 Master 설정 -echo "Replica DB에서 Master 연결 설정 중..." +log "Replica DB에서 Master 연결 설정 중..." mysql -h rabbit-mysql-replica -u root -p1234 << EOF CHANGE MASTER TO MASTER_HOST='rabbit-mysql-master', @@ -70,40 +78,33 @@ CHANGE MASTER TO MASTER_RETRY_COUNT=3; START SLAVE; EOF +sublog "Master 연결 설정 완료" -# 복제 연결 대기 및 상태 확인 -echo "복제 연결 대기 중..." +log "복제 연결 대기 및 상태 확인 중..." for i in {1..30}; do - SLAVE_STATUS=$(mysql -h rabbit-mysql-replica -u root -p1234 -e "SHOW SLAVE STATUS\G" 2>/dev/null) - IO_RUNNING=$(echo "$SLAVE_STATUS" | grep "Slave_IO_Running:" | awk '{print $2}') - SQL_RUNNING=$(echo "$SLAVE_STATUS" | grep "Slave_SQL_Running:" | awk '{print $2}') - - echo "시도 $i/30 - IO Running: $IO_RUNNING, SQL Running: $SQL_RUNNING" - - if [ "$IO_RUNNING" = "Yes" ] && [ "$SQL_RUNNING" = "Yes" ]; then - echo "✅ MySQL Master-Replica 복제 설정이 성공적으로 완료되었습니다!" - - # 복제 상태 상세 정보 출력 - echo "=== 복제 상태 상세 정보 ===" - mysql -h rabbit-mysql-replica -u root -p1234 -e "SHOW SLAVE STATUS\G" | grep -E "(Slave_IO_Running|Slave_SQL_Running|Master_Host|Master_User|Read_Master_Log_Pos|Exec_Master_Log_Pos)" - exit 0 - elif [ "$IO_RUNNING" = "No" ]; then - echo "❌ IO 스레드 연결 실패. 오류 확인 중..." - LAST_IO_ERROR=$(echo "$SLAVE_STATUS" | grep "Last_IO_Error:" | cut -d':' -f2- | xargs) - if [ -n "$LAST_IO_ERROR" ]; then - echo "IO 오류: $LAST_IO_ERROR" - fi - break + SLAVE_STATUS=$(mysql -h rabbit-mysql-replica -u root -p1234 -e "SHOW SLAVE STATUS\G" 2>/dev/null) + IO_RUNNING=$(echo "$SLAVE_STATUS" | grep "Slave_IO_Running:" | awk '{print $2}') + SQL_RUNNING=$(echo "$SLAVE_STATUS" | grep "Slave_SQL_Running:" | awk '{print $2}') + + sublog "시도 $i/30 - IO Running: $IO_RUNNING, SQL Running: $SQL_RUNNING" + + if [ "$IO_RUNNING" = "Yes" ] && [ "$SQL_RUNNING" = "Yes" ]; then + sublog "✅ MySQL Master-Replica 복제 설정이 성공적으로 완료되었습니다!" + log "복제 상태 상세 정보 출력" + mysql -h rabbit-mysql-replica -u root -p1234 -e "SHOW SLAVE STATUS\G" | grep -E "(Slave_IO_Running|Slave_SQL_Running|Master_Host|Master_User|Read_Master_Log_Pos|Exec_Master_Log_Pos)" + exit 0 + elif [ "$IO_RUNNING" = "No" ]; then + suberr "IO 스레드 연결 실패. 오류 확인 중..." + LAST_IO_ERROR=$(echo "$SLAVE_STATUS" | grep "Last_IO_Error:" | cut -d':' -f2- | xargs) + if [ -n "$LAST_IO_ERROR" ]; then + suberr "IO 오류: $LAST_IO_ERROR" fi - - sleep 2 + break + fi + sleep 2 done -echo "❌ 복제 설정에 문제가 발생했습니다." -echo "최종 상태: IO Running: $IO_RUNNING, SQL Running: $SQL_RUNNING" - -# 오류 정보 출력 -echo "=== 복제 오류 정보 ===" +suberr "복제 설정에 문제가 발생했습니다. 최종 상태: IO Running: $IO_RUNNING, SQL Running: $SQL_RUNNING" +log "복제 오류 정보 출력" mysql -h rabbit-mysql-replica -u root -p1234 -e "SHOW SLAVE STATUS\G" | grep -E "(Last_IO_Error|Last_SQL_Error)" - exit 1 From 494e9f9635830b069e63b535c90cc2ded3f60583 Mon Sep 17 00:00:00 2001 From: seungheonlee Date: Sun, 31 Aug 2025 16:54:13 +0900 Subject: [PATCH 7/7] =?UTF-8?q?chore:=20setup-replication.sh=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=EB=AA=85=20entrypoint.sh=EB=A1=9C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker-compose-db.yml | 4 ++-- mysql/{setup-replication.sh => entrypoint.sh} | 0 2 files changed, 2 insertions(+), 2 deletions(-) rename mysql/{setup-replication.sh => entrypoint.sh} (100%) diff --git a/docker-compose-db.yml b/docker-compose-db.yml index e9c892d..d297c1a 100644 --- a/docker-compose-db.yml +++ b/docker-compose-db.yml @@ -48,8 +48,8 @@ services: image: mysql:8.0 container_name: mysql-replication-setup volumes: - - ./mysql/setup-replication.sh:/setup-replication.sh - command: [ "/bin/bash", "/setup-replication.sh" ] + - ./mysql/entrypoint.sh:/entrypoint.sh + command: [ "/bin/bash", "/entrypoint.sh" ] networks: [ rabbit-db ] depends_on: mysql_master: diff --git a/mysql/setup-replication.sh b/mysql/entrypoint.sh similarity index 100% rename from mysql/setup-replication.sh rename to mysql/entrypoint.sh