前言:

对于单台服务器上的OS安装,我们可以手动进行,若是一个集群服务中的多个节点超过50台服务器的OS安装,手动安装显然是不可行的,因此作为系统运维工程师自动化运维是我们必不可缺的技能。

PXE是一种引导协议,它通过网络来启动系统安装过程。

一、环境准备

在Centos6 操作系统上实现PXE的自动化部署OS,所依赖的服务有:dhcp, tftp, fileserver(http,ftp,nfs),依赖的安装源:kickstart文件,可引导内核-vimlinuz,initrd.img文件。

PXE自动化部署OS拓扑图

节点准备:节点zly准备两块网卡,将网卡eth0做外接网卡,将第二块网卡eth1做PXE完成自动化部署OS,网卡eth0桥接模式,网卡eth1内网模式vmnet2;

[root@ zly ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1DEVICE=eth1BOOTPROTO=staticONBOOT=yesIPADDR=192.168.20.3NETMASK=255.255.255.0GATEWAY=192.168.20.3HWADDR=00:0C:29:73:9F:1B..................[root@ zly ~]# ifconfigeth0      Link encap:Ethernet  HWaddr 00:0C:29:73:9F:11           inet addr:172.16.20.8  Bcast:172.16.255.255  Mask:255.255.0.0          inet6 addr: fe80::20c:29ff:fe73:9f11/64 Scope:Link          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1          RX packets:12463 errors:0 dropped:0 overruns:0 frame:0          TX packets:6010 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:1000          RX bytes:11748882 (11.2 MiB)  TX bytes:515051 (502.9 KiB)eth1      Link encap:Ethernet  HWaddr 00:0C:29:73:9F:1B           inet addr:192.168.20.3  Bcast:192.168.20.255  Mask:255.255.255.0          inet6 addr: fe80::20c:29ff:fe73:9f1b/64 Scope:Link          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1          RX packets:0 errors:0 dropped:0 overruns:0 frame:0          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:1000          RX bytes:0 (0.0 b)  TX bytes:468 (468.0 b)[root@ zly ~]# service network restart

二、下载镜像光盘至本地准备PXE所依赖的安装源

1、下载光盘镜像,并查看所需要的文件;

[root@zly ~]# lftp http://172.16.0.1/cobbler/ks_mirrorcd: received redirection to `http://172.16.0.1/cobbler/ks_mirror/'cd ok, cwd=/cobbler/ks_mirrorlftp 172.16.0.1:/cobbler/ks_mirror/centos-6.4-x86_64> ls-rw-r--r--           14  2013-03-06 02:28  CentOS_BuildTagdrwxr-xr-x            -  2013-07-05 07:42  EFI........drwxr-xr-x            -  2013-07-05 07:42  cachedrwxr-xr-x            -  2013-07-05 07:42  p_w_picpathsdrwxr-xr-x            -  2013-07-05 07:42  isolinuxdrwxr-xr-x            -  2013-07-05 07:42  repodatalftp 172.16.0.1:/cobbler/ks_mirror/centos-6.4-x86_64> mirror isolinux/Total: 1 directory, 11 files, 0 symlinks                       New: 11 files, 0 symlinks37045107 bytes transferred in 3 seconds (10.82M/s)lftp 172.16.0.1:/cobbler/ks_mirror/centos-6.4-x86_64> mirror p_w_picpaths/Total: 2 directories, 7 files, 0 symlinks                        New: 7 files, 0 symlinks207128660 bytes transferred in 28 seconds (7.18M/s)lftp 172.16.0.1:/cobbler/ks_mirror/centos-6.4-x86_64> bye................................[root@ zly ~]# ls isolinux/boot.cat  grub.conf   isolinux.bin  memtest     TRANS.TBL     vmlinuzboot.msg  initrd.img  isolinux.cfg  splash.jpg  vesamenu.c32[root@ zly ~]# ls p_w_picpaths/pxeboot/initrd.img  TRANS.TBL  vmlinuz

2、创建挂载光盘镜像的目录并挂载光盘

[root@zly ~]# mkdir /var/ftp/pub/centos-6.4-x86_64 -pvmkdir: created directory `/var/ftp/pub/centos-6.4-x86_64'[root@zly ~]# mount -r /dev/cdrom /var/ftp/pub/centos-6.4-x86_64/[root@zly ~]# cd /var/ftp/pub/centos-6.4-x86_64/[root@zly centos-6.4-x86_64]# lsCentOS_BuildTag  isolinux                  RPM-GPG-KEY-CentOS-Debug-6EFI              Packages                  RPM-GPG-KEY-CentOS-Security-6EULA             RELEASE-NOTES-en-US.html  RPM-GPG-KEY-CentOS-Testing-6GPL              repodata                  TRANS.TBLp_w_picpaths           RPM-GPG-KEY-CentOS-6

三、安装PXE实现部署所依赖的服务

1、安装vsftpd服务,并启动服务;

[root@zly ~]# yum -y install vsftpdLoaded plugins: fastestmirror, refresh-packagekit, securityLoading mirror speeds from cached hostfileSetting up Install ProcessResolving Dependencies--> Running transaction check---> Package vsftpd.x86_64 0:2.2.2-11.el6_3.1 will be installed--> Finished Dependency Resolution.................................Installed:  vsftpd.x86_64 0:2.2.2-11.el6_3.1Complete!.................................[root@ zly ~]# service vsftpd startStarting vsftpd for vsftpd:                                [  OK  ][root@ zly ~]# ss -tanlp | grep vsftpdLISTEN     0      32                        *:21                       *:*      users:(("vsftpd",8968,3))

2、确认已配置好的yum源,并安装dhcp

[root@zly ~]# vim /etc/yum.repos.d/centos.repo[yum1]name=centos6.4baseurl=http://172.16.0.1/cobbler/ks_mirror/centos-6.4-x86_64/enable=1gpgcheck=0[yum2]name=fedorabaseurl=http://172.16.0.1/fedora-epel/6/x86_64/enable=1gpgcheck=0[yum3]name=extrbaseurl=http://172.16.0.1/centos/6/extras/x86_64/enable=1gpgcheck=0........................[root@zly ~]# yum -y install dhcpLoaded plugins: fastestmirror, refresh-packagekit, securityLoading mirror speeds from cached hostfileSetting up Install ProcessResolving Dependencies--> Running transaction check---> Package dhcp.x86_64 12:4.1.1-34.P1.el6.centos will be installed--> Finished Dependency Resolution.................Installed:  dhcp.x86_64 12:4.1.1-34.P1.el6.centos                                       Complete!

四、修改dhcp的配置文件

1、dhcp服务是使用PXE的前提,通过dhcp服务可以告知tftp服务的地址,以及要加载的文件;

[root@zly ~]# cp /usr/share/doc/dhcpdhclient-4.1.1/    dhcp-4.1.1/        dhcp-common-4.1.1/[root@zly ~]# cp /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcp/dhcpd.confcp: overwrite `/etc/dhcp/dhcpd.conf'? y[root@zly ~]# vim /etc/dhcp/dhcpd.confoption domain-name "xiaozheng.com";option domain-name-servers 172.16.20.8;default-lease-time 43200;max-lease-time 86400;..................subnet 192.168.20.0 netmask 255.255.255.0 {        range 192.168.20.1 192.168.20.100        option routers 192.168.20.3}:.,$s@^\([^#]\)@#\1@g  注释当前行及其之后的行[root@ zly ~]# service dhcpd startStarting dhcpd:                                            [  OK  ][root@ zly ~]# ss -uanlp | grep dhcpUNCONN     0      0                         *:67                       *:*      users:(("dhcpd",6578,7))

2、在配置文件中添加指向tftp服务器及PXE文件目录

[root@ zly ~]# vim /etc/dhcp/dhcpd.confsubnet 192.168.20.0 netmask 255.255.255.0{        range 192.168.20.1 192.168.20.100;        option routers 192.168.20.3;}next-server 192.168.20.3;filename="pxelinux.0";........[root@ zly ~]# service dhcpd restartShutting down dhcpd:                                       [  OK  ]Starting dhcpd:                                            [  OK  ]

五、tftp服务的安装配置

1、安装使用tftp服务,查看tftp根目录的‘pxelinux'存放路径,并启动xinetd服务;

[root@ zly ~]# yum -y install syslinux tftp tftp-serverLoaded plugins: fastestmirror, refresh-packagekit, securityLoading mirror speeds from cached hostfileSetting up Install ProcessResolving Dependencies--> Running transaction check---> Package syslinux.x86_64 0:4.02-8.el6 will be installed---> Package tftp.x86_64 0:0.49-7.el6 will be installed---> Package tftp-server.x86_64 0:0.49-7.el6 will be installed........................Installed:  syslinux.x86_64 0:4.02-8.el6               tftp.x86_64 0:0.49-7.el6            tftp-server.x86_64 0:0.49-7.el6          Dependency Installed:  xinetd.x86_64 2:2.3.14-38.el6                                                 Complete!.........................[root@ zly ~]# cd /etc/xinetd.d/[root@ zly xinetd.d]# lschargen-dgram   daytime-stream  echo-dgram   tcpmux-server  time-streamchargen-stream  discard-dgram   echo-stream  tftpdaytime-dgram   discard-stream  rsync        time-dgram[root@ zly xinetd.d]# vim tftp        server                  = /usr/sbin/in.tftpd        server_args             = -s /var/lib/tftpboot        disable                 = yes..............................[root@ zly xinetd.d]# chkconfig tftp on[root@ zly xinetd.d]# service xinetd restartStopping xinetd:                                           [  OK  ]Starting xinetd:                                           [  OK  ][root@ zly xinetd.d]# ss -uanlpState       Recv-Q Send-Q        Local Address:Port          Peer Address:PortUNCONN      0      0                         *:751                      *:*      users:(("rpc.statd",1423,5))UNCONN      0      0                         *:111                      *:*      users:(("rpcbind",1404,6))UNCONN      0      0                         *:631                      *:*      users:(("cupsd",1547,9))UNCONN      0      0                         *:67                       *:*      users:(("dhcpd",6664,7))UNCONN      0      0                         *:69                       *:*      users:(("xinetd",6752,5))

六、配置PXE部署OS所需的安装源

1、从所下载的镜像文件中添加PXE部署OS所需要的相关文件;

[root@ zly ~]# lsanaconda-ks.cfg  install.log         isolinux     pxelinux.0p_w_picpaths           install.log.syslog  package.xml[root@ zly ~]# cd isolinux/[root@ zly isolinux]# lsboot.cat  grub.conf   isolinux.bin  memtest     TRANS.TBL     vmlinuzboot.msg  initrd.img  isolinux.cfg  splash.jpg  vesamenu.c32......................[root@ zly isolinux]# cp {boot.msg,splash.jpg,vesamenu.c32} /var/lib/tftpboot/[root@ zly isolinux]# cd /var/lib/tftpboot/[root@ zly tftpboot]# lsboot.msg  pxelinux.0  splash.jpg  vesamenu.c32......................[root@ zly ~]# cp p_w_picpaths/pxeboot/{vmlinuz,initrd.img} /var/lib/tftpboot/[root@ zly ~]# ls /var/lib/tftpboot/boot.msg  initrd.img  pxelinux.0  splash.jpg  vesamenu.c32  vmlinuz

2、创建PXE配置文件

[root@ zly ~]# mkdir /var/lib/tftpboot/pxelinux.cfg[root@ zly ~]# cd /var/lib/tftpboot/pxelinux.cfg/[root@ zly pxelinux.cfg]# ls[root@ zly pxelinux.cfg]# cp /root/isolinux/isolinux.cfg default[root@ zly pxelinux.cfg]# lsdefault[root@ zly pxelinux.cfg]# vim defaultlabel linux  menu label ^Install or upgrade an existing system  menu default  kernel vmlinuz  append initrd=initrd.img ks=ftp://192.168.20.3/pub/kickstart/centos6.cfg  ...............[root@ zly pxelinux.cfg]# chmod +x default

七、配置PXE所需的kickstart文件

1、创建kickstart文件;

[root@ zly pxelinux.cfg]# mkdir -pv /var/lib/pub/kickstart/mkdir: created directory `/var/lib/pub'mkdir: created directory `/var/lib/pub/kickstart/'[root@ zly pxelinux.cfg]# vim default[root@ zly pxelinux.cfg]# yum -y install system-config-kickstart[root@ zly pxelinux.cfg]#  system-config-kickstartXlib:  extension "RANDR" missing on display "localhost:11.0"./usr/share/system-config-kickstart/kickstartGui.py:103: GtkWarning: GtkSpinButton: setting an adjustment with non-zero page size is deprecated  xml = gtk.glade.XML ("/usr/share/system-config-kickstart/system-config-kickstart.glade", domain="system-config-kickstart")Loaded plugins: fastestmirror, refresh-packagekitLoading mirror speeds from cached hostfile.................

*(基图形化界面创建)

需要修改内容如下,其他选项使用默认配置

1)修改安装方法

