上次给大家介绍了ansible结合docker安装smokeping(地址http://dl528888.blog.51cto.com/2382721/1538444),现在在介绍一下ansible结合docker安装redis主从(redis版本是2.6.17)。  

目前ansible使用的人比较多,docker也最近比较流行,而且做测试的话,ansible+docker是个很好的组合。本文主要是介绍如何使用ansible的playbook安装redis主从,可以自动的做好主从,这样节省安装时间、减少问题产生。

docker版本

09:49:09 # docker versionClient version: 0.11.1Client API version: 1.11Go version (client): go1.2.1Git commit (client): fb99f99/0.11.1Server version: 0.11.1Server API version: 1.11Git commit (server): fb99f99/0.11.1Go version (server): go1.2.1Last stable version: 1.1.2, please update docker

ansible版本

09:51:55 # ansible --versionansible 1.4.3

1、查看已有的docker镜像

09:39:26 # docker p_w_picpathsREPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZEubuntu              3.0                 6cee55276528        9 weeks ago         369.8 MBcentos5             3.0                 e08d23b09189        9 weeks ago         840.9 MBcentos6             3.0                 e94a3b24a19b        9 weeks ago         415.9 MB

2、查看当前运行的docker容器

09:39:47 # docker ps -aCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                   NAMES846efb9e4d7a        ubuntu:3.0          /usr/sbin/sshd -D   2 weeks ago         Up 6 days           0.0.0.0:49167->22/tcp   ubuntu-test1        b9a9e6f2caed        centos6:3.0         /usr/sbin/sshd -D   3 weeks ago         Up 6 days           0.0.0.0:49166->22/tcp   zabbix-server       978fff134b18        centos6:3.0         /usr/sbin/sshd -D   5 weeks ago         Up 6 days           0.0.0.0:49165->22/tcp   centos6-test5       root@ip-10-10-10-10:/etc/ansible/roles/redis_install/templates

3、加载测试容器

由于是做redis主从,所以需要2个容器

09:39:50 # time  docker inspect $(docker run -d -p 22  --name="redis-master" centos6:3.0 /usr/sbin/sshd -D)|grep -i address|awk -F '"' '{print $4}'172.17.0.5real	0m6.369suser	0m0.036ssys	0m0.049sroot@ip-10-10-10-10:/etc/ansible/roles/redis_install/templates09:40:25 # time  docker inspect $(docker run -d -p 22  --name="redis-slave" centos6:3.0 /usr/sbin/sshd -D)|grep -i address|awk -F '"' '{print $4}'172.17.0.6real	0m12.567suser	0m0.047ssys	0m0.060s

然后把新容器的ip加入到ansible的hosts里

root@ip-10-10-10-10:/etc/ansible/roles/redis_install/templates09:40:46 # echo "172.17.0.5">>/etc/ansible/hostsroot@ip-10-10-10-10:/etc/ansible/roles/redis_install/templates09:40:58 # echo "172.17.0.6">>/etc/ansible/hostsroot@ip-10-10-10-10:/etc/ansible/roles/redis_install/templates

4、ansible安装redis的信息

09:52:11 # cat /etc/ansible/roles/redis_install/vars/main.yml redis_dir: /dataredis_version: 2.6.17redis_port: 6379redis_conf_dir: /data/redis-2.6.17/confredis_db_dir: /data/redis-2.6.17/dbredis_log_dir: /data/redis-2.6.17/log

5、下面是redis的playbook结构

09:55:12 # tree /etc/ansible/roles/redis_install /etc/ansible/roles/redis_delete/etc/ansible/roles/redis_install├── files│   └── redis-2.6.17.tar.gz├── handlers├── meta│   └── main.yml├── tasks│   ├── copy.yml│   ├── delete.yml│   ├── install.yml│   └── main.yml├── templates│   └── redis.conf└── vars    └── main.yml/etc/ansible/roles/redis_delete├── files├── handlers├── meta│   └── main.yml├── tasks│   ├── delete.yml│   └── main.yml├── templates│   └── delete_redis.sh└── vars    └── main.yml12 directories, 13 files

6、playbook安装redis的内容是

09:57:09 # cat redis_install.yml ---- hosts: "`host`"  remote_user: "`user`"  gather_facts: True  roles:    - common    - redis_install

7、playbook删除redis的内容是

09:57:31 # cat redis_delete.yml ---- hosts: "`host`"  remote_user: "`user`"  gather_facts: True  roles:    - redis_delete

下面是安装redis主从

8、安装redis master

09:43:11 # time ansible-playbook redis_install.yml --extra-vars "host=172.17.0.5 user=root" -kSSH password: PLAY [172.17.0.5] ************************************************************* GATHERING FACTS *************************************************************** ok: [172.17.0.5]TASK: [common | Install initializtion require software] *********************** changed: [172.17.0.5]TASK: [redis_install | Copy Redis Software To Redhat Client] ****************** ok: [172.17.0.5]TASK: [redis_install | Create Redis Install Dir] ****************************** ok: [172.17.0.5]TASK: [redis_install | Uncompression Redis Software To Redhat Client] ********* changed: [172.17.0.5]TASK: [redis_install | Copy Redis Config To Redhat Client] ******************** changed: [172.17.0.5]TASK: [redis_install | Create Soft Link In Redhat Client] ********************* changed: [172.17.0.5] => (item=redis-cli)changed: [172.17.0.5] => (item=redis-server)TASK: [redis_install | Check Boot Start In Redhat Client] ********************* failed: [172.17.0.5] => {"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.007160", "end": "2014-08-13 09:45:18.660617", "item": "", "rc": 1, "start": "2014-08-13 09:45:18.653457"}stdout: 0...ignoringTASK: [redis_install | Add Boot Start In Redhat Client] *********************** changed: [172.17.0.5]TASK: [redis_install | Start Redis Service In Redhat Client] ****************** changed: [172.17.0.5]TASK: [redis_install | Delete Redis compression Software In Redhat Client] **** changed: [172.17.0.5]PLAY RECAP ******************************************************************** 172.17.0.5                 : ok=11   changed=8    unreachable=0    failed=0   real	0m12.757suser	0m3.453ssys	0m0.497s

9、查看redis master安装情况

09:48:39 # ssh 172.17.0.5root@172.17.0.5's password: Last login: Wed Aug 13 09:45:28 2014 from 172.17.42.1root@06762530db8c:~09:48:43 # ps -ef|grep redisroot       294     1  0 09:45 ?        00:00:00 /usr/bin/redis-server /data/redis-2.6.17/conf/redis_6379.confroot       343   329  0 09:48 pts/0    00:00:00 grep redisroot@06762530db8c:~09:48:46 # cat /etc/rc.local #!/bin/sh## This script will be executed *after* all the other init scripts.# You can put your own initialization stuff in here if you don't# want to do the full Sys V style init stuff.touch /var/lock/subsys/local/usr/bin/redis-server /data/redis-2.6.17/conf/redis_6379.confroot@06762530db8c:~09:48:52 # redis-cli -h 172.17.0.5 -p 6379redis 172.17.0.5:6379> keys *(empty list or set)redis 172.17.0.5:6379>

10、安装redis slave

09:45:20 # time ansible-playbook redis_install.yml --extra-vars "host=172.17.0.6 user=root redis_master_ip=172.17.0.5 redis_master_port=6379" -kSSH password: PLAY [172.17.0.6] ************************************************************* GATHERING FACTS *************************************************************** ok: [172.17.0.6]TASK: [common | Install initializtion require software] *********************** changed: [172.17.0.6]TASK: [redis_install | Copy Redis Software To Redhat Client] ****************** changed: [172.17.0.6]TASK: [redis_install | Create Redis Install Dir] ****************************** ok: [172.17.0.6]TASK: [redis_install | Uncompression Redis Software To Redhat Client] ********* changed: [172.17.0.6]TASK: [redis_install | Copy Redis Config To Redhat Client] ******************** changed: [172.17.0.6]TASK: [redis_install | Create Soft Link In Redhat Client] ********************* changed: [172.17.0.6] => (item=redis-cli)changed: [172.17.0.6] => (item=redis-server)TASK: [redis_install | Check Boot Start In Redhat Client] ********************* failed: [172.17.0.6] => {"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.006751", "end": "2014-08-13 09:49:07.569522", "item": "", "rc": 1, "start": "2014-08-13 09:49:07.562771"}stdout: 0...ignoringTASK: [redis_install | Add Boot Start In Redhat Client] *********************** changed: [172.17.0.6]TASK: [redis_install | Start Redis Service In Redhat Client] ****************** changed: [172.17.0.6]TASK: [redis_install | Delete Redis compression Software In Redhat Client] **** changed: [172.17.0.6]PLAY RECAP ******************************************************************** 172.17.0.6                 : ok=11   changed=9    unreachable=0    failed=0   real	1m48.579suser	0m6.781ssys	0m0.654s

11、查看redis slave安装情况

09:59:24 # ssh 172.17.0.6root@172.17.0.6's password: root@dfec766fbaa7:~09:59:28 # lltotal 0root@dfec766fbaa7:~09:59:31 # ps -ef|grep redisroot       197     1  0 09:49 ?        00:00:00 /usr/bin/redis-server /data/redis-2.6.17/conf/redis_6379.confroot       227   212  0 09:59 pts/0    00:00:00 grep redis09:59:49 # grep slave /data/redis-2.6.17/conf/redis_6379.conf |egrep -v "^#"slaveof 172.17.0.5 6379slave-serve-stale-data yesslave-read-only yesslave-priority 100client-output-buffer-limit slave 256mb 64mb 60root@dfec766fbaa7:~10:00:09 # redis-cli -h 172.17.0.6 -p 6379redis 172.17.0.6:6379> keys *(empty list or set)redis 172.17.0.6:6379> info# Serverredis_version:2.6.17redis_git_sha1:00000000redis_git_dirty:0redis_mode:standaloneos:Linux 2.6.32-431.17.1.el6.x86_64 x86_64arch_bits:64multiplexing_api:epollgcc_version:4.4.7process_id:197run_id:81207df2727abe534ae606afc0cb80016f75a422tcp_port:6379uptime_in_seconds:685uptime_in_days:0hz:10lru_clock:280339# Clientsconnected_clients:2client_longest_output_list:0client_biggest_input_buf:0blocked_clients:0# Memoryused_memory:878072used_memory_human:857.49Kused_memory_rss:7569408used_memory_peak:877280used_memory_peak_human:856.72Kused_memory_lua:31744mem_fragmentation_ratio:8.62mem_allocator:jemalloc-3.2.0# Persistenceloading:0rdb_changes_since_last_save:0rdb_bgsave_in_progress:0rdb_last_save_time:1407894548rdb_last_bgsave_status:okrdb_last_bgsave_time_sec:-1rdb_current_bgsave_time_sec:-1aof_enabled:0aof_rewrite_in_progress:0aof_rewrite_scheduled:0aof_last_rewrite_time_sec:-1aof_current_rewrite_time_sec:-1aof_last_bgrewrite_status:ok# Statstotal_connections_received:1total_commands_processed:70instantaneous_ops_per_sec:0rejected_connections:0expired_keys:0evicted_keys:0keyspace_hits:0keyspace_misses:0pubsub_channels:0pubsub_patterns:0latest_fork_usec:0# Replicationrole:slavemaster_host:172.17.0.5master_port:6379master_link_status:upmaster_last_io_seconds_ago:1master_sync_in_progress:0slave_priority:100slave_read_only:1connected_slaves:0# CPUused_cpu_sys:0.24used_cpu_user:0.10used_cpu_sys_children:0.00used_cpu_user_children:0.00# Keyspace

可以从上面的Replication里看到主从做好了,信息如下图

然后去redis master里查看主从信息

12、redis 主从测试

A.redis master插入key

redis 172.17.0.5:6379> set master 1OKredis 172.17.0.5:6379> set slave 0OKredis 172.17.0.5:6379> get master"1"redis 172.17.0.5:6379> get slave"0"

B.redis slave读取key

redis 172.17.0.6:6379> keys *1) "master"2) "slave"redis 172.17.0.6:6379> get master"1"redis 172.17.0.6:6379> get slave"0"

