본문 바로가기
한화시스템 Beyond SW Camp/기반기술

[리눅스] 서버 구축 최종 (nginx, HAProxy, Tomcat, mariaDB, Keepalived)

by taeh00n 2024. 12. 5.

 ※ 이 글에서 다룬 기반기술, 프론트엔드, 백엔드, 데브옵스 등 풀스택 개발 지식은 모두 한화시스템 Beyond SW Camp 12기에서 배운 내용을 복습한 것입니다.

 

1. nginx

2. HAProxy

3. Tomcat
4. mariaDB
5. Keepalived

 


1-1. nginx 설치 

apt update
apt install -y nginx

 

1-2. nginx 설정 파일

vi /etc/nginx/sites-enabled/default

 

(1) 톰캣으로 연결 시

upstream tomcat_backend {
        server [1번 톰캣 IP 주소]:8080;
        server [2번 톰캣 IP 주소]:8080;
}

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;

        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                try_files $uri $uri/ =404;
        }


        location /api {
            rewrite ^/api(/.*)$ $1 break;
            proxy_pass http://tomcat_backend;  # 업스트림 그룹으로 프록시
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
}

 

(2) HAProxy로 연결 시 

upstream haproxy_backend {
    server [HAProxy IP 주소]:[HAProxy 포트];
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name _;

    location / {
        proxy_pass http://haproxy_backend;  # HAProxy로 트래픽 전달
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

 

1-3. 프론트엔드 파일 다운

cd /var/www/html
wget [프론트엔드 파일 링크]
# 압축해제 필요 시
tar zxvf [프론트엔드 파일]

 


2-1. HAProxy 설치 

apt update
apt install haproxy

2-2. HAProxy 설정 파일

vi /etc/haproxy/haproxy.cfg

 

nginx 분산 시 

listen stats
    bind *:9000
    mode  http
    option dontlog-normal
    stats enable
    stats realm Haproxy\ Statistics
    stats uri /stats
    
frontend webserver
  bind *:80
  mode http
  default_backend nginx-server
  
backend nginx-server
  mode http
  balance roundrobin 
  option httpchk GET /
  server nginx1 [nginx1서버 IP주소]:80 check 
  server nginx2 [nginx2서버 IP주소]:80 check

 

mariaDB 분산 시

listen stats
    bind *:9000
    mode  http
    option dontlog-normal
    stats enable
    stats realm Haproxy\ Statistics
    stats uri /stats

listen mariadb
  bind :3306
  mode tcp
  balance roundrobin
  server db1 [첫 번째 DB 서버 IP]:3306 check
  server db2 [두 번째 DB 서버 IP]:3306 check

 

2-3. HAProxy 설정 적용

systemctl restart haproxy

 

/stats로 이동 시 부하 분산 모니터링 가능


3-1. 자바 설치

apt update
apt install -y openjdk-17-jdk

 

3-2. Tomcat 다운로드

wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.33/bin/apache-tomcat-10.1.33.tar.gz

 

3-3. Tomcat 압축해제

tar zxvf apache-tomcat-10.1.33.tar.gz

 

3-4. 폴더 이동

mv apache-tomcat-10.1.33 /usr/local/tomcat10

 

3-5. JSP 코드 파일 생성

rm -rf /usr/local/tomcat10/webapps/ROOT/*
vi /usr/local/tomcat10/webapps/ROOT/[파일이름].jsp

이때 jsp 파일 넣기

 

3-6. 라이브러리 다운

cd /usr/local/tomcat10/lib
wget [mariadb-java-client-3.3.3.jar 파일 다운로드 링크]

 

3-7. Tomcat 실행

/usr/local/tomcat10/bin/shutdown.sh
/usr/local/tomcat10/bin/startup.sh

 

3-8. Tomcat 실행 확인

ps -ef | grep /usr/bin/java
netstat -anlp | grep :8080

3-9. JSP 파일 다운 및 압축 해제

	cd /usr/local/tomcat10/webapps/ROOT
	wget [jsp 파일 링크]
	tar zxvf [jsp 파일]

 


4-1. mariaDB 다운로드 

apt update
apt install -y mariadb-server

4-2. mariaDB 초기화

mysql_secure_installation

4-3. mariaDB 설정 

vi /etc/mysql/mariadb.conf.d/50-server.cnf
# 27번째 줄 수정
bind-address            = 0.0.0.0

 

Master-Slave (Active - Passive) 설정 시

# 마스터
[mariadb]
log-bin				# 로그 활성화
server_id=1			# Master 서버의 고유 ID 설정
log-basename=master1		# 로그 파일의 기본 이름 설정
binlog-format=mixed		# 로그 포맷 혼합 모드로 설정
[mariadb]
server_id=2		# Slave 서버의 고유 ID 설정

 

Master-Slave (Active - Active) 설정 시

# 마스터, 슬레이브
[mariadb]
log-bin				# 로그 활성화
server_id=1			# Master 서버의 고유 ID 설정
log-basename=master1		# 로그 파일의 기본 이름 설정
binlog-format=mixed		# 로그 포맷 혼합 모드로 설정
# 슬레이브, 마스터
[mariadb]
log-bin				# 로그 활성화
server_id=2			# Master 서버의 고유 ID 설정
log-basename=master2	# 로그 파일의 기본 이름 설정
binlog-format=mixed		# 로그 포맷 혼합 모드로 설정

4-4. mariaDB 실행

mariadb -u root -p

CREATE DATABASE [생성할 DB이름];

CREATE USER '[아이디]'@'%' IDENTIFIED BY '[비밀번호]';
# 모든 권한 주기
GRANT ALL PRIVILEGES ON [DB이름].* TO '[아이디]'@'%';
# SLAVE 복제 권한
GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%';
FLUSH PRIVILEGES;

SELECT user, host FROM mysql.user;

 

Master-Slave 설정

CHANGE MASTER TO
	MASTER_HOST='[Master 서버 IP]',
	MASTER_USER='slave_user',
	MASTER_PASSWORD='qwer1234',
	MASTER_PORT=3306,
	MASTER_LOG_FILE='[마스터에서 show master status 했을 때 File 이름]',
	MASTER_LOG_POS=[마스터에서 show master status 했을 때 position 번호],
	MASTER_CONNECT_RETRY=10;
          
START SLAVE;
STOP SLAVE;
RESET SLAVE;

SHOW MASTER STATUS;
SHOW SLAVE STATUS\G;

 


5-1. Keepalived (DB 서버에 설치)

apt udpate
apt install -y keepalived

 

5-2. Keepalived 설정파일 생성 (Master-Active 설정)

vi /etc/keepalived/keepalived.conf

 

 

global_defs {
    router_id node01
    enable_script_security
    script_user root
}

vrrp_script track_mariadb {
    script "systemctl is-active mariadb"
    interval 5
    fall 4
    rise 2
}

vrrp_instance VRRP1 {
    state MASTER
    interface ens33
    virtual_router_id 101
    priority 200
    advert_int 1
    virtual_ipaddress {
        [가상 IP 주소]/24
    }

    track_script {
        track_mariadb 
    }
}

 

5-3. keepalived 설정파일 생성 (Slave- Standby 설정)

vi /etc/keepalived/keepalived.conf

 

global_defs {
    router_id node02
    enable_script_security
    script_user root
}

vrrp_script track_mariadb {
    script "systemctl is-active mariadb"
    interval 5
    fall 4
    rise 2
}

vrrp_instance VRRP1 {
    state BACKUP
    interface ens33
    virtual_router_id 101
    priority 100
    advert_int 1
    virtual_ipaddress {
        [가상 IP 주소]/24
    }

    track_script {
        track_mariadb
    }
}