期末架构-自动部署系统

本节PPT链接

我们之前安装系统都是一台一台的装

现在我们学习并行安装---网络安装

使用KickStart 项目的软件cobbler(网页版的kickstart)软件安装(见ppt图片)

 

PXE-Kickstart的运行原理

PXE,是指预启动执行环境,解决了网络启动

部署之前确定一下环境是否设置好了,是否和教程中一致

kickstart的原理(白海明讲)

客户机内存至少2个G

客户端想要装系统,他的电脑必须要支持网络启动功能,要支持这个功能必须要有一个网卡,

网卡有一个功能PXE,具体来说这是一个芯片,这个芯片的作用就是在主板加电以后,网卡也会跟着加电,加点以后,这个芯片就会向网络发送4个dhcp包,来获得IP

...

 

 

克隆之前,模板机的操作

第一个操作(一关闭)

要关闭NetworkManager,否则会报错

systemctl  stop  NetworkManager

systemctl  disabled  NetworkManager

# 然后再ifup  eth0 #  这个命令不用

[root@Luffy-pizza ~]# systemctl is-active NetworkManager
inactive
[root@Luffy-pizza ~]# systemctl is-enabled NetworkManager
disabled

 

第二个操作(一删除)

之前在使用6的时候我们讲过要一清两删

7不用这么麻烦,只用删除/etc/sysconfig/network-scripts/ifcfg-eth0和eth1 里面的UUID 就可以了

第三个操作(配置yum源)

配置yum源,在下载系统的网站中

https://opsx.alibaba.com/mirror在centos列后面可以找到帮助

点击帮助,复制命令即可

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

修改默认的 yum源

如果没法上网,从以下方法依次排查

   78  ping www.baidu.com     ping 外网
   79  ping 223.5.5.5    ping 设置的DNS 看DNS对不对
   80  route -n             查看路由
   81  ping 10.0.0.254   ping网关

 

还要修改epel源,依赖

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

使用命令yum makecache 在服务器上生成缓存

注意:如果阿里云的不好使,一般使用清华的,

yum repolist 查看yum源

第四个操作

确认做完了,关机halt ,做快照

部署其他服务,部署kickstart服务器

根据PXE-KickStart 原理详细见PPT中本节PPT链接

克隆做好的模板机一台,修改主机名

[root@hah ~]# hostnamectl set-hostname kickstart
[root@hah ~]# cat /etc/hostname 
kickstart

 

安装第一个软件DHCP(配合PPT)

dhcp是给我们的客户端分发IP地址的,

首先关掉虚拟机的dhcp

[root@kickstart ~]# rpm -qa dhcp
dhcp-4.2.5-68.el7.centos.1.x86_64
[root@kickstart ~]# rpm -ql dhcp
..

 

/etc/dhcp/dhcpd.conf  就是需要配置的配置文件

[root@kickstart ~]# cat /etc/dhcp/dhcpd.conf 
#
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.example
#   see dhcpd.conf(5) man page
#
[root@kickstart ~]# 告诉我们这个文件是空的,我们去 一个文件中找例子

 

PPT中写好了,详细的解释看PPT

cat >>/etc/dhcp/dhcpd.conf<<EOF
subnet 172.16.1.0 netmask 255.255.255.0 {
range 172.16.1.100 172.16.1.199;
option subnet-mask 255.255.255.0;
default-lease-time 21600;
max-lease-time 43200;
next-server 172.16.1.201;
filename "/pxelinux.0";
}
EOF

 

上面地址从100-199,有100个地址,但是实际我们只能用一半(在第1和第5步都会消耗一个IP)

但是有几百,几千台服务器,我们怎么来部署系统,一般是,利用交换机分批次的安装

最好不要让dhcp开机自启动,我们可不希望一开机就装系统

[root@kickstart ~]# systemctl start dhcpd.service 
[root@kickstart ~]# systemctl is-enable dhcpd.service 
Unknown operation 'is-enable'.
[root@kickstart ~]# systemctl is-enabled dhcpd.service 
disabled

 

开一个窗口查看日志

[root@kickstart ~]# tail /var/log/messages -f
Mar 14 03:50:44 Luffy-pizza dhcpd: Listening on LPF/eth1/00:0c:29:95:f5:98/172.16.1.0/24
Mar 14 03:50:44 Luffy-pizza dhcpd: Sending on   LPF/eth1/00:0c:29:95:f5:98/172.16.1.0/24
已经开始监听了

 

启动了之后,再克隆一台虚拟机,记得添加一个网卡,并且在一个LAN区段

