Freesunny

[redis] monitoring 하는 방법

Redis

redis 를 통해서 들어오는 커맨드를 확인하는 방법

$ redis-cli -h localhost -p 6379 -a PASSWORD monitor
$ redis-cli -h localhost -p 6379 -a PASSWORD monitor | grep -v DEL

grep 을 이용하여, 필요한 문자열이 포함된(제외된) 메시지만 출력할수 있다.

[redis] WARNING you have Transparent Huge Pages (THP) support enabled in your kernel.

Redis

redis log 중에서 아래의 로그가 있을 경우 

WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. 
This will create latency and memory usage issues with Redis. 
To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, 

 

root 계정으로 아래의 명령을 입력하면 된다.

# cat /sys/kernel/mm/transparent_hugepage/enabled

 

 

설정 확인

# cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]

 

재부팅시 적용이 가능하도록 rc.local 에 등록한다.

os 에 따라 기본적으로 rc.local 비활성화 되어 있는 경우가 있으니, 비활성화 되어 있다면 이곳을 참조한다.

'Redis' 카테고리의 다른 글

[redis] monitoring 하는 방법  (0) 2019.11.26
Redis Cluster 구축 (redis cluster + predixy)  (0) 2019.11.05

Redis Cluster 구축 (redis cluster + predixy)

Redis

기존에 redis server 4 대 와 twemproxy 5 대를 가지고 서비스에 사용하고 있었는데, 

redis server 가 replication을 사용하지 않는 구조여서 redis cluster로 변경하였다.


redis cluster 방식으로 변경을 결정한 이유는 
* twemproxy 환경에서 redis server를 replication 구조로 가지고 가려면, sentinel 도 필요하고

* twemproxy는 master/slave를 자동으로 인식할 수 없기 때문에, redis master 서버 장애 발생 시, sentinel을 통해서 twemproxy 설정을 변경하고 재시작하는 스크립트를 구성해줘야 때문....

 

요구사항

* Redis Server는 Master/Slave 구조를 가진다. (Master 장애 시 Slave 가 Master로 승격)

* Sharding 구조 (필요시 노드 추가/삭제 가 가능하여함)

* 단일 엔드 포인트로 Sharding 처리 (APP에는 하나만의 endpoint 만 등록, proxy에서 모든 리다이렉션 처리)

 

redis cluster를 이용하여 sharding과 replication를 사용하기로 했고,

cluster 사용 시 node들 간의 리다이렉션 처리가 문제가 있는데, 이러한 문제를 해결할 수 있는 predixy를 사용해 보기로 하였다.

 

* 참고 사이트 : http://blog.haandol.com/2018/09/07/redis-cluster-predixy.html

 

기존 구조 : redis server 4대, twemproxy 5 대

기존 구조 : redis server 4 대 + twemproxy 5 대

 

변경되는 구조 : redis server 8 대, predixy 4 대  

변경되는 구조 : redis server 8 대, predixy 4 대

 

1. 서버 설치

 

OS : Ubuntu 18.04

Redis : redis 5.0.6 (패키지로 설치)

Redis 패키지 설치 방법

$ sudo add-apt-repository ppa:chris-lea/redis-server
$ sudo apt update
$ sudo apt -y install redis-server
$ sudo systemctl enable --now redis-server

/etc/redis.conf 편집

디폴트 설정에서 아래의 부분만 변경하고, 그 밖에 설정은 서비스에 맞게 설정한다.

bind 0.0.0.0                          # 다른 노드, proxy 에서 접속을 위해서
port 8000                             # 서비스에 필요한 포트로 변경
masterauth  xxxxxxxxxxx               # master/slave 간에 사용하는 패스워드
requirepass yyyyyyyyyyy               # cluster node 간에 사용하는 패스워드
cluster-enabled yes                   # cluster 활성화
cluster-config-file nodes-8000.conf   # cluster config 파일네임
cluster-node-timeout 5000             # default 150000 > 5000

redis-server 재시작 (sudo systemctl restart redis-server.service)

모든 redis 노드들을 동일하게 세팅한다.

 

 

2. create cluster

 

redis 노드 중에 1대에 접속하여 redis-cli로 cluster를 만든다. (노드들의 ip 및 포트는 설정에 맞춰서 입력)

redis-cli --cluster create \
192.168.0.218:8000 \
192.168.0.220:8000 \
192.168.0.222:8000 \
192.168.0.226:8000 \
192.168.0.219:8000 \
192.168.0.221:8000 \
192.168.0.223:8000 \
192.168.0.225:8000 \
--cluster-replicas 1 -a yyyyyyyyyyy

 

