自动化是趋势,随着云平台的发展,也会逐渐的更火爆,以前手动安装软件、或者脚本安装的话,会被一键式自动化给逐步代替,上次介绍了运维自动化之ansible playbook一键化安装mysql主从(地址http://dl528888.blog.51cto.com/2382721/1617692),这次给大家介绍如何使用ansible来一键化安装redis主从。
本次还是使用docker虚拟2个测试环境,下面是环境介绍
一、各软件版本
1、docker软件版本
1
2
3
4
5
6
7
8
9
10
|
[root@docker-test3 ~]
# docker version
Client version: 1.5.0
Client API version: 1.17
Go version (client): go1.4.1
Git commit (client): a8a31ef
OS
/Arch
(client): linux
/amd64
Server version: 1.5.0
Server API version: 1.17
Go version (server): go1.4.1
Git commit (server): a8a31ef
|
2、docker存储
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
[root@docker-test3 ~]
# docker info
Containers: 2
Images: 20
Storage Driver: devicemapper
Pool Name: docker-253:1-1430610-pool
Pool Blocksize: 65.54 kB
Backing Filesystem: <unknown>
Data
file
:
/dev/loop0
Metadata
file
:
/dev/loop1
Data Space Used: 2.614 GB
Data Space Total: 107.4 GB
Metadata Space Used: 3.105 MB
Metadata Space Total: 2.147 GB
Udev Sync Supported:
false
Data loop
file
:
/var/lib/docker/devicemapper/devicemapper/data
Metadata loop
file
:
/var/lib/docker/devicemapper/devicemapper/metadata
Library Version: 1.02.82-git (2013-10-04)
Execution Driver: native-0.2
Kernel Version: 3.18.3-1.el7.elrepo.x86_64
Operating System: CentOS Linux 7 (Core)
CPUs: 16
Total Memory: 31.41 GiB
Name: docker-test3
ID: 52R3:7K2Q:EADX:MXSC:BZ4I:NMC2:WUBX:4IV7:GUBQ:NIHH:TGDL:LLRW
|
3、ansible版本
1
2
|
[root@puppet ansible-examples]
# ansible --version
ansible 1.7
|
4、docker虚拟容器信息
5、redis版本
2.6.17
6、docker容器情况
1
2
3
4
|
[root@docker-test3 ~]
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
18a34f7944c7 docker.ops-chukong.com:5000
/centos6-http
:new "
/usr/bin/supervisor
25 minutes ago Up 25 minutes redis-slave
6388c15147d9 docker.ops-chukong.com:5000
/centos6-http
:new "
/usr/bin/supervisor
26 minutes ago Up 26 minutes redis-master
|
可以看到容器启动正常
二、ansible playbook部署
1、添加路由
在ansible里添加一条到这个主机的路由,方便直接ansible对容器的ip进行部署mysql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[root@puppet ansible]
# route add -net 172.16.0.0 netmask 255.255.0.0 gw 10.10.17.3
ping
那2个容器ip
[root@puppet ansible]
# ping -c 2 172.16.1.57
PING 172.16.1.57 (172.16.1.57) 56(84) bytes of data.
64 bytes from 172.16.1.57: icmp_seq=1 ttl=63
time
=0.846 ms
64 bytes from 172.16.1.57: icmp_seq=2 ttl=63
time
=0.121 ms
--- 172.16.1.57
ping
statistics ---
2 packets transmitted, 2 received, 0% packet loss,
time
1001ms
rtt min
/avg/max/mdev
= 0.121
/0
.483
/0
.846
/0
.363 ms
You have mail
in
/var/spool/mail/root
[root@puppet ansible]
# ping -c 2 172.16.1.58
PING 172.16.1.58 (172.16.1.58) 56(84) bytes of data.
64 bytes from 172.16.1.58: icmp_seq=1 ttl=63
time
=0.672 ms
64 bytes from 172.16.1.58: icmp_seq=2 ttl=63
time
=0.111 ms
--- 172.16.1.58
ping
statistics ---
2 packets transmitted, 2 received, 0% packet loss,
time
999ms
rtt min
/avg/max/mdev
= 0.111
/0
.391
/0
.672
/0
.281 ms
|
可以看到通了,把这2个ip放到ansible的hosts里
2、添加hosts
1
2
3
4
|
[root@puppet ansible]
# tail -n3 /etc/ansible/hosts
[container_redis]
172.16.1.57:22
172.16.1.58:22
|
3、使用ansible部署redis
A、部署redis master,在redis-master里部署,下面是命令
1
|
time
ansible-playbook redis_install.yml --extra-vars
"host=172.16.1.57 user=root"
-k
|
下面是开始部署
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
[root@puppet ansible]
# time ansible-playbook redis_install.yml --extra-vars "host=172.16.1.57 user=root" -k
SSH password:
PLAY [172.16.1.57] ************************************************************
GATHERING FACTS ***************************************************************
ok: [172.16.1.57]
TASK: [common | Install initializtion require software] ***********************
changed: [172.16.1.57]
TASK: [redis_install | Copy Redis Software To Redhat Client] ******************
changed: [172.16.1.57]
TASK: [redis_install | Create Redis Install Dir] ******************************
ok: [172.16.1.57]
TASK: [redis_install | Uncompression Redis Software To Redhat Client] *********
changed: [172.16.1.57]
TASK: [redis_install | Copy Redis Config To Redhat Client] ********************
changed: [172.16.1.57]
TASK: [redis_install | Create Soft Link In Redhat Client] *********************
changed: [172.16.1.57] => (item=redis-cli)
changed: [172.16.1.57] => (item=redis-server)
TASK: [redis_install | Check Boot Start In Redhat Client] *********************
failed: [172.16.1.57] => {
"changed"
:
true
,
"cmd"
:
"grep -c '/usr/bin/redis-server /data/redis-2.6.17/conf/redis_6379.conf' /etc/rc.local"
,
"delta"
:
"0:00:00.240436"
,
"end"
:
"2015-03-27 09:25:42.110919"
,
"rc"
: 1,
"start"
:
"2015-03-27 09:25:41.870483"
}
stdout: 0
...ignoring
TASK: [redis_install | Add Boot Start In Redhat Client] ***********************
changed: [172.16.1.57]
TASK: [redis_install | Start Redis Service In Redhat Client] ******************
changed: [172.16.1.57]
TASK: [redis_install | Delete Redis compression Software In Redhat Client] ****
changed: [172.16.1.57]
PLAY RECAP ********************************************************************
172.16.1.57 : ok=11 changed=9 unreachable=0 failed=0
real 1m51.933s
user 0m7.517s
sys 0m0.505s
|
可以看到2分钟之内部署完成,最浪费时间的是yum安装基础库,想提速可以预先安装基础yum库
现在去redis-master里查看
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
[root@puppet ansible]
# ssh 172.16.1.57
root@172.16.1.57's password:
Last login: Mon Nov 17 14:10:39 2014 from 172.17.42.1
root@6388c15147d9:~
09:28:39
# netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID
/Program
name
tcp 0 0 172.16.1.57:6379 0.0.0.0:* LISTEN 412
/redis-server
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 266
/sshd
tcp 0 0 :::80 :::* LISTEN 13
/httpd
tcp 0 0 :::22 :::* LISTEN 266
/sshd
root@6388c15147d9:~
09:28:41
# ps -ef|grep redis
root 412 1 0 09:25 ? 00:00:00
/usr/bin/redis-server
/data/redis-2
.6.17
/conf/redis_6379
.conf
root 443 428 0 09:28 pts
/0
00:00:00
grep
redis
root@6388c15147d9:~
09:28:43
# redis-cli -h 172.16.1.57 -p 6379
redis 172.16.1.57:6379> keys *
(empty list or
set
)
redis 172.16.1.57:6379>
set
abc 1
OK
redis 172.16.1.57:6379>
set
test
a
OK
redis 172.16.1.57:6379> get abc
"1"
redis 172.16.1.57:6379> get
test
"a"
redis 172.16.1.57:6379>
|
可以看到redis安装正常,并且我添加了2个key,等部署slave后再查看是否同步这2个key。
B、部署从库
在redis-slave节点部署,命令是
1
|
time
ansible-playbook redis_slave_install.yml --extra-vars
"host=172.16.1.58 user=root redis_master_ip=172.16.1.57 redis_master_port=6379"
-k
|
其中redis_master_ip是redis主的ip,redis_master_port是redis主中redis的端口。
下面开始部署
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
[root@puppet ansible]
# time ansible-playbook redis_slave_install.yml --extra-vars "host=172.16.1.58 user=root redis_master_ip=172.16.1.57 redis_master_port=6379" -k
SSH password:
PLAY [172.16.1.58] ************************************************************
GATHERING FACTS ***************************************************************
ok: [172.16.1.58]
TASK: [common | Install initializtion require software] ***********************
changed: [172.16.1.58]
TASK: [redis_slave_install | Copy Redis Software To Redhat Client] ************
changed: [172.16.1.58]
TASK: [redis_slave_install | Create Redis Install Dir] ************************
ok: [172.16.1.58]
TASK: [redis_slave_install | Uncompression Redis Software To Redhat Client] ***
changed: [172.16.1.58]
TASK: [redis_slave_install | Copy Redis Config To Redhat Client] **************
changed: [172.16.1.58]
TASK: [redis_slave_install | Create Soft Link In Redhat Client] ***************
changed: [172.16.1.58] => (item=redis-cli)
changed: [172.16.1.58] => (item=redis-server)
TASK: [redis_slave_install | Check Boot Start In Redhat Client] ***************
failed: [172.16.1.58] => {
"changed"
:
true
,
"cmd"
:
"grep -c '/usr/bin/redis-server /data/redis-2.6.17/conf/redis_6379.conf' /etc/rc.local"
,
"delta"
:
"0:00:00.238492"
,
"end"
:
"2015-03-27 09:28:16.316417"
,
"rc"
: 1,
"start"
:
"2015-03-27 09:28:16.077925"
}
stdout: 0
...ignoring
TASK: [redis_slave_install | Add Boot Start In Redhat Client] *****************
changed: [172.16.1.58]
TASK: [redis_slave_install | Start Redis Service In Redhat Client] ************
changed: [172.16.1.58]
TASK: [redis_slave_install | Delete Redis compression Software In Redhat Client] ***
changed: [172.16.1.58]
PLAY RECAP ********************************************************************
172.16.1.58 : ok=11 changed=9 unreachable=0 failed=0
real 1m52.635s
user 0m7.409s
sys 0m0.495s
|
下面开始测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
[root@puppet ansible]
# ssh 172.16.1.58
root@172.16.1.58's password:
Last login: Mon Nov 17 14:10:39 2014 from 172.17.42.1
root@18a34f7944c7:~
09:29:46
# netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID
/Program
name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 266
/sshd
tcp 0 0 172.16.1.58:6379 0.0.0.0:* LISTEN 412
/redis-server
tcp 0 0 :::22 :::* LISTEN 266
/sshd
tcp 0 0 :::80 :::* LISTEN 13
/httpd
root@18a34f7944c7:~
09:29:48
# redis-cli -h 172.16.1.58 -p 6379
redis 172.16.1.58:6379>
exit
root@18a34f7944c7:~
09:30:01
# redis-cli -h 172.16.1.58 -p 6379
redis 172.16.1.58:6379> keys *
1)
"test"
2)
"abc"
redis 172.16.1.58:6379> get
test
"a"
redis 172.16.1.58:6379> get abc
"1"
redis 172.16.1.58:6379>
|
可以看到redis slave里redis安装完成,并且跟redis master里的redis做了主从,之前的2个key也都能查看。
三、ansible模块地址
地址是
1
|
https:
//github
.com
/dl528888/ansible-examples/tree/master/redis_master_slave_install_2
.6.17
|
有问题大家留言
本文转自 reinxu 51CTO博客,原文链接:http://blog.51cto.com/dl528888/1625280,如需转载请自行联系原作者