确保内存、硬盘、LAN区段设置好,启动虚拟机

启动后,首先‘扫描eth0’,没有等到,然后就关闭了,然后在eth1等到了,

分配了IP172.16.1.100 从DHCP172.16.1.201

最后因为TFTP没有配置,提示超时(在PPT中也有讲解)

查看我们的日志tailf /var/log/messages

 

Mar 14 03:58:31 Luffy-pizza dhcpd: DHCPDISCOVER from 00:0c:29:94:c5:9b via eth1
# DHCP 发现了 
Mar 14 03:58:32 Luffy-pizza dhcpd: DHCPOFFER on 172.16.1.100 to 00:0c:29:94:c5:9b via eth1
# 发IP
Mar 14 03:58:33 Luffy-pizza dhcpd: DHCPREQUEST for 172.16.1.100 (172.16.1.201) from 00:0c:29:94:c5:9b via eth1
# 客户端说收到了
Mar 14 03:58:33 Luffy-pizza dhcpd: DHCPACK on 172.16.1.100 to 00:0c:29:94:c5:9b via eth1
Mar 14 04:01:01 Luffy-pizza systemd: Started Session 5 of user root.
Mar 14 04:01:01 Luffy-pizza systemd: Starting Session 5 of user root.

 

 

抓包查看DHCP与next server

dhcp服务完成之后,客户端有了IP,我们怎么知道客户端向谁要对应的配置,和对应的文件?

一个是通过日志,一个是通过抓包来看。我们常用的抓包软件一个叫wireshark

但是传输的数据在LAN区段中,通过vm8是抓不到的,需要在LINUX下安装 wireshark

yum install -y wireshark
确认有两个网卡,并且可以ping 通 ping 172.16.1.61

默认监听的是eth0,加上参数修改

tshark  -ni  eth1      -n的作用是禁止将端口解析成服务名,回车,就开始监听LAN区段了

重启新建的要装系统的客户端虚拟机,就可以看到日志

 

配置TFTP服务

TFTP服务的作用是给客户端下载启动文件,PXE的镜像和软件

安装

[root@kickstart ~]# yum -y install tftp-server

 

启动

[root@kickstart ~]# systemctl start tftp.socket 

 

这个时候在查看日志或者看客户端启动过程,提示的是找不到文件

我们切换到TFTP服务的根目录下

[root@kickstart ~]# cd /var/lib/tftpboot/
[root@kickstart tftpboot]# ll
total 0
[root@kickstart tftpboot]# 

 

装完软件syslinux就有了

[root@kickstart tftpboot]# yum install syslinux -y

 

找一下prelinux.0文件的位置

[root@kickstart tftpboot]# rpm -ql syslinux |grep pxelinux.0
/usr/share/syslinux/gpxelinux.0
/usr/share/syslinux/pxelinux.0

 

将文件复制到/var/lib/tftpboot/

[root@kickstart tftpboot]# cp /usr/share/syslinux/pxelinux.0  /var/lib/tftpboot/

 

继续重复上面的重启客户端的操作,出现了新的问题,即ppt中(那些年我们才过的坑(3))

因为缺少pxelinux.0这个小系统的配置文件(default文件决定了安装界面是什么样子),这个文件在系统安装盘中可以找到

解决方案见PPT

下图表示出了tftboot文件下的文件都是来自哪里

 

apache安装?

ppt里面叫安装HTTP服务

见PPT,作用也在ppt中标明

OK!可以装系统了

PXE-kickstart部署原理重现-ks配置文件详解

1、新建虚拟机的内存至少2个G,否则会报错(磁盘空间不足),客户机启动之后,添加了两块网卡,其中一块是LAN区段的,和我们的DHCP服务器在一个局域网,客户机就发UDP广播请求IP地址,

2、DHCP服务器在分配IP的同时,给了客户机一个next server 告诉他去tftp服务下载引导系统启动的文件(如pxelinux.0),会经常改的文件叫pxelinux.cfg/default,配置我们安装系统的样式,

[root@kickstart html]# vim /var/lib/tftpboot/pxelinux.cfg/default 

 

 

3、选择完成之后,下载好了文件,那么安装系统的软件包在哪里找呢?在http服务找软件包

在http服务下载自动应答文件(就不用手动选择语言和分区等操作)

注:

1)、因为我们没有修改内核的参数,所以新装的系统的IP网卡的名字是ens34.即自动生成的

通过在default配置文件中添加参数可以修改网卡名

append initrd=initrd.img inst.repo=http://172.16.1.201/CentOS7/ net.ifnames=0 biosdevname=0

 

