Docker 搭建Pika分片多主集群(Docker & Codis & Pika& Replication & Sharding)

Docker 搭建Pika分片多主集群

  • 环境准备
    • 依赖
    • 安装Docker
    • 安装redis-cli
  • 基本单例
    • 启动
    • 测试
  • 带配置的单实例
    • 编写配置
    • 启动命令
  • 主从搭建
    • 环境
    • 准备
    • 编写配置
    • 启动命令
    • 测试
  • Docker Stack 搭建主从
    • 创建swarm overlay网络
    • 启动firewalld
    • 创建目录
    • 编写Docker Stack配置
    • 启动
  • 双主搭建

环境准备

依赖

  • CentOS7.6

安装Docker

参照安装(点击)

安装redis-cli

预先安装redis-cli用于测试pika的连接。

yum install -y redis-cli

基本单例

启动

docker pull pikadb/pika:v3.2.2
docker run -p 6379:9221 --name pika --hostname pika \
-d pikadb/pika:v3.2.2 \
./bin/pika -c conf/pika.conf

测试

redis-cli set 1 a
redis-cli get 1

连接成功

带配置的单实例

编写配置

mkdir -p /opt/volumns/pika/conf/
vim /opt/volumns/pika/conf/pika.conf

填入以下配置

# Pika port
port : 9221

# Thread Number
# pika是多线程的, 该参数能够配置pika的线程数量, 不建议配置值超过部署服务器的CPU核心数量 
thread-num : 1

# Thread Pool Size
# 处理命令用户请求命令线程池的大小
thread-pool-size : 12

# Sync Thread Number
# sync 主从同步时候从库执行主库传递过来命令的线程数量
sync-thread-num : 6

# sync 处理线程的任务队列大小, 不建议修改
sync-buffer-size : 10

# Pika log path
# Pika日志目录, 用于存放INFO, WARNING, ERROR日志以及用于同步的binlog(write2fine)文件
log-path : ./log/

# Pika db path
# Pika数据目录
db-path : ./db/

# Pika write-buffer-size
# Pika 底层引擎的write_buffer_size配置, 设置越大写入性能越好但会在buffer刷盘时带来更大的IO负载, 请依据使用场景合理配置
write-buffer-size : 268435456

# Pika timeout
# Pika 的连接超时时间配置, 单位为秒, 当连接无请求时(进入sleep状态)开始从配置时间倒计时, 当倒计时为0时pika将强行
# 断开该连接, 可以通过合理配置该参数避免可能出现的pika连接数用尽问题, 该参数默认值为60
timeout : 31536000

# Requirepass
# 密码管理员密码, 默认为空, 如果该参数与下方的userpass参数相同(包括同时为空), 则userpass参数将自动失效, 所有用户均为
# 管理员身份不受userblacklist参数的限制
requirepass : adminpass

# Masterauth
# 同步验证密码, 用于slave(从库)连接master(主库)请求同步时进行验证, 该参数需要与master(主库)的requirepass一致
masterauth :

# Userpass
# 用户密码, 默认为空, 如果该参数与上方的userpass参数相同(包括同时为空), 则本参数将自动失效, 所有用户均为管理员身份不
# 受userblacklist参数的限制
userpass :

# User Blacklist
# 指令黑名单, 能够限制通过userpass登录的用户, 这些用户将不能使用黑名单中的指令, 指令之间使用","隔开, 默认为空
# 建议将高风险命令配置在该参数中
userblacklist : FLUSHALL, SHUTDOWN, KEYS, CONFIG

# if this option is set to 'classic', that means pika support multiple DB, in
# this mode, option databases enable
# if this option is set to 'sharding', that means pika support multiple Table, you
# can specify slot num for each table, in this mode, option default-slot-num enable
# Pika instance mode [classic | sharding]
# 分为经典模式和分片模式,[classic | sharding],经典模式中支持多db的配置
instance-mode : classic

# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT  where
# dbid is a number between 0 and 'databases' - 1, limited in [1, 8]
# 经典模式下下指定db的数量,使用方式和redis一致
databases : 1

# default slot number each table in sharding mode
# 分片模式下每一个table中默认的slot数量
default-slot-num : 1024

# Dump Prefix
# Pika的dump文件名称前缀, bgsave后生成的文件将以该前缀命名
dump-prefix :

# daemonize  [yes | no] 
# 守护进程模式  [yes | no]
#daemonize : yes
 
# slotmigrate  [yes | no], pika3.0.0暂不支持该参数
#slotmigrate : no

# Dump Path
# Pika dump目录设置, bgsave后生成的文件将存放在该目录中
dump-path : ./dump/

# Expire-dump-days
# dump目录过期时间, 单位为天, 默认为0即永不过期
dump-expire : 0

# pidfile Path
# pidfile Path pid文件目录
pidfile : ./pika.pid

# Max Connection 
# pika最大连接数配置参数
maxclients : 20000

# the per file size of sst to compact, defalut is 2M
# rocks-db的sst文件体积, sst文件是层级的, 文件越小, 速度越快, 合并代价越低, 但文件数量就会超多, 而文件越大, 速度相对变慢, 合并代价大, 但文件数量会很少, 默认是 20M
target-file-size-base : 20971520

# Expire-logs-days
# binlog(write2file)文件保留时间, 7天, 最小为1, 超过7天的文件会被自动清理
expire-logs-days : 7

# Expire-logs-nums 
# binlog(write2file)文件最大数量, 200个, 最小为10, 超过200个就开始自动清理, 始终保留200个
expire-logs-nums : 10

