目的
在Redis Codis 部署安装的文章中, 介绍了通过 fe 在 Web 上搭建 codis 的基本步骤和方法, 也介绍了 codis-admin 的相关说明, 为了更好的熟悉 codis-admin 的使用, 本文将使用 codis-admin 直接搭建 codis 集群(和 fe 进行相关的对比). 这样做的另一个目的是为实现自动化脚本部署的时候做相关的准备.
环境
和Redis Codis 部署安装中的环境一样, 包括各个服务的 IP 和端口, 以及安装方法.
机器 | 服务 | 端口 | 端口说明 | 依赖 |
192.168.163.131/132/133(Ubuntu 16.04) | Codis | 7021/7022 | server 端口: 主 / 从(三台) | GO |
11080 | proxy 管理端口(三台) | |||
18080 | dashboard 管理端口(一台) | |||
10890 | fe 管理端口(一台) | |||
10086 | sentinel(三台) | |||
192.168.163.131/132/133(Ubuntu 16.04) | zookeeper | 2181 | zk 客户端监听端口(三台) | JDK |
2888 | zk 内部通讯端口(三台) | |||
3888 | zk 选举端口(三台) |
开始
首先要保证 ZooKeeper,Dashboard,Proxy,Server,Sentine,fe 等都已经开启. 根据Redis Codis 部署安装文章的安装和配置, 进行 codis 相关组件的开启, 关闭(ZK 通过上篇文文章说明):
开启关闭进程
1)codis-server
开启(三台)
- codis-server /etc/codis/codis-server/redis7021.conf
- codis-server /etc/codis/codis-server/redis7022.conf
关闭(三台)
进入到 Redis 里面进行 shutdown 或则 kill.
2)dashboard
开启(一台)
codis-dashboard --ncpu=1 --config=/etc/codis/codis-dashboard/dashboard.toml --log=/usr/local/codis/logs/dashboard.log --log-level=warn &
关闭(一台)
- codis-admin --dashboard=192.168.163.131:18080 --shutdown
- 3)codis-fe
开启(一台)
- cd /etc/codis/codis-fe/
- codis-admin --dashboard-list --zookeeper=192.168.163.131:2181 | tee codis.JSON
- codis-fe --ncpu=1 --dashboard-list=/etc/codis/codis-fe/codis.JSON --listen=192.168.163.131:18090 --log=/usr/local/codis/logs/fe.log --log-level=warn --assets-dir=/usr/local/codis/bin/assets/ &
关闭(一台)
- ps -ef|grep codis-fe|grep -v grep|awk '{print $2}'|xargs kill
- 4)Proxy
开启(三台)
codis-proxy --ncpu=1 --config=/etc/codis/codis-proxy/proxy.toml --log=/usr/local/codis/logs/proxy.log --log-level=warn &
关闭(三台)
- codis-admin --proxy=192.168.163.131:11080 --shutdown
- codis-admin --proxy=192.168.163.132:11080 --shutdown
- codis-admin --proxy=192.168.163.133:11080 --shutdown
codis-admin 应用
1)Proxy
1 添加 Proxy 到 Dashboard, 并且设置成 Online(3 个).
Proxy 进程起来之后一直处于 proxy waiting online ... 状态, 不会接受请求 (该状态不会注册到 zk 的 codis3 和 jodis 里). 先加入到 Dashboard, 注册到 zk(jodis,codis3) 并设置成 online:
- codis-admin --dashboard=192.168.163.131:18080 --create-proxy --addr=192.168.163.131:11080
- codis-admin --dashboard=192.168.163.132:18080 --create-proxy --addr=192.168.163.131:11080
- codis-admin --dashboard=192.168.163.133:18080 --create-proxy --addr=192.168.163.131:11080
等于 codis-fe 的操作:
执行完毕之后, API 会调用:
- [WARN] [0xc4202bf440] API call /API/topom/proxy/create/4c0ca749efb5aad2b20b8d84b1bb6905/192.168.163.133:11080 from 192.168.163.131:44504 [192.168.163.1]
- [WARN] create proxy-[cfa02d4002da74e6b1f3b51f1416aa1d]
可以在日志里看到 proxy is working ...
2: 从 Dashboard 上删除掉 Proxy.
codis-admin --dashboard=192.168.163.131:18080 --remove-proxy --addr=192.168.163.133:11080
等于 codis-fe 的操作:
执行完毕之后, API 会调用:
- [WARN] [0xc4202bf440] API call /API/topom/proxy/remove/4c0ca749efb5aad2b20b8d84b1bb6905/cfa02d4002da74e6b1f3b51f1416aa1d/0 from 192.168.163.132:42134 []
- [WARN] remove proxy-[cfa02d4002da74e6b1f3b51f1416aa1d]
需要注意的是, 执行该操作之后, 不仅从 DashBoard 退出, 而且也会把进程退出掉, 后续要开启才能加入.
3: 重新同步所有的 slots, 当 Proxy 的 slots 出现异常的时候执行.
codis-admin --dashboard=192.168.163.131:18080 --reinit-proxy --addr=192.168.163.131:11080
等于 codis-fe 的操作:
执行完毕之后, API 会调用:
- [WARN] [0xc4202bf440] API call /API/topom/proxy/reinit/4c0ca749efb5aad2b20b8d84b1bb6905/4f07158ae347d67b1af825fd8e84b2f9 from 192.168.163.131:44612 [192.168.163.1]
- [WARN] proxy-[4f07158ae347d67b1af825fd8e84b2f9] reinit:
- 2)Group
1 添加 Group 到 Dashboard(3 个)
- codis-admin --dashboard=192.168.163.131:18080 --create-group --gid=1
- codis-admin --dashboard=192.168.163.131:18080 --create-group --gid=2
- codis-admin --dashboard=192.168.163.131:18080 --create-group --gid=3
等于 codis-fe 的操作:
执行完毕之后, API 会调用:
- [WARN] [0xc4202bf440] API call /API/topom/group/create/4c0ca749efb5aad2b20b8d84b1bb6905/1 from 192.168.163.132:42138 []
- [WARN] create group-[1]:
- {
- "id": 1,
- "servers": [],
- "promoting": {},
- "out_of_sync": false
- }
- ...
2 删除 Group
codis-admin --dashboard=192.168.163.131:18080 --remove-group --gid=4
等于 codis-fe 的操作:
执行完毕之后, API 会调用:
- [WARN] [0xc4202bf440] API call /API/topom/group/remove/4c0ca749efb5aad2b20b8d84b1bb6905/4 from 192.168.163.132:42146 []
- [WARN] remove group-[4]:
- {
- "id": 4,
- "servers": [],
- "promoting": {},
- "out_of_sync": false
- }
3 添加 Server 到 Group
- #group1
- codis-admin --dashboard=192.168.163.131:18080 --group-add --gid=1 --addr=192.168.163.131:7021
- codis-admin --dashboard=192.168.163.131:18080 --group-add --gid=1 --addr=192.168.163.132:7022
- #group2
- ...
- #group3
- ...
等于 codis-fe 的操作:
执行完之后, API 会调用:
- [WARN] [0xc4202bf440] API call /API/topom/group/add/4c0ca749efb5aad2b20b8d84b1bb6905/1/192.168.163.132:7022 from 192.168.163.132:42156 []
- [WARN] update group-[1]:
- {
- "id": 1,
- "servers": [
- {
- "server": "192.168.163.131:7021",
- "datacenter": "",
- "action": {},
- "replica_group": false
- },
- {
- "server": "192.168.163.132:7022",
- "datacenter": "",
- "action": {},
- "replica_group": false
- }
- ],
- "promoting": {},
- "out_of_sync": false
- }
- ...
3 移除 Group 中的 Server
codis-admin --dashboard=192.168.163.131:18080 --group-del --gid=1 --addr=192.168.163.132:7022
等于 codis-fe 的操作:
执行完后, 调用的 API:
- [WARN] [0xc4202bf440] API call /API/topom/group/del/4c0ca749efb5aad2b20b8d84b1bb6905/1/192.168.163.132:7022 from 192.168.163.132:42158 []
- [WARN] update group-[1]:
- {
- "id": 1,
- "servers": [
- {
- "server": "192.168.163.131:7021",
- "datacenter": "",
- "action": {},
- "replica_group": false
- }
- ],
- "promoting": {},
- "out_of_sync": false
- }
4 给 Group 添加 Server
需要注意, 做高可用的话每个 Group 里需要至少 2 个 Server, 一主一从. 默认第 2 个 Server 是从.
codis-admin --dashboard=192.168.163.131:18080 --sync-action --create --addr=192.168.163.132:7022 -- 第 2 个 server
等于 codis-fe 的操作:
执行完后, 调用的 API:
- [WARN] [0xc4202bf440] API call /API/topom/group/action/create/4c0ca749efb5aad2b20b8d84b1bb6905/192.168.163.132:7022 from 192.168.163.132:42172 []
- [WARN] update group-[1]
- ...
- [WARN] server-[192.168.163.132:7022] action prepare
- [WARN] update group-[1]
- ...
- [WARN] sync-[192.168.163.132:7022] process action
- [WARN] server-[192.168.163.132:7022] action failed = false
- [WARN] update group-[1]:
- {
- "id": 1,
- "servers": [
- {
- "server": "192.168.163.131:7021",
- "datacenter": "",
- "action": {},
- "replica_group": false
- },
- {
- "server": "192.168.163.132:7022",
- "datacenter": "",
- "action": {
- "state": "synced"
- },
- "replica_group": false
- }
- ],
- "promoting": {},
- "out_of_sync": false
- }
5 提升 Group 的一个 Slave 为 Master
codis-admin --dashboard=192.168.163.131:18080 --promote-server --gid=3 --addr=192.168.163.133:7021
等于 codis-fe 的操作:
执行完后, 调用的 API:
- [WARN] [0xc4202bf440] API call /API/topom/group/promote/4c0ca749efb5aad2b20b8d84b1bb6905/3/192.168.163.133:7021 from 192.168.163.132:42190 []
- [WARN] group-[3] will promote index = 1
- [WARN] update group-[3]
- "state": "preparing"
- ...
- [WARN] group-[3] resync to prepared
- [WARN] update group-[3]:
- "state": "prepared"
- ...
- [WARN] update group-[3]:
- "state": "finished"
- ...
- [WARN] group-[3] resync to finished
- [WARN] update group-[3]:
- {
- "id": 3,
- "servers": [
- {
- "server": "192.168.163.133:7021",
- "datacenter": "",
- "action": {},
- "replica_group": false
- },
- {
- "server": "192.168.163.131:7022",
- "datacenter": "",
- "action": {},
- "replica_group": false
- }
- ],
- "promoting": {},
- "out_of_sync": false
- }
这里还要注意, Slave 提升成 Master 之后, 老 Master 还需要执行 slaveof 才能进行新的复制.
- codis-admin --dashboard=192.168.163.131:18080 --sync-action --create --addr=192.168.163.131:7022
- 3)Slots
注意: 此时, 所有的 slots 都处在 offline 状态, 需要进行分配 slots. 否则客户端会报错:
ERR handle request, slot is not ready, may be offline
1 分配 slots, 离开 offline 状态. 迁移某一段范围的 slots 到指定的 group, 分配所有的 slots 到 Group 中.
- codis-admin --dashboard=192.168.163.131:18080 --slot-action --create-range --beg=1 --end=100 --gid=1
- codis-admin --dashboard=192.168.163.131:18080 --slot-action --create-range --beg=101 --end=1000 --gid=2
- codis-admin --dashboard=192.168.163.131:18080 --slot-action --create-range --beg=1001 --end=1023 --gid=3
等于 codis-fe 的操作:
执行完后, 调用的 API:
- [WARN] [0xc4202bf440] API call /API/topom/slots/action/create-range/4c0ca749efb5aad2b20b8d84b1bb6905/0/0/1 from 192.168.163.131:45936 [192.168.163.1]
- [WARN] update slot-[0]:
- "state": "pending",
- ...
- [WARN] slot-[0] action prepare:
- "state": "pending",
- [WARN] update slot-[0]:
- "state": "preparing",
- ...
- [WARN] slot-[0] resync to prepared
- "state": "prepared",
- ...
- [WARN] slot-[0] resync to migrating
- "state": "migrating",
- ...
- [WARN] slot-[0] process action
- [WARN] slot-[0] action complete
- "state": "migrating",
- ...
- [WARN] slot-[0] resync to finished
- "state": "finished",
- ...
注意: 正在迁移的 slots 中的 key 被访问, 该 key 会被立马迁移到新的 group, 才能被访问. 迁移好之后的 slots 通过 --slots-status 查看, 发生了变化:
- {
- "id": 0,
"backend_addr": "192.168.163.131:7021", -- 迁移后新增
"backend_addr_group_id": 1, -- 迁移后新增
- "forward_method": 1
- },
2 迁移 slots, 迁移指定数量的 slots 从一个 Group 到另一个 Group.
从 Group2 中迁移 100 个 slots 到 Group3 中:
codis-admin --dashboard=192.168.163.131:18080 --slot-action --create-some --gid-from=2 --gid-to=3 --num-slots=100
等于 codis-fe 的操作:
执行完后, 调用的 API:
- [WARN] [0xc4202bf440] API call /API/topom/slots/action/create-some/4c0ca749efb5aad2b20b8d84b1bb6905/2/3/100 from 192.168.163.132:42498 []
- "state": "pending",
- ...
- [WARN] slot-[104] action prepare:
- "state": "pending",
- ...
- "state": "preparing",
- [WARN] slot-[104] resync to prepared
- "state": "prepared",
- [WARN] slot-[104] resync to migrating
- "state": "migrating",
- [WARN] slot-[104] resync to finished
- "state": "finished",
- ...
注意, 勾选 Action Status 可以查看迁移的进度.
3 停止, 开启 Slots 迁移.
停止:
codis-admin --dashboard=192.168.163.131:18080 --slot-action --disabled=1
等于 codis-fe 的操作:
执行完后, 调用的 API:
- [WARN] [0xc4202bf440] API call /API/topom/slots/action/disabled/4c0ca749efb5aad2b20b8d84b1bb6905/1 from 192.168.163.132:42524 []
- [WARN] set action disabled = true
开启:
codis-admin --dashboard=192.168.163.131:18080 --slot-action --disabled=0
等于 codis-fe 的操作:
执行完后, 调用的 API:
- [WARN] [0xc4202bf440] API call /API/topom/slots/action/disabled/4c0ca749efb5aad2b20b8d84b1bb6905/0 from 192.168.163.132:42526 []
- [WARN] set action disabled = false
4 平均分配 slots, 各个 Group 平均分配 1024 个 slots.
codis-admin --dashboard=192.168.163.131:18080 --rebalance --confirm
--confirm 表示执行 rebalance, 不加表示查看(不执行)
等于 codis-fe 的操作:
执行完后, 调用的 API:
- [WARN] [0xc4202bf440] API call /API/topom/slots/rebalance/4c0ca749efb5aad2b20b8d84b1bb6905/1 from 192.168.163.132:42534 []
- ...
- [WARN] slot-[882] resync to finished
- ...
5 slots 迁移的时间间隔
codis-admin --dashboard=192.168.163.131:18080 --slot-action --interval=1000
等于 codis-fe 的操作:
执行完后, 调用的 API:
- [WARN] [0xc4202bf440] API call /API/topom/slots/action/interval/4c0ca749efb5aad2b20b8d84b1bb6905/1000 from 192.168.163.132:42540 []
- [WARN] set action interval = 1000
4)Sentinel 高可用
1 添加 Sentinel(3 个)
- codis-admin --dashboard=192.168.163.131:18080 --sentinel-add --addr=192.168.163.131:10086
- codis-admin --dashboard=192.168.163.131:18080 --sentinel-add --addr=192.168.163.132:10086
- codis-admin --dashboard=192.168.163.131:18080 --sentinel-add --addr=192.168.163.133:10086
等于 codis-fe 的操作:
执行完后, 调用的 API:
- [WARN] [0xc4202bf440] API call /API/topom/sentinels/add/4c0ca749efb5aad2b20b8d84b1bb6905/192.168.163.131:10086 from 192.168.163.132:42544 []
- [WARN] update sentinel:
- {
- "servers": [
- "192.168.163.131:10086"
- ],
- "out_of_sync": true
- }
- ...
为了保证 Sentinel 服务的正常, 需要再 resync 下 sentinel:
codis-admin --dashboard=192.168.163.131:18080 --sentinel-resync
等于 codis-fe 的操作:
执行完后, 调用的 API:
- [WARN] [0xc4202bf440] API call /API/topom/sentinels/resync-all/4c0ca749efb5aad2b20b8d84b1bb6905 from 192.168.163.132:42550 []
- [WARN] rewatch sentinels = [192.168.163.131:10086 192.168.163.132:10086 192.168.163.133:10086]
2 移除 Sentinel
codis-admin --dashboard=192.168.163.131:18080 --sentinel-del --addr=192.168.163.133:10086
等于 codis-fe 的操作:
执行完后, 调用的 API:
[WARN] [0xc4202bf440] API call /API/topom/sentinels/del/4c0ca749efb5aad2b20b8d84b1bb6905/192.168.163.133:10086/0 from 192.168.163.132:42598 []
到此, 通过 codis-admin 已经完成了 Codis 的搭建.
总结
通过本文的说明, 进一步理顺了 codis-admin 的各个命令. 可以不依赖 fe 的 Web 界面进行 Codis 集群的搭建, 可以为自动化的一些脚本做好更好的支持工作.
来源: https://www.cnblogs.com/zhoujinyi/p/9950105.html