2)、没有IP地址,因为DHCP服务没有开启

[root@kickstart html]# systemctl start dhcpd.service 
[root@kickstart html]# systemctl start tftp.socket 
[root@kickstart html]# systemctl start httpd.service

 

 

如何自动部署(编写KS文件)

即自动应答文件,装系统的过程都在这个文件里面放着呢

见PPT中3中不同的编辑ks文件的方法

系统默认的ks文件

 

[root@kickstart ~]# cat anaconda-ks.cfg 
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Use graphical install
graphical
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=sda
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8

# Network information
network  --bootproto=static --device=eth0 --gateway=10.0.0.254 --ip=10.0.0.201 --nameserver=223.5.5.5 --netmask=255.255.255.0 --ipv6=auto --activate
network  --bootproto=static --device=eth1 --ip=172.16.1.0 --netmask=255.255.255.0 --ipv6=auto --activate
network  --hostname=Luffy-pizza

# Root password
rootpw --iscrypted $6$g.4ZvAHfTTcmO4Pj$LV0z.V8qYH0DP89NxCJS2SejOfcSvQZDq.TuFT6SsffDVsp7Z.tyDQEYXRSt3KGcsxzqvjoLtVNfLLL/9lxQE0
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc --nontp
user --groups=wheel --name=pizza --password=$6$Zm0fYVfS3AY3MIvL$tvzUY8g2Vx883oFL9lL35LHvvkdlrFh4kxRXht6FbJ9BTjJD846SFD7AGCZt/POvzMKmPOOWQs8BCtQUkx8Ri/ --iscrypted --gecos="pizza"
# System bootloader configuration
bootloader --location=mbr --boot-drive=sda
# Partition clearing information
clearpart --none --initlabel
# Disk partitioning information
part swap --fstype="swap" --ondisk=sda --size=1024
part /boot --fstype="xfs" --ondisk=sda --size=1024
part / --fstype="xfs" --ondisk=sda --size=100351

%packages
@^minimal
@compat-libraries
@core
@debugging
@development

%end

%addon com_redhat_kdump --disable --reserve-mb='auto'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
[root@kickstart ~]# 

 

白海明手动配置好的ks文件

#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
# old format: keyboard us
# new format:
keyboard --vckeymap=us --xlayouts='us'
# Root password
rootpw --iscrypted $1$0ZMwWP.q$wjRkxcadGHkUBDxxzVUYK.
# Use network installation
url --url="http://192.168.10.42/dvd"
# System language
lang en_US
repo --name="Red Hat Enterprise Linux" --baseurl=http://192.168.10.42/dvd --cost=100
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use text mode install
text
firstboot --disable
# SELinux configuration
selinux --disabled

# System services
services --enabled="chronyd"
ignoredisk --only-use=sda
# Firewall configuration
firewall --disabled
# Network information
network  --bootproto=dhcp --device=ens33
# Reboot after installation
reboot
# System timezone
timezone America/New_York
# System bootloader configuration
bootloader --append="crashkernel=auto" --location=mbr --boot-drive=sda
#autopart --type=lvm
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="xfs" --size=500
part swap --fstype="swap" --size=2048
part / --fstype="xfs" --grow --size=1

%packages
@core
%end

 

李导配置好的

# Kickstart Configurator for CentOS 7 by yao zhang
install
url --url="http://172.16.1.201/CentOS7/"
text
lang en_US.UTF-8
keyboard us
zerombr # 把mbr清空
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
network  --bootproto=static --device=eth0 --gateway=10.0.0.254 --ip=10.0.0.202 --nameserver=223.5.5.5 --netmask=255.255.255.0 --activate
network  --bootproto=static --device=eth1 --ip=172.16.1.202 --netmask=255.255.255.0 --activate
network  --hostname=Cobbler
#network --bootproto=dhcp --device=eth1 --onboot=yes --noipv6 --hostname=CentOS7
timezone --utc Asia/Shanghai
authconfig --enableshadow --passalgo=sha512
rootpw  --iscrypted $6$X20eRtuZhkHznTb4$dK0BJByOSAWSDD8jccLVFz0CscijS9ldMWwpoCw/ZEjYw2BTQYGWlgKsn945fFTjRC658UXjuocwJbAjVI5D6/
#rootpw 123456 明文的密码
clearpart --all --initlabel # 清空分区
part /boot --fstype xfs --size 1024
part swap --size 1024
part / --fstype xfs --size 1 --grow
firstboot --disable
selinux --disabled
firewall --disabled
logging --level=info
reboot