# Root-connection-num 
# root用户连接保证数量:2个, 即时Max Connection用完, 该参数也能确保本地(127.0.0.1)有2个连接可以同来登陆pika
root-connection-num : 2

# Slowlog-write-errorlog
slowlog-write-errorlog : yes

# Slowlog-log-slower-than
# 慢日志记录时间, 单位为微秒, pika的慢日志记录在pika-ERROR.log中, pika没有类似redis slow log的慢日志提取api
slowlog-log-slower-than : 10000

# Slowlog-max-len
slowlog-max-len : 128

# slave是否是只读状态(yes/no, 1/0)
# slave-read-only : 0

# Pika db sync path
# Pika db 同步路径配置参数
db-sync-path : ./dbsync/
# db sync speed(MB) max is set to 1024MB, min is set to 0, and if below 0 or above 1024, the value will be adjust to 1024
# 该参数能够控制全量同步时的传输速度, 合理配置该参数能够避免网卡被用尽, 该参数范围为1~125, 意为:1mb~125mb,当该参数
# 被配置为小于0或大于125, 该参数会被自动配置为125
db-sync-speed : -1

# The slave priority
slave-priority : 100

# network interface
# 指定网卡
# network-interface : eth1

# replication
# 同步参数配置, 适用于从库节点(slave), 该参数格式为ip:port, 例如192.168.1.2:6666, 启动后该示例会自动向192.168.1.2的
# 6666端口发送同步请求
# slaveof : master-ip:master-port

# CronTask, format 1: start-end/ratio, like 02-04/60, pika will check to schedule compaction between 2 to 4 o'clock everyday
#                   if the freesize/disksize > 60%.
#           format 2: week/start-end/ratio, like 3/02-04/60, pika will check to schedule compaction between 2 to 4 o'clock
#                   every wednesday, if the freesize/disksize > 60%.
#           NOTICE: if compact-interval is set, compact-cron will be mask and disable.
#
#compact-cron : 3/02-04/60

# Compact-interval, format: interval/ratio, like 6/60, pika will check to schedule compaction every 6 hours,
#                           if the freesize/disksize > 60%. NOTICE:compact-interval is prior than compact-cron;
#compact-interval :

# server-id for hub
# 配置双主或Hub需要的server id, 不使用双主或Hub请忽略该参数
server-id : 1

# 双主配置, 不使用双主请忽略以下配置

# 双主对端Ip
# double-master-ip :

# 双主对端Port
# double-master-port :

# 双主对端server id
# double-master-server-id :
 
# 自动全量compact, 通过配置的参数每天定时触发一次自动全量compact, 特别适合存在多数据结构大量过期、删除、key名称复用的场景
# 参数格式为:"启动时间(小时)-结束时间(小时)/磁盘空余空间百分比", 例如你需要配置一个每天在凌晨3~4点之间自动compact的任务
# 同时该任务仅仅在磁盘空余空间不低于30%的时候执行, 那么应配置为:03-04/30, 该参数默认为空
# compact-cron :

# 自动全量compact, 该参与与compact-cron的区别为, compact-cron每天仅在指定时间段执行, 而compact-interval则以配置时间为周期
# 循环执行, 例如你需要配置一个每4小时执行一次的自动compact任务, 同时该任务仅仅在磁盘空余空间不低于30%的时候执行, 那么该参
# 数应配置为:4/30, 该参数默认为空
# compact-interval :

# 从库实例权重设置, 仅配合哨兵使用,无其它功能, 权重低的slave会优先选举为主库, 该参数默认为0(不参与选举)
# slave-priority :

# 该参数仅适用于pika跨版本同步时不同版本的binlog能够兼容并成功解析, 该参数可配置为[new | old]
# 当该参数被配置为new时, 该实例仅能作为3.0.0及以上版本pika的从库, 与pika2.3.3~2.3.5不兼容
# 当该参数被配置为old时, 该时候仅能作为2.3.3~2.3.5版本pika的从库, 与pika3.0.0及以上版本不兼容
# 该参数默认值为new, 该参数可在没有配置同步关系的时候通过config set动态调整, 一旦配置了同步关系则不可动态修改
# 需要先执行slaveof no one关闭同步配置, 之后即可通过config set动态修改
# identify-binlog-type : new


###################
#Critical Settings#
#    危险参数      #
###################
# write_binlog  [yes | no]
write-binlog : yes
# binlog file size: default is 100M,  limited in [1K, 2G]
# write2file文件体积, 默认为100MB, 一旦启动不可修改,  limited in [1K, 2G]
binlog-file-size : 104857600
# Automatically triggers a small compaction according statistics
# Use the cache to store up to 'max-cache-statistic-keys' keys
# if 'max-cache-statistic-keys' set to '0', that means turn off the statistics function
# it also doesn't automatically trigger a small compact feature
max-cache-statistic-keys : 0
# When 'delete' or 'overwrite' a specific multi-data structure key 'small-compaction-threshold' times,
# a small compact is triggered automatically, default is 5000, limited in [1, 100000]
small-compaction-threshold : 5000
# If the total size of all live memtables of all the DBs exceeds
# the limit, a flush will be triggered in the next DB to which the next write
# is issued.
max-write-buffer-size : 10737418240
# Compression

# 压缩方式[snappy | zlib | none]默认为snappy, 一旦启动不可修改
compression : snappy

# max-background-flushes: default is 1, limited in [1, 4]
# 指定后台flush线程数量, 默认为1, 范围为[1, 4]
max-background-flushes : 1

# max-background-compactions: default is 2, limited in [1, 8]
# 指定后台压缩线程数量, 默认为1, 范围为[1, 8]
max-background-compactions : 2

