# 时间同步服务、关闭防火墙、IP 映射

NTP 是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源做同步化,提供高精准度的时间校正。Hadoop 集群对时间要求很高,主节点与各从节点的时间都必须要同步。配置时间同步服务主要是为了进行集群间的时间同步。Hadoop 集群配置时间同步服务的步骤如下:

方式一:在线安装 ntp 服务,在 master 节点安装 NTP 服务。

  • (1)不换任何下载源的情况下,在 master 节点安装 NTP 服务

安装NTP服务
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
[root@master ~]# yum -y install ntp
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package ntp.x86_64 0:4.2.6p5-15.el6.centos will be installed
--> Processing Dependency: ntpdate = 4.2.6p5-15.el6.centos for package: ntp-4.2.6p5-15.el6.centos.x86_64
--> Running transaction check
---> Package ntpdate.x86_64 0:4.2.6p5-15.el6.centos will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================
Package Arch Version Repository Size
========================================================================================
Installing:
ntp x86_64 4.2.6p5-15.el6.centos updates 600 k
Installing for dependencies:
ntpdate x86_64 4.2.6p5-15.el6.centos updates 79 k

Transaction Summary
========================================================================================
Install 2 Package(s)

Total download size: 679 k
Installed size: 1.8 M
Downloading Packages:
(1/2): ntp-4.2.6p5-15.el6.centos.x86_64.rpm | 600 kB 03:13
(2/2): ntpdate-4.2.6p5-15.el6.centos.x86_64.rpm | 79 kB 00:04
----------------------------------------------------------------------------------------
Total 3.4 kB/s | 679 kB 03:19
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Importing GPG key 0xC105B9DE:
Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org>
Package: centos-release-6-5.el6.centos.11.1.x86_64 (@anaconda-CentOS-201311272149.x86_64/6.5)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : ntpdate-4.2.6p5-15.el6.centos.x86_64 1/2
Installing : ntp-4.2.6p5-15.el6.centos.x86_64 2/2
Verifying : ntpdate-4.2.6p5-15.el6.centos.x86_64 1/2
Verifying : ntp-4.2.6p5-15.el6.centos.x86_64 2/2

Installed:
ntp.x86_64 0:4.2.6p5-15.el6.centos

Dependency Installed:
ntpdate.x86_64 0:4.2.6p5-15.el6.centos

Complete!
[root@master ~]#

注意:不换镜像源,在线安装 ntp 服务很容易出现错误

  • 切换下载源的情况下,在 master 节点安装 NTP 服务(推荐),操作流程如下:

ntp服务安装
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1、安装wget软件
yum install -y wget

2、备份
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

3、下载新的 CentOS-Base.repo 到 /etc/yum.repos.d/
CentOS 5
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo

CentOS 6
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

CentOS 7
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

4、之后运行yum makecache生成缓存

方式二:离线安装 ntp 服务

  • (1)使用 xshell 软件上传 ntp 两个离线安装包到 /opt 目录下,先输入 “rpm -ivh ntpdate-4.2.6p5-12.el6.centos.2.x86_64.rpm” 安装,然后输入 “rpm -ivh ntp-4.2.6p5-12.el6.centos.2.x86_64.rpm

离线安装ntp软件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@master opt]# ll
total 341828
-rw-r--r--. 1 root root 195257604 Dec 1 04:21 hadoop-2.6.0.tar_4.gz
-rw-r--r--. 1 root root 153512879 Dec 1 04:21 jdk-7u79-linux-x64.tar.gz
-rw-r--r--. 1 root root 614088 Dec 1 05:20 ntp-4.2.6p5-12.el6.centos.2.x86_64.rpm
-rw-r--r--. 1 root root 80736 Dec 1 05:20 ntpdate-4.2.6p5-12.el6.centos.2.x86_64.rpm
-rw-r--r--. 1 root root 560272 Dec 1 11:21 wget-1.14-18.el7_6.1.x86_64.rpm
[root@master opt]# rpm -ivh ntpdate-4.2.6p5-12.el6.centos.2.x86_64.rpm
warning: ntpdate-4.2.6p5-12.el6.centos.2.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Preparing... ########################################### [100%]
package ntpdate-4.2.6p5-12.el6.centos.2.x86_64 is already installed
[root@master opt]# rpm -ivh ntp-4.2.6p5-12.el6.centos.2.x86_64.rpm
warning: ntp-4.2.6p5-12.el6.centos.2.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Preparing... ########################################### [100%]
package ntp-4.2.6p5-12.el6.centos.2.x86_64 is already installed
[root@master opt]# vi /etc/ntp.conf
[root@master opt]#

  • (2)设置 master 节点为 NTP 服务主节点,那么其配置如下。
    使用命令 “vi /etc/ntp.conf” 打开 /etc/ntp.conf 文件,注释掉以 server 开头的行,并添加:
    添加的信息
    1
    2
    3
    restrict 192.168.128.2 mask 255.255.255.0 nomodify notrap
    server 127.127.1.0
    fudge 127.127.1.0 stratum 10

/etc/ntp.conf文件信息
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
# Permit all access over the loopback interface.  This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict -6 ::1

# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst

#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client

# Enable public key cryptography.
#crypto

修改后的/etc/ntp.conf文件信息
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
# Permit all access over the loopback interface.  This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict -6 ::1

# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
restrict 192.168.146.2 mask 255.255.255.0 nomodify notrap # 修改对应的ip地址
server 127.127.1.0
fudge 127.127.1.0 stratum 10

#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client

  • (3)关闭防火墙
    状执行命令 “service iptables stop & chkconfig iptables off” 永久性关闭防火墙,"service iptables status" 查看关闭状态。

    关闭防火墙
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    [root@master ~]# service iptables stop & chkconfig iptables off
    [1] 1268
    [root@master ~]# iptables: Setting chains to policy ACCEPT:[ OK ]
    iptables: Flushing firewall rules: [ OK ]
    iptables: Unloading modules: [ OK ]

    [1]+ Done service iptables stop #如果关闭卡住,按一下回车键
    [root@master ~]# service iptables status
    iptables: Firewall is not running.
    [1]+ Done service iptables stop
    [root@master ~]#

  • (4) 在 master 配置 IP 映射,在 vi /etc/hosts 添加
    192.168.128.130 master # 自己的 ip
    192.168.128.131 slave1
    192.168.128.132 slave2

配置ip映射
1
2
3
4
5
6
[root@master ~]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.146.171 master #配置的ip 对应的主机名 ip存在不一样需要修改
192.168.146.172 slave1
192.168.146.173 slave2