一、环境:

服务器一台,已安装centos7.5系统,做ansible服务器;

客户机三台:hadoop-master(192.168.1.18)、hadoop-slave1(192.168.1.19)、hadoop-slave2(192.168.1.20)

二、ansible软件安装:

[root@centos75 ~]# yum install ansible

三、ansible配置过程:

1、服务器与客户机之间的免密配置:

(1)生成密钥: ssh-keygen -t rsa

(2)传递密钥:

[root@centos75 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.1.18

[root@centos75 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.1.19

[root@centos75 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.1.20

2、ansible配置

(1)Inventory主机清单配置:

[root@centos75 ~]# vi /etc/ansible/hosts

...

[hadoop]

192.168.1.[18:20]  #这是一种IP地址段表示方式,也可单列每个IP地址。

(2)配置ansible.cfg:

[root@centos75 ~]# vi /etc/ansible/ansible.cfg

...

host_key_checking = False  #禁用每次执行ansbile命令检查ssh key host

...

log_path = /var/log/ansible.log  #开启日志记录

...

[accelerate]  #ansible连接加速配置

#accelerate_port = 5099

accelerate_port = 10000

...

accelerate_multi_key = yes

...

deprecation_warnings = False  #屏蔽弃用告警提示,减少不必要的信息显示

...

四、测试

[root@centos75 ~]# ansible all -m ping

192.168.1.20 | SUCCESS => {

"changed": false,

"ping": "pong"

}

192.168.1.18 | SUCCESS => {

"changed": false,

"ping": "pong"

}

192.168.1.19 | SUCCESS => {

"changed": false,

"ping": "pong"

}

上述信息表明ansible管理对象已全部ping通,ansible配置正常。

五、使用示例

(1) Ad-Hoc模式:

修改Hadoop三台集群服务器的/etc/hosts文件:

[root@centos75 ~]# vi hosts

#127.0.1.1  hadoop-master

192.168.1.18  hadoop-master

192.168.1.19  hadoop-slave1

192.168.1.20  hadoop-slave2

# The following lines are desirable for IPv6 capable hosts

::1  ip6-localhost ip6-loopback

fe00::0 ip6-localnet

ff00::0 ip6-mcastprefix

ff02::1 ip6-allnodes

ff02::2 ip6-allrouters

~

[root@centos75 ~]# ansible hadoop -m copy -a "src=/root/hosts dest=/etc/hosts"

192.168.1.20 | SUCCESS => {

"changed": true,

"checksum": "214f72ce3329805c07748997e11313fffb03f667",

"dest": "/etc/hosts",

"gid": 0,

"group": "root",

"md5sum": "127193e1ec4773ce0195636c5ac2bf3a",

"mode": "0644",

"owner": "root",

"size": 298,

"src": "/root/.ansible/tmp/ansible-tmp-1536384515.76-109467000571031/source",

"state": "file",

"uid": 0

}

192.168.1.18 | SUCCESS => {

"changed": true,

"checksum": "214f72ce3329805c07748997e11313fffb03f667",

"dest": "/etc/hosts",

"gid": 0,

"group": "root",

"md5sum": "127193e1ec4773ce0195636c5ac2bf3a",

"mode": "0644",

"owner": "root",

"size": 298,

"src": "/root/.ansible/tmp/ansible-tmp-1536384515.74-269105082907411/source",

"state": "file",

"uid": 0

}

192.168.1.19 | SUCCESS => {

"changed": true,

"checksum": "214f72ce3329805c07748997e11313fffb03f667",

"dest": "/etc/hosts",

"gid": 0,

"group": "root",

"md5sum": "127193e1ec4773ce0195636c5ac2bf3a",

"mode": "0644",

"owner": "root",

"size": 298,

"src": "/root/.ansible/tmp/ansible-tmp-1536384515.75-259083114686776/source",

"state": "file",

"uid": 0

}

还可使用命令查看各客户机hosts文件内容:

ansible hadoop -m shell -a 'cat /etc/hosts'

ansible hadoop -m shell -a 'ls -lhat /etc/hosts'

(2) playbook剧本模式:

启动Hadoop集群服务:

[root@centos75 ~]# vi hadoop-start.yml

---

#“---”符号在yml文件中只能在开头出现一次,多次出现会报错;另外,此符号省略也可,不知为何,待继续研究...

- hosts: hadoop

#注意:“-”符号后必须有空格;“:”后面也必须有空格。

tasks:

#注意:缩进按两个空格规范,不能使用TAB!

- name: startup hadoop datanode services

shell: /root/hadoop-2.7.3/sbin/hadoop-daemon.sh start datanode  #尽管集群服务器上已配置hadoop-2.7.3/sbin的环境变量,但这里必须使用绝对路径

- hosts: 192.168.1.18

tasks:

- name: startup hadoop namenode services

shell: /root/hadoop-2.7.3/sbin/hadoop-daemon.sh start namenode

~

[root@centos75 ~]# ansible-playbook hadoop-start.yml

PLAY [hadoop] ******************************************************************

TASK [Gathering Facts] *********************************************************

ok: [192.168.1.20]

ok: [192.168.1.19]

ok: [192.168.1.18]

TASK [startup hadoop datanode services] ****************************************

changed: [192.168.1.19]

changed: [192.168.1.18]

changed: [192.168.1.20]

PLAY [192.168.1.18] ************************************************************

TASK [Gathering Facts] *********************************************************

ok: [192.168.1.18]

TASK [startup hadoop namenode services] ****************************************

changed: [192.168.1.18]

PLAY RECAP *********************************************************************

192.168.1.18  : ok=4  changed=2  unreachable=0  failed=0

192.168.1.19  : ok=2  changed=1  unreachable=0  failed=0

192.168.1.20  : ok=2  changed=1  unreachable=0  failed=0

可在集群服务器上观察服务启动情况:

root@hadoop-master:~# jps

8976 DataNode

9231 Jps

9093 NameNode

root@hadoop-slave1:~# jps

7058 Jps

6972 DataNode

停止hadoop集群服务:

[root@centos75 ~]# vi hadoop-stop.yml

---

- hosts: hadoop

tasks:

- name: stop hadoop datanode services

shell: /root/hadoop-2.7.3/sbin/hadoop-daemon.sh stop datanode

- hosts: 192.168.1.18

tasks:

- name: stop hadoop namenode services

shell: /root/hadoop-2.7.3/sbin/hadoop-daemon.sh stop namenode

~

[root@centos75 ~]# ansible-playbook hadoop-stop.yml

PLAY [hadoop] ******************************************************************

TASK [Gathering Facts] *********************************************************

ok: [192.168.1.20]

ok: [192.168.1.19]

ok: [192.168.1.18]

TASK [stop hadoop datanode services] *******************************************

changed: [192.168.1.20]

changed: [192.168.1.19]

changed: [192.168.1.18]

PLAY [192.168.1.18] ************************************************************

TASK [Gathering Facts] *********************************************************

ok: [192.168.1.18]

TASK [stop hadoop namenode services] *******************************************

changed: [192.168.1.18]

PLAY RECAP *********************************************************************

192.168.1.18  : ok=4  changed=2  unreachable=0  failed=0

192.168.1.19  : ok=2  changed=1  unreachable=0  failed=0

192.168.1.20  : ok=2  changed=1  unreachable=0  failed=0

上述过程可看出,ansible已实现了对集群服务启停作业的集中控制。

使用ansible控制Hadoop服务的启动和停止的更多相关文章

  1. 通过命令窗口控制mysql服务的启动与停止

    mysql服务的启动: 以管理员的身份运行cmd命令窗口,输入命名 net start mysql 如果不是以管理员的身份运行cmd,会提示如下错误 mysql服务的停止: 以管理员的身份运行cmd命 ...

  2. Centos7.3_x86_64通过systemctl控制tomcat8.0.46启动和停止

    Centos7.3_x86_64通过systemctl控制tomcat8..46启动和停止 之前在centos 6上通过脚本控制tomcat 启动和停止的脚本,虽然在centos 7也可以使用,但ce ...

  3. Linux上服务的启动,停止和重启

    (1)查看所有的服务 [berry@berry:practice] service Usage: service < option > | --status-all | [ service ...

  4. mysql服务的启动和停止 net stop mysql net start mysql

    第一招.mysql服务的启动和停止 net stop mysql net start mysql 第二招.登陆mysql 语法如下: mysql -u用户名-p用户密码 键入命令mysql -uroo ...

  5. Windows服务之启动、停止、暂停、继续

    原文:Windows服务之启动.停止.暂停.继续 Windows服务之启动.停止.暂停.继续 2011-11-09 15:07:37     我来说两句 收藏    我要投稿    [字体:小 大] ...

  6. Jenkins关闭、重启,Jenkins服务的启动、停止方法。

     一.Jenkins关闭.重启 1.关闭Jenkins 只需要在访问jenkins服务器的网址url地址后加上exit,关闭Jenkins服务. 例如:http://localhost:8081/ex ...

  7. 管理weblogic服务的启动和停止

    2012-11-10 12:58 26036人阅读 评论(4) 收藏 举报 分类: WebLogic(10) 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 介绍 Weblog ...

  8. linux系统下apache服务的启动、停止、重启命令

    本文章简单的介绍了关于linux下在利用命令来操作apache的基本操作如启动.停止.重启等操作,对入门者不错的选择.本文假设你的apahce安装目录为 usr local apache2,这些方法适 ...

  9. hadoop历史服务的启动与停止

    a.配置项(在分布式环境中配置) 1.RPC访问地址 mapreduce.jobhistory.address 2.HTTP访问地址 mapreduce.jobhistory.webapp.addre ...

随机推荐

  1. 怎么成为一名WEB前端开发工程师

       对于刚开始学的人来说,web 就是HTML+CSS+JavaScript其实我们是可以这样理解的.web工程师负责或参与Web产品的页面开发,包含PC端.移动APP内嵌移动端.微信小程序.web ...

  2. LeetCode--Array--Remove Duplicates from Sorted Array (Easy)

    26. Remove Duplicates from Sorted Array (Easy) Given a sorted array nums, remove the duplicates in-p ...

  3. Pytest 单元测试框架

    1.pytest 是 python 的第三方单元测试框架,比自带 unittest 更简洁和高效 2.安装 pytest pip install pytest 3.验证 pytest 是否安装成功 p ...

  4. 蓝桥杯备战(一)3n+1问题

    [问题描述] 考虑如下的序列生成算法:从整数 n 开始,如果 n 是偶数,把它除以 2:如果 n 是奇数,把它乘 3 加1.用新得到的值重复上述步骤,直到 n = 1 时停止.例如,n = 22 时该 ...

  5. [zoj3627]模拟吧

    思路:情况只可能是2种,两个人一直向一边走,或者有一个人折回来,对于后一种,枚举折回来的位置就行了.不过要注意两个方向都要处理下. #pragma comment(linker, "/STA ...

  6. flink入门学习

    Flink学习笔记 一.简介 1.定义: ​ 针对流数据和批数据的分布式处理引擎.它主要是由 Java 代码实现.. 2.应用场景: ​ 流数据:把所有任务当成流来处理,处理观察和分析连续事件产生的数 ...

  7. git版本控制系统小白教程(下)

    前言:本文主要介绍git版本控制系统的一些基础使用,适合小白入门,因为内容较多,会分为两部分进行分享,查看上部请点传送门. 删除文件 ​ git删除文件一般有三种情况,第一种是在工作区修改了文件,但是 ...

  8. 基于Nettty打造自己的MVC服务器

    最近开始折腾Netty,体验下NIO编程.既然学习了,就要做点东西出来,要不然不容易掌握学到的东西.在Netty的官方demo上都有各种case的sample,打造Http服务器的核心代码就是从Sam ...

  9. Jmeter简单性能测试练习

    项目描述: 被测网站:xqtesting.blog.51cto.com 指标:响应时间以及错误率 场景:线程数20   测试步骤: 测试计划 线程组 http请求 监听器 运行脚本 查看报告 1.添加 ...

  10. Nginx服务器的安装和卸载

    Nginx的安装 安装Nginx之前,需要先获取Nginx的安装文件.我们可以在http://nginx.org/en/download.html获取各个版本的Nginx安装文件.大家可以按照自己的需 ...