分类: Redis
Docker环境下安装部署Redis5.0挂载外部配置和数据

作为一种新兴的虚拟化方式,Docker 跟传统的虚拟化方式相比具有众多的优势。
docker-redis.png
本文记录docker下安装部署redis5.0的过程:
os版本centos7.5
先更新 yum 软件管理器,然后再安装 Docker

[root@localhost /] yum -y update
[root@localhost /] yum install -y docker

验证安装,查看 Docker 版本信息

[root@localhost /] docker -v
Docker version 1.13.1, build 8633870/1.13.1
You have new mail in /var/spool/mail/root

启动、停止、重启docker服务、加入开机启动

systemctl start docker
systemctl stop docker
systemctl restart docker
systemctl enable docker

安装redis5.0镜像

[root@sso-nginx-b ~]# systemctl start docker
[root@sso-nginx-b ~]# docker pull redis:5.0
Trying to pull repository docker.io/library/redis ... 
5.0: Pulling from docker.io/library/redis
f17d81b4b692: Pull complete 
b32474098757: Pull complete 
8980cabe8bc2: Pull complete 
2719bdbf9516: Pull complete 
f306130d78e3: Pull complete 
3e09204f8155: Pull complete 
Digest: sha256:481678b4b5ea1cb4e8d38ed6677b2da9b9e057cf7e1b6c988ba96651c6f6eff3
Status: Downloaded newer image for docker.io/redis:5.0

创建redis运行目录及文件

mkdir -p /data/docker/redis
touch /data/docker/redis/redis.conf

redis.conf文件内容如下:

bind 172.17.0.2 127.0.0.1
#bind 0.0.0.0
protected-mode yes
port 6380
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no   #如果daemonize yes无法启动容器
supervised no
pidfile /data/docker/redis/6380.pid
loglevel notice
logfile "6380.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

创建redis实例:

docker run -d --privileged=true --restart=always -p 6380:6380 -v /data/docker/redis/redis.conf:/etc/redis/redis.conf -v /data/docker/redis:/data --name redistest1 redis:5.0 redis-server /etc/redis/redis.conf --appendonly yes

实例说明如下:

--privileged=true 容器内的root拥有真正root权限,否则容器内root只是外部普通用户权限
--restart=always 开机启动容器
-p 6380:6380 映射宿主机6380端口到docker端口6380
-v /data/docker/redis/redis.conf:/etc/redis/redis.conf   映射配置文件
-v /data/docker/redis:/data  映射数据目录地址
--name redistest1 redis:5.0 redis-server /etc/redis/redis.conf   #docker名字 redis实例名 配置文件
--appendonly yes:开启持久化

查看redistest1容器的IP地址:

docker inspect redistest1 | grep -i address
            "LinkLocalIPv6Address": "",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "GlobalIPv6Address": "",
            "IPAddress": "172.17.0.2",
            "MacAddress": "02:42:ac:11:00:02",
                    "IPAddress": "172.17.0.2",
                    "GlobalIPv6Address": "",
                    "MacAddress": "02:42:ac:11:00:02"

将/data/docker/redis/redis.conf里面的bind地址修改为

bind 172.17.0.2 127.0.0.1

重启redistest1容器:

docker restart redistest1

进入redis控制台:

redis-cli -h 172.17.0.2 -p 6380  

172.17.0.2是redistest1容器的IP地址,也可以用宿主机IP地址进入
启动另外一个redis实例:

docker run -d --privileged=true --restart=always -p 6381:6381 -v /data/docker/redis-6381/redis.conf:/etc/redis/redis.conf -v /data/docker/redis-6381:/data --name redistest2 redis:5.0 redis-server /etc/redis/redis.conf --appendonly yes

至此,docker上安装部署redis完毕.


相关博文:

发表新评论