安装准备
1、简介
本文分享使用 docker 运行 redis 单实例的方法,虽然非常简单,但里面却有我本人分享的非常硬生产的干货。
2、安装环境
Ubuntu 18.04.5
Docker 19.03.6
redis-server 6.0.6
3、安装规划
使用 docker 运行一个 Redis 的单实例
安装步骤
1、拉取镜像
执行如下命令拉取 docker 仓库中的最新的 redis 镜像。
docker pull redis
注:如需要拉取指定版本,只需要将上面的命令作如下调整,比如拉取 3.2 的 redis 镜像:
docker pull redis:3.2
示例:
$ docker pull redis
Using default tag: latest
latest: Pulling from library/redis
d121f8d1c412: Downloading
2f9874741855: Download complete
d92da09ebfd4: Download complete
bdfa64b72752: Download complete
e748e6f663b9: Download complete
eb1c8b66e2a1: Download complete
latest: Pulling from library/redis
d121f8d1c412: Pull complete
2f9874741855: Pull complete
d92da09ebfd4: Pull complete
bdfa64b72752: Pull complete
e748e6f663b9: Pull complete
eb1c8b66e2a1: Pull complete
Digest: sha256:1cfb205a988a9dae5f025c57b92e9643ec0e7ccff6e66bc639d8a5f95bba928c
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
2、运行 Redis Server
执行如下命令使用 docker 在后台运行一个 redis server 的进程。
docker run --name redis -p 127.0.0.1:6379:6379 -d redis
注:
(1) –name redis 表示为该容器命令为 redis,这个可自己更改,也可不传(自动随机命名),名称不能与运行过的容器名称重复。
(2) -p 127.0.0.1:6379:6379 需要解释一下,如果是生产的环境强烈建议加上 127.0.0.1 这个参数,它表示只监听来自本服务器的 6379 端口访问。
这样的目的是为了 Redis 的使用更加安全,请相信我。Redis 此前一直存在严重的 BUG,使黑客能够通过 Redis 服务漏洞直接访问服务器的资源,从而完全控制你的服务器,即使是你设置了密码也能轻松破解。
我之前就中过这样的招,被黑客控制服务器,然后使用服务器资源去继续扫描更多的 6379 端口去尝试破解,然后用服务器资源来进行不法行为,比如挖矿等…
一般我们的 Redis 都是用作缓存,尽量将其设计成本机访问。
(3) 如果你真的有其他服务器来访问 Redis 的情况,建议一定做好安全措施,比如设置防火墙,跳板机,更改端口为非 6379 端口等等。这一点非常重要,切忌不要把你的服务器的 6379 端口暴露在英特网上。
示例:
$ docker run -d \
--name redis \
--restart always \
-p 127.0.0.1:6379:6379 \
-v /data/redis:/data \
redis redis-server --appendonly yes
00e61202e2858cc86af03f6e320809aa6ec07a6d31186b0e082f6f8ce6d041a9
3、验证 Redis Server
(1) 验证 docker redis 进程
执行如下命令查看 redis docker 进程是否存在
docker ps | grep redis
示例:
$ docker ps | grep redis
00e61202e285 redis "docker-entrypoint.s…" 19 minutes ago Up 19 minutes 127.0.0.1:6379->6379/tcp redis
如上所述表示 docker redis 进程正在运行。
(2) 查看 redis-server 版本号
$ docker exec -it redis redis-server -v
Redis server v=6.0.6 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=97204d6a2bf74d2c
可以看到我们的 redis-server 是 6.0.6 版本。
注:如果你上面运行容器时的名称自定义了,就把命令中的 “… -it redis …” 中的 redis 换成你的容器名字就行,也可以用容器 ID 的开头的几个字符。下同。
(3) 查看 redis 运行日志
➜ ~ docker logs redis
1:C 12 Oct 2020 17:48:00.002 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 12 Oct 2020 17:48:00.002 # Redis version=6.0.6, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 12 Oct 2020 17:48:00.002 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 12 Oct 2020 17:48:00.004 * Running mode=standalone, port=6379.
1:M 12 Oct 2020 17:48:00.004 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 12 Oct 2020 17:48:00.004 # Server initialized
1:M 12 Oct 2020 17:48:00.004 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. 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.
1:M 12 Oct 2020 17:48:00.004 # 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, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1:M 12 Oct 2020 17:48:00.004 * Ready to accept connections
3、访问 Redis
在你的 docker 服务器的命令行执行如下命令,即可连接到容器名为 redis 的容器。
docker exec -it redis redis-cli
示例:
$ docker exec -it redis redis-cli
127.0.0.1:6379> set name jiangzl
OK
127.0.0.1:6379> get name
"jiangzl"
127.0.0.1:6379>
总结
本文只是分享了最简单地使用 docker 运行 redis 的方法,如果你需要更多个性化的配置与运行方式,请参考附录中的 docker hub 中的 redis 主页。
附录
参考:
https://hub.docker.com/_/redis?tab=description
Redis 持久化官方文档:
https://redis.io/topics/persistence