[Redis] TLS 서버 구축하기 (CentOS)

gweowe·2023년 7월 25일
1

개인적으로 Terraform Enterprise 구축 관련하여 공부를 하다보니까, Terraform Enterprise HA를 구성하려면 TLS가 적용된 Redis 서버가 필요해져서 구축한 김에 간단하게 정리를 해보았습니다.

사전 준비 사항

1. C 컴파일러 설치

sudo yum groupinstall "Development Tools"

C 컴파일러는 Redis를 빌드하기 위해서 사용됩니다.

2. Python3 설치

sudo yum install python3

Python3 또한 Redis를 빌드하기 위해서 사용됩니다.

3. jemalloc 설치

sudo yum install jemalloc jemalloc-devel

jemalloc은 Redis에서 메모리 할당 성능을 향상시키기 위해 사용됩니다.

4. openssl 설치

sudo yum install openssl openssl-devel

Redis를 컴파일을 하기 위해 설치합니다.

5. tcl 설치

sudo yum install tcl

Redis는 tcl을 사용하여 스크립팅을 수행합니다.

Redis 설치

1. Redis 설치파일 가져오기

wget https://download.redis.io/releases//redis-7.0.12.tar.gz

https://download.redis.io/releases/ 에 들어가시면 사용 가능한 버전을 확인하실 수 있습니다.

2. 압축 해제

tar zxf redis-7.0.12.tar.gz

받아온 Redis 파일의 압축을 해제합니다.

3. 파일 이동 후, 빌드 시작

cd redis-7.0.9

sudo make install BUILD_TLS=yes

압축 해제한 폴더에 접근하여 빌드를 시작합니다.

make distclean

빌드가 꼬일 경우, 위와 같은 명령어를 사용하면 다시 처음으로 돌아갈 수 있습니다.

Redis 실행

1. Redis 파일 설정

TLS를 사용하여 Redis를 실행시키기 위해 redis.conf를 수정합니다.

Redis.conf
# ..............................생략..............................

bind 0.0.0.0

# ..............................생략..............................

# port 6379

# ..............................생략..............................

port 0
tls-port 6379

# ..............................생략..............................

tls-cert-file <crt파일 경로>
tls-key-file <key파일 경로>

# ..............................생략..............................

tls-client-cert-file <클라이언트 crt파일 경로>
tls-client-key-file <클라이언트 key파일 경로>

# ..............................생략..............................

tls-ca-cert-file <ca인증서 경로>

# ..............................생략..............................

requirepass <비밀번호>

# ..............................생략..............................

여기서 tls-client-cert-filetls-client-key-file은 기본적으로 tls-cert-filetls-key-file이랑 동일하게 설정되지만, 클라이언트가 다른 인증서를 필요로 할 경우에 설정합니다.

2-1. Redis 서버 직접 실행

redis.conf파일을 사용하여 Redis 서버를 실행합니다.

sudo ./src/redis-server redis.conf
Output :
6114:C 25 Jul 2023 14:56:03.538 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6114:C 25 Jul 2023 14:56:03.538 # Redis version=7.0.12, bits=64, commit=00000000, modified=0, pid=6114, just started
6114:C 25 Jul 2023 14:56:03.538 # Configuration loaded
6114:M 25 Jul 2023 14:56:03.539 * Increased maximum number of open files to 10032 (it was originally set to 1024).
6114:M 25 Jul 2023 14:56:03.539 * monotonic clock: POSIX clock_gettime
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 7.0.12 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 6114
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           https://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

6114:M 25 Jul 2023 14:56:03.539 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
6114:M 25 Jul 2023 14:56:03.539 # Server initialized
6114:M 25 Jul 2023 14:56:03.539 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
6114:M 25 Jul 2023 14:56:03.540 * Loading RDB produced by version 7.0.12
6114:M 25 Jul 2023 14:56:03.540 * RDB age 117 seconds
6114:M 25 Jul 2023 14:56:03.540 * RDB memory usage when created 0.82 Mb
6114:M 25 Jul 2023 14:56:03.540 * Done loading RDB, keys loaded: 0, keys expired: 0.
6114:M 25 Jul 2023 14:56:03.540 * DB loaded from disk: 0.000 seconds
6114:M 25 Jul 2023 14:56:03.540 * Ready to accept connections

정상적으로 실행될 경우, 에러 없이 위와 같이 출력됩니다.

2-2. Redis 서버 서비스 실행

