redis使用,基本上都是自己修改好配置文件再启动。redis5以后,docker中都找不到配置文件了(容器内部会自己启动),所以我们再启动容器的时候 需要 自己指定配置文件,然后启动redis。
1. 单机版redis容器
1.1 准备redis配置文件
用docker拉取好redis以后,查看镜像信息,能看到版本号:
从redis官网下载对应的版本,拿出其中的配置文件。
配置文件中,可以根据需求配置:
- 1.1 注释掉bind的ip:
# 127.0.0.1
- 1.2 关闭保护模式,使能远程访问:
protected-mode no
- 1.3 修改端口:
port 6379
- 1.4 后台守护进程关闭着(因为docker有-d命令,这个就不要开启了,否则容器启动错误,不要开启,不要开启):
daemonize no
- 1.5 修改pid文件:
pidfile /var/run/redis_6379.pid
- 1.6 修改数据库文件:
dbfilename dump_6379.rdb
- 1.7 开启AOF持久化:
appendonly yes
- 1.8 修改数据库目录(rdb和aof都在这个目录,默认是在/data/文件夹下,不用动):
dir ./
- 1.9 放开密码注释,添加密码:
requirepass redis123
注意:数据库持久化默认机制如下,搜索save可以修改
最后将redis配置文件上传到此目录:After 3600 seconds (an hour) if at least 1 key changed After 300 seconds (5 minutes) if at least 100 keys changed After 60 seconds if at least 10000 keys changed
sudo mkdir /home/soft/redis/conf
1.2 启动容器
sudo docker run -d --restart=always \
--name redis \
-p 6379:6379 \
--privileged=true \
-v /home/soft/redis/conf/redis.conf:/etc/redis/redis.conf \
-v /home/soft/redis/data:/data \
redis \
redis-server /etc/redis/redis.conf
或者用host模式,不做端口映射:
sudo docker run -d --restart=always \
--name redis \
--net host \
--privileged=true \
-v /home/soft/redis/conf/redis.conf:/etc/redis/redis.conf \
-v /home/soft/redis/data:/data \
redis \
redis-server /etc/redis/redis.conf
2. redis-cluster集群
redis-clus集群主要是启动后,进行命令操作集群的,这里制作容器编排,具体配置查看:
2.1 配置文件
暂定6台容器,3主3从的cluster模式,需要6份配置文件,目录分别为:/home/soft/redis-cluster/{6380、6381、6382、6383、6384、6385}/conf/
配置文件除了单机版修改的以外,还需要开启集群:
cluster-enabled yes # 启动集群模式(去掉注释)
cluster-config-file nodes-6381.conf # 集群节点信息文件
cluster-node-timeout 10000
masterauth "redis123" # 添加密码,没有可以不设置,要是有,所有节点密码要一致
2.2 docker-compose.yml文件
端口映射麻烦,直接用的host模式,也可以用bridge模式,创建网络并指定ip。
cd /home/soft/redis-cluster
sudo vim redis-cluster.yml
写入以下配置:
version: '3.9'
services:
# redis6380配置
redis6380:
image: redis
hostname: redis6380
container_name: redis6380
restart: always
network_mode: "host"
privileged: true
volumes:
- /home/soft/redis-cluster/6380/conf/redis6380.conf:/etc/redis/redis6380.conf
- /home/soft/redis-cluster/6380/data:/data
command: ["redis-server", "/etc/redis/redis6380.conf"]
# redis6381配置
redis6381:
image: redis
hostname: redis6381
container_name: redis6381
restart: always
network_mode: "host"
privileged: true
volumes:
- /home/soft/redis-cluster/6381/conf/redis6381.conf:/etc/redis/redis6381.conf
- /home/soft/redis-cluster/6381/data:/data
command: ["redis-server", "/etc/redis/redis6381.conf"]
# redis6382配置
redis6382:
image: redis
hostname: redis6382
container_name: redis6382
restart: always
network_mode: "host"
privileged: true
volumes:
- /home/soft/redis-cluster/6382/conf/redis6382.conf:/etc/redis/redis6382.conf
- /home/soft/redis-cluster/6382/data:/data
command: ["redis-server", "/etc/redis/redis6382.conf"]
# redis6383配置
redis6383:
image: redis
hostname: redis6383
container_name: redis6383
restart: always
network_mode: "host"
privileged: true
volumes:
- /home/soft/redis-cluster/6383/conf/redis6383.conf:/etc/redis/redis6383.conf
- /home/soft/redis-cluster/6383/data:/data
command: ["redis-server", "/etc/redis/redis6383.conf"]
# redis6384配置
redis6384:
image: redis
hostname: redis6384
container_name: redis6384
restart: always
network_mode: "host"
privileged: true
volumes:
- /home/soft/redis-cluster/6384/conf/redis6384.conf:/etc/redis/redis6384.conf
- /home/soft/redis-cluster/6384/data:/data
command: ["redis-server", "/etc/redis/redis6384.conf"]
# redis6385配置
redis6385:
image: redis
hostname: redis6385
container_name: redis6385
restart: always
network_mode: "host"
privileged: true
volumes:
- /home/soft/redis-cluster/6385/conf/redis6385.conf:/etc/redis/redis6385.conf
- /home/soft/redis-cluster/6385/data:/data
command: ["redis-server", "/etc/redis/redis6385.conf"]
启动并查看:
sudo docker compose -f redis-cluster.yml up -d
sudo docker compose -f redis-cluster.yml ps
up
网友评论