# max-cache-files default is 5000
# DB可以使用的打开文件的数量, 默认为5000
max-cache-files : 5000

# max_bytes_for_level_multiplier: default is 10, you can change it to 5
# pika引擎中层级因子, 用于控制每个层级与上一层级总容量的倍数关系, 默认为10(倍), 允许调整为5(倍) 
max-bytes-for-level-multiplier : 10
# BlockBasedTable block_size, default 4k
# block-size: 4096
# block LRU cache, default 8M, 0 to disable
# block-cache: 8388608
# whether the block cache is shared among the RocksDB instances, default is per CF
# share-block-cache: no
# whether or not index and filter blocks is stored in block cache
# cache-index-and-filter-blocks: no
# when set to yes, bloomfilter of the last level will not be built
# optimize-filters-for-hits: no
# https://github.com/facebook/rocksdb/wiki/Leveled-Compaction#levels-target-size
# level-compaction-dynamic-level-bytes: no

配置解读:

启动命令

docker run -p 6379:9221 --name pika --hostname pika \
-v /opt/volumns/pika/conf:/pika/output/conf:ro \
-v /opt/volumns/pika/db:/pika/output/db \
-v /opt/volumns/pika/log:/pika/output/log \
-v /etc/localtime:/etc/localtime \
--restart=always --privileged=true \
-d pikadb/pika:v3.2.2 \
./bin/pika -c conf/pika.conf

主从搭建

环境

  • 虚拟机vm1,vm2
  • CentOS7.6
  • 安装了docker,在同一个虚拟机中

准备

删除db目录

rm -rm /opt/volumns/pika/db 

安装redis-cli

yum install -y epel-release
yum install -y redis

编写配置

主配置,hostname为pika-1

# Pika port
port : 6379

# Thread Number
# pika是多线程的, 该参数能够配置pika的线程数量, 不建议配置值超过部署服务器的CPU核心数量 
thread-num : 1

# Thread Pool Size
# 处理命令用户请求命令线程池的大小
thread-pool-size : 12

# Sync Thread Number
# sync 主从同步时候从库执行主库传递过来命令的线程数量
sync-thread-num : 6

# sync 处理线程的任务队列大小, 不建议修改
sync-buffer-size : 10

# Pika log path
# Pika日志目录, 用于存放INFO, WARNING, ERROR日志以及用于同步的binlog(write2fine)文件
log-path : ./log/

# Pika db path
# Pika数据目录
db-path : ./db/

# Pika write-buffer-size
# Pika 底层引擎的write_buffer_size配置, 设置越大写入性能越好但会在buffer刷盘时带来更大的IO负载, 请依据使用场景合理配置
write-buffer-size : 268435456

# Pika timeout
# Pika 的连接超时时间配置, 单位为秒, 当连接无请求时(进入sleep状态)开始从配置时间倒计时, 当倒计时为0时pika将强行
# 断开该连接, 可以通过合理配置该参数避免可能出现的pika连接数用尽问题, 该参数默认值为60
timeout : 31536000

# Requirepass
# 密码管理员密码, 默认为空, 如果该参数与下方的userpass参数相同(包括同时为空), 则userpass参数将自动失效, 所有用户均为
# 管理员身份不受userblacklist参数的限制
requirepass : adminpass

# Masterauth
# 同步验证密码, 用于slave(从库)连接master(主库)请求同步时进行验证, 该参数需要与master(主库)的requirepass一致
masterauth : adminpass

# Userpass
# 用户密码, 默认为空, 如果该参数与上方的userpass参数相同(包括同时为空), 则本参数将自动失效, 所有用户均为管理员身份不
# 受userblacklist参数的限制
userpass :

# User Blacklist
# 指令黑名单, 能够限制通过userpass登录的用户, 这些用户将不能使用黑名单中的指令, 指令之间使用","隔开, 默认为空
# 建议将高风险命令配置在该参数中
userblacklist : FLUSHALL, SHUTDOWN, KEYS, CONFIG

# if this option is set to 'classic', that means pika support multiple DB, in
# this mode, option databases enable
# if this option is set to 'sharding', that means pika support multiple Table, you
# can specify slot num for each table, in this mode, option default-slot-num enable
# Pika instance mode [classic | sharding]
# 分为经典模式和分片模式,[classic | sharding],经典模式中支持多db的配置
instance-mode : classic

# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT  where
# dbid is a number between 0 and 'databases' - 1, limited in [1, 8]
# 经典模式下下指定db的数量,使用方式和redis一致
databases : 1

# default slot number each table in sharding mode
# 分片模式下每一个table中默认的slot数量
default-slot-num : 1024

# Dump Prefix
# Pika的dump文件名称前缀, bgsave后生成的文件将以该前缀命名
dump-prefix :

# daemonize  [yes | no] 
# 守护进程模式  [yes | no]
#daemonize : yes
 
# slotmigrate  [yes | no], pika3.0.0暂不支持该参数
#slotmigrate : no

# Dump Path
# Pika dump目录设置, bgsave后生成的文件将存放在该目录中
dump-path : ./dump/

# Expire-dump-days
# dump目录过期时间, 单位为天, 默认为0即永不过期
dump-expire : 0

# pidfile Path
# pidfile Path pid文件目录
pidfile : ./pika.pid

# Max Connection 
# pika最大连接数配置参数
maxclients : 20000

# the per file size of sst to compact, defalut is 2M
# rocks-db的sst文件体积, sst文件是层级的, 文件越小, 速度越快, 合并代价越低, 但文件数量就会超多, 而文件越大, 速度相对变慢, 合并代价大, 但文件数量会很少, 默认是 20M
target-file-size-base : 20971520