redis-cli로 node 상태 확인

$ redis-cli -8000 -a yyyyyyyyyyy
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:8000> cluster info
d57127549b3a9e0f4c0485c99c6a7c766efb18a9 192.168.0.225:8000@18000 slave 691dd7088a7fccc52abe41a46746a73bca626c53 0 1572927545000 8 connected
691dd7088a7fccc52abe41a46746a73bca626c53 192.168.0.222:8000@18000 master - 0 1572927545528 3 connected 8192-12287
de8facc5f968f66e34ad7f40a88bd56129f63b58 192.168.0.220:8000@18000 slave 792e379426277a0d9d9c2586ab5d174d5a9fc5f5 0 1572927545528 9 connected
3e63ba3104a715e8c34009ef63e846171813b00b 192.168.0.221:8000@18000 slave 04342d3da7cba17e3b7e9506c1becab2fef8ca79 0 1572927546000 6 connected
5770c42d086c1ed6414358172109f9c6dcff4a5d 192.168.0.219:8000@18000 master - 0 1572927546934 10 connected 12288-16383
328b18957c6b0822d8abccba4d498b297871d271 192.168.0.226:8000@18000 slave 5770c42d086c1ed6414358172109f9c6dcff4a5d 0 1572927545428 10 connected
792e379426277a0d9d9c2586ab5d174d5a9fc5f5 192.168.0.223:8000@18000 master - 0 1572927546434 9 connected 4096-8191
04342d3da7cba17e3b7e9506c1becab2fef8ca79 192.168.0.218:8000@18000 myself,master - 0 1572927545000 1 connected 0-4095

 

3. predixy 설정

 

docker-ce, docker-compose 설치

$ sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
$ sudo apt update
$ sudo apt install -y docker-ce
$ sudo usermod -aG docker $USER
$ sudo chmod +/usr/local/bin/docker-compose
$ sudo ln -/usr/local/bin/docker-compose /usr/bin/docker-compose
 

* docker-compose 최신 버전은 1.24.1이었으나, 간헐적인 지연 현상이 발생하여, 1.23.2로 설치하였다.

 

predixy docker-compose 파일 설치 (Haandol님 저장소 clone)

$ git clone https://github.com/haandol/predixy
$ cd predixy
 

 

conf/predix.conf 수정

$ cat conf/predixy.conf | egrep -"^#|^$"
Name redis-proxy
Bind 0.0.0.0:9000 # 컨테이너의 서비스 포트로 변경
WorkerThreads 4
MaxMemory 0
ClientTimeout 300
BufSize 4096
LogRotate 1d
LogVerbSample 0
LogDebugSample 0
LogInfoSample 10000
LogNoticeSample 1
LogWarnSample 1
LogErrorSample 1
Include auth.conf
Include cluster.conf
Include latency.conf

 

conf/cluster.conf 수정

$ cat conf/cluster.conf | egrep -"^#|^$"
ClusterServerPool {
    Password yyyyyyyyyyy  # predixy > redis clsuter 접속시 사용할 패스워드
    MasterReadPriority 60
    StaticSlaveReadPriority 50
    DynamicSlaveReadPriority 50
    RefreshInterval 1
    ServerTimeout 1
    ServerFailureLimit 10
    ServerRetryTimeout 1
    KeepAlive 120
    Servers {
        + 192.168.0.218:8000
        + 192.168.0.219:8000
        + 192.168.0.220:8000
        + 192.168.0.221:8000
        + 192.168.0.222:8000
        + 192.168.0.223:8000
        + 192.168.0.226:8000
        + 192.168.0.225:8000
    }
}

* 설정상으로는 Master/Slave 의 Read 처리 비율도 조정이 가능한 것으로 보인다.

 

conf/auth.conf 수정

$ cat conf/auth.conf | egrep -"^#|^$"
Authority {
    Auth zzzzzzzzzzz {
        Mode read
    }
    Auth zzzzzzzzzzz {
        Mode write
    }
    Auth zzzzzzzzzzz {
        Mode admin
    }
}

* app > predixy 접속 시 사용할 패스워드 설정

 

docker-compose.yml

$ cat docker-compose.yml
version: '3.4'
services:
  predixy:
    restart: always
    image: haandol/predixy:latest
    volumes:
      - ./conf:/etc/predixy/conf
    ports:
      - 9000:9000

* 재부팅해도 다시 실행되도록,

* 컨테이너 포트, 호스트 포트 수정

 

실행

$ docker-compose up -d

 

접속 후 predixy 서비스 포트로 접속을 하면 됨.
마스터 장애 시 슬레이브가 마스터로 변경되는 것을 확인할 수 있다.