Ansible部署Redis集群
Ansible部署Redis集群
实验环境
Ansible:192.168.1.1(控制端)
Redis4:192.168.1.4(被控端)
生产环境至少需要三台Ansible服务端
实验目的
使用Ansible执行Playbook的方式安装部署Redis集群
实验步骤
第一步
部署安装Ansible,我这里采用之前已经安装好的Ansible,其实也简单,就是yum安装
只需要设置对被控端的免密登录以及配置文件中的被控端即可
免密登录简单,自己做即可,直接来调整Ansible的hosts文件
[root@ansible ~]# vim /etc/ansible/hosts
添加
[redis]
192.168.1.4
192.168.1.5
192.168.1.6
检测主机之间连通性,全部显示SUCCESS即可
[root@ansible ~]# ansible redis -m ping
192.168.1.4 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
第二步
编写Playbook剧本,需要将redis包拖到Ansible控制端(192.168.1.1)
[root@ansible ~]# vim redis.yml
- hosts: redis
remote_user: root
tasks:
- unarchive: src=/root/redis-4.0.6.tar.gz dest=/usr/src
- copy: src=/usr/src/redis-4.0.6 dest=/usr/local/ remote_src=yes
- shell: mv /usr/local/redis-4.0.6 /usr/local/redis
- shell: make && make install
args:
chdir: /usr/local/redis
- file: path=/usr/local/cluster state=directory
- file: path=/usr/local/cluster/7000 state=directory
- file: path=/usr/local/cluster/7001 state=directory
- file: path=/usr/local/cluster/7002 state=directory
- file: path=/usr/local/cluster/7003 state=directory
- file: path=/usr/local/cluster/7004 state=directory
- file: path=/usr/local/cluster/7005 state=directory
- copy: src=/usr/local/redis/redis.conf dest=/usr/local/cluster/7000 remote_src=yes
- replace: path=/usr/local/cluster/7000/redis.conf regexp='port 6379' replace='port 7000'
- replace: path=/usr/local/cluster/7000/redis.conf regexp='daemonize no' replace='daemonize yes'
- replace: path=/usr/local/cluster/7000/redis.conf regexp='bind 127.0.0.1' replace='bind 192.168.1.4'
- replace: path=/usr/local/cluster/7000/redis.conf regexp='appendonly no' replace='appendonly yes'
- replace: path=/usr/local/cluster/7000/redis.conf regexp='appendfilename "appendonly.aof"' replace='appendfilename "appendonly-7000.aof"'
- replace: path=/usr/local/cluster/7000/redis.conf regexp='# cluster-enabled yes' replace='cluster-enabled yes'
- replace: path=/usr/local/cluster/7000/redis.conf regexp='# cluster-config-file nodes-6379.conf' replace='cluster-config-file nodes-7000.conf'
- replace: path=/usr/local/cluster/7000/redis.conf regexp='# cluster-node-timeout 15000' replace='cluster-node-timeout 5000'
- copy: src=/usr/local/cluster/7000/redis.conf dest=/usr/local/cluster/7001/ remote_src=yes
- copy: src=/usr/local/cluster/7000/redis.conf dest=/usr/local/cluster/7002/ remote_src=yes
- copy: src=/usr/local/cluster/7000/redis.conf dest=/usr/local/cluster/7003/ remote_src=yes
- copy: src=/usr/local/cluster/7000/redis.conf dest=/usr/local/cluster/7004/ remote_src=yes
- copy: src=/usr/local/cluster/7000/redis.conf dest=/usr/local/cluster/7005/ remote_src=yes
- replace: path=/usr/local/cluster/7001/redis.conf regexp='7000' replace='7001'
- replace: path=/usr/local/cluster/7002/redis.conf regexp='7000' replace='7002'
- replace: path=/usr/local/cluster/7003/redis.conf regexp='7000' replace='7003'
- replace: path=/usr/local/cluster/7004/redis.conf regexp='7000' replace='7004'
- replace: path=/usr/local/cluster/7005/redis.conf regexp='7000' replace='7005'
- shell: redis-server /usr/local/cluster/7000/redis.conf
- shell: redis-server /usr/local/cluster/7001/redis.conf
- shell: redis-server /usr/local/cluster/7002/redis.conf
- shell: redis-server /usr/local/cluster/7003/redis.conf
- shell: redis-server /usr/local/cluster/7004/redis.conf
- shell: redis-server /usr/local/cluster/7005/redis.conf
- yum: name=ruby
- copy: src=/root/redis-3.3.0.gem dest=/root
- shell: gem install redis-3.3.0.gem
搭建集群因为需要人工操作,所以需要在redis(192.168.1.4)主机进行操作
[root@localhost ~]# cd /usr/local/redis/src/
[root@localhost src]# ./redis-trib.rb create --replicas 1 192.168.1.4:7000 192.168.1.4:7001 192.168.1.4:7002 192.168.1.4:7003 192.168.1.4:7004 192.168.1.4:7005
# 输入yes,集群创建成功
[root@localhost src]# ./redis-trib.rb check 192.168.1.4:7000 # 检测集群状态
[root@localhost src]# ./redis-trib.rb info 192.168.1.4:7000 # 查看集群信息