kickstart自动化部署

kickstart自动化部署

软件版本
nginx1.14.0
节点IP系统功能CPU内存硬盘
node110.80.10.1centos7.9kickstart4核心8GB20GB
node2自动分配centos7.9client4核心8GB20GB
node3自动分配centos6.10client4核心8GB20GB

centos7的手动安装简介

下载centos7.9和centos6.10的minimal镜像做准备:

centos7.9镜像下载地址:https://mirrors.aliyun.com/centos-vault/6.10/isos/x86_64/
centos6.10镜像下载地址:https://mirrors.aliyun.com/centos/7.9.2009/isos/x86_64/

vmware虚拟机安装centos7:

  • 需要手动挂载iso。

  • 需要手动设置时间日期、键盘、磁盘分区、网络等。

物理机手动安装centos7:

  • 需要使用u盘刻录。

  • 需要手动设置时间日期、键盘、磁盘分区、网络等。

kickstart项目能够实现centos7的全自动安装,也就是安装过程自动化,会比手动安装方便很多。

启动说明和kickstart流程简介

服务器的启动优先级,按优先级来启动,如果优先级高的找不到系统启动,则一级一级往下自动选择:

  • 从u盘启动:物理机手动安装系统一般从u盘启动。

  • 从光驱启动:虚拟机手动安装系统一般从光驱启动。

  • 从网络启动:从网络启动会使用pxe客户端,它可以自行获取ip,加载启动文件,自行安装操作系统。

  • 从硬盘启动:安装好系统后就可以从硬盘启动,启动后就能使用系统。

网络启动中的pxe客户端介绍:

  • pxe全称为preboot execute environment,预启动执行环境。

  • pxe客户端会使用dhcp客户端去获取ip。

  • pxe客户端会使用tftp协议去下载预启动文件。

pxe客户端对应的服务器端:

  • 既然pxe客户端会使用dhcp客户端去获取ip,那我们就需要搭建dhcp服务器。

  • 既然pxe客户端会使用tftp协议去下载预启动文件,那我们就需要搭建tftp服务器。

pxe网络启动全自动安装系统的流程介绍:

  • pxe客户端—>dhcp服务器:获取ip和tftp的地址。

  • pxe客户端—>tftp服务器:去下载预启动文件。

  • pxe客户端—>tftp服务器:获取引导文件,引导安装。这边可设置多种安装,例如centos6和centos7的安装。

  • pxe客户端—>tftp服务器:获取安装需要的vmliuz和initrm.img。

  • 内存加载vmliuz和initrd.img,然后去下载安装文件,全自动安装。

vmware通过网络启动说明:

  • 默认通过网络启动pxe客户端会报错。

dhcp服务器的安装和配置

dhcp服务器说明:

  • dhcp服务器用来给用户分配ip地址。

  • dhcp服务器还能给pxe客户端配置tftp的信息:地址和预启动文件。

  • 需要关闭虚拟机的dhcp。

node1

下载安装dhcp:

1
# yum install -y dhcp

修改dhcp配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# cp /etc/dhcp/dhcpd.conf{,.bak}
# vim /etc/dhcp/dhcpd.conf
allow booting;
allow bootp;

class "pxeclients"{
match if substring(option vendor-class-identifier,0,9) = "PXEClient";
filename "pxelinux.0";
next-server 10.80.10.1;
}

subnet 10.80.10.0 netmask 255.255.255.0 {
range dynamic-bootp 10.80.10.100 10.80.10.200;
option subnet-mask 255.255.0.0;
option routers 10.80.0.2;
option domain-name-servers 114.114.114.114;
option time-offset -18000;
default-lease-time 216000;
max-lease-time 432000;
}
  • allow:允许pxe。

  • next-server:tftp服务器的ip信息,可以配置在非dhcp服务器。

  • filename:tftp服务器的预启动文件。

  • subnet:配置dhcp服务器的网络信息。

  • range dynamic-bootp:设置分配的地址范围。

  • subnet-mask:配置分配地址的子网掩码。

  • option routers:配置分配的路由地址。

  • option domain-name-servers:配置分配的dns地址。

