docker版本
09:49:09 # docker version
Client version: 0.11.1
Client API version: 1.11
Go version (client): go1.2.1
Git commit (client): fb99f99/0.11.1
Server version: 0.11.1
Server API version: 1.11
Git commit (server): fb99f99/0.11.1
Go version (server): go1.2.1
Last stable version: 1.1.2, please update docker
ansible版本
09:51:55 # ansible --version
ansible 1.4.3
1、查看已有的docker镜像
09:39:26 # docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu 3.0 6cee55276528 9 weeks ago 369.8 MB
centos5 3.0 e08d23b09189 9 weeks ago 840.9 MB
centos6 3.0 e94a3b24a19b 9 weeks ago 415.9 MB
2、查看当前运行的docker容器
09:39:47 # docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
846efb9e4d7a ubuntu:3.0 /usr/sbin/sshd-D 2 weeks ago Up 6 days 0.0.0.0:49167->22/tcpubuntu-test1
b9a9e6f2caed centos6:3.0 /usr/sbin/sshd-D 3 weeks ago Up 6 days 0.0.0.0:49166->22/tcpzabbix-server
978fff134b18 centos6:3.0 /usr/sbin/sshd-D 5 weeks ago Up 6 days 0.0.0.0:49165->22/tcpcentos6-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.5
real 0m6.369s
user 0m0.036s
sys 0m0.049s
root@ip-10-10-10-10:/etc/ansible/roles/redis_install/templates
09: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.6
real 0m12.567s
user 0m0.047s
sys 0m0.060s
然后把新容器的ip加入到ansible的hosts里
root@ip-10-10-10-10:/etc/ansible/roles/redis_install/templates
09:40:46 # echo "172.17.0.5">>/etc/ansible/hosts
root@ip-10-10-10-10:/etc/ansible/roles/redis_install/templates
09:40:58 # echo "172.17.0.6">>/etc/ansible/hosts
root@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: /data
redis_version: 2.6.17
redis_port: 6379
redis_conf_dir: /data/redis-2.6.17/conf
redis_db_dir: /data/redis-2.6.17/db
redis_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.yml
12 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" -k
SSH 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
...ignoring
TASK: [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.757s
user 0m3.453s
sys 0m0.497s
9、查看redis master安装情况
09:48:39 # ssh 172.17.0.5
root@172.17.0.5's password:
Last login: Wed Aug 13 09:45:28 2014 from 172.17.42.1
root@06762530db8c:~
09:48:43 # ps -ef|grep redis
root 294 1 0 09:45 ? 00:00:00 /usr/bin/redis-server/data/redis-2.6.17/conf/redis_6379.conf
root 343 329 0 09:48 pts/000:00:00 grepredis
root@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.conf
root@06762530db8c:~
09:48:52 # redis-cli -h 172.17.0.5 -p 6379
redis 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" -k
SSH 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
...ignoring
TASK: [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.579s
user 0m6.781s
sys 0m0.654s
11、查看redis slave安装情况
09:59:24 # ssh 172.17.0.6
root@172.17.0.6's password:
root@dfec766fbaa7:~
09:59:28 # ll
total 0
root@dfec766fbaa7:~
09:59:31 # ps -ef|grep redis
root 197 1 0 09:49 ? 00:00:00 /usr/bin/redis-server/data/redis-2.6.17/conf/redis_6379.conf
root 227 212 0 09:59 pts/000:00:00 grepredis
09:59:49 # grep slave /data/redis-2.6.17/conf/redis_6379.conf |egrep -v "^#"
slaveof 172.17.0.5 6379
slave-serve-stale-data yes
slave-read-only yes
slave-priority 100
client-output-buffer-limit slave 256mb 64mb 60
root@dfec766fbaa7:~
10:00:09 # redis-cli -h 172.17.0.6 -p 6379
redis 172.17.0.6:6379> keys *
(empty list or set)
redis 172.17.0.6:6379> info
# Server
redis_version:2.6.17
redis_git_sha1:00000000
redis_git_dirty:0
redis_mode:standalone
os:Linux 2.6.32-431.17.1.el6.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.4.7
process_id:197
run_id:81207df2727abe534ae606afc0cb80016f75a422
tcp_port:6379
uptime_in_seconds:685
uptime_in_days:0
hz:10
lru_clock:280339
# Clients
connected_clients:2
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
# Memory
used_memory:878072
used_memory_human:857.49K
used_memory_rss:7569408
used_memory_peak:877280
used_memory_peak_human:856.72K
used_memory_lua:31744
mem_fragmentation_ratio:8.62
mem_allocator:jemalloc-3.2.0
# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1407894548
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
# Stats
total_connections_received:1
total_commands_processed:70
instantaneous_ops_per_sec:0
rejected_connections:0
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
# Replication
role:slave
master_host:172.17.0.5
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_priority:100
slave_read_only:1
connected_slaves:0
# CPU
used_cpu_sys:0.24
used_cpu_user:0.10
used_cpu_sys_children:0.00
used_cpu_user_children:0.00
# Keyspace
可以从上面的Replication里看到主从做好了,信息如下图
然后去redis master里查看主从信息
12、redis 主从测试
A.redis master插入key
redis 172.17.0.5:6379> setmaster 1
OK
redis 172.17.0.5:6379> setslave 0
OK
redis 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 instand 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 syncstarted
[197] 13 Aug 09:49:09.881 * Non blocking connect forSYNC 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 inmemory
[197] 13 Aug 09:49:10.035 * MASTER <-> SLAVE sync: Finished with success
[197] 13 Aug 10:04:56.980 * 1 changes in900 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" -k
SSH 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.929s
user 0m1.942s
sys 0m0.250s
root@ip-10-10-10-10:/etc/ansible
10:09:18 # time ansible-playbook redis_delete.yml --extra-vars "host=172.17.0.6 user=root" -k
SSH 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.897s
user 0m1.726s
sys 0m0.258s
15、删除后测试
A.redis master上
10:09:55 # ifconfig
eth0 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/64Scope: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/128Scope: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 redis
root 431 329 0 10:09 pts/000:00:00 grepredis
root@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/local
root@06762530db8c:~
10:10:01 # ll /data/
total 4
drwxr-xr-x 2 root root 4096 Jun 5 10:21 soft
root@06762530db8c:~
B. redis slave上
10:08:09 # ifconfig
eth0 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/64Scope: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/128Scope: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 redis
root 319 212 0 10:10 pts/000:00:00 grepredis
root@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/local
root@dfec766fbaa7:~
10:10:39 # ll /data/
total 4
drwxr-xr-x 2 root root 4096 Jun 5 10:21 soft
root@dfec766fbaa7:~
如果大家想使用我的例子,可以从github里下载(地址是https://github.com/dl528888/ansible-examples/tree/master/redis_install),然后放到/etc/ansible目录里,下面是内容
转载自:http://blog.51cto.com/dl528888/1539251