linre 发表于 2014-11-20 11:38:11

运维自动化之ansible playbook结合docker安装smokeping

本次介绍ansible的paly book结合docker进行虚拟机里安装2.6.8版本smokeping(apache版本是2.4.7)。

docker版本

1
2
3
4
5
6
7
8
9
10
09:26:53 # 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版本


1
2
09:28:13 # ansible --version
ansible 1.4.3




1、查看docker已有镜像

1
2
3
4
5
09:25:55 # 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




可以看到有3个镜像,1个是ubuntu,1个centos5,1个centos6.我这里打算使用centos6来弄。
2、加载新容器

1
2
3
4
5
6
09:31:01 # timedocker inspect $(docker run -d -p 22 -p 80:80 --name="smokeping" centos6:3.0 /usr/sbin/sshd -D)|grep -i address|awk -F '"' '{print $4}'
172.17.0.5

real    0m4.737s
user    0m0.038s
sys 0m0.054s




可以看到4秒就加载新容器完成,并且镜像为centos6系统,容器名为smokeping,开启了ssh服务,并且暴漏了22与80端口。
下面在从后端看看本机都运行了哪些容器

1
2
3
4
5
6
09:31:29 # docker ps -a
CONTAINER ID      IMAGE               COMMAND             CREATED            STATUS            PORTS                                       NAMES
56b70c31a07e      centos6:3.0         /usr/sbin/sshd -D   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp, 0.0.0.0:49156->22/tcp   smokeping         
846efb9e4d7a      ubuntu:3.0          /usr/sbin/sshd -D   12 days ago          Up 4 days         0.0.0.0:49167->22/tcp                     ubuntu-test1      
b9a9e6f2caed      centos6:3.0         /usr/sbin/sshd -D   3 weeks ago          Up 4 days         0.0.0.0:49166->22/tcp                     zabbix-server      
978fff134b18      centos6:3.0         /usr/sbin/sshd -D   4 weeks ago          Up 4 days         0.0.0.0:49165->22/tcp                     centos6-test5




下面是ansible安装smokeping的部分
3、ansible安装smokeping的信息


1
2
3
4
5
09:34:06 # cat smokeping_install/vars/main.yml
smokeping_dir: /usr/local/smokeping
smokeping_version: 2.6.8
smokeping_user: admin
smokeping_passwd: '()TF%penst*&MedHU'




可以看到smokeping安装目录是/usr/local/smokeping,安装版本是2.6.8,登陆的账户是admin,登陆密码是()TF%penst*&MedHU
4、下面是安装smokeping的playbook结构

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
09:36:25 # tree smokeping_*
smokeping_delete
├── files
│   └── delete_smokeping.sh
├── handlers
├── meta
│   └── main.yml
├── tasks
│   ├── copy.yml
│   ├── delete.yml
│   └── main.yml
├── templates
└── vars
    ├── main.xml
    ├── main.yml
    └── vars
      └── main.yml
smokeping_install
├── files
│   ├── rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
│   └── smokeping.tar.gz
├── handlers
├── meta
│   └── main.yml
├── tasks
│   ├── copy.yml
│   ├── delete.yml
│   ├── install.yml
│   └── main.yml
├── templates
│   ├── config
│   └── smokeping.conf
└── vars
    └── main.yml

13 directories, 18 files




5、playbook安装smokeping的内容是

1
2
3
4
5
6
7
8
9
10
09:37:27 # cat smokeping_install.yml
---
- hosts: "{{host}}"
remote_user: "{{user}}"
gather_facts: True
roles:
    - common
    - pcre_install
    - apache_install
    - smokeping_install




6、playbook删除smokeping的内容是

1
2
3
4
5
6
7
8
9
09:38:01 # cat smokeping_delete.yml
---
- hosts: "{{host}}"
remote_user: "{{user}}"
gather_facts: True
roles:
    - apache_delete
    - pcre_delete
    - smokeping_delete




下面是安装与测试过程
7、把docker新容器smokeping的ip加入到ansible的hosts里

1
echo "172.17.0.5">>/etc/ansible/hosts




8、安装smokeping

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
09:39:10 # time ansible-playbook smokeping_install.yml --extra-vars "host=172.17.0.5 user=root" -k
SSH password:

PLAY *************************************************************

GATHERING FACTS ***************************************************************
ok:

TASK: ***********************
changed:

TASK: ********************
changed:

TASK: ***********
changed:

TASK: ******************
changed:

TASK: ****************
changed: => (item=httpd-2.4.7.tar.gz)
changed: => (item=libiconv.tar.gz)

TASK: ******************
changed:

TASK: *******
changed:

TASK: ******************
changed:

TASK: ************
changed:

TASK: ***
changed:

TASK: *******************************
ok:

TASK: ******************
changed:

TASK: ****************
changed:

