redis

redis 1부 - 설치 ...

drscg 2019. 4. 8. 11:19

redis는 Remote Dictionary Systerm의 약자로서 원격 케시 서버라 할 수 있다. NoSQL로 분류되기도 한다.
Memory를 사용하여 고속으로 <Key, Value> 스타일의 data를 저장하고 가져올 수 있는 원격 시스템으로 정의할 수 있다.

다양한 Data type(List, Set, Sorted Set, Hash, String등)을 지원하며, 당연하게도 Replication도 지원한다.

이 게시물에서는 Redis의 설치 방법을 알아보자

설치할 OS는 CentOS 6.9 이다.
redis는 windows, aix등의 OS에 대해 지원하지 않는다.
이들 OS에 대한 binary는 다른 경로를 통해 구해야 한다고 들었다.

ip: 10.0.0.1, port: 16000 으로 가정한다.
redis는 기본적으로 6000 port를 사용하는데, 기본을 별로 좋아하지 않아서 ... ^^

1. Directory

아래와 같은 directory 구조를 사용할 것이다.

/home/backup/redis   // redis 설치 home
  conf // 설정 파일 *.conf
  data // data 파일 *.rdb
  log // log 파일 *.log
  pid // process id *.id
  redis-5.0.4 // redis source file의 압축을 푼 directory
  redis-5.0.4.tar.gz // redis source file


2. download and compile

redis는 source 형태로 배포되므로, 직접 build 해야 한다.
아래 명령어에서 #으로 시작하는 command는 root 로 실행해야 한다.

compiler와 필요한 library를 설치한다.

# yum install epel-release
# yum install gcc jemalloc jemalloc-devel tcl


최신 source를 가져와 compile한다. 현재 redis 최신 버전은 5.0.4 이다.
redis를 download해 build할 directory(redis)를 생성한다.
이 directory에, 설정 파일, log, data를 저장하게 된다.
압축을 풀고, build하고, 설치한다.

$ cd /home/backup
$ mkdir redis
$ cd redis
$ wget http://download.redis.io/releases/redis-5.0.4.tar.gz
$ tar xzf redis-5.0.4.tar.gz
$ ls -al
drwxrwxr-x 6 backup backup    4096 Apr  3 15:59 redis-5.0.4
-rw-rw-r-- 1 backup backup 1966337 Mar 19 01:29 redis-5.0.4.tar.gz
$ cd redis-5.0.4
$ make
# make install


아래 경로에서 redis 실행 파일을 볼 수 있다.
압축을 풀고, build하고, 설치한다.

$ ls -al /usr/local/bin
total 62096
drwxr-xr-x.  2 root root     4096 Apr  1 14:51 .
drwxr-xr-x. 18 root root     4096 Dec  4 15:14 ..
-rwxr-xr-x.  1 root root  7925655 Jan  9 11:16 docker-compose
-rwxr-xr-x.  1 root root  9202963 Apr  1 14:51 redis-benchmark
-rwxr-xr-x.  1 root root 12266338 Apr  1 14:51 redis-check-aof
-rwxr-xr-x.  1 root root 12266338 Apr  1 14:51 redis-check-rdb
-rwxr-xr-x.  1 root root  9580080 Apr  1 14:51 redis-cli
lrwxrwxrwx.  1 root root       12 Apr  1 14:51 redis-sentinel -> redis-server
-rwxr-xr-x.  1 root root 12266338 Apr  1 14:51 redis-server


3. setting

위에서 생성한 direcory에 설정파일, log, data를 저장할 directory를 생성한다.

$ pwd
/home/backup/redis
$ mkdir conf data log pid
$ ls -al
drwxrwxr-x 2 backup backup    4096 Apr  3 16:05 conf
drwxrwxr-x 2 backup backup    4096 Apr  3 16:05 data
drwxrwxr-x 2 backup backup    4096 Apr  3 16:05 log
drwxrwxr-x 2 backup backup    4096 Apr  3 16:05 pid
drwxrwxr-x 6 backup backup    4096 Apr  3 15:59 redis-5.0.4
-rw-rw-r-- 1 backup backup 1966337 Mar 19 01:29 redis-5.0.4.tar.gz


port, 설정파일, log의 위치 등을 지정하고 이를 서비스로 등록하는 shell을 제공한다. 이를 실행해 보자.
port 값은 해당 시스템에서 사용하지 않는 임의의 port를 사용하면 된다. 여기에서는 16000 port를 사용하겠다.

$ cd redis-5.0.4/utils
# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379] 16000
Please select the redis config file name [/etc/redis/16000.conf] /home/backup/redis/conf/redis_16000.conf
Please select the redis log file name [/var/log/redis_16000.log] /home/backup/redis/log/redis_16000.log
Please select the data directory for this instance [/var/lib/redis/16000] /home/backup/redis/data/redis_16000
Please select the redis executable path [/usr/local/bin/redis-server] enter
Selected config:
Port           : 16000
Config file    : /home/backup/redis/conf/redis_16000.conf
Log file       : /home/backup/redis/log/redis_16000.log
Data dir       : /home/backup/redis/data/redis_16000
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort. enter
Copied /tmp/16000.conf => /etc/init.d/redis_16000
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!


이 shell을 실행시키면, 다음과 같은 결과를 얻을 수 있다.
- 설정 파일 생성: /home/backup/redis/conf/redis_16000.conf
- service로 등록 : /etc/init.d/redis_16000
- 시스템 reboot시에 자동으로 시작되도록 등록

그냥 실행해도 동작은 하겠지만, 향후 replication setting 그리고 보안 issue 해결을 위해, 몇 가지를 수정하자.

