diff --git a/docker-compose-db.yml b/docker-compose-db.yml index 6254cb6..d297c1a 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 @@ -50,13 +44,13 @@ services: retries: 10 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" ] - networks: - - rabbit-db + - ./mysql/entrypoint.sh:/entrypoint.sh + command: [ "/bin/bash", "/entrypoint.sh" ] + networks: [ rabbit-db ] depends_on: mysql_master: condition: service_healthy diff --git a/docker-compose-elk.yml b/docker-compose-elk.yml index 0e99b3f..c94c655 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/ @@ -29,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} @@ -47,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} @@ -67,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: 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 diff --git a/mysql/entrypoint.sh b/mysql/entrypoint.sh new file mode 100755 index 0000000..e0ac17c --- /dev/null +++ b/mysql/entrypoint.sh @@ -0,0 +1,110 @@ +#!/bin/bash + +# MySQL Master-Replica 복제 설정 자동화 스크립트 +set -e + +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 + sublog "Master DB 연결 대기 중..." + sleep 3 +done +sublog "Master DB 연결됨" + +log "Replica DB 연결 대기 중..." +until mysql -h rabbit-mysql-replica -u root -p1234 -e "SELECT 1" > /dev/null 2>&1; do + sublog "Replica DB 연결 대기 중..." + sleep 3 +done +sublog "Replica DB 연결됨" + +log "DB 초기화 완료 대기 중..." +sleep 10 +sublog "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 + 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 + sublog "복제 사용자가 생성되었습니다." +else + sublog "복제 사용자가 이미 존재합니다." +fi + +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}') +sublog "Master File: $MASTER_FILE" +sublog "Master Position: $MASTER_POSITION" + +if [ -z "$MASTER_FILE" ] || [ -z "$MASTER_POSITION" ]; then + suberr "Master 상태를 가져올 수 없습니다." + exit 1 +fi + +log "기존 복제 설정 정리 중..." +mysql -h rabbit-mysql-replica -u root -p1234 << EOF +STOP SLAVE; +RESET SLAVE ALL; +EOF +sublog "기존 복제 설정 정리 완료" + +log "Replica DB에서 Master 연결 설정 중..." +mysql -h rabbit-mysql-replica -u root -p1234 << EOF +CHANGE MASTER TO + MASTER_HOST='rabbit-mysql-master', + MASTER_USER='replica', + MASTER_PASSWORD='1234', + MASTER_LOG_FILE='$MASTER_FILE', + MASTER_LOG_POS=$MASTER_POSITION, + MASTER_CONNECT_RETRY=10, + MASTER_RETRY_COUNT=3; +START SLAVE; +EOF +sublog "Master 연결 설정 완료" + +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}') + + 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 + break + fi + sleep 2 +done + +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 diff --git a/mysql/setup-replication.sh b/mysql/setup-replication.sh deleted file mode 100755 index 12bb831..0000000 --- a/mysql/setup-replication.sh +++ /dev/null @@ -1,109 +0,0 @@ -#!/bin/bash - -# MySQL Master-Replica 복제 설정 자동화 스크립트 -set -e - -echo "MySQL Master-Replica 복제 설정을 시작합니다..." - -# Master DB가 완전히 시작될 때까지 대기 -echo "Master DB 연결 대기 중..." -until mysql -h rabbit-mysql-master -u root -p1234 -e "SELECT 1" > /dev/null 2>&1; do - echo "Master DB 연결 대기 중..." - sleep 3 -done - -# Replica DB가 완전히 시작될 때까지 대기 -echo "Replica DB 연결 대기 중..." -until mysql -h rabbit-mysql-replica -u root -p1234 -e "SELECT 1" > /dev/null 2>&1; do - echo "Replica DB 연결 대기 중..." - sleep 3 -done - -# 추가 안정화 대기 시간 -echo "DB 초기화 완료 대기 중..." -sleep 10 - -# Master DB에서 복제 사용자가 생성되었는지 확인 -echo "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 -CREATE USER IF NOT EXISTS 'replica'@'%' IDENTIFIED WITH mysql_native_password BY '1234'; -GRANT REPLICATION SLAVE ON *.* TO 'replica'@'%'; -FLUSH PRIVILEGES; -EOF - echo "복제 사용자가 생성되었습니다." -fi - -# Master DB에서 바이너리 로그 상태 확인 -echo "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" - -if [ -z "$MASTER_FILE" ] || [ -z "$MASTER_POSITION" ]; then - echo "❌ Master 상태를 가져올 수 없습니다." - exit 1 -fi - -# 기존 복제 설정 정리 -echo "기존 복제 설정 정리 중..." -mysql -h rabbit-mysql-replica -u root -p1234 << EOF -STOP SLAVE; -RESET SLAVE ALL; -EOF - -# Replica DB에서 Master 설정 -echo "Replica DB에서 Master 연결 설정 중..." -mysql -h rabbit-mysql-replica -u root -p1234 << EOF -CHANGE MASTER TO - MASTER_HOST='rabbit-mysql-master', - MASTER_USER='replica', - MASTER_PASSWORD='1234', - MASTER_LOG_FILE='$MASTER_FILE', - MASTER_LOG_POS=$MASTER_POSITION, - MASTER_CONNECT_RETRY=10, - MASTER_RETRY_COUNT=3; -START SLAVE; -EOF - -# 복제 연결 대기 및 상태 확인 -echo "복제 연결 대기 중..." -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 - fi - - sleep 2 -done - -echo "❌ 복제 설정에 문제가 발생했습니다." -echo "최종 상태: IO Running: $IO_RUNNING, SQL Running: $SQL_RUNNING" - -# 오류 정보 출력 -echo "=== 복제 오류 정보 ===" -mysql -h rabbit-mysql-replica -u root -p1234 -e "SHOW SLAVE STATUS\G" | grep -E "(Last_IO_Error|Last_SQL_Error)" - -exit 1