1. 简介
Redis(全称:Remote Dictionary Server,即远程字典服务器)是一个开源的高性能键值数据库和缓存系统。Redis 的数据结构支持字符串、哈希表、列表、集合和有序集合等类型。同时,Redis 还提供了丰富的操作指令,例如 GET/SET、INCR/DECR、HGET/HSET、LPUSH/RPUSH、SADD/SMEMBERS、ZADD/ZRANGE 等。除此之外,Redis 还支持事务、过期时间、发布/订阅等特性,能够方便地实现各种高效的数据存储和读取方案。
2. docker创建网络
docker network create hadoop-network
# 查看
docker network ls
3. 下载二进制文件
wget http://download.redis.io/releases/redis-7.0.3.tar.gz
4. 准备所需配置文件
cat > config/master/redis.conf << EOF
bind 0.0.0.0
daemonize yes
logfile "/usr/local/redis/redis.log"
dir /opt/apache/redis/data
masterauth 123456
requirepass 123456
appendonly yes
EOF
参数说明:
bind # 监听ip,多个ip用空格分隔,监听所有的IP地址
daemonize yes # 允许后台启动
logfile # 日志路径
dir # 数据库备份文件存放目录
masterauth # slave连接master密码,master可省略
requirepass # 设置master连接密码,slave可省略
appendonly # 在/opt/apache/redis/data目录生成appendonly.aof文件,将每一次写操作请求都追加到appendonly.aof 文件中
cat > config/slave/redis.conf << EOF
bind 0.0.0.0
daemonize yes
logfile "/usr/local/redis/redis.log"
dir /opt/apache/redis/data
replicaof redis-master 6379
masterauth 123456
requirepass 123456
appendonly yes
EOF
参数说明:
bind # 监听ip,多个ip用空格分隔,监听所有的IP地址
daemonize yes # 允许后台启动
logfile # 日志路径
dir # 数据库备份文件存放目录
replicaof # replicaof用于追随某个节点的redis,被追随的节点为主节点,追随的为从节点。就是设置master节点
masterauth # slave连接master密码,master可省略
requirepass # 设置master连接密码,slave可省略
appendonly # 在/opt/apache/redis/data目录生成appendonly.aof文件,将每一次写操作请求都追加到appendonly.aof 文件中
cat > bootstrap.sh << EOF
#!/usr/bin/env sh
wait_for() {
echo Waiting for $1 to listen on $2...
sleep 1
while ! nc -z $1 $2; do echo waiting...; sleep 1s; done
}
node_type=$1
if [ $node_type = "slave" ];then
wait_for redis-master 6379
fi
/usr/local/bin/redis-server /usr/local/redis/redis.conf
tail -f /usr/local/redis/redis.log
EOF
5. 构建镜像的Dockerfile
FROM centos:7.9
RUN rm -f /etc/localtime && \
ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo "Asia/Shanghai" > /etc/timezone
RUN export LANG=zh_CN.UTF-8
RUN yum -y install net-tools telnet wget nc less gcc gcc++ make curl vim
RUN mkdir /opt/apache/
# 编译安装 redis
ENV REDIS_VERSION 7.0.3
ADD redis-${REDIS_VERSION}.tar.gz /opt/apache/
ENV REDIS_HOME /opt/apache/redis
RUN ln -s /opt/apache/redis-${REDIS_VERSION} $REDIS_HOME
# 开始编译
RUN cd $REDIS_HOME && make && make install
# 创建数据目录
RUN mkdir ${REDIS_HOME}/data
# copy bootstrap.sh
COPY bootstrap.sh /opt/apache/
RUN chmod +x /opt/apache/bootstrap.sh
WORKDIR $REDIS_HOME
# 需要查看构建镜像详细过程则需要加上 --progress=plain 选项
docker build -t redis:7.0.3 . --no-cache --progress=plain
### 参数解释
# -t:指定镜像名称
# . :当前目录Dockerfile
# -f:指定Dockerfile路径
# --no-cache:不缓存
6. docker-compose.yml文件
version: '3'
services:
redis-master:
image: redis:7.0.3
container_name: redis-master
hostname: redis-master
restart: always
volumes:
- ./config/master/redis.conf:/usr/local/redis/redis.conf
ports:
- "36379:6379"
#command: ["sh","-c","/opt/apache/bootstrap.sh master"]
command: ["sh","-c","/usr/local/bin/redis-server /usr/local/redis/redis.conf ; tail -f /usr/local/redis/redis.log"]
networks:
- hadoop-network
healthcheck:
test: ["CMD-SHELL", "netstat -tnlp|grep 6379 || exit 1"]
interval: 10s
timeout: 20s
retries: 3
redis-slave:
image: redis:7.0.3
restart: always
deploy:
#两个从节点
replicas: 2
volumes:
- ./config/slave/redis.conf:/usr/local/redis/redis.conf
ports:
- "6379"
command: ["sh","-c","/opt/apache/bootstrap.sh slave"]
networks:
- hadoop-network
healthcheck:
test: ["CMD-SHELL", "netstat -tnlp|grep 6379 || exit 1"]
interval: 10s
timeout: 10s
retries: 3
# 连接外部网络
networks:
hadoop-network:
external: true
docker-compose up -d
docker-compose logs -f
docker-compose ps
7. 检查验证
docker exec -it redis-master bash
# 登录
redis-cli -h redis-master
# 输入密码,配置文件里配置的密码
redis-master:6379> auth 123456
# 查看集群信息
redis-master:6379> info replication
# 非交互式,但是不建议,因为密码直接在history可以查到,安全问题
redis-cli -h redis-master -a 123456 info replication
8. 常用操作命令
SET key value: 设置字符串类型的键值对。
GET key: 获取指定 key 的值。
DEL key: 删除指定的 key 。
INCR key: 将指定 key 的值加 1。
DECR key: 将指定 key 的值减 1。
EXISTS key: 判断 key 是否存在。
EXPIRE key seconds: 设置 key 的过期时间,以秒为单位。
TTL key: 获取 key 的剩余生存时间,以秒为单位。
KEYS pattern: 查找所有符合给定模式的 key。
FLUSHALL: 删除所有 key。
一些高级命令,可以用于处理 Redis 的复杂数据结构和实现事务等功能:
LPUSH key value: 将一个元素添加到列表的头部。
RPUSH key value: 将一个元素添加到列表的尾部。
LPOP key: 弹出并返回列表的头部元素。
RPOP key: 弹出并返回列表的尾部元素。
SADD key members: 将一个或多个元素添加到集合中。
SMEMBERS key: 返回集合中的所有成员。
ZADD key score member: 将元素和分值添加到有序集合中。
ZREVRANGE key start stop: 反向获取有序集合中指定分值范围内的所有成员。
MULTI: 开始事务。
EXEC: 执行事务中所有命令。
WATCH key: 监视指定 key 。如果在执行事务期间该 key 发生了变化,事务将被取消。
参考资料: