Redis容器部署主从复制数据持久化

在工作中,我们经常会遇到使用redis的场景:比如一些固定的数据,高频访问且基本不变的数据,变化频率低的都可以存入缓存中,加快系统的访问速度。目的就是为了提高系统的查询效率,提高性能。

部署redis镜像方法有多种,可以直接拉取官网redis镜像使用,也可根据自己需求自定义部署redis来制作镜像。下面介绍拉取官网镜像部署redis容器。

redis容器部署单点

Redis容器部署及数据持久化

0610

1. 安装docker与docker-compose

# 卸载老版本docker
[root@docker ~]# yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine

[root@docker ~]# yum -y install epel-release wget
[root@docker ~]# wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
[root@docker ~]# sed -i 's#download.docker.com#mirrors.tuna.tsinghua.edu.cn/docker-ce#g' /etc/yum.repos.d/docker-ce.repo
[root@docker ~]# yum -y install docker-ce

# 启动并配置镜像加速
[root@docker ~]# systemctl start docker.service && systemctl enable docker.service

[root@docker ~]# cat /etc/docker/daemon.json
{
  "registry-mirrors": ["https://registry.docker-cn.com"]
}

[root@docker ~]# systemctl restart docker.service
  • 安装docker-compose
[root@docker ~]# curl -L "https://github.com/docker/compose/releases/download/1.25.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
[root@docker ~]# chmod +x /usr/local/bin/docker-compose
[root@docker ~]# ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

2. 先决条件

[root@docker ~]# docker pull redis:4.0

[root@docker ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               4.0                 191c4017dcdd        4 weeks ago         89.3MB

[root@docker ~]# mkdir -p /data/redis/master/{conf,data}
[root@docker ~]# mkdir -p /data/redis/slave/conf
[root@docker ~]# cat /data/redis/conf/redis.conf
[root@docker ~]# cat /data/redis/master/conf/redis.conf
requirepass 123456
protected-mode no
dbfilename dump.rdb
save 300 10
appendonly yes
appendfsync everysec

[root@docker ~]# cat /data/redis/slave/conf/redis.conf
requirepass 123456
protected-mode no
dbfilename dump.rdb
save 300 10
appendonly yes
appendfsync everysec
slaveof redis_m_compose 6379
masterauth 123456

3. 创建docker-compose.yml文件

[root@docker ~]# cat /data/redis/docker-compose.yml 
version: '3'
services:
  redis_m_compose:
    image: redis:4.0
    container_name: redis_m
    restart: always
    ports:
      - 16379:6379
    volumes:
      - ./master/conf/redis.conf:/redis.conf
      - ./master/data:/data
    command: redis-server /redis.conf

  redis_s_compose:
    image: redis:4.0
    container_name: redis_s
    restart: always
    ports:
      - 16380:6379
    depends_on:
      - redis_m_compose
    volumes:
      - ./slave/conf/redis.conf:/redis.conf
    command: redis-server /redis.conf

4 docker-compose启动redis容器

[root@docker ~]# cd /data/redis/
[root@docker redis]# docker-compose up -d
Creating network "redis_default" with the default driver
Creating redis_m ... done
Creating redis_s ... done

[root@docker redis]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
1a0274893497        redis:4.0           "docker-entrypoint.s…"   12 seconds ago      Up 11 seconds       0.0.0.0:16380->6379/tcp   redis_s
c8d2bc74b574        redis:4.0           "docker-entrypoint.s…"   13 seconds ago      Up 12 seconds       0.0.0.0:16379->6379/tcp   redis_m

# 连接主库
[root@docker ~]# docker exec -it redis_m bash
root@de34c8f03b92:/data# cd /usr/local/bin/
root@de34c8f03b92:/usr/local/bin# ./redis-cli 
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> set name dbawsp.com
OK
127.0.0.1:6379> get name
"dbawsp.com"

# 连接从库
[root@docker ~]# docker exec -it redis_s bash
root@1a0274893497:/data# cd /usr/local/bin/
root@1a0274893497:/usr/local/bin# ./redis-cli 
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> get name
"dbawsp.com"

# 查看主从复制状态信息
127.0.0.1:6379> info
...(略)
# Replication
role:master
connected_slaves:1
slave0:ip=172.19.0.3,port=6379,state=online,offset=98,lag=1
master_replid:b30e1ddf5bfbdafffed10392dbc973c182b9a54b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:98
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:98
...(略)

5. 销毁两个容器

[root@docker redis]# docker-compose down
Stopping redis_s ... done
Stopping redis_m ... done
Removing redis_s ... done
Removing redis_m ... done
Removing network redis_default

[root@docker redis]# ll -sh /data/redis/master/data/
total 8.0K
4.0K -rw-r--r-- 1 polkitd input  63 May 26 06:21 appendonly.aof
4.0K -rw-r--r-- 1 polkitd input 199 May 26 06:24 dump.rdb

[root@docker redis]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

6. 重建Redis容器

[root@docker redis]# docker-compose up -d
Creating network "redis_default" with the default driver
Creating redis_m ... done
Creating redis_s ... done

[root@docker redis]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
e86f8160d934        redis:4.0           "docker-entrypoint.s…"   5 seconds ago       Up 4 seconds        0.0.0.0:16380->6379/tcp   redis_s
d2e00d2d3fc8        redis:4.0           "docker-entrypoint.s…"   6 seconds ago       Up 5 seconds        0.0.0.0:16379->6379/tcp   redis_m

# 连接主库
[root@docker ~]# docker exec -it redis_m bash
root@d2e00d2d3fc8:/data# cd /usr/local/bin/
root@d2e00d2d3fc8:/usr/local/bin# ./redis-cli 
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> get name
"dbawsp.com"

# 连接从库
[root@docker ~]# docker exec -it redis_s bash
root@e86f8160d934:/data# cd /usr/local/bin/
root@e86f8160d934:/usr/local/bin# ./redis-cli 
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> get name
"dbawsp.com"

# 查看主从复制状态信息
...(略)
# Replication
role:master
connected_slaves:1
slave0:ip=172.20.0.3,port=6379,state=online,offset=112,lag=0
master_replid:734351a80cd0b94ed77c83e3c9ac4138f17e5187
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:112
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:112
...(略)
「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!
0 条回复 A 作者 M 管理员
    所有的伟大,都源于一个勇敢的开始!
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论