# Expire-logs-days
# binlog(write2file)文件保留时间, 7天, 最小为1, 超过7天的文件会被自动清理
expire-logs-days : 7

# Expire-logs-nums 
# binlog(write2file)文件最大数量, 200个, 最小为10, 超过200个就开始自动清理, 始终保留200个
expire-logs-nums : 10

# Root-connection-num 
# root用户连接保证数量:2个, 即时Max Connection用完, 该参数也能确保本地(127.0.0.1)有2个连接可以同来登陆pika
root-connection-num : 2

# Slowlog-write-errorlog
slowlog-write-errorlog : yes

# Slowlog-log-slower-than
# 慢日志记录时间, 单位为微秒, pika的慢日志记录在pika-ERROR.log中, pika没有类似redis slow log的慢日志提取api
slowlog-log-slower-than : 10000

# Slowlog-max-len
slowlog-max-len : 128

# slave是否是只读状态(yes/no, 1/0)
# slave-read-only : 0

# Pika db sync path
# Pika db 同步路径配置参数
db-sync-path : ./dbsync/
# db sync speed(MB) max is set to 1024MB, min is set to 0, and if below 0 or above 1024, the value will be adjust to 1024
# 该参数能够控制全量同步时的传输速度, 合理配置该参数能够避免网卡被用尽, 该参数范围为1~125, 意为:1mb~125mb,当该参数
# 被配置为小于0或大于125, 该参数会被自动配置为125
db-sync-speed : -1

# The slave priority
slave-priority : 100

# network interface
# 指定网卡
# network-interface : eth1

# replication
# 同步参数配置, 适用于从库节点(slave), 该参数格式为ip:port, 例如192.168.1.2:6666, 启动后该示例会自动向192.168.1.2的
# 6666端口发送同步请求
# slaveof : master-ip:master-port

# CronTask, format 1: start-end/ratio, like 02-04/60, pika will check to schedule compaction between 2 to 4 o'clock everyday
#                   if the freesize/disksize > 60%.
#           format 2: week/start-end/ratio, like 3/02-04/60, pika will check to schedule compaction between 2 to 4 o'clock
#                   every wednesday, if the freesize/disksize > 60%.
#           NOTICE: if compact-interval is set, compact-cron will be mask and disable.
#
#compact-cron : 3/02-04/60

# Compact-interval, format: interval/ratio, like 6/60, pika will check to schedule compaction every 6 hours,
#                           if the freesize/disksize > 60%. NOTICE:compact-interval is prior than compact-cron;
#compact-interval :

# server-id for hub
# 配置双主或Hub需要的server id, 不使用双主或Hub请忽略该参数
server-id : 1

# 双主配置, 不使用双主请忽略以下配置

# 双主对端Ip
# double-master-ip :

# 双主对端Port
# double-master-port :

# 双主对端server id
# double-master-server-id :
 
# 自动全量compact, 通过配置的参数每天定时触发一次自动全量compact, 特别适合存在多数据结构大量过期、删除、key名称复用的场景
# 参数格式为:"启动时间(小时)-结束时间(小时)/磁盘空余空间百分比", 例如你需要配置一个每天在凌晨3~4点之间自动compact的任务
# 同时该任务仅仅在磁盘空余空间不低于30%的时候执行, 那么应配置为:03-04/30, 该参数默认为空
# compact-cron :

# 自动全量compact, 该参与与compact-cron的区别为, compact-cron每天仅在指定时间段执行, 而compact-interval则以配置时间为周期
# 循环执行, 例如你需要配置一个每4小时执行一次的自动compact任务, 同时该任务仅仅在磁盘空余空间不低于30%的时候执行, 那么该参
# 数应配置为:4/30, 该参数默认为空
# compact-interval :

# 从库实例权重设置, 仅配合哨兵使用,无其它功能, 权重低的slave会优先选举为主库, 该参数默认为0(不参与选举)
# slave-priority :

# 该参数仅适用于pika跨版本同步时不同版本的binlog能够兼容并成功解析, 该参数可配置为[new | old]
# 当该参数被配置为new时, 该实例仅能作为3.0.0及以上版本pika的从库, 与pika2.3.3~2.3.5不兼容
# 当该参数被配置为old时, 该时候仅能作为2.3.3~2.3.5版本pika的从库, 与pika3.0.0及以上版本不兼容
# 该参数默认值为new, 该参数可在没有配置同步关系的时候通过config set动态调整, 一旦配置了同步关系则不可动态修改
# 需要先执行slaveof no one关闭同步配置, 之后即可通过config set动态修改
# identify-binlog-type : new


###################
#Critical Settings#
#    危险参数      #
###################
# write_binlog  [yes | no]
write-binlog : yes
# binlog file size: default is 100M,  limited in [1K, 2G]
# write2file文件体积, 默认为100MB, 一旦启动不可修改,  limited in [1K, 2G]
binlog-file-size : 104857600
# Automatically triggers a small compaction according statistics
# Use the cache to store up to 'max-cache-statistic-keys' keys
# if 'max-cache-statistic-keys' set to '0', that means turn off the statistics function
# it also doesn't automatically trigger a small compact feature
max-cache-statistic-keys : 0
# When 'delete' or 'overwrite' a specific multi-data structure key 'small-compaction-threshold' times,
# a small compact is triggered automatically, default is 5000, limited in [1, 100000]
small-compaction-threshold : 5000
# If the total size of all live memtables of all the DBs exceeds
# the limit, a flush will be triggered in the next DB to which the next write
# is issued.
max-write-buffer-size : 10737418240
# Compression

