노력과 삽질 퇴적물

AWS: 원격 캐싱서버, Redis&memcached 본문

프로그래밍note/서버 및 통신

AWS: 원격 캐싱서버, Redis&memcached

MTG 2019. 6. 6. 20:28

* 세팅환경

> AWS

> Ubuntu Server 16.04 LTS (HVM), SSD Volume Type

> 레디스(Redis)와 맴캐시디(memcached)를 AWS에서 제공하는 메뉴가 아닌 EC2를 이용한 서버 인스턴스에 설치 및 세팅.

(Redis self-host in EC2)

> apt-get 명령을 이용한 패키지 설치


1
2
3
4
5
6
7
8
9
10
11
12
13
14
login as: ubuntu
Authenticating with public key "imported-openssh-key"
Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.4.0-1065-aws x86_64)
... ... ...
... ... ...
See "man sudo_root" for details.
 
ubuntu:~$ sudo passwd root
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
ubuntu:~$ su
Password:
root:/home/ubuntu#
cs






1. 레디스 설치


1) apt-get

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
root:/home/ubuntu# apt-get update
... ... ...
... ... ...
Fetched 25.5 MB in 4s (5,348 kB/s)
Reading package lists... Done
root:/redis-stable# apt full-upgrade -y
... ... ...
... ... ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...
root:/home/ubuntu# apt-get install build-essential tcl
... ... ...
... ... ...
Setting up tcl (8.6.0+9) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...
root:/home/ubuntu# wget http://download.redis.io/redis-stable.tar.gz
... ... ...
... ... ...
redis-stable.tar.gz      100%[===============================>]   1.69M  1.05MB/s    in 1.6s
 
2018-09-06 14:10:50 (1.05 MB/s) - ‘redis-stable.tar.gz’ saved [1772849/1772849]
cs



2) make

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
root@ip-172-31-31-252:/home/ubuntu# tar xvzf redis-stable.tar.gz
redis-stable/
... ... ...
... ... ...
root:/home/ubuntu# mv redis-stable /
root:/# cd /redis-stable
root:/redis-stable# make
... ... ...
... ... ...
Hint: It's a good idea to run 'make test' ;)
make[1]: Leaving directory '/redis-stable/src'
root@ip-xxx-xx-xxx-xxx:/redis-stable# make test
... ... ...
... ... ...
\o/ All tests passed without errors!
Cleanup: may take some time... OK
make[1]: Leaving directory '/redis-stable/src'
root@ip-172-31-12-66:/redis-stable# sudo make install
cd src && make install
make[1]: Entering directory '/redis-stable/src'
Hint: It's a good idea to run 'make test' ;)
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: Leaving directory '/redis-stable/src'
cs



3) 서비스 등록

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
root:/redis-stable# mkdir /etc/redis
root:/redis-stable# cp /redis-stable/redis.conf /etc/redis
root:/redis-stable# vi /etc/redis/redis.conf
 
#       They do not enable continuous liveness pings back to your supervisor.
supervised systemd        #ROW 147
... ... ...
... ... ...
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /var/lib/redis        #ROW 263
 
:wq
 
root:/redis-stable# vi /etc/systemd/system/redis.service
[Unit]
Description=Redis In-Memory Data Store
After=network.target
 
[Service]
User=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always
 
[Install]
WantedBy=multi-user.target
cs


