프록시

2023. 8. 30. 17:20docker

프록시 관련 디렉토리 생성

mkdir -p /db/proxysql/data /db/proxysql/conf

chmod 777 /db/proxysql /db/proxysql/data /db/proxysql/conf

 

 

설정 파일 생성

touch /db/proxysql/conf/proxysql.cnf

chmod 644 /db/proxysql/conf/proxysql.cnf

vi /db/proxysql/conf/proxysql.cnf

 

 

설정 파일 내용

datadir="/var/lib/proxysql"
admin_variables=
{
admin_credentials="admin:admin;radmin:radmin"
mysql_ifaces="0.0.0.0:6032"
}
mysql_variables=
{
threads=4
max_connections=2048
default_query_delay=0
default_query_timeout=36000000
have_compress=true
poll_timeout=2000
interfaces="0.0.0.0:6033"
default_schema="information_schema"
stacksize=1048576
server_version="5.5.30"
connect_timeout_server=3000
monitor_username="monitor"
monitor_password="monitor"
monitor_history=600000
monitor_connect_interval=60000
monitor_ping_interval=10000
monitor_read_only_interval=1500
monitor_read_only_timeout=500
ping_interval_server_msec=120000
ping_timeout_server=500
commands_stats=true
sessions_sort=true
connect_retries_on_failure=10
}

 

 

 

본격적으로 proxy를 설치해보자. 참고 사이트는 다음과 같다.

https://proxysql.com/documentation/configuration-file/

 

Configuration file - ProxySQL

The ProxySQL configuration file ProxySQL uses a multi-layers configuration system, specifically a 3 layers of configuration. In this specific architecture, the majority of the configuration present in the configuration file is read and parsed only during t

proxysql.com

 

 

이미지 설치

 docker run -it --name proxysql -h proxysql -p 16032:6032 -p 16033:6033 \

--net mybridge --net-alias=proxysql \

-v /db/proxysql/data:/var/lib/proxysql \

-v /db/proxysql/conf/proxysql.cnf:/etc/proxysql.cnf \

-d proxysql/proxysql

 

 

설치 확인

 

 

DB에서 proxy에 접근할 유저 생성

docker exec -it -uroot db001 /bin/bash

mysql -uroot -p12345

 

create database testdb default character set utf8;

 

create user 'appuser'@'%' identified by '12345';

grant select, insert, update, delete on testdb.* to 'appuser'@'%';

 

create user 'monitor'@'%' identified by 'monitor';

grant replication client on *.* to 'monitor'@'%';

 

flush privileges;

 

 

proxy DB 설정

1. hostgroup 10번, hostgroup 20번 2개 등록

2. hostgroup 10번 : write용 transaction 처리 -> db001 등록

    hostgroup 20번 : read용 transaction 처리 -> db00[ 1 | 2 | 3 ]

 

 

mysql -h127.0.0.1 -P16032 -uradmin -pradmin --prompt "ProxySQL Admin>"

 

insert into mysql_servers(hostgroup_id, hostname, port) values(20, 'db001', 3306);

insert into mysql_servers(hostgroup_id, hostname, port) values(20, 'db002', 3306);

insert into mysql_servers(hostgroup_id, hostname, port) values(20, 'db003', 3306);

 

insert into mysql_replication_hostgroups values(10, 20 ,'read_only','');

load mysql servers to runtime; -> 적용

save mysql servers to disk; -> 영구 저장

 

 

insert into mysql_users(username, password, default_hostgroup, transaction_persistent) values('appuser', '12345', 10, 0);

load mysql users to runtime;

save mysql users to disk;

 

 

이제, proxy 서버가 app 서버로부터 받은 쿼리를 sql 서버로 분기시켜줄 룰을 생성해보자.

 

insert into mysql_query_rules(rule_id,active,match_pattern,destination_hostgroup) values(1,1,'^select.*for update$',10);

insert into mysql_query_rules(rule_id,active,match_pattern,destination_hostgroup) values(2,1,'^select',20);

 

load mysql query rules to runtime;

save mysql query rules to disk;

 

 

read transaction 로드 분산 확인

touch app_test_conn.sh

chmod 777 app_test_conn.sh

vi app_test_conn.sh

./app_test_conn.sh

확인

 

 

스크립트 내용

#!/bin/bash

while true;

do

    mysql -uappuser -p12345 -h172.17.0.1 -P16033 -N -e "select @@hostname,now()" 2>&1 | grep -v "Warning"

    sleep 1

done

-N : 결과를 탭 또는 공백으로 구분된 형식이 아닌 "clean" 모드로 출력한다.

-e "select @@hostname,now()" : MySQL 서버에 실행할 쿼리를 지정한다. 이 경우에는 @@hostname (현재 호스트명)과 now() (현재 날짜와 시간)을 출력하는 쿼리를 실행한다.

2>&1 : 표준 에러 출력(2)을 표준 출력(1)으로 리디렉션한다. 즉, 표준 출력과 표준 에러를 같은 스트림으로 합친다.
참고) 표준 입력(0)

| grep -v "Warning" : 파이프(|)를 사용하여 이전 명령어의 출력을 받아 "Warning"이라는 문자열을 포함하지 않는 라인만 필터링하여 출력한다.

 

 

write transaction 처리 확인

스크립트 내용 수정

#!/bin/bash

while true;

do

    mysql -uappuser -p12345 -h172.17.0.1 -P16033 -N -e "insert into testdb.insert_test select @@hostname,now()" 2>&1 | grep -v "Warning"

    sleep 1

done

 

 

기록될 테이블 생성

docker exec -it -uroot db001 /bin/bash

mysql -uroot -p12345

use testdb;

create table insert_test(hostname varchar(5) not null, insert_time datetime not null);

flush privileges;

 

 

스크립트 실행

 

 

확인

'docker' 카테고리의 다른 글

docker 기본 사항  (0) 2023.10.04
docker 설치  (0) 2023.10.04
자동화 실습  (0) 2023.08.31
Orchestrator 설정  (0) 2023.08.10
Orchestrator를 활용한 cluster 생성  (0) 2023.08.07