启动dhcp,配置开机自启:

1
2
3
# systemctl start dhcpd
# systemctl enable dhcpd
# systemctl status dhcpd

查看端口和进程:

1
2
3
4
5
# netstat -tulnp | grep dhcp
udp 0 0 0.0.0.0:67 0.0.0.0:* 30721/dhcpd
# ps -ef | grep dhcp
dhcpd 30721 1 0 19:21 ? 00:00:00 /usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid
root 31053 8091 0 19:22 pts/0 00:00:00 grep --color=auto dhcp

tftp服务器的安装和配置

tftp服务器:

  • tftp服务器可用来传输文件。

  • pxe客户端会来tftp服务器上拉取预启动文件pxelinux.0。

node1

下载安装tftp:

1
# yum install -y xinetd tftp-server

修改tftp配置文件:

1
2
3
4
# cp /etc/xinetd.d/tftp{,.bak}
# vim /etc/xinetd.d/tftp
# 14行,修改配置
disable = no
  • disable:改为no说明要启动tftp服务器。

  • server_args:设置tftp服务器的根目录,pxelinux.0默认会到根目录下查找。

启动tftp,设置开机自启:

1
2
3
# systemctl restart xinetd
# systemctl enable xinetd
# systemctl status xinetd

查看端口和进程:

1
2
3
4
5
# netstat -tulnp | grep xinetd
udp 0 0 0.0.0.0:69 0.0.0.0:* 8930/xinetd
# ps -ef | grep xinetd
root 32263 1 0 19:23 ? 00:00:00 /usr/sbin/xinetd -stayalive -pidfile /var/run/xinetd.pid
root 32491 8091 0 19:23 pts/0 00:00:00 grep --color=auto xinetd

安装pxelinux.0引导文件,会安装到tftp的根目录下/var/lib/tftpboot/:

1
2
3
# yum install -y syslinux-tftpboot
# ls /var/lib/tftpboot/pxelinux.0
/var/lib/tftpboot/pxelinux.0

编辑引导配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# mkdir -p /var/lib/tftpboot/pxelinux.cfg
# vim /var/lib/tftpboot/pxelinux.cfg/default
default vesamenu.c32
timeout 300

menu title welcome to kickstart auto install system

label install_centos7
menu label install centos7
kernel centos7/vmlinuz
append initrd=centos7/initrd.img inst.ks=http://10.80.10.1/ks7.cfg

label install_rescue
menu label install rescue
menu default
kernel centos7/vmlinuz
append initrd=centos7/initrd.img inst.ks=http://10.80.10.1/ks7.cfg rescue
  • timeout 300:超时30s。

  • menu title:供选择时的标题展现。

  • kernel:配置内核。

  • append:配置内存加载的文件。

挂载centos7镜像准备自动安装

node1

上传CentOS-7-x86_64-Minimal-2009.iso:

1
2
3
# cd /usr/local/src/
# ls CentOS-7-x86_64-Minimal-2009.iso
CentOS-7-x86_64-Minimal-2009.iso

镜像挂载:

1
2
3
4
5
# mkdir -p /var/www/html/centos7
# mount -o loop /usr/local/src/CentOS-7-x86_64-Minimal-2009.iso /var/www/html/centos7
mount: /dev/loop0 is write-protected, mounting read-only
# ls /var/www/html/centos7/
CentOS_BuildTag EFI EULA GPL images isolinux LiveOS Packages repodata RPM-GPG-KEY-CentOS-7 RPM-GPG-KEY-CentOS-Testing-7 TRANS.TBL

拷贝内核文件:

1
2
3
4
5
# mkdir -p /var/lib/tftpboot/centos7
# cd /var/www/html/centos7/isolinux/
# cp vmlinuz initrd.img /var/lib/tftpboot/centos7/
# ls /var/lib/tftpboot/centos7/
initrd.img vmlinuz