1
2
3
4
5
6
7
8
root:/redis-stable# adduser --system --group --no-create-home redis
Adding system user `redis' (UID 112) ...
Adding new group `redis' (GID 116) ...
Adding new user `redis' (UID 112) with group `redis' ...
Not creating home directory `/home/redis'.
root:/redis-stable# mkdir /var/lib/redis
root:/redis-stable# chown redis:redis /var/lib/redis
root:/redis-stable# chmod 770 /var/lib/redis
cs


4) 테스트

테스트 1

root:/redis-stable# systemctl start redis

root:/redis-stable# systemctl status redis

● redis.service - Redis In-Memory Data Store

   Loaded: loaded (/etc/systemd/system/redis.service; disabled; vendor preset: enabled)

   Active: active (running) since Mon 2018-09-03 08:56:39 UTC; 10s ago

... ... ...

... ... ...


테스트 2

root:/redis-stable# redis-cli

127.0.0.1:6379> ping

PONG

127.0.0.1:6379> set test "It's working!"

OK

127.0.0.1:6379> exit



5) config

> 암호 설정

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
rootroot@:/home/ubuntu# redis-cli
127.0.0.1:6379> config set stop-writes-on-bgsave-error no
127.0.0.1:6379> exit
 
rootroot@:/home/ubuntu# vi /etc/redis/redis.conf
... ... ....
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
requirepass this_is_my_password_for_redis
 
# Command renaming.
:wq
root@:/home/ubuntu# systemctl restart redis
rootroot@:/home/ubuntu# redis-cli
127.0.0.1:6379> AUTH PASSWORD
(error) ERR invalid password
127.0.0.1:6379> CONFIG SET requirepass "this_is_my_password_for_redis"
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth this_is_my_password_for_redis
OK
cs



6) 자동시작 등록

1
2
root:/redis-stable# systemctl enable redis
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /etc/systemd/system/redis.service.
cs






2. memcached


1) apt-get

1
2
3
4
5
6
7
8
root@:/# apt-get update
root@:/# apt-get install memcached -y
Reading package lists... Done
... ... ...
... ... ...
root@:/# apt-get install libmemcached-tools -y
... ... ...
... ... ...
cs



2) conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
root@:/# vi /etc/memcached.conf
(스크립트 수정)
... ... ...
... ... ...
# it's listening on a firewalled interface.
-0.0.0.0
#       To disable UDP (while leaving TCP unaffected)
-0
:wq!
root@:/# systemctl restart memcached
root@:/# netstat -plunt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      28590/memcached
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      28236/redis-server
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1275/sshd
tcp6       0      0 :::22                   :::*                    LISTEN      1275/sshd
udp        0      0 0.0.0.0:68              0.0.0.0:*                           893/dhclient
... ... ...
... ... ...
root@:/# memcstat --servers="127.0.0.1"
Server: 127.0.0.1 (11211)
         pid: 5744
         uptime: 55
         time: 1535965789
         version: 1.4.25
         libevent: 2.0.21-stable
... ... ...
... ... ...
cs


주의사항.

> 현재 설정에서는 등록된 유저명 기입을 하지 않아도 memcstat 명령어가 정상작동.

> 보안설정은 해당 포스트에서 생략.






3. 방화벽


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
root@:/home/ubuntu# ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup
rootroot@:/home/ubuntu# ufw allow 22
Rule added
Rule added (v6)
rootroot@:/home/ubuntu# ufw allow 11211
Rule added
Rule added (v6)
rootroot@:/home/ubuntu# ufw allow 6379
Rule added
Rule added (v6)
rootroot@:/home/ubuntu# ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip
 
To                         Action      From
--                         ------      ----
22                         ALLOW IN    Anywhere
11211                      ALLOW IN    Anywhere
6379                       ALLOW IN    Anywhere
22 (v6)                    ALLOW IN    Anywhere (v6)
11211 (v6)                 ALLOW IN    Anywhere (v6)
6379 (v6)                  ALLOW IN    Anywhere (v6)
cs






기타. 참조자료


1) 레디스

Redis Quick Start – Redis

How To Install and Configure Redis on Ubuntu 16.04 | DigitalOcean

How to install Redis on Ubuntu 16.04?


MISCONF Redis is configured to save RDB snapshots - Stack Overflow



2) 맴캐쉬

How To Install and Secure Memcached on Ubuntu 16.04 | DigitalOcean

How to Setup Memcache on Ubuntu 16 - Globo.Tech






기타. 변경이력


일자

변경이력

2019-06-06

 초안


'프로그래밍note > 서버 및 통신' 카테고리의 다른 글

NoSQL: 레디스 zip버전 윈도우 로컬서버  (0) 2022.10.11
우분투: 레드마인 설치  (0) 2019.06.30
Memcached&Redis(레디스)  (0) 2018.04.27
AWS: 우분투 SVN서버  (0) 2018.04.10
DB: NoSQL과 Amazon DynamoDB  (0) 2017.05.11