可以看到主从建立完成

下面是redis slave的日志情况

10:08:04 # cat /data/redis-2.6.17/log/redis_6379.log                 _._                                                             _.-``__ ''-._                                                   _.-``    `.  `_.  ''-._           Redis 2.6.17 (00000000/0) 64 bit  .-`` .-```.  ```\/    _.,_ ''-._                                    (    '      ,       .-`  | `,    )     Running in stand alone mode |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379 |    `-._   `._    /     _.-'    |     PID: 197  `-._    `-._  `-./  _.-'    _.-'                                    |`-._`-._    `-.__.-'    _.-'_.-'|                                   |    `-._`-._        _.-'_.-'    |           http://redis.io          `-._    `-._`-.__.-'_.-'    _.-'                                    |`-._`-._    `-.__.-'    _.-'_.-'|                                   |    `-._`-._        _.-'_.-'    |                                    `-._    `-._`-.__.-'_.-'    _.-'                                         `-._    `-.__.-'    _.-'                                                 `-._        _.-'                                                         `-.__.-'                                               [197] 13 Aug 09:49:08.878 # Server started, Redis version 2.6.17[197] 13 Aug 09:49:08.879 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.[197] 13 Aug 09:49:08.879 * The server is now ready to accept connections on port 6379[197] 13 Aug 09:49:09.879 * Connecting to MASTER 172.17.0.5:6379[197] 13 Aug 09:49:09.879 * MASTER <-> SLAVE sync started[197] 13 Aug 09:49:09.881 * Non blocking connect for SYNC fired the event.[197] 13 Aug 09:49:09.881 * Master replied to PING, replication can continue...[197] 13 Aug 09:49:10.035 * MASTER <-> SLAVE sync: receiving 18 bytes from master[197] 13 Aug 09:49:10.035 * MASTER <-> SLAVE sync: Loading DB in memory[197] 13 Aug 09:49:10.035 * MASTER <-> SLAVE sync: Finished with success[197] 13 Aug 10:04:56.980 * 1 changes in 900 seconds. Saving...[197] 13 Aug 10:04:56.982 * Background saving started by pid 233[233] 13 Aug 10:04:57.226 * DB saved on disk[233] 13 Aug 10:04:57.227 * RDB: 6 MB of memory used by copy-on-write[197] 13 Aug 10:04:57.293 * Background saving terminated with success