redis.service파일을 사용하여 Redis를 실행합니다.

sudo vi /usr/lib/systemd/system/redis.service
redis.service
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/home/gweowe3/redis-7.0.12/src/redis-server /home/gweowe3/redis-7.0.12/redis.conf
ExecStop=/home/gweowe3/redis-7.0.12/src/redis-cli shutdown
Restart=always
User=root
Group=root


[Install]
WantedBy=multi-user.target
alias=redis.serviceㅂ

위의 내용을 참고하여 service 파일을 해당 위치에 생성하고, 서비스를 실행합니다.

sudo systemctl start redis.service

제대로 실행되었는지 확인합니다.

sudo systemctl status redis.service
Output :
● redis.service - Redis persistent key-value database
   Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/redis.service.d
           └─limit.conf
   Active: active (running) since 월 2023-08-07 14:42:06 KST; 1 day 2h ago
 Main PID: 10806 (redis-server)
   CGroup: /system.slice/redis.service
           └─10806 /home/gweowe3/redis-7.0.12/src/redis-server 0.0.0.0:6379

 8월 08 16:56:16 c0378584D.static.as2116.net redis-server[10806]: 10806:M 08 Aug 2023 16:56:16.022 * 100 changes in 300 seconds. Saving...
 8월 08 16:56:16 c0378584D.static.as2116.net redis-server[10806]: 10806:M 08 Aug 2023 16:56:16.023 * Background saving started by pid 11762
 8월 08 16:56:16 c0378584D.static.as2116.net redis-server[10806]: 11762:C 08 Aug 2023 16:56:16.030 * DB saved on disk
 8월 08 16:56:16 c0378584D.static.as2116.net redis-server[10806]: 11762:C 08 Aug 2023 16:56:16.031 * Fork CoW for RDB: current 4 MB, peak 4 MB, average 3 MB
 8월 08 16:56:16 c0378584D.static.as2116.net redis-server[10806]: 10806:M 08 Aug 2023 16:56:16.123 * Background saving terminated with success
 8월 08 17:01:17 c0378584D.static.as2116.net redis-server[10806]: 10806:M 08 Aug 2023 17:01:17.093 * 100 changes in 300 seconds. Saving...
 8월 08 17:01:17 c0378584D.static.as2116.net redis-server[10806]: 10806:M 08 Aug 2023 17:01:17.094 * Background saving started by pid 11779
 8월 08 17:01:17 c0378584D.static.as2116.net redis-server[10806]: 11779:C 08 Aug 2023 17:01:17.099 * DB saved on disk
 8월 08 17:01:17 c0378584D.static.as2116.net redis-server[10806]: 11779:C 08 Aug 2023 17:01:17.100 * Fork CoW for RDB: current 2 MB, peak 2 MB, average 2 MB
 8월 08 17:01:17 c0378584D.static.as2116.net redis-server[10806]: 10806:M 08 Aug 2023 17:01:17.194 * Background saving terminated with success

에러 없이 잘 실행되고 있음을 확인할 수 있습니다.

테스트

1. 클라이언트에서 접속

redis-cli를 사용하여 클라이언트에서 서버에 접속을 시도합니다.

redis-cli -h <서버주소> -p <포트번호>

서버에서 실행중인 Redis의 로그를 확인해보면 TLS 인증이 되지 않아서 연결이 끊겼음을 확인할 수 있습니다.

Output :
6125:M 25 Jul 2023 15:00:48.921 # Error accepting a client connection: error:140760FC:SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol (conn: fd=7)

2. 클라이언트에서 인증서를 사용하여 접속

이번에는 인증서를 사용하여 접속해보겠습니다.

redis-cli -h <서버주소> -p <포트번호> --tls --cert <crt파일 경로> --key <key파일 경로> --cacert <ca인증서 경로>

인증서를 사용하여 접속했기 때문에, 접속 에러가 발생하지 않습니다.

3. 비밀번호 로그인

AUTH <비밀번호>

redis.conf파일에서 지정한 비밀번호로 접속합니다.

4. PING 테스트

Redis의 테스트 기능인 PING을 보내보겠습니다.

<서버주소>:<포트번호>> PING
Output :
PONG

정상적으로 Output이 출력되는 것을 확인할 수 있습니다.

profile
정리하는 공간

2개의 댓글

comment-user-thumbnail
2023년 7월 25일

많은 도움이 되었습니다, 감사합니다.

1개의 답글