먼저, redis의 conf 파일을 수정하자.
conf 파일은 위의 install_server.sh 에서 지정한 위치에 있다.
파일을 열어서, 아래 내용에 해당하는 부분을 확인하고 수정하자.
각 line의 가장 앞에 #이 있으면, remark를 의미하니 유의하자.

# vi /home/backup/redis/conf/redis_16000.conf
bind 10.0.0.1
port 16000
requirepass 123!@#qwe
masterauth 123!@#qwe
loglevel notice
dir /home/backup/redis/data/redis_16000
pidfile /home/backup/redis/pid/redis_16000.pid
logfile /home/backup/redis/log/redis_16000.log


각 항목은 아래와 같은 의미를 가지고 있다.

bind redis 의 ip
port redis가 listen할 port
requirepass 보안 설정, client가 접속시에 사용할 password
replication시에 master에 접속하기 위한 password
현재는 master이지만, 향후 slave가 될 경우에 사용
masterauth master의 requirepass와 동일해야 한다.
loglevel log level
dir data file의 경로
pidfile process id file의 경로
logfile log file의 경로


그리고, service를 확인해 보자.

# ls -al /etc/init.d/redis_16000
-rwxr-xr-x 1 root root 1727 Apr  3 16:16 /etc/init.d/redis_16000
# chkconfig --list | grep redis
redis_16000     0:off 1:off 2:on 3:on 4:on 5:on 6:off


service로 실행 시 나타날 수 있는 몇 가지 문제를 해결하기 위해, 해당 파일을 수정하자.

# vi /etcc/init.d/redis_16000
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_16000.pid
CONF="/home/backup/redis/conf/redis_16000.conf"
REDISPORT="16000"
###############
~~~
$CLIEXEC -p $REDISPORT shutdown


파일을 열어보면, 위와 같이 보일 것이다. 아래 처럼 수정하자.

EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/home/backup/redis/pid/redis_16000.pid
CONF="/home/backup/redis/conf/redis_16000.conf"
REDISHOST="10.0.0.1"
REDISPORT="16000"
PASSWORD="123!@#qwe"
###############
~~~
$CLIEXEC -h $REDISHOST -p $REDISPORT -a $PASSWORD shutdown


4. execute

자. 설정 작업이 끝났다. redis를 시작해 보자.

# service redis_16000 start
Starting Redis server...


process를 확인해 보자

# ps -ef | grep redis
root      1423     1  0 18:31 ?            00:00:00 /usr/local/bin/redis-server 10.0.0.1:16000                     
root      1428  1253  0 18:31 pts/0    00:00:00 grep redis


log 파일을 확인해 보면, 아래와 같이 나올 것이다. 그러면 잘 setting한 것이다.

# vi /home/backup/redis/log/redis_16000.log
1367:C 03 Apr 2019 17:51:39.819 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1367:C 03 Apr 2019 17:51:39.819 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=1367, just started
1367:C 03 Apr 2019 17:51:39.819 # Configuration loaded
1368:M 03 Apr 2019 17:51:39.820 * Increased maximum number of open files to 10032 (it was originally set to 1024).
               _._
          _.-``__ ''-._
     _.-``    `.  `_.  ''-._           Redis 5.0.4 (00000000/0) 64 bit
 .-`` .-```.  ```\/    _.,_ ''-._
(    '      ,       .-`  | `,    )     Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'|     Port: 16000
|    `-._   `._    /     _.-'    |     PID: 1368
 `-._    `-._  `-./  _.-'    _.-'
|`-._`-._    `-.__.-'    _.-'_.-'|
|    `-._`-._        _.-'_.-'    |           http://redis.io
 `-._    `-._`-.__.-'_.-'    _.-'
|`-._`-._    `-.__.-'    _.-'_.-'|
|    `-._`-._        _.-'_.-'    |
 `-._    `-._`-.__.-'_.-'    _.-'
     `-._    `-.__.-'    _.-'
         `-._        _.-'
             `-.__.-'

1368:M 03 Apr 2019 17:51:39.821 # Server initialized
1368:M 03 Apr 2019 17:51:39.821 * DB loaded from disk: 0.000 seconds
1368:M 03 Apr 2019 17:51:39.821 * Ready to accept connections


그러면, command line client인 redis-cli를 이용하여, 접속해 보자.

# redis-cli -h 10.0.0.1 -p 16000 -a 123\!@#qwe
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.0.1:16000> info Replication
# Replication
role:master
connected_slaves:0
master_replid:c45f6d4a00dadc41d44c5a17229540f7b0fce95c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
10.0.0.1:16000> set foo bar
OK
10.0.0.1:16000> get foo
"bar"
10.0.0.1:16000> exit


위에서 사용한 command를 간단히 설명하면 다음과 같다.  

redis-cli -h 10.0.0.1 -p 16000 -a 123\!@#qwe 10.0.0.1의 16000에서 동작하는 redis에 password 123!@#qwe로 접속
info Replication Replication 정보
set foo value foo 라는 key와 bar 라는 value를 저장
get foo foo 라는 key를 가진 vaule를 return
exit 종료


참고로, redis 종료 command는 다음과 같다.

# service redis_16000 stop
Stopping ...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Redis stopped


여기까지가, redis stand alone 설치 작업이 완료되었다.
2부에서는, redis를 replacation으로 구성해 보겠다.

'redis' 카테고리의 다른 글

redis 4부 - jedis를 이용한 client sample ...  (0) 2019.04.08
redis 3부 - Warning 제거 ...  (0) 2019.04.08
redis 2부 - replication ...  (0) 2019.04.08