运维自动化之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,如需转载请自行联系原作者



相关文章
|
2天前
|
存储 Linux Docker
arm安装docker与docker-copose
现在,你已经成功在ARM架构的设备上安装了Docker和Docker Compose。你可以使用它们来管理容器和容器化应用程序。请注意,ARM设备上的Docker支持可能受到限制,某些容器可能不兼容。确保你的容器映像支持ARM架构,以便在ARM设备上正确运行。
12 5
|
6天前
|
机器学习/深度学习 运维 持续交付
构建高效自动化运维体系:Ansible与Docker的完美结合构建高效机器学习模型的五大技巧
【4月更文挑战第30天】 在当今快速发展的云计算和微服务架构时代,自动化运维已成为维持系统稳定性和提高效率的关键。本文将探讨如何通过结合Ansible和Docker技术构建一个高效的自动化运维体系。文章不仅介绍了Ansible与Docker的基本原理和优势,还详细阐述了如何整合这两种技术以简化部署流程、加强版本控制,并提高整体运维效率。通过案例分析,我们将展示这一组合在实际环境中的应用效果,以及它如何帮助企业实现持续集成和持续部署(CI/CD)的目标。 【4月更文挑战第30天】 在数据驱动的时代,构建一个高效的机器学习模型是获取洞察力和预测未来趋势的关键步骤。本文将分享五种实用的技巧,帮助数
|
6天前
|
NoSQL Redis Docker
使用docker安装redis
该文档介绍了如何使用Docker快速搭建Redis数据库,以便于Spring Boot学习。主要内容包括获取Redis镜像、创建容器、配置持久化存储目录和修改默认配置文件,以及检查和访问Redis容器服务。此外,还提到若需外部访问,需开启宿主机防火墙相应端口。注意,本教程不深入讲解Docker,若想深入学习Docker,建议另寻专门课程。
|
6天前
|
Linux Docker 容器
centos7安装docker图文详解
该文档提供了在CentOS上安装Docker的步骤:检查系统内核版本(需大于3.10),更新yum,卸载旧版Docker,安装yum-utils和依赖包,设置Docker仓库,列出并选择Docker版本,安装Docker,最后启动并设置Docker开机启动,通过`docker version`验证安装是否成功。
|
6天前
|
NoSQL 网络协议 MongoDB
docker安装mongodb(单点)图文详解
该文档提供了一个快速搭建MongoDB环境的指南,适用于开发和学习,但不适用于生产。主要步骤包括:1) 使用Docker创建数据卷`mongo_data_db`和`mongo_data_configdb`。2) 拉取`mongo`镜像并运行名为`mymongo`的容器,映射端口并挂载数据卷。3) 初始化管理员账号,创建具有`root`权限的用户`admin`,密码为`adminpwd`。4) 防火墙开放端口27017。最后,提到了使用第三方客户端进行连接。
|
6天前
|
弹性计算 Shell 数据安全/隐私保护
自动化构建和部署Docker容器
【4月更文挑战第30天】
10 0
|
6天前
|
Shell 数据安全/隐私保护 Docker
如何使用Docker安装FTP服务器?
【4月更文挑战第25天】
12 0
如何使用Docker安装FTP服务器?
|
6天前
|
数据安全/隐私保护 Docker Sentinel
docker安装Sentinel
docker安装Sentinel
|
7天前
|
存储 关系型数据库 MySQL
docker安装mysql8忽略大小写
docker安装mysql8忽略大小写
|
7天前
|
关系型数据库 MySQL 开发工具
Docker安装mysql8.0
Docker安装mysql8.0

热门文章

最新文章