# 压缩方式[snappy | zlib | none]默认为snappy, 一旦启动不可修改
compression : snappy

# max-background-flushes: default is 1, limited in [1, 4]
# 指定后台flush线程数量, 默认为1, 范围为[1, 4]
max-background-flushes : 1

# max-background-compactions: default is 2, limited in [1, 8]
# 指定后台压缩线程数量, 默认为1, 范围为[1, 8]
max-background-compactions : 2

# max-cache-files default is 5000
# DB可以使用的打开文件的数量, 默认为5000
max-cache-files : 5000

# max_bytes_for_level_multiplier: default is 10, you can change it to 5
# pika引擎中层级因子, 用于控制每个层级与上一层级总容量的倍数关系, 默认为10(倍), 允许调整为5(倍) 
max-bytes-for-level-multiplier : 10
# BlockBasedTable block_size, default 4k
# block-size: 4096
# block LRU cache, default 8M, 0 to disable
# block-cache: 8388608
# whether the block cache is shared among the RocksDB instances, default is per CF
# share-block-cache: no
# whether or not index and filter blocks is stored in block cache
# cache-index-and-filter-blocks: no
# when set to yes, bloomfilter of the last level will not be built
# optimize-filters-for-hits: no
# https://github.com/facebook/rocksdb/wiki/Leveled-Compaction#levels-target-size
# level-compaction-dynamic-level-bytes: no

从配置,需要注意这里必须把slave-read-only : 1打开。

# Pika port
port : 6379

# Thread Number
# pika是多线程的, 该参数能够配置pika的线程数量, 不建议配置值超过部署服务器的CPU核心数量 
thread-num : 1

# Thread Pool Size
# 处理命令用户请求命令线程池的大小
thread-pool-size : 12

# Sync Thread Number
# sync 主从同步时候从库执行主库传递过来命令的线程数量
sync-thread-num : 6

# sync 处理线程的任务队列大小, 不建议修改
sync-buffer-size : 10

# Pika log path
# Pika日志目录, 用于存放INFO, WARNING, ERROR日志以及用于同步的binlog(write2fine)文件
log-path : ./log/

# Pika db path
# Pika数据目录
db-path : ./db/

# Pika write-buffer-size
# Pika 底层引擎的write_buffer_size配置, 设置越大写入性能越好但会在buffer刷盘时带来更大的IO负载, 请依据使用场景合理配置
write-buffer-size : 268435456

# Pika timeout
# Pika 的连接超时时间配置, 单位为秒, 当连接无请求时(进入sleep状态)开始从配置时间倒计时, 当倒计时为0时pika将强行
# 断开该连接, 可以通过合理配置该参数避免可能出现的pika连接数用尽问题, 该参数默认值为60
timeout : 31536000

# Requirepass
# 密码管理员密码, 默认为空, 如果该参数与下方的userpass参数相同(包括同时为空), 则userpass参数将自动失效, 所有用户均为
# 管理员身份不受userblacklist参数的限制
requirepass : adminpass

# Masterauth
# 同步验证密码, 用于slave(从库)连接master(主库)请求同步时进行验证, 该参数需要与master(主库)的requirepass一致
masterauth : adminpass

# Userpass
# 用户密码, 默认为空, 如果该参数与上方的userpass参数相同(包括同时为空), 则本参数将自动失效, 所有用户均为管理员身份不
# 受userblacklist参数的限制
userpass :

# User Blacklist
# 指令黑名单, 能够限制通过userpass登录的用户, 这些用户将不能使用黑名单中的指令, 指令之间使用","隔开, 默认为空
# 建议将高风险命令配置在该参数中
userblacklist : FLUSHALL, SHUTDOWN, KEYS, CONFIG

# if this option is set to 'classic', that means pika support multiple DB, in
# this mode, option databases enable
# if this option is set to 'sharding', that means pika support multiple Table, you
# can specify slot num for each table, in this mode, option default-slot-num enable
# Pika instance mode [classic | sharding]
# 分为经典模式和分片模式,[classic | sharding],经典模式中支持多db的配置
instance-mode : classic

# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT  where
# dbid is a number between 0 and 'databases' - 1, limited in [1, 8]
# 经典模式下下指定db的数量,使用方式和redis一致
databases : 1

# default slot number each table in sharding mode
# 分片模式下每一个table中默认的slot数量
default-slot-num : 1024

# Dump Prefix
# Pika的dump文件名称前缀, bgsave后生成的文件将以该前缀命名
dump-prefix :

# daemonize  [yes | no] 
# 守护进程模式  [yes | no]
#daemonize : yes
 
# slotmigrate  [yes | no], pika3.0.0暂不支持该参数
#slotmigrate : no

# Dump Path
# Pika dump目录设置, bgsave后生成的文件将存放在该目录中
dump-path : ./dump/

# Expire-dump-days
# dump目录过期时间, 单位为天, 默认为0即永不过期
dump-expire : 0

# pidfile Path
# pidfile Path pid文件目录
pidfile : ./pika.pid

# Max Connection 
# pika最大连接数配置参数
maxclients : 20000

# the per file size of sst to compact, defalut is 2M
# rocks-db的sst文件体积, sst文件是层级的, 文件越小, 速度越快, 合并代价越低, 但文件数量就会超多, 而文件越大, 速度相对变慢, 合并代价大, 但文件数量会很少, 默认是 20M
target-file-size-base : 20971520

