跳转至

01-实验环境准备-centos7.x

硬件设备准备

电脑一台,内存>=8G,可用磁盘空间大于 80G。

安装 VMware Workstation Pro 虚拟机软件,用于创建虚拟机。

创建两台虚拟机,安装 CentOS-7.x-x86_64 系统。

实验环境详情

主机名称 IP地址(NAT) 描述
linux-node1 eth0: 192.168.56.11 1VCPU,2G内存,一块系统盘sda50G(动态)
linux-node2 eth0: 192.168.56.12 1VCPU,2G内存,一块系统盘sda50G(动态)
linux-node3 eth0: 192.168.56.13 1VCPU,2G内存,一块系统盘sda50G(动态)

备注: 安装的时候将网卡命名为 eth0,eth1格式

虚拟机环境准备

安装操作系统 CentOS-7-x86_64。

l基本系统: 1VCPU+2048M 内存+50G(动态)硬盘。

  1. 网络选择:使用网络地址转换( NAT)。

  2. 软件包选择: Minimal Install。

  3. 关闭 iptables 和 SELinux。

设置所有节点的主机名和 IP 地址, 使用/etc/hosts 做好主机名解析。

基础模板环境制作

创建虚拟机: 请按要求创建虚拟机。 操作系统安装: 为了统一环境,保证实验的通用性,建议将网卡名称设置为 eth*,不使用 CentOS7 默 认的网卡命名规则。所以需要在安装的时候,增加内核参数。

image-20201009093212406

点击 Tab,打开 kernel 启动选项,增加 net.ifnames=0 biosdevname=0 回车即可安装

image-20201009093234686

主机名称解析

[root@localhost ~]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.56.11 linux-node1 linux-node1.example.com
192.168.56.12 linux-node2 linux-node2.example.com
192.168.56.13 linux-node3 linux-node3.example.com

DNS域名解析配置

[root@localhost ~]# vi /etc/resolv.conf
nameserver 114.114.114.114

YUM仓库配置

安装阿里云的base源与EPEL源 仓库和常用命令

