运维自动化之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  # 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端口。

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

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 [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、删除

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 [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、删除后测试

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: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





 本文转自 reinxu 51CTO博客,原文链接:http://blog.51cto.com/dl528888/1538444,如需转载请自行联系原作者



相关文章
|
关系型数据库 应用服务中间件 nginx
Docker一键安装中间件(RocketMq、Nginx、MySql、Minio、Jenkins、Redis)
本系列脚本提供RocketMQ、Nginx、MySQL、MinIO、Jenkins和Redis的Docker一键安装与配置方案,适用于快速部署微服务基础环境。
|
安全 Linux iOS开发
Nessus Professional 10.10 Auto Installer for RHEL 10, AlmaLinux 10, Rocky Linux 10 - Nessus 自动化安装程序
Nessus Professional 10.10 Auto Installer for RHEL 10, AlmaLinux 10, Rocky Linux 10 - Nessus 自动化安装程序
1056 6
Nessus Professional 10.10 Auto Installer for RHEL 10, AlmaLinux 10, Rocky Linux 10 - Nessus 自动化安装程序
|
Ubuntu 安全 iOS开发
Nessus Professional 10.10 Auto Installer for Ubuntu 24.04 - Nessus 自动化安装程序
Nessus Professional 10.10 Auto Installer for Ubuntu 24.04 - Nessus 自动化安装程序
1587 5
|
安全 Ubuntu iOS开发
Nessus Professional 10.10 Auto Installer for Windows - Nessus 自动化安装程序
Nessus Professional 10.10 Auto Installer for Windows - Nessus 自动化安装程序
434 3
Nessus Professional 10.10 Auto Installer for Windows - Nessus 自动化安装程序
|
关系型数据库 数据库 PostgreSQL
docker 安装 Postgres 17.6
本文介绍如何通过Docker安装和配置PostgreSQL 17.6。内容包括拉取镜像、导出配置文件、运行容器并挂载数据与配置文件目录,以及进入容器使用psql操作数据库的完整步骤,便于持久化管理和自定义配置。
2037 3
docker 安装 Postgres 17.6
|
安全 Ubuntu iOS开发
Nessus Professional 10.10 Auto Installer for macOS Tahoe - Nessus 自动化安装程序
Nessus Professional 10.10 Auto Installer for macOS Tahoe - Nessus 自动化安装程序
408 5
|
NoSQL 算法 Redis
【Docker】(3)学习Docker中 镜像与容器数据卷、映射关系!手把手带你安装 MySql主从同步 和 Redis三主三从集群!并且进行主从切换与扩容操作,还有分析 哈希分区 等知识点!
Union文件系统(UnionFS)是一种**分层、轻量级并且高性能的文件系统**,它支持对文件系统的修改作为一次提交来一层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下(unite several directories into a single virtual filesystem) Union 文件系统是 Docker 镜像的基础。 镜像可以通过分层来进行继承,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像。
1279 6
|
Java Linux 虚拟化
【Docker】(1)Docker的概述与架构,手把手带你安装Docker,云原生路上不可缺少的一门技术!
1. Docker简介 1.1 Docker是什么 为什么docker会出现? 假定您在开发一款平台项目,您的开发环境具有特定的配置。其他开发人员身处的环境配置也各有不同。 您正在开发的应用依赖于您当前的配置且还要依赖于某些配置文件。 您的企业还拥有标准化的测试和生产环境,且具有自身的配置和一系列支持文件。 **要求:**希望尽可能多在本地模拟这些环境而不产生重新创建服务器环境的开销 问题: 要如何确保应用能够在这些环境中运行和通过质量检测? 在部署过程中不出现令人头疼的版本、配置问题 无需重新编写代码和进行故障修复
1050 2
|
Ubuntu 安全 Linux
Linux Docker & Docker Compose 一键安装
该脚本由轩辕镜像官方维护,支持多种Linux发行版,可一键安装Docker与docker-compose,并自动配置镜像加速源。脚本开源、安全、高效,适用于Ubuntu、CentOS等系统,内置国内镜像源,提升下载速度,同时提供一键回滚功能,保障安装稳定性。
5100 1
|
存储 NoSQL MongoDB
Docker中安装MongoDB并配置数据、日志、配置文件持久化。
现在,你有了一个运行在Docker中的MongoDB,它拥有自己的小空间,对高楼大厦的崩塌视而不见(会话丢失和数据不持久化的问题)。这个MongoDB的数据、日志、配置文件都会妥妥地保存在你为它精心准备的地方,天旋地转,它也不会失去一丁点儿宝贵的记忆(即使在容器重启后)。
2118 4