设置开机自动挂载镜像:

1
2
3
# vim /etc/rc.local
# 尾行,添加配置
mount -o loop /usr/local/src/CentOS-7-x86_64-Minimal-2009.iso /var/www/html/centos7
1
# chmod +x /etc/rc.d/rc.local

配合pex搭建nginx服务器

node1

下载安装nginx:

下载地址:https://nginx.org/en/download.html

1
2
3
4
5
6
7
# yum install -y wget gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
# cd /usr/local/src/
# wget http://nginx.org/download/nginx-1.14.0.tar.gz
# tar -xzvf nginx-1.14.0.tar.gz
# cd nginx-1.14.0
# ./configure --prefix=/usr/local/nginx
# make -j 4 && make -j 4 install
1
2
3
4
# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments: --prefix=/usr/local/nginx

修改配置文件:

1
2
3
4
5
6
7
# vim /usr/local/nginx/conf/nginx.conf
# 43~46行,修改配置
location / {
root /var/www/html/;
index index.html index.htm;
autoindex on;
}

启动nginx:

1
# /usr/local/nginx/sbin/nginx

浏览器访问:http://10.80.10.1/centos7/

编辑centos7的kickstart配置文件:

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
# vim /var/www/html/ks7.cfg
firewall --disabled
install
rootpw 123456
auth --useshadow --passalgo=sha512
#graphical
text
firstboot --disable
keyboard us
lang en_US
selinux --disabled
logging --level=debug
zerombr
reboot
timezone Asia/Shanghai
network --bootproto=dhcp --device=ens33 --onboot=on --noipv6
bootloader --location=mbr
clearpart --all --initlabel
part / --asprimary --fstype="ext4" --size=5000
part /boot --fstype="ext4" --size=200
part /data --fstype="ext4" --grow --size=1
url --url http://10.80.10.1/centos7

%packages
%end
%post
/usr/bin/echo "run post script"
/usr/bin/echo "kickstart install centos7" > /tmp/kickstart
/usr/bin/yum install -y net-tools
%end
  • install:全新安装。

  • text:文本安装。

  • firstboot –disable:设置代理。

  • keyboard:键盘总局英文。

  • lang en_US:语言选择英文。

  • zerombr:清除mbr分区。

  • reboot:安装完毕后重启。

  • clearpart:清除分区,磁盘有数据需要小心,默认选项一定不能放安装的这里。

  • part:设置分区–size指定分区大小。–grow指定使用剩余所有空间,那样的话–size大小随意。

  • url:指定包的位置。

  • %packages和%end中间放安装包,最小化安装不用配置。

  • %post和%end中间放安装后的自定义代码,用来做一些初始化。

硬盘全部分区都给/根分区的配置如下:
part / –asprimary –fstype=”ext4” –grow –size=1

验证全自动安装centos7

pxe网络启动全自动安装系统的流程介绍:

  • pxe客户端—>dhcp服务器:获取ip和tftp的地址。

  • pxe客户端—>tftp服务器:去下载预启动文件pxelinux.0,运行预启动文件。

  • pxe客户端—>tftp服务器:获取引导文件(pxelinux.cfg/default),引导安装。这边可设置多种安装,例如centos6和centos7的安装。

  • pxe客户端—>tftp服务器:获取安装需要的vmliuz和initrm.img。

  • 内存加载vmliuz和initrd.img,然后去下载安装文件,全自动安装。

  • 安装会获取kickstart配置文件—>http服务器获取kickstart配置文件。

  • 根据kickstart配置文件完成自动化安装,注意http服务器的配置。

创建虚拟机注意:

  • 在vmware上新建一台虚拟,不使用dvd的模式手动安装,使用网络全自动安装。

  • 注意内存至少要2G,内存不足会报错,磁盘可以默认20G。