13、删除redis安装

无论redis主从,删除都是一样的命令

09:57:32 # time ansible-playbook redis_delete.yml --extra-vars "host=172.17.0.5 user=root" -kSSH password: PLAY [172.17.0.5] ************************************************************* GATHERING FACTS *************************************************************** ok: [172.17.0.5]TASK: [redis_delete | Stop Redis Service In Redhat Client] ******************** changed: [172.17.0.5]TASK: [redis_delete | Delete Redis Soft Line In Redhat Client] **************** changed: [172.17.0.5] => (item=redis-cli)changed: [172.17.0.5] => (item=redis-server)TASK: [redis_delete | Delete Redis Dir In Redhat Client] ********************** changed: [172.17.0.5]TASK: [redis_delete | Delete Boot Start In Redhat Client] ********************* changed: [172.17.0.5]PLAY RECAP ******************************************************************** 172.17.0.5                 : ok=5    changed=4    unreachable=0    failed=0   real	0m6.929suser	0m1.942ssys	0m0.250sroot@ip-10-10-10-10:/etc/ansible10:09:18 # time ansible-playbook redis_delete.yml --extra-vars "host=172.17.0.6 user=root" -kSSH password: PLAY [172.17.0.6] ************************************************************* GATHERING FACTS *************************************************************** ok: [172.17.0.6]TASK: [redis_delete | Stop Redis Service In Redhat Client] ******************** changed: [172.17.0.6]TASK: [redis_delete | Delete Redis Soft Line In Redhat Client] **************** changed: [172.17.0.6] => (item=redis-cli)changed: [172.17.0.6] => (item=redis-server)TASK: [redis_delete | Delete Redis Dir In Redhat Client] ********************** changed: [172.17.0.6]TASK: [redis_delete | Delete Boot Start In Redhat Client] ********************* changed: [172.17.0.6]PLAY RECAP ******************************************************************** 172.17.0.6                 : ok=5    changed=4    unreachable=0    failed=0   real	0m5.897suser	0m1.726ssys	0m0.258s