# Expire-logs-days
# binlog(write2file)文件保留时间, 7天, 最小为1, 超过7天的文件会被自动清理
expire-logs-days : 7

# Expire-logs-nums 
# binlog(write2file)文件最大数量, 200个, 最小为10, 超过200个就开始自动清理, 始终保留200个
expire-logs-nums : 10

# Root-connection-num 
# root用户连接保证数量:2个, 即时Max Connection用完, 该参数也能确保本地(127.0.0.1)有2个连接可以同来登陆pika
root-connection-num : 2

# Slowlog-write-errorlog
slowlog-write-errorlog : yes

# Slowlog-log-slower-than
# 慢日志记录时间, 单位为微秒, pika的慢日志记录在pika-ERROR.log中, pika没有类似redis slow log的慢日志提取api
slowlog-log-slower-than : 10000

# Slowlog-max-len
slowlog-max-len : 128

# slave是否是只读状态(yes/no, 1/0)
slave-read-only : 1

# Pika db sync path
# Pika db 同步路径配置参数
db-sync-path : ./dbsync/
# db sync speed(MB) max is set to 1024MB, min is set to 0, and if below 0 or above 1024, the value will be adjust to 1024
# 该参数能够控制全量同步时的传输速度, 合理配置该参数能够避免网卡被用尽, 该参数范围为1~125, 意为:1mb~125mb,当该参数
# 被配置为小于0或大于125时, 该参数会被自动配置为125
db-sync-speed : -1

# The slave priority
slave-priority : 100

# network interface
# 指定网卡
# network-interface : eth1

# replication
# 同步参数配置, 适用于从库节点(slave), 该参数格式为ip:port, 例如192.168.1.2:6666, 启动后该示例会自动向192.168.1.2的
# 6666端口发送同步请求
slaveof : pika-1:6379

# CronTask, format 1: start-end/ratio, like 02-04/60, pika will check to schedule compaction between 2 to 4 o'clock everyday
#                   if the freesize/disksize > 60%.
#           format 2: week/start-end/ratio, like 3/02-04/60, pika will check to schedule compaction between 2 to 4 o'clock
#                   every wednesday, if the freesize/disksize > 60%.
#           NOTICE: if compact-interval is set, compact-cron will be mask and disable.
#
#compact-cron : 3/02-04/60

# Compact-interval, format: interval/ratio, like 6/60, pika will check to schedule compaction every 6 hours,
#                           if the freesize/disksize > 60%. NOTICE:compact-interval is prior than compact-cron;
#compact-interval :

# server-id for hub
# 配置双主或Hub需要的server id, 不使用双主或Hub请忽略该参数
server-id : 1

# 双主配置, 不使用双主请忽略以下配置

# 双主对端Ip
# double-master-ip :

# 双主对端Port
# double-master-port :

# 双主对端server id
# double-master-server-id :
 
# 自动全量compact, 通过配置的参数每天定时触发一次自动全量compact, 特别适合存在多数据结构大量过期、删除、key名称复用的场景
# 参数格式为:"启动时间(小时)-结束时间(小时)/磁盘空余空间百分比", 例如你需要配置一个每天在凌晨3点~4点之间自动compact的任务
# 同时该任务仅仅在磁盘空余空间不低于30%的时候执行, 那么应配置为:03-04/30, 该参数默认为空
# compact-cron :

# 自动全量compact, 该参与与compact-cron的区别为, compact-cron每天仅在指定时间段执行, 而compact-interval则以配置时间为周期
# 循环执行, 例如你需要配置一个每4小时执行一次的自动compact任务, 同时该任务仅仅在磁盘空余空间不低于30%的时候执行, 那么该参
# 数应配置为:4/30, 该参数默认为空
# compact-interval :

# 从库实例权重设置, 仅配合哨兵使用,无其它功能, 权重低的slave会优先选举为主库, 该参数默认为0(不参与选举)
# slave-priority :

# 该参数仅适用于pika跨版本同步时不同版本的binlog能够兼容并成功解析, 该参数可配置为[new | old]
# 当该参数被配置为new时, 该实例仅能作为3.0.0及以上版本pika的从库, 与pika2.3.3~2.3.5不兼容
# 当该参数被配置为old时, 该时候仅能作为2.3.3~2.3.5版本pika的从库, 与pika3.0.0及以上版本不兼容
# 该参数默认值为new, 该参数可在没有配置同步关系的时候通过config set动态调整, 一旦配置了同步关系则不可动态修改
# 需要先执行slaveof no one关闭同步配置, 之后即可通过config set动态修改
# identify-binlog-type : new


###################
#Critical Settings#
#    危险参数      #
###################
# write_binlog  [yes | no]
write-binlog : yes
# binlog file size: default is 100M,  limited in [1K, 2G]
# write2file文件体积, 默认为100MB, 一旦启动不可修改,  limited in [1K, 2G]
binlog-file-size : 104857600
# Automatically triggers a small compaction according statistics
# Use the cache to store up to 'max-cache-statistic-keys' keys
# if 'max-cache-statistic-keys' set to '0', that means turn off the statistics function
# it also doesn't automatically trigger a small compact feature
max-cache-statistic-keys : 0
# When 'delete' or 'overwrite' a specific multi-data structure key 'small-compaction-threshold' times,
# a small compact is triggered automatically, default is 5000, limited in [1, 100000]
small-compaction-threshold : 5000
# If the total size of all live memtables of all the DBs exceeds
# the limit, a flush will be triggered in the next DB to which the next write
# is issued.
max-write-buffer-size : 10737418240
# Compression