%packages
@^minimal # @软件包的名字
@compat-libraries
@debugging
@development
tree
nmap
sysstat
lrzsz
dos2unix
telnet
wget
vim
bash-completion
%end

%post
systemctl disable postfix.service # 脚本 和ks文件放在一起
%end

查找帮助文件,去官网http://access.redhat.com/documenttion/en-us/red.......有中文

生成暗文密码的命令在PPT中

 

default文件决定了怎么安装系统,去哪里找系统,暂时了解这几行就可以了,就这几行起作用

# 注释...说明一下centos7 kickstart configure
default ks
timeout 50 prompt 0 label ks kernel vmlinuz append initrd
=initrd.img ks=http://172.16.1.201/ks_config/CentOS7-ks.cfg net.ifnames=0 biosdevname=0 ksdevice=eth1

 

注:ksdevice指定网卡
将default保存
在服务器创建目录并上传ks文件,或者是编写
脚本也放在这个目录下

做完之后,试一下能不能访问到default中的ks文件的目录
[root@kickstart ks_config]# curl http://172.16.1.201/ks_config/CentOS7-ks.cfg

 

试一下能不能下载软件
[root@kickstart ks_config]# curl http://172.16.1.201/CentOS7/

 

可以欢快的装系统了!!
注:kickstart重启后没有将光盘挂载放在启动任务中,开机需要再次挂载,才能正常安装系统

Cobbler安装系统(kickstart的升级版)

 把主机名改为cobbler

hosts文件添加10.0.0.202   cobbler

修改网卡(0和1),改为静态的

因为我的配置文件中是已经设置成了静态的,所以不用修改,但是无法联网

所有的设置都是正确的,直到我先重启cobbler,在关闭kickstart后,可以上网了!

在开启kickstart后,kickstart没有收到影响!

---------------------------------------------------------------------------------------------------------------

有一个坑,使用Centos6部署cobbler的时候,一直安装不上Django14.需要单独下载文件安装

在centos7中不存在这个问题!

--------------------------------------------------------------------------------------------------------------------

开始安装cobbler

# cobbler在epel源中,跟新epel源,将默认的yum源改为epel
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 -y install cobbler cobbler-web dhcp tftp-server pykickstart httpd python-django
# 帮助文档在官网manuals中
# 启动cobbler systemctl start cobblerd.service
# 启动http systemctl start httpd.service # 检查 cobbler check
# 按照提示一个一个修正。里边大部分都可以解决
[root@Cobbler ~]# cobbler check
The following are potential configuration items that you may want to fix:

1 : The 'server' field in /etc/cobbler/settings must be set to something other than localhost, or kickstarting features will not work.  This should be a resolvable hostname or IP for the boot server as reachable by all machines that will use it.
server后面不能写127.0.0.1,写本地IP地址或者主机名
2 : For PXE to be functional, the 'next_server' field in /etc/cobbler/settings must be set to something other than 127.0.0.1, and should match the IP of the boot server on the PXE network. next_server不能是172.0.0.1,改成和上面一样的
3 : change 'disable' to 'no' in /etc/xinetd.d/tftp
将默认关闭的yes改成no
4 : Some network boot-loaders are missing from /var/lib/cobbler/loaders, you may run 'cobbler get-loaders' to download them, or, if you only want to handle x86/x86_64 netbooting, you may ensure that you have installed a *recent* version of the syslinux package installed and can ignore this message entirely. Files in this directory, should you want to support all architectures, should include pxelinux.0, menu.c32, elilo.efi, and yaboot. The 'cobbler get-loaders' command is the easiest way to resolve these requirements. 下载引导
5 : enable and start rsyncd.service with systemctl
开启rsrsyncd.service并设置开机自启动
6 : debmirror package is not installed, it will be required to manage debian deployments and repositories 7 : The default password used by the sample templates for newly installed machines (default_password_crypted in /etc/cobbler/settings) is still set to 'cobbler' and should be changed, try: "openssl passwd -1 -salt 'random-phrase-here' 'your-password-here'" to generate new one 默认的密码cobbler,并提供了修改命令openssl passwd -1
8 : fencing tools were not found, and are required to use the (optional) power management features. install cman or fence-agents to use them fencing tools 工具 和脑裂有关

Restart cobblerd
and then run 'cobbler sync' to apply changes.

cobbler是可以自己去管理dhcp服务的,先配置一下他们,开两个窗口,一个检查,一个配置

 

主要配置文件的位置 /etc/cobbler/settings  
默认配置文件修改 /etc/cobbler/dhcp.template

