首页 > 代码库 > 运维自动化之ansible playbook结合docker安装smokeping

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

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

docker版本

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版本

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

1、查看docker已有镜像

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、加载新容器

09:31:01 # time  docker 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端口。

下面在从后端看看本机都运行了哪些容器

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的信息

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结构

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的内容是

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的内容是

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里

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

8、安装smokeping

09:39:10 # time ansible-playbook smokeping_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: [pcre_install | Copy Pcre Software To Redhat Client] ******************** 
changed: [172.17.0.5]

TASK: [pcre_install | Uncompression Pcre Software In Redhat Client] *********** 
changed: [172.17.0.5]

TASK: [pcre_install | Delete Pcre Software In Redhat Client] ****************** 
changed: [172.17.0.5]

TASK: [apache_install | Copy Apache Software To Redhat Client] **************** 
changed: [172.17.0.5] => (item=httpd-2.4.7.tar.gz)
changed: [172.17.0.5] => (item=libiconv.tar.gz)

TASK: [apache_install | Create Apache User In Redhat Client] ****************** 
changed: [172.17.0.5]

TASK: [apache_install | Uncompression Apache Software To Redhat Client] ******* 
changed: [172.17.0.5]

TASK: [apache_install | Copy Apache Config To Redhat Client] ****************** 
changed: [172.17.0.5]

TASK: [apache_install | Copy Apache Vhost Config To Redhat Client] ************ 
changed: [172.17.0.5]

TASK: [apache_install | Copy Apache Start Service Script  To Redhat Client] *** 
changed: [172.17.0.5]

TASK: [apache_install | Create Lib Install Dir] ******************************* 
ok: [172.17.0.5]

TASK: [apache_install | Check Apache Iconv In Redhat Client] ****************** 
changed: [172.17.0.5]

TASK: [apache_install | Install Apache Iconv In Redhat Client] **************** 
changed: [172.17.0.5]

TASK: [apache_install | Check Lib In Config In Redhat Client] ***************** 
failed: [172.17.0.5] => {"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: [apache_install | Install Apache Iconv In Redhat Client] **************** 
changed: [172.17.0.5]

TASK: [apache_install | Create Apache Dir] ************************************ 
changed: [172.17.0.5] => (item=/data/webroot/apache)
changed: [172.17.0.5] => (item=/data/webroot/apache/logs)
changed: [172.17.0.5] => (item=/data/webroot/apache/vhost)

TASK: [apache_install | Install Check Script In Redhat Client] **************** 
changed: [172.17.0.5]

TASK: [apache_install | Create Index Html To Redhat Client] ******************* 
changed: [172.17.0.5]

TASK: [apache_install | Start Apache Service In Redhat Client] **************** 
changed: [172.17.0.5]

TASK: [apache_install | Add Boot Start Apache Service In Redhat Client] ******* 
changed: [172.17.0.5]

TASK: [apache_install | Delete Apache compression Software In Redhat Client] *** 
changed: [172.17.0.5]

TASK: [smokeping_install | Create Smokeping Install Dir] ********************** 
changed: [172.17.0.5]

TASK: [smokeping_install | Copy Smokeping Software To Redhat Client] ********** 
changed: [172.17.0.5] => (item=rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm)
changed: [172.17.0.5] => (item=smokeping.tar.gz)

TASK: [smokeping_install | Install Epel Yum Repo] ***************************** 
changed: [172.17.0.5]

TASK: [smokeping_install | Install Require Software] ************************** 
changed: [172.17.0.5]

TASK: [smokeping_install | Uncompression  Smokeping] ************************** 
changed: [172.17.0.5]

TASK: [smokeping_install | Copy Smokeping Config To Redhat Client] ************ 
changed: [172.17.0.5]

TASK: [smokeping_install | Copy Smokeping Httpd Config To Redhat Client] ****** 
changed: [172.17.0.5]

TASK: [smokeping_install | Check Boot Start In Redhat Client] ***************** 
failed: [172.17.0.5] => {"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: [smokeping_install | Add Boot Start In Redhat Client] ******************* 
changed: [172.17.0.5]

TASK: [smokeping_install | Modify Smokeping Dir Permission In Redhat Client] *** 
changed: [172.17.0.5]

TASK: [smokeping_install | Start Smokeping Service In Redhat Client] ********** 
changed: [172.17.0.5]

TASK: [smokeping_install | Restart Apache Service In Redhat Client] *********** 
changed: [172.17.0.5]

TASK: [smokeping_install | Delete Installed File] ***************************** 
changed: [172.17.0.5]

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、安装后测试

wKiom1PoInjjIePxAACBGl7w5fg476.jpg

查看smokeping

wKiom1PoItTggjuhAAGrTdO6Ihw932.jpg

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

wKiom1PoIxHQYHtkAADwWndNQgA311.jpg

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

wKioL1PoJGrjwA7CAAVKSkSX8wU360.jpg

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

10、删除

09:46:08 # time ansible-playbook smokeping_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: [apache_delete | Stop Httpd Service In RedHat Client] ******************* 
changed: [172.17.0.5]

TASK: [apache_delete | Delete Boot Start In RedHat Client] ******************** 
changed: [172.17.0.5]

TASK: [apache_delete | Delete Apache Dir In RedHat Client] ******************** 
changed: [172.17.0.5]

TASK: [apache_delete | Delete Apache Service Script In RedHat Client] ********* 
changed: [172.17.0.5]

TASK: [apache_delete | Delete Apache User] ************************************ 
changed: [172.17.0.5]

TASK: [pcre_delete | Delete Pcre] ********************************************* 
changed: [172.17.0.5]

TASK: [smokeping_delete | Stop Smokeping Service In Redhat Client] ************ 
changed: [172.17.0.5]

TASK: [smokeping_delete | Delete Smokeping Dir In Redhat Client] ************** 
changed: [172.17.0.5]

TASK: [smokeping_delete | Delete Boot Start In Redhat Client] ***************** 
changed: [172.17.0.5]

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


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

11、删除后测试

09:59:45 # ssh 172.17.0.5
root@172.17.0.5‘s password: 
root@56b70c31a07e:~
09:59:57 # ifconfig 
eth0      Link encap:Ethernet  HWaddr A6:80:57:2D:D3:F1  
          inet addr:172.17.0.5  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::a480:57ff:fe2d:d3f1/64 Scope:Link
          UP BROADCAST RUNNING  MTU:1500  Metric: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.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric: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      1017  1002  0 10:00 pts/0    00:00:00 grep httpd
root@56b70c31a07e:~
10:00:04 # ps -ef|grep smokeping
root      1019  1002  0 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 23  2011 bin
drwxr-xr-x 2 root root 4096 Sep 23  2011 etc
drwxr-xr-x 2 root root 4096 Sep 23  2011 games
drwxr-xr-x 2 root root 4096 Sep 23  2011 include
drwxr-xr-x 2 root root 4096 Aug 11 09:40 lib
drwxr-xr-x 2 root root 4096 Sep 23  2011 lib64
drwxr-xr-x 2 root root 4096 Sep 23  2011 libexec
drwxr-xr-x 2 root root 4096 Sep 23  2011 sbin
drwxr-xr-x 5 root root 4096 Sep 23  2011 share
drwxr-xr-x 2 root root 4096 Sep 23  2011 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/ansible-examples/tree/master/smokeping_install),然后放到/etc/ansible目录里,下面是内容

wKiom1PoJXfDxRdDAAHarjz6Fe0814.jpg

本文出自 “吟—技术交流” 博客,请务必保留此出处http://dl528888.blog.51cto.com/2382721/1538444