Linux部署集群准备
介绍
在前面,我们所学习安装的软件,都是以单机模式运行的。
后续,我们将要学习大数据相关的软件部署,所以后续我们所安装的软件服务,大多数都是以集群化(多台服务器共同工作)模式运行的。
所以,在当前小节,我们需要完成集群化环境的前置准备,包括创建多台虚拟机,配置主机名映射,SSH免密登录等等。
部署
配置多台Linux虚拟机
安装集群化软件,首要条件就是要有多台Linux服务器可用。
我们可以使用VMware提供的克隆功能,将我们的虚拟机额外克隆出3台来使用。
-
首先,关机当前CentOS系统虚拟机(可以使用root用户执行
init 0
来快速关机) -
新建文件夹
文件夹起名为:
虚拟机集群
-
克隆
-
同样的操作克隆出:node2和node3
-
开启node1,修改主机名为node1,并修改固定IP为:
192.168.149.131
# 修改主机名 hostnamectl set-hostname node1# 修改IP地址 vim /etc/sysconfig/network-scripts/ifcfg-ens33 IPADDR="192.168.149.131"# 重启网卡 systemctl stop network systemctl start network # 或者直接 systemctl restart network
-
同样的操作启动node2和node3,
修改node2主机名为node2,设置IP为
192.168.149.132
修改node2主机名为node3,设置IP为
192.168.149.133
-
配置
Xshell
,配置连接到node1、node2、node3的连接为了简单起见,建议配置root用户登录
准备主机名映射
-
在Windows系统中修改hosts文件,填入如下内容:
如果使用MacOS系统,请:
sudo su -
,切换到root- 修改
/etc/hosts
文件
192.168.149.131 node1 192.168.149.132 node2 192.168.149.133 node3
-
在3台Linux的/etc/hosts文件中,填入如下内容(3台都要添加)
192.168.149.131 node1 192.168.149.132 node2 192.168.149.133 node3
配置SSH免密登录
简介
SSH服务是一种用于远程登录的安全认证协议。
使用Xshell
远程连接到Linux,就是使用的SSH服务。
SSH服务支持:
- 通过账户+密码的认证方式来做用户认证
- 通过账户+秘钥文件的方式做用户认证
SSH可以让我们通过SSH命令,远程的登陆到其它的主机上,比如:
在node1执行:ssh root@node2
,将以root用户登录node2服务器,输入密码即可成功登陆
或者ssh node2
,将以当前用户直接登陆到node2服务器。
SSH免密配置
后续安装的集群化软件,多数需要远程登录以及远程执行命令,我们可以简单起见,配置三台Linux服务器之间的免密码互相SSH登陆
-
在每一台机器都执行:
ssh-keygen -t rsa -b 4096
,一路回车到底即可示例输出
[root@node1 ~]# ssh-keygen -t rsa -b 4096 Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:aLNaRbRGpAagFBjBeX6K1y3ceZRnHZrvik6h20Wsuj0 root@node1 The key's randomart image is: +---[RSA 4096]----+ |==+.. .+ | |o+ . . + . . | |. o o + . + . | | . o + o.= . | | . = * S.oo. | | . o = B..+ . | | . +..o .. | | o *Eo . | | . +o=... | +----[SHA256]-----+ [root@node1 ~]#
-
在每一台机器都执行:
ssh-copy-id node1 ssh-copy-id node2 ssh-copy-id node3
示例输出
[root@node1 ~]# ssh-copy-id node1 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host 'node1 (192.168.149.131)' can't be established. ECDSA key fingerprint is SHA256:UJULWpe9PpdjrmEBO/1SQMmmfkvmuDBVZ2QCGz+8Il0. ECDSA key fingerprint is MD5:82:4f:8d:bd:f5:ca:8d:ba:99:dd:65:08:2a:a5:50:92. Are you sure you want to continue connecting (yes/no)? /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed The authenticity of host 'node1 (192.168.149.131)' can't be established. ECDSA key fingerprint is SHA256:UJULWpe9PpdjrmEBO/1SQMmmfkvmuDBVZ2QCGz+8Il0. ECDSA key fingerprint is MD5:82:4f:8d:bd:f5:ca:8d:ba:99:dd:65:08:2a:a5:50:92. Are you sure you want to continue connecting (yes/no)? /usr/bin/ssh-copy-id: ERROR: Host key verification failed.[root@node1 ~]# ssh-copy-id node1 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host 'node1 (192.168.149.131)' can't be established. ECDSA key fingerprint is SHA256:UJULWpe9PpdjrmEBO/1SQMmmfkvmuDBVZ2QCGz+8Il0. ECDSA key fingerprint is MD5:82:4f:8d:bd:f5:ca:8d:ba:99:dd:65:08:2a:a5:50:92. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@node1's password: Permission denied, please try again. root@node1's password: Number of key(s) added: 1Now try logging into the machine, with: "ssh 'node1'" and check to make sure that only the key(s) you wanted were added.[root@node1 ~]# ssh-copy-id node2 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@node2's password: Number of key(s) added: 1Now try logging into the machine, with: "ssh 'node2'" and check to make sure that only the key(s) you wanted were added.[root@node1 ~]# ssh-copy-id node3 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host 'node3 (192.168.149.133)' can't be established. ECDSA key fingerprint is SHA256:UJULWpe9PpdjrmEBO/1SQMmmfkvmuDBVZ2QCGz+8Il0. ECDSA key fingerprint is MD5:82:4f:8d:bd:f5:ca:8d:ba:99:dd:65:08:2a:a5:50:92. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@node3's password: Number of key(s) added: 1Now try logging into the machine, with: "ssh 'node3'" and check to make sure that only the key(s) you wanted were added.
-
执行完毕后,node1、node2、node3之间将完成root用户之间的免密互通
[root@node1 ~]# ssh node3 Last login: Wed Feb 28 12:10:09 2024 from 192.168.149.1 [root@node3 ~]# exit 登出 Connection to node3 closed. [root@node1 ~]# ssh node2 Last login: Wed Feb 28 12:18:11 2024 from node1 [root@node2 ~]# exit 登出 Connection to node2 closed. [root@node1 ~]#
配置JDK环境
后续的大数据集群软件,多数是需要Java运行环境的,所以我们为每一台机器都配置JDK环境。
JDK配置参阅:Tomcat
安装部署环节。
参考文章
06-Linux部署Tomcat:http://t.csdnimg.cn/VmRq6
关闭防火墙和SELinux
集群化软件之间需要通过端口互相通讯,为了避免出现网络不通的问题,我们可以简单的在集群内部关闭防火墙。
在每一台机器都执行
systemctl stop firewalld
systemctl disable firewalld
Linux有一个安全模块:SELinux
,用以限制用户和程序的相关权限,来确保系统的安全稳定。
SELinux
的配置同防火墙一样,非常复杂,课程中不多涉及,后续视情况可以出一章SELinux
的配置课程。
在当前,我们只需要关闭SELinux
功能,避免导致后面的软件运行出现问题即可,
在每一台机器都执行
vim /etc/sysconfig/selinux# 将第7行,SELINUX=enforcing 改为
SELINUX=disabled
# 保存退出后,重启虚拟机即可,千万要注意disabled单词不要写错,不然无法启动系统
添加快照
为了避免后续出现问题,在完成上述设置后关机(只有关机后的快照才能被克隆,不关机设置的快照只能还原),为每一台虚拟机都制作快照,留待使用。
补充命令 - scp
后续的安装部署操作,我们将会频繁的在多台服务器之间相互传输数据。
为了更加方面的互相传输,我们补充一个命令:scp
scp命令是cp命令的升级版,即:ssh cp,通过SSH协议完成文件的复制。
其主要的功能就是:在不同的Linux服务器之间,通过SSH
协议互相传输文件。
只要知晓服务器的账户和密码(或密钥),即可通过SCP互传文件。
语法:
scp [-r] 参数1 参数2
- -r选项用于复制文件夹使用,如果复制文件夹,必须使用-r
- 参数1:本机路径 或 远程目标路径
- 参数2:远程目标路径 或 本机路径
示例
- 将本机上的
jdk
文件夹, 以root的身份复制到node2的/export/server/
内,同SSH登陆一样,账户名可以省略(使用本机当前的同名账户登陆)
scp -r /export/server/jdk root@node2:/export/server/
- 将远程node2的
jdk
文件夹,复制到本机的/export/server/
内
scp -r node2:/export/server/jdk /export/server/
- scp命令的高级用法
cd /export/server
# 将本机当前路径的jdk文件夹,复制到node2服务器的同名路径下
scp -r jdk node2:`pwd`/# 将本机当前路径的jdk文件夹,复制到node2服务器的同名路径下
scp -r jdk node2:$PWD