15、删除后测试

A.redis master上

10:09:55 # ifconfigeth0      Link encap:Ethernet  HWaddr C6:3A:D2:D9:DD:D9            inet addr:172.17.0.5  Bcast:0.0.0.0  Mask:255.255.0.0          inet6 addr: fe80::c43a:d2ff:fed9:ddd9/64 Scope:Link          UP BROADCAST RUNNING  MTU:1500  Metric:1          RX packets:13236 errors:0 dropped:0 overruns:0 frame:0          TX packets:12843 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:1000           RX bytes:26106313 (24.8 MiB)  TX bytes:906412 (885.1 KiB)lo        Link encap:Local Loopback            inet addr:127.0.0.1  Mask:255.0.0.0          inet6 addr: ::1/128 Scope:Host          UP LOOPBACK RUNNING  MTU:16436  Metric:1          RX packets:196 errors:0 dropped:0 overruns:0 frame:0          TX packets:196 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:0           RX bytes:9665 (9.4 KiB)  TX bytes:9665 (9.4 KiB)root@06762530db8c:~10:09:56 # ps -ef|grep redisroot       431   329  0 10:09 pts/0    00:00:00 grep redisroot@06762530db8c:~10:09:59 # cat /etc/rc.local #!/bin/sh## This script will be executed *after* all the other init scripts.# You can put your own initialization stuff in here if you don't# want to do the full Sys V style init stuff.touch /var/lock/subsys/localroot@06762530db8c:~10:10:01 # ll /data/total 4drwxr-xr-x 2 root root 4096 Jun  5 10:21 softroot@06762530db8c:~