# 压缩方式[snappy | zlib | none]默认为snappy, 一旦启动不可修改
compression : snappy

# max-background-flushes: default is 1, limited in [1, 4]
# 指定后台flush线程数量, 默认为1, 范围为[1, 4]
max-background-flushes : 1

# max-background-compactions: default is 2, limited in [1, 8]
# 指定后台压缩线程数量, 默认为1, 范围为[1, 8]
max-background-compactions : 2

# max-cache-files default is 5000
# DB可以使用的打开文件的数量, 默认为5000
max-cache-files : 5000

# max_bytes_for_level_multiplier: default is 10, you can change it to 5
# pika引擎中层级因子, 用于控制每个层级与上一层级总容量的倍数关系, 默认为10(倍), 允许调整为5(倍) 
max-bytes-for-level-multiplier : 10
# BlockBasedTable block_size, default 4k
# block-size: 4096
# block LRU cache, default 8M, 0 to disable
# block-cache: 8388608
# whether the block cache is shared among the RocksDB instances, default is per CF
# share-block-cache: no
# whether or not index and filter blocks is stored in block cache
# cache-index-and-filter-blocks: no
# when set to yes, bloomfilter of the last level will not be built
# optimize-filters-for-hits: no
# https://github.com/facebook/rocksdb/wiki/Leveled-Compaction#levels-target-size
# level-compaction-dynamic-level-bytes: no

启动命令

pika内部会自动计算端口,根据访问端口直接加2000,
在vm1启动pika-1, 启动时添加host解析到pika2

docker run -p 6379:6379 -p 8379:8379 \
--name pika-1 --hostname pika-1 \
--add-host pika-2:192.168.1.50 \
-v /opt/volumns/pika/conf:/pika/output/conf:ro \
-v /opt/volumns/pika/db:/pika/output/db \
-v /opt/volumns/pika/log:/pika/output/log \
-v /etc/localtime:/etc/localtime \
--restart=always --privileged=true \
-d pikadb/pika:v3.2.2 \
./bin/pika -c conf/pika.conf

在vm2启动pika-2, 启动时添加host解析到pika1

docker run -p 6379:6379 -p 8379:8379 \
--name pika-2 --hostname pika-2 \
--add-host pika-1:192.168.1.49 \
-v /opt/volumns/pika/conf:/pika/output/conf:ro \
-v /opt/volumns/pika/db:/pika/output/db \
-v /opt/volumns/pika/log:/pika/output/log \
-v /etc/localtime:/etc/localtime \
--restart=always --privileged=true \
-d pikadb/pika:v3.2.2 \
./bin/pika -c conf/pika.conf

在vm1上启动redis-cli 查看info

redis-cli info
# Server
pika_version:3.2.2
pika_git_sha:2c32458388a0541ead55fa3c42deefd55f9aca88
pika_build_compile_date: Aug 16 2019
os:Linux 3.10.0-957.5.1.el7.x86_64 x86_64
arch_bits:64
process_id:1
tcp_port:6379
thread_num:1
sync_thread_num:6
uptime_in_seconds:109
uptime_in_days:1
config_file:conf/pika.conf
server_id:1

# Data
db_size:942253
db_size_human:0M
log_size:4269880
log_size_human:4M
compression:snappy
used_memory:8000
used_memory_human:0M
db_memtable_usage:8000
db_tablereader_usage:0
db_fatal:0
db_fatal_msg:NULL

# Clients
connected_clients:1

# Stats
total_connections_received:3
instantaneous_ops_per_sec:0
total_commands_processed:1
is_bgsaving:No
is_scaning_keyspace:No
is_compact:No
compact_cron:
compact_interval:

# CPU
used_cpu_sys:0.16
used_cpu_user:0.07
used_cpu_sys_children:0.01
used_cpu_user_children:0.00

# Replication(MASTER)
role:master
connected_slaves:1
slave0:ip=172.17.0.2,port=6379,conn_fd=53,lag=(db0:0)
db0 binlog_offset=0 69,safety_purge=none

# Keyspace
# Time:1970-01-01 08:00:00
db0 Strings_keys=0, expires=0, invaild_keys=0
db0 Hashes_keys=0, expires=0, invaild_keys=0
db0 Lists_keys=0, expires=0, invaild_keys=0
db0 Zsets_keys=0, expires=0, invaild_keys=0
db0 Sets_keys=0, expires=0, invaild_keys=0

在vm2上启动redis-cli查看info

redis-cli info
# Server
pika_version:3.2.2
pika_git_sha:2c32458388a0541ead55fa3c42deefd55f9aca88
pika_build_compile_date: Aug 16 2019
os:Linux 3.10.0-957.5.1.el7.x86_64 x86_64
arch_bits:64
process_id:1
tcp_port:6379
thread_num:1
sync_thread_num:6
uptime_in_seconds:2
uptime_in_days:1
config_file:conf/pika.conf
server_id:1

# Data
db_size:962597
db_size_human:0M
log_size:4314889
log_size_human:4M
compression:snappy
used_memory:8042
used_memory_human:0M
db_memtable_usage:8000
db_tablereader_usage:42
db_fatal:0
db_fatal_msg:NULL

# Clients
connected_clients:1

# Stats
total_connections_received:1
instantaneous_ops_per_sec:0
total_commands_processed:1
is_bgsaving:No
is_scaning_keyspace:No
is_compact:No
compact_cron:
compact_interval:

# CPU
used_cpu_sys:0.07
used_cpu_user:0.03
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Replication(SLAVE)
role:slave
master_host:pika-1
master_port:6379
master_link_status:up
slave_priority:100
slave_read_only:1
db0 binlog_offset=0 69,safety_purge=none