2)修改磁盘分区

3)修改安装后脚本,并保存

至此kickstart文件创建完毕保存则可以使用

2、修改已经创建的kickstart文件,并重启vsftpd服务

[root@ zly ~]# lsanaconda-ks.cfg  p_w_picpaths       install.log.syslog  package.xmlcentos6.cfg      install.log  isolinux            pxelinux.0[root@ zly ~]# cat anaconda-ks.cfg[root@ zly ~]# vim centos6.cfgurl --url="ftp://192.168.20.3/pub/centos-6.4-x86_64"............clearpart --all --initlabelpart /boot --fstype=ext4 --size=200part pv.008002 --size=61440volgroup vg0 --pesize=8192 pv.008002logvol / --fstype=ext4 --name=root --vgname=vg0 --size=20480logvol swap --name=swap --vgname=vg0 --size=2048logvol /usr --fstype=ext4 --name=usr --vgname=vg0 --size=10240logvol /var --fstype=ext4 --name=var --vgname=vg0 --size=20480...........[root@ zly ~]# service vsftpd restartShutting down vsftpd:                                      [  OK  ]Starting vsftpd for vsftpd:                                [  OK  ]

八、使用PXE完成自动化部署OS

新建虚拟机安装OS,将其网卡连接模式改为内网模式vmnet2,启动安装流程,部署过程如下:

1)OS安装选择界面

2)等待NetworkManager配置网卡

3)从属检测

4)安装OS包

5)安装完毕后,重启主机即可使用已安装的系统

至此,基于PXE的自动化部署OS已经基本完成,后续自动化部署敬请关注!