TASK: *****************
failed: => {"changed": true, "cmd": "grep -c /usr/local/lib/ /etc/ld.so.conf ", "delta": "0:00:00.006524", "end": "2014-08-11 09:40:48.822372", "item": "", "rc": 1, "start": "2014-08-11 09:40:48.815848"}
stdout: 0
...ignoring

TASK: ****************
changed:

TASK: ************************************
changed: => (item=/data/webroot/apache)
changed: => (item=/data/webroot/apache/logs)
changed: => (item=/data/webroot/apache/vhost)

TASK: ****************
changed:

TASK: *******************
changed:

TASK: ****************
changed:

TASK: *******
changed:

TASK: ***
changed:

TASK: **********************
changed:

TASK: **********
changed: => (item=rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm)
changed: => (item=smokeping.tar.gz)

TASK: *****************************
changed:

TASK: **************************
changed:

TASK: **************************
changed:

TASK: ************
changed:

TASK: ******
changed:

TASK: *****************
failed: => {"changed": true, "cmd": "grep -c '/usr/local/smokeping/bin/smokeping --logfile=/var/log/smokeping.log 2>&1 &' /etc/rc.local ", "delta": "0:00:00.007325", "end": "2014-08-11 09:46:03.045378", "item": "", "rc": 1, "start": "2014-08-11 09:46:03.038053"}
stdout: 0
...ignoring

TASK: *******************
changed:

TASK: ***
changed:

TASK: **********
changed:

TASK: ***********
changed:

TASK: *****************************
changed:

PLAY RECAP ********************************************************************
172.17.0.5               : ok=35   changed=33   unreachable=0    failed=0   


real    6m52.934s
user    0m15.104s
sys 0m1.632s




花费6分52秒完成,花费时间多的主要是yum安装基础库与使用epel安装smokeping依赖库。
9、安装后测试

查看smokeping

由于我做了安全认证,需要输入账户与密码,这个账户与密码就上文第3步的账户与密码

然后默认的其他网络监控(包括1、2、3线电信、移动、联通、铁通与教育网的节点监控已经做好)

如果有其他的监控需要自己来添加即可。
10、删除

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
09:46:08 # time ansible-playbook smokeping_delete.yml --extra-vars "host=172.17.0.5 user=root" -k
SSH password:

PLAY *************************************************************

GATHERING FACTS ***************************************************************
ok:

TASK: *******************
changed:

TASK: ********************
changed:

TASK: ********************
changed:

TASK: *********
changed:

TASK: ************************************
changed:

TASK: *********************************************
changed:

TASK: ************
changed:

TASK: **************
changed:

TASK: *****************
changed:

PLAY RECAP ********************************************************************
172.17.0.5               : ok=10   changed=9    unreachable=0    failed=0   


real    0m9.326s
user    0m2.914s
sys 0m0.446s




11、删除后测试

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
49
50
09:59:45 # ssh 172.17.0.5
root@172.17.0.5's password:
root@56b70c31a07e:~
09:59:57 # ifconfig
eth0      Link encap:EthernetHWaddr A6:80:57:2D:D3:F1
          inet addr:172.17.0.5Bcast:0.0.0.0Mask:255.255.0.0
          inet6 addr: fe80::a480:57ff:fe2d:d3f1/64 Scope:Link
          UP BROADCAST RUNNINGMTU:1500Metric:1
          RX packets:84520 errors:0 dropped:0 overruns:0 frame:0
          TX packets:86385 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:69075188 (65.8 MiB)TX bytes:9400662 (8.9 MiB)

lo      Link encap:Local Loopback
          inet addr:127.0.0.1Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNINGMTU:16436Metric:1
          RX packets:2 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:168 (168.0 b)TX bytes:168 (168.0 b)

root@56b70c31a07e:~
09:59:59 # ps -ef|grep httpd
root      101710020 10:00 pts/0    00:00:00 grep httpd
root@56b70c31a07e:~
10:00:04 # ps -ef|grep smokeping
root      101910020 10:00 pts/0    00:00:00 grep smokeping
root@56b70c31a07e:~
10:00:07 # ll /usr/local/
total 40
drwxr-xr-x 2 root root 4096 Sep 232011 bin
drwxr-xr-x 2 root root 4096 Sep 232011 etc
drwxr-xr-x 2 root root 4096 Sep 232011 games
drwxr-xr-x 2 root root 4096 Sep 232011 include
drwxr-xr-x 2 root root 4096 Aug 11 09:40 lib
drwxr-xr-x 2 root root 4096 Sep 232011 lib64
drwxr-xr-x 2 root root 4096 Sep 232011 libexec
drwxr-xr-x 2 root root 4096 Sep 232011 sbin
drwxr-xr-x 5 root root 4096 Sep 232011 share
drwxr-xr-x 2 root root 4096 Sep 232011 src
root@56b70c31a07e:~
10:00:11 # 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




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


页: [1]
查看完整版本: 运维自动化之ansible playbook结合docker安装smokeping