# Keyspace
# Time:1970-01-01 08:00:00
db0 Strings_keys=0, expires=0, invaild_keys=0
db0 Hashes_keys=0, expires=0, invaild_keys=0
db0 Lists_keys=0, expires=0, invaild_keys=0
db0 Zsets_keys=0, expires=0, invaild_keys=0
db0 Sets_keys=0, expires=0, invaild_keys=0

观察Replication部分两边连接成功。

检查pika-1日志

docker logs pika-1

显示

I0907 07:22:46.754061     1 pika.cc:187] Server at: conf/pika.conf
I0907 07:22:46.756409     1 pika_server.cc:168] Using Networker Interface: eth0
I0907 07:22:46.757133     1 pika_server.cc:211] host: 172.17.0.2 port: 6379
I0907 07:22:46.757198     1 pika_server.cc:88] Worker queue limit is 20100
I0907 07:22:46.804188     1 pika_partition.cc:92] db0 DB Success
I0907 07:22:46.804298     1 pika_binlog.cc:106] Binlog: Find the exist file.
I0907 07:22:47.815882     1 pika_server.cc:264] Pika Server going to start
I0907 07:23:19.918262    20 pika_server.cc:732] Add New Slave, 172.17.0.2:6379
I0907 07:23:20.021631    21 pika_repl_server_conn.cc:96] Receive Trysync, Slave ip: 172.17.0.2, Slave port:6379, Partition: db0, filenum: 0, pro_offset: 69
I0907 07:23:20.021733    21 pika_rm.cc:163] Add Slave Node, partition: (db0:0), ip_port: 172.17.0.2:6379
I0907 07:23:20.021745    21 pika_repl_server_conn.cc:137] Partition: db0 TrySync Success, Session: 0

检查pika-2日志

docker logs pika-2

显示

I0907 07:23:18.840204     1 pika.cc:187] Server at: conf/pika.conf
I0907 07:23:18.842579     1 pika_server.cc:168] Using Networker Interface: eth0
I0907 07:23:18.843348     1 pika_server.cc:211] host: 172.17.0.2 port: 6379
I0907 07:23:18.843382     1 pika_server.cc:88] Worker queue limit is 20100
I0907 07:23:18.901234     1 pika_partition.cc:92] db0 DB Success
I0907 07:23:18.901347     1 pika_binlog.cc:106] Binlog: Find the exist file.
I0907 07:23:19.911180     1 pika_server.cc:264] Pika Server going to start
I0907 07:23:19.917335    42 pika_repl_client.cc:145] Try Send Meta Sync Request to Master (pika-1:6379)
I0907 07:23:19.920464     7 pika_server.cc:543] Mark try connect finish
I0907 07:23:19.920529     7 pika_repl_client_conn.cc:129] Finish to handle meta sync response
I0907 07:23:20.024412     8 pika_repl_client_conn.cc:210] Partition: db0 TrySync Ok

主从连接成功

测试

在vm1上执行

redis-cli set test test

在vm2上执行

redis-cli get test

数据同步成功

Docker Stack 搭建主从

创建swarm overlay网络

创建swarm overlay网络(点击)

启动firewalld

启动slave依赖于firewalld,vm1,vm2上启动firewall

systemctl start firewalld

创建目录

docker stack无法自动创建目录,需要先创建挂载目录,主从配置同上。

mkdir -p /opt/volumns/pika/conf
mkdir -p /opt/volumns/pika/db
mkdir -p /opt/volumns/pika/log

编写Docker Stack配置

vi /opt/pika.yaml 

添加如下配置

version: "3"
services:
  master:
    image: pikadb/pika:v3.2.2
    hostname: master
    command: ./bin/pika -c conf/pika.conf
    networks:
      - overlay
    ports:
      - 6379:6379
      - 8379:8379
    extra_hosts:
      - pika-1:192.168.1.49
      - pika-2:192.168.1.50
    volumes:
      - /opt/volumns/pika/conf:/pika/output/conf:ro
      - /opt/volumns/pika/db:/pika/output/db
      - /opt/volumns/pika/log:/pika/output/log
      - /etc/localtime:/etc/localtime
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==vm1
  slave:
    image: pikadb/pika:v3.2.2
    hostname: slave
    command: ./bin/pika -c conf/pika.conf
    networks:
      - overlay
    ports:
      - 16379:6379
      - 18379:8379
    extra_hosts:
      - pika-1:192.168.1.49
      - pika-2:192.168.1.50
    volumes:
      - /opt/volumns/pika/conf:/pika/output/conf:ro
      - /opt/volumns/pika/db:/pika/output/db
      - /opt/volumns/pika/log:/pika/output/log
      - /etc/localtime:/etc/localtime
    deploy:
      restart_policy:
        condition: on-failure
      replicas: 1
      placement:
        constraints:
          - node.hostname==vm2
networks:
  overlay:
    driver: overlay

启动

docker stack deploy -c pika.yaml pika

查看启动结果

docker service ps pika

显示

ID            NAME           IMAGE               NODE  DESIRED STATE  CURRENT STATE           ERROR  PORTS
mxrjzue5h780  pika_master.1  pikadb/pika:v3.2.2  vm1   Running        Running 10 seconds ago         
ktj93m11sp0y  pika_slave.1   pikadb/pika:v3.2.2  vm2   Running        Running 11 seconds ago         

双主搭建

截止2019年9月7日,双主版本目前并不支持3.2.2版本,支持范围为2.3.0 ~ 3.0.11

你可能感兴趣的:(Docker)