【转载】Spark集群环境搭建——服务器环境初始化
转自doublexi:
Spark也是属于Hadoop生态圈的一部分,需要用到Hadoop框架里的HDFS存储和YARN调度,可以用Spark来替换MR做分布式计算引擎。
接下来,讲解一下spark集群环境的搭建部署。
一、集群规划
系统初始化:
1、设置系统IP (三台机器都要设置)
| 010203040506070809101112131415161718192021222324252627 | # 先查看selinux状态[root@localhost ~]# getenforcepermissive[root@localhost ~]# setenforce 0# 修改selinux状态为disabled[root@localhost ~]# vim /etc/sysconfig/selinux# This file controls the state of SELinux on the system.# SELINUX= can take one of these three values:# enforcing - SELinux security policy is enforced.# permissive - SELinux prints warnings instead of enforcing.# disabled - No SELinux policy is loaded.SELINUX=disabled # 修改为disabled# SELINUXTYPE= can take one of three values:# targeted - Targeted processes are protected,# minimum - Modification of targeted policy. Only selected processes are protected.# mls - Multi Level Security protection.SELINUXTYPE=targeted [root@localhost ~]# 关闭firewalld# 查看当前状态[root@localhost ~]# systemctl status firewalld# 关闭[root@localhost ~]# systemctl stop firewalld[root@localhost ~]# systemctl disable firewalld# 清除iptables规则[root@localhost ~]# iptables -F |
3、更换yum源,可选 (三台机器一起设置)
| 0102 | yum install -y net-tools vim lrzsz wget tree lsof tcpdump bash-completion.noarch ntp zip unzip git lvm2yum install -y gcc gcc-c++ libstdc++ make cmake curl bind-utils |
5、设置hostname (三台机器一起设置)
1) 在第一台机器上生成一对钥匙,公钥和私钥
| 01 | # 输入命令,生成秘钥 |
当前用户的宿主目录下的.ssh目录多了两个文件,一个是私钥,一个是公钥
| 01 | ll /root/.ssh/ |
2) 将公钥拷贝给要免密码登录的机器
将本机生成的公钥id_rsa.pub文件拷贝到另外两台机器,就可以从另外两台机器免密登录本机了。
拷贝到dev-spark-slave-171上:
| 01 | # ssh-copy-id root@dev-spark-slave-171 |
拷贝到dev-spark-slave-172上:
| 01 | # ssh-copy-id root@dev-spark-slave-172 |
| 01 |
注意:
注意: 1.主机名和ip都可以(确保配置了主机名 ip的映射) 2.如果出现-bash: ssh-copy-id: command not found,说明ssh-copy-id这个指令没有找到,自己安装下即可 安装命令:yum -y install openssh-clients
如果感觉ssh-copy-id这个命令不好使,你也可以自己新建authorized_keys文件,把公钥贴进去
| 0102030405060708091011 | # 在另外两台机器上# 查看.ssh目录是否存在ll /root/.ssh# 不存在则创建mkdir /root/.ssh# 编辑authorized_keys,将第一台机器上生成的id_rsa.pub文件里的内容复制,粘贴在这里。vim /root/.ssh/authorized_keys# 因为是自己创建的文件,需要修改权限,文件目录权限不能错chown -R root.root /root/.sshchmod 700 /root/.sshchmod 600 /root/.ssh/authorized_keys |
3)拷贝完成之后,会在要免密登录的机器(另外两台机器)上生成授权密码文件
在dev-spark-slave-171上,尝试免密登录dev-spark-master-206
| 01 | ssh root@dev-spark-master-206 |
在dev-spark-slave-172上,尝试免密登录dev-spark-master-206
| 01 | ssh root@dev-spark-master-206 |
4)、在另外两台机器上,重复上述1-3步骤。
注意:免密码登录是单向的,必须要在另外两台机器也生成一对公钥私钥,并把公钥发送到其他服务器上。
最后的结果是:三台机器上,每台机器都有其他两台机器的公钥。才能实现三台机器互相免密登录。
7、设置时间 (三台机器)
时间同步的方式:在集群中找一台机?,作为时间服务?。(以master为主)
| 01020304 | 通过网络连接外网进行时钟同步,必须保证虚拟机连上外网ntpdate us.pool.ntp.org;阿里云时钟同步服务器ntpdate ntp4.aliyun.com |
集群中其他机?与这台机?定时的同步时间,比如,每 隔十分钟,同步一次时间。
8.1、时间服务?配置(必须root用户)(在master上设置)
第一步:确定是否安装了ntpd的服务
| 0102030405060708 | 如果没有安装,可以进行在线安装yum -y install ntp启动ntpd的服务systemctl start ntpd设置ntpd的服务开机启动systemctl enable ntpd确定是否安装了ntpd的服务rpm -qa | grep ntpd |
第二步:编辑/etc/ntp.conf
| 010203040506070809101112 | 编辑第一台机器?的/etc/ntp.confvim /etc/ntp.conf在文件中添加如下内容restrict 192.168.90.0 mask 255.255.255.0 nomodify notrap注释一下四?行行内容#server 0.centos.pool.ntp.org#server 1.centos.pool.ntp.org#server 2.centos.pool.ntp.org#server 3.centos.pool.ntp.org去掉以下内容的注释,如果没有这两行注释,那就自己添加上server 127.127.1.0 # local clockfudge 127.127.1.0 stratum 10 |
配置以下内容,保证BIOS与系统时间同步 vim /etc/sysconfig/ntpd
添加一行内容
| 01 | SYNC_HWLOCK=yes |
第三步:
重新启动ntpd
| 0102 | systemctl restart ntpdsystemctl status ntpd |
8.2、其他机?配置(必须root用户)
第一步:在其他机?配置10分钟与时间服务?同步一次
| 010203 | # 另外两台机?与192.168.90.206进行时钟同步# crontab -e*/10 * * * * /usr/sbin/ntpdate 192.168.90.206 |
9、安装jdk (三台机器都要配置)
rsync 远程同步工具
rsync主要用于备份和镜像。具有速度快、避免复制相同内容和支持符号链接的优点。
rsync和scp区别:用rsync做文件的复制要比scp的速度快,rsync只对差异文件做更新。
scp是把所有文件都复制过去。
基本语法
| 01 | rsync -rvl $pdir/$fname $user@$host:$pdir/$fname |
命令 选项参数 要拷贝的文件路径/名称 目的用户@主机:目的路径/名称
rsync案例
10.1、三台虚拟机安装rsync (系统默认已经安装了)
| 01 | yum install rsync -y |
10.2、期望脚本
脚本+要同步的文件名称
10.3. 说明:在/usr/local/bin这个目录下存放的脚本,root用户可以在系统任何地方直接执行。
10.4. 脚本实现
| 0102 | touch rsync-scriptvim rsync-script |
脚本内容如下:
| 010203040506070809101112131415161718192021222324252627 | #!/bin/bash#1 获取命令输入参数的个数,如果个数为0,直接退出命令paramnum=$# if((paramnum==0)); then echo no params; exit;fi #2 根据传入参数获取文件名称p1=$1file_name=`basename $p1`echo fname=$file_name #3 获取输入参数的绝对路径pdir=`cd -P $(dirname $p1); pwd`echo pdir=$pdir #4 获取用户名称user=`whoami` #5 循环执行rsynchost_arr=("dev-spark-master-206" "dev-spark-slave-171" "dev-spark-slave-172")for host in ${host_arr[@]}; do echo ------------------- $host -------------- rsync -rvl $pdir/$file_name $user@$host:$pdirdone |
赋予权限:
| 0102 | chmod 777 rsync-scriptcp rsync-script /usr/local/sbin/ |
测试:
| 010203040506070809101112131415161718192021 | touch test# rsync-script test fname=testpdir=/data/apps/shell------------------- dev-spark-master-206 --------------sending incremental file list sent 40 bytes received 12 bytes 104.00 bytes/sectotal size is 0 speedup is 0.00------------------- dev-spark-slave-171 --------------sending incremental file listtest sent 83 bytes received 35 bytes 236.00 bytes/sectotal size is 0 speedup is 0.00------------------- dev-spark-slave-172 --------------sending incremental file listtest sent 83 bytes received 35 bytes 78.67 bytes/sectotal size is 0 speedup is 0.00 |