B. redis slave上

10:08:09 # ifconfigeth0      Link encap:Ethernet  HWaddr BE:97:0C:B2:02:D9            inet addr:172.17.0.6  Bcast:0.0.0.0  Mask:255.255.0.0          inet6 addr: fe80::bc97:cff:feb2:2d9/64 Scope:Link          UP BROADCAST RUNNING  MTU:1500  Metric:1          RX packets:13062 errors:0 dropped:0 overruns:0 frame:0          TX packets:12704 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:1000           RX bytes:25967216 (24.7 MiB)  TX bytes:842881 (823.1 KiB)lo        Link encap:Local Loopback            inet addr:127.0.0.1  Mask:255.0.0.0          inet6 addr: ::1/128 Scope:Host          UP LOOPBACK RUNNING  MTU:16436  Metric:1          RX packets:86 errors:0 dropped:0 overruns:0 frame:0          TX packets:86 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:0           RX bytes:5288 (5.1 KiB)  TX bytes:5288 (5.1 KiB)root@dfec766fbaa7:~10:10:32 # ps -ef|grep redisroot       319   212  0 10:10 pts/0    00:00:00 grep redisroot@dfec766fbaa7:~10:10:37 # cat /etc/rc.local #!/bin/sh## This script will be executed *after* all the other init scripts.# You can put your own initialization stuff in here if you don't# want to do the full Sys V style init stuff.touch /var/lock/subsys/localroot@dfec766fbaa7:~10:10:39 # ll /data/total 4drwxr-xr-x 2 root root 4096 Jun  5 10:21 softroot@dfec766fbaa7:~

如果大家想使用我的例子,可以从github里下载(地址是https://github.com/dl528888/ansible-examples/tree/master/redis_install),然后放到/etc/ansible目录里,下面是内容