防止误重装,防止不停的重装 sed -i 's/pxe_just_once: 0/pxe_just_once: 1/' /etc/cobbler/settings 配置Cobbler统一管理DHCP 将0改为1 sed -i 's/manage_dhcp: 0/manage_dhcp: 1/' /etc/cobbler/settings 配置DHCP Cobbler模版,修改IP地址,并删除22和23两行 sed -i.ori 's#192.168.1#172.16.1#g;22d;23d' /etc/cobbler/dhcp.template
# 解决了上面的大部分问题。只剩下两个,不用解决也行,根据提示运行命名
# 启动服务,同步配置 systemctl start rsyncd systemctl enable rsyncd systemctl enable tftp.socket systemctl start tftp.socket systemctl restart cobblerd.service cobbler sync # 修改完后 就启动了 # web操作cobbler https://10.0.0.202/cobbler_web 账号密码默认均为cobbler --实际中已经修改成了123456 接下来就在网页上操作,enjoy!

 

 

并没有访问成功,在网上找到解决方案 https://blog.51cto.com/12643266/2339793 跟新django版本

仍是使用的密码cobbler

使用Cobbler

导入镜像

和kickstart类似,首先需要一个镜像

左边的Action---import DVD

prefix是名字,一定要写上 。默认的路径是/var/www/cobbler/ks_mirror里面

arch 是64位,之前默认是i386

path一定要写上,/mnt/下一定要有光盘,要将光盘挂载到/mnt/下。mount  /dev/cdrom  /mnt/

点击run开始同步,完成之后events中显示complete,正在同步就是running

它不像kickstart可以直接读取光盘内容,必须要导入到系统目录中

进行配置

Configuration---settings

Configuration---distros:发行版本,就是导入进来了什么系统,可以修改一些参数,比如

  加入net.ifnames=0 biosdevname=0 修改网卡名

  修改这一项就可以了,save

Configuration---Kickstart Templates: 放了默认的ks文件,只是 一般都不用,只是为了学习用的吧

看一个编辑好的配置文件

# Cobbler for Kickstart Configurator for CentOS 7 by yao zhang
install
url --url=$tree    # 使用了tree变量,变量在Distros中定义了
text   # 文本模式安装
lang en_US.UTF-8   # 字符集
keyboard us  # 键盘语言
zerombr  # 清空mbr
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"  # 启动的
#Network information  network_config是一个文件名,在/var/lib/snippets下
$SNIPPET('network_config')   # 通过这一条短命令实现网络配置,
#network --bootproto=dhcp --device=eth0 --onboot=yes --noipv6 --hostname=CentOS7
timezone --utc Asia/Shanghai
authconfig --enableshadow --passalgo=sha512
rootpw  --iscrypted $default_password_crypted
clearpart --all --initlabel
part /boot --fstype xfs --size 1024
part swap --size 1024
part / --fstype xfs --size 1 --grow
firstboot --disable  # 系统初始化的软件
selinux --disabled
firewall --disabled
logging --level=info
reboot

%pre
$SNIPPET('log_ks_pre')  #记录日志
$SNIPPET('kickstart_start')  # 开启ks
$SNIPPET('pre_install_network_config') # 安装之前对网络的预配置
# Enable installation monitoring 
$SNIPPET('pre_anamon') # 监控系统安装的步骤
%end

%packages
@^minimal
@compat-libraries
@core
@debugging
@development
bash-completion
chrony
dos2unix
kexec-tools
lrzsz
nmap
sysstat
telnet
tree
vim
wget
%end

%post
systemctl disable postfix.service
%end

 

把这个文件保存到Configuration---Kickstart Templates中

在系统中查看在不在

[root@Cobbler centos7.4-x86_64]# ll /var/lib/cobbler/kickstarts/
total 60
-rw-r--r-- 1 root root 1028 Mar 14 16:44 centos7.ks

 

Configuration---Profiles

单击上传的系统,或者进入编辑

将kickstart 选择我们我刚刚创建的ks文件

kernel options 可以加上那两个内核参数,也可以不加

save!

自定义系统

我们要自动安装系统,它是根据system里面的配置来安装了。所以我们可以创建一个自定义的system

 

单击add,下面的内容才能继续添加

mac地址在(虚拟机网卡---高级---可以找到MAC地址)

save!!

做完修改要点击一下Action下的Sync 。相当于在命令行执行 cobbler  sync

注意:有时候在恢复快照的时候,虚拟机的MAC地址会变,要留意

 

posted @ 2019-03-21 17:08  游小刀  阅读(1105)  评论(0编辑  收藏  举报
levels of contents