rm -f /etc/yum.repos.d/*.repo
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum repolist

安装腾讯云base源与EPEL源 仓库和常用命令

rm -f /etc/yum.repos.d/*.repo
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos7_base.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.cloud.tencent.com/repo/epel-7.repo
yum clean all
yum repolist

安装常用软件包

安装centos常用软件包(可选配置)

yum install -y net-tools vim lrzsz tree screen lsof tcpdump bash-completion bash-completion-extras wget ntp

关闭NetworkManager

systemctl disable NetworkManager && systemctl stop NetworkManager

关闭防火墙

systemctl disable firewalld && systemctl stop firewalld

关闭SELinux

关闭并确认 SELinux 处于关闭状态

sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config && grep 'SELINUX=disabled' /etc/selinux/config 
setenforce 0
getenforce

配置SSH服务

sed -i 's%#UseDNS yes%UseDNS no%g' /etc/ssh/sshd_config
systemctl restart sshd

配置NTP服务

安装chrony

yum -y install chrony ntp

编辑chrony配置文件

[root@localhost ~]# vim /etc/chrony.conf
server ntp1.aliyun.com iburst

启动chrony服务

systemctl enable chronyd.service
systemctl start chronyd.service

验证ntp服务客户端是否同步时间正常

[root@localhost ~]# ntpstat 
synchronised to NTP server (120.25.115.20) at stratum 3
   time correct to within 49 ms
   polling server every 64 s

调整文件描述符

检查当前 root 账号下的 max open files 值

ulimit -n

如果为默认的 1024,建议通过修改配置文件调整为 102400 或更大。

注意: limits.conf 初始文件的备份

cat >>/etc/security/limits.conf<< EOF
root soft nofile 102400
root hard nofile 102400
EOF

修改后,重新使用 root 登录检查是否生效

ulimit -n

历史修改的方法

ulimit -n 102400

PIP国内源(可选)

在线安装时,依赖 pip,需要配置可用的 pip 源

腾讯源

mkdir -p ~/.pip/
cat > ~/.pip/pip.conf<<EOF

[global]
index-url = https://mirrors.cloud.tencent.com/pypi/simple
trusted-host = mirrors.cloud.tencent.com
EOF

清华源

mkdir -p ~/.pip/
cat > ~/.pip/pip.conf<<EOF
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple

[install]
trusted-host=pypi.tuna.tsinghua.edu.cn
EOF

设置为能连上的 pip 源,以腾讯云镜像源加速 pip 为例。默认的 pip 源配置通常无法使用,验证方式

如下在每台机器上对 pip.conf 中配置的 url 进行操作:curl http://xxxxxxx,若能正常返回列表信息则为成功。

关于pip升级问题(目前centos默认的pip版本与最新的pip版本跨度太大,无法正常升级,可以通过升级到制定版本后再升级到最新版本)

yum -y install python-pip
pip install --upgrade pip==10.0.1
pip install --upgrade pip
pip -V

配置主机名称

[root@localhost ~]# hostname linux-node-moban
[root@localhost ~]# echo "linux-node-moban" >/etc/hostname

中文乱码问题

需要修改locale.conf配置文件

# vim /etc/locale.conf
LANG="en_US.UTF-8"
# source   /etc/locale.conf

清理模板虚拟机

> /var/log/messages
> /var/log/secure
history -c
> /root/.bash_history
poweroff

清理完毕模板虚拟机后,将模板虚拟机关机即可

挂载数据盘(可选)

mkfs.xfs -f /dev/sdb
mkdir -p /data
mount /dev/sdb /data/
 echo "/dev/sdb                                  /data                   xfs     defaults        0 0" >>/etc/fstab ; cat /etc/fstab |grep data

docker镜像源配置(可选)

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://1v0q5mvy.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo systemctl status docker

克隆虚拟机

将模板虚拟机关机,基于以上的虚拟机,开始克隆三台虚拟机

主机名称 IP地址 机器配置
linux-node1 192.168.56.11/24 1c2g 50GB系统盘(有条件的可以2c4g)
linux-node2 192.168.56.12/24 1c2g 50GB系统盘(有条件的可以2c4g)
linux-node3 192.168.56.13/24 1c2g 50GB系统盘(有条件的可以2c4g)

备注:以上机器配置可根据个人电脑实际配置灵活调整即可,最小可调整为1C2G 50GB系统盘

linux-node1虚拟机配置

配置主机名称

hostname linux-node1
echo "linux-node1" > /etc/hostname
检查验证:
[root@linux-node1 ~]# hostname
linux-node1

配置IP地址

cat >/etc/sysconfig/network-scripts/ifcfg-eth0<<EOF
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.56.11
NETMASK=255.255.255.0
GATEWAY=192.168.56.2
EOF
systemctl restart network

linux-node2虚拟机配置

配置主机名称

hostname linux-node2
echo "linux-node2" > /etc/hostname
检查验证:
[root@linux-node2 ~]# hostname
linux-node2

配置IP地址

cat >/etc/sysconfig/network-scripts/ifcfg-eth0<<EOF
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.56.12
NETMASK=255.255.255.0
GATEWAY=192.168.56.2
EOF
systemctl restart network

linux-node3虚拟机配置

配置主机名称

hostname linux-node3
echo "linux-node3" > /etc/hostname
检查验证:
[root@linux-node3 ~]# hostname
linux-node3

配置IP地址

cat >/etc/sysconfig/network-scripts/ifcfg-eth0<<EOF
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.56.13
NETMASK=255.255.255.0
GATEWAY=192.168.56.2
EOF
systemctl restart network

虚拟机快照

给虚拟机做快照(system_init) 好的,现在是给刚安装的虚拟机做快照的时候了,以便于随时回到一个刚初始化完毕的 系统中,有效的减少,我们在学习过程中的环境准备的时间。 同时,请确保实验环境的一致 性,便于顺利的完成本部分的所有实验。

配置eth网卡名称(可选)

安装完系统以后修改

1.编辑网卡信息

[root@linux-node2~]# cd /etc/sysconfig/network-scripts/  #进入网卡目录
[root@linux-node2network-scripts]# mv ifcfg-eno16777728 ifcfg-eth0  #重命名网卡名称
[root@linux-node2 network-scripts]# cat ifcfg-eth0  #编辑网卡信息
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
NAME=eth0  #name修改为eth0
ONBOOT=yes
IPADDR=192.168.56.12
NETMASK=255.255.255.0
GATEWAY=192.168.56.2
DNS1=192.168.56.2

2.修改grub

[root@linux-node2 ~]# vim /etc/sysconfig/grub  #编辑内核信息,添加红色字段的
GRUB_TIMEOUT=5
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto rhgb net.ifnames=0 biosdevname=0 quiet"
GRUB_DISABLE_RECOVERY="true"
[root@linux-node2 ~]# grub2-mkconfig -o /boot/grub2/grub.cfg  #生成启动菜单
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-229.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-229.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-1100f7e6c97d4afaad2e396403ba7f61
Found initrd image: /boot/initramfs-0-rescue-1100f7e6c97d4afaad2e396403ba7f61.img
Done

3.验证是否修改成功

[root@linux-node2 ~]# reboot  #必须重启系统生效
[root@linux-node2 ~]# yum install net-tools  #默认centos7不支持ifconfig 需要看装net-tools包
[root@linux-node2 ~]# ifconfig eth0  #在次查看网卡信息
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.56.12  netmask 255.255.255.0  broadcast 192.168.56.255
        inet6 fe80::20c:29ff:fe5c:7bb1  prefixlen 64 scopeid 0x20<link>
        ether 00:0c:29:5c:7b:b1  txqueuelen 1000  (Ethernet)
        RX packets 152  bytes 14503 (14.1 KiB)
        RX errors 0  dropped 0 overruns 0  frame 0
        TX packets 98  bytes 14402 (14.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0 collisions 0

镜像制作原则(可选)

给虚拟机做快照 好的,现在是给刚安装的虚拟机做快照的时候了,以便于随时回到一个刚初始化完毕的 系统中,有效的减少,我们在学习过程中的环境准备的时间。 同时,请确保实验环境的一致 性,便于顺利的完成本部分的所有实验。

镜像制作原则:

分区的时候,只分一个/根分区,并不需要swap分区,由于虚拟机的磁盘性能就不好,如果设置了swap分区,当swap工作的时候,性能会更差。例如阿里云主机,就没有交换分区。

关闭selinux,关闭iptables
安装基础软件的包:net-tools lrzsz screen tree vim wget

镜像制作注意:

(1)镜像制作需要删除网卡(eth0)中的UUID,如果有udev(/etc/udev/rules.d/70-persistent-ipoib.rules)的规则也要删除

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
UUID=d027ee0c-bc78-457b-b983-e94cf65a863e
HWADDR=52:54:00:15:12:b2
[root@localhost ~]# > /etc/udev/rules.d/70-persistent-net.rules