验证centos7安装,启动虚拟机,进入安装界面,选择install centos7,回车确认:

等待安装,需要一段时间,这里安装完后会自动重启:

node2

进入正常的启动界面,root密码123456,进行测试:

1
2
3
# ifconfig
# cat /tmp/kickstart
# df -h

centos6的全自动安装配置

node1

添加centos6引导配置:

1
2
3
4
5
6
# vim /var/lib/tftpboot/pxelinux.cfg/default
# 尾行,添加配置
label install_centos6
menu label install centos6
kernel centos6/vmlinuz
append initrd=centos6/initrd.img ks=http://10.80.10.1/ks6.cfg

编辑centos6的kickstart配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# vim /var/www/html/ks6.cfg
firewall --disabled
install
rootpw 123456
auth --useshadow --passalgo=sha512
#graphical
text
firstboot --disable
keyboard us
lang en_US
selinux --disabled
logging --level=debug
zerombr
reboot
timezone Asia/Shanghai
network --bootproto=dhcp --device=eth0 --onboot=on --noipv6
bootloader --location=mbr
clearpart --all --initlabel
part / --asprimary --fstype="ext4" --grow --size=1
url --url http://10.80.10.1/centos6

%post
/bin/echo "kickstart install centos6" > /tmp/kickstart
%end

上传CentOS-6.10-x86_64-minimal.iso:

1
2
3
# cd /usr/local/src/
# ls CentOS-6.10-x86_64-minimal.iso
CentOS-6.10-x86_64-minimal.iso

镜像挂载:

1
2
3
# mkdir -p /var/www/html/centos6
# mount -o loop /usr/local/src/CentOS-6.10-x86_64-minimal.iso /var/www/html/centos6
mount: /dev/loop1 is write-protected, mounting read-only
1
2
3
# ls /var/www/html/centos6
CentOS_BuildTag EULA images Packages repodata RPM-GPG-KEY-CentOS-Debug-6 RPM-GPG-KEY-CentOS-Testing-6
EFI GPL isolinux RELEASE-NOTES-en-US.html RPM-GPG-KEY-CentOS-6 RPM-GPG-KEY-CentOS-Security-6 TRANS.TBL

拷贝内核文件:

1
2
3
4
5
# mkdir -p /var/lib/tftpboot/centos6
# cd /var/www/html/centos6/isolinux/
# cp vmlinuz initrd.img /var/lib/tftpboot/centos6/
# ls /var/lib/tftpboot/centos6/
initrd.img vmlinuz

镜像挂载,设置开机自动挂载:

1
2
3
# vim /etc/rc.local
# 尾行,添加配置
mount -o loop /usr/local/src/CentOS-6.10-x86_64-minimal.iso /var/www/html/centos6
1
# chmod +x /etc/rc.d/rc.local

浏览器访问:http://10.80.10.1/centos6/

验证全自动安装centos6和救援模式

创建虚拟机注意:

  • 在vmware上新建一台虚拟,不使用dvd的模式手动安装,使用网络全自动安装。

  • 内存配置了2G,但是centos6可以1G。

验证centos6安装,启动虚拟机,进入安装界面,选择install centos6,回车确认:

等待安装,需要一段时间,这里安装完后会自动重启:

node3

进入正常的启动界面,进行测试:

1
2
3
# ifconfig
# cat /tmp/kickstart
# df -h

验证救援模式,因为是用的centos7的内核,所以使用centos7的虚拟机测试。重启虚拟机,按F12进入网络启动模式,重新进入安装界面,选择install rescue,回车确认:

等待进入系统,输入3,进入shell:

node2

可以挂载目录,进行修复,这里系统盘是/dev/sda2,具体看kickstart的配置:

1
2
3
4
5
# ls /dev/sd*
# mkdir -p /mnt/kickstart
# mount /dev/sda2 /mnt/kickstart/
# cd /mnt/kickstart/
# ls -rht