前言:
html
對於單臺服務器上的OS安裝,咱們能夠手動進行,如果一個集羣服務中的多個節點超過50臺服務器的OS安裝,手動安裝顯然是不可行的,所以做爲系統運維工程師自動化運維是咱們必不可缺的技能。
linux
PXE是一種引導協議,它經過網絡來啓動系統安裝過程。
vim
1、環境準備centos
在Centos6 操做系統上實現PXE的自動化部署OS,所依賴的服務有:dhcp, tftp, fileserver(http,ftp,nfs),依賴的安裝源:kickstart文件,可引導內核-vimlinuz,initrd.img文件。 服務器
PXE自動化部署OS拓撲圖
網絡
節點準備:節點zly準備兩塊網卡,將網卡eth0作外接網卡,將第二塊網卡eth1作PXE完成自動化部署OS,網卡eth0橋接模式,網卡eth1內網模式vmnet2;app
[root@ zly ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1 DEVICE=eth1 BOOTPROTO=static ONBOOT=yes IPADDR=192.168.20.3 NETMASK=255.255.255.0 GATEWAY=192.168.20.3 HWADDR=00:0C:29:73:9F:1B .................. [root@ zly ~]# ifconfig eth0 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
2、下載鏡像光盤至本地準備PXE所依賴的安裝源運維
一、下載光盤鏡像,並查看所須要的文件;
dom
[root@zly ~]# lftp http://172.16.0.1/cobbler/ks_mirror cd: received redirection to `http://172.16.0.1/cobbler/ks_mirror/' cd ok, cwd=/cobbler/ks_mirror lftp 172.16.0.1:/cobbler/ks_mirror/centos-6.4-x86_64> ls -rw-r--r-- 14 2013-03-06 02:28 CentOS_BuildTag drwxr-xr-x - 2013-07-05 07:42 EFI ........ drwxr-xr-x - 2013-07-05 07:42 cache drwxr-xr-x - 2013-07-05 07:42 p_w_picpaths drwxr-xr-x - 2013-07-05 07:42 isolinux drwxr-xr-x - 2013-07-05 07:42 repodata lftp 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 symlinks 37045107 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 symlinks 207128660 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 vmlinuz boot.msg initrd.img isolinux.cfg splash.jpg vesamenu.c32 [root@ zly ~]# ls p_w_picpaths/pxeboot/ initrd.img TRANS.TBL vmlinuz
二、建立掛載光盤鏡像的目錄並掛載光盤tcp
[root@zly ~]# mkdir /var/ftp/pub/centos-6.4-x86_64 -pv mkdir: 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]# ls CentOS_BuildTag isolinux RPM-GPG-KEY-CentOS-Debug-6 EFI Packages RPM-GPG-KEY-CentOS-Security-6 EULA RELEASE-NOTES-en-US.html RPM-GPG-KEY-CentOS-Testing-6 GPL repodata TRANS.TBL p_w_picpaths RPM-GPG-KEY-CentOS-6
3、安裝PXE實現部署所依賴的服務
一、安裝vsftpd服務,並啓動服務;
[root@zly ~]# yum -y install vsftpd Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile Setting up Install Process Resolving 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.1 Complete! ................................. [root@ zly ~]# service vsftpd start Starting vsftpd for vsftpd: [ OK ] [root@ zly ~]# ss -tanlp | grep vsftpd LISTEN 0 32 *:21 *:* users:(("vsftpd",8968,3))
二、確認已配置好的yum源,並安裝dhcp
[root@zly ~]# vim /etc/yum.repos.d/centos.repo [yum1] name=centos6.4 baseurl=http://172.16.0.1/cobbler/ks_mirror/centos-6.4-x86_64/ enable=1 gpgcheck=0 [yum2] name=fedora baseurl=http://172.16.0.1/fedora-epel/6/x86_64/ enable=1 gpgcheck=0 [yum3] name=extr baseurl=http://172.16.0.1/centos/6/extras/x86_64/ enable=1 gpgcheck=0 ........................ [root@zly ~]# yum -y install dhcp Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile Setting up Install Process Resolving 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!
4、修改dhcp的配置文件
一、dhcp服務是使用PXE的前提,經過dhcp服務能夠告知tftp服務的地址,以及要加載的文件;
[root@zly ~]# cp /usr/share/doc/dhcp dhclient-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.conf cp: overwrite `/etc/dhcp/dhcpd.conf'? y [root@zly ~]# vim /etc/dhcp/dhcpd.conf option 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 start Starting dhcpd: [ OK ] [root@ zly ~]# ss -uanlp | grep dhcp UNCONN 0 0 *:67 *:* users:(("dhcpd",6578,7))
二、在配置文件中添加指向tftp服務器及PXE文件目錄
[root@ zly ~]# vim /etc/dhcp/dhcpd.conf 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; } next-server 192.168.20.3; filename="pxelinux.0"; ........ [root@ zly ~]# service dhcpd restart Shutting down dhcpd: [ OK ] Starting dhcpd: [ OK ]
5、tftp服務的安裝配置
一、安裝使用tftp服務,查看tftp根目錄的‘pxelinux'存放路徑,並啓動xinetd服務;
[root@ zly ~]# yum -y install syslinux tftp tftp-server Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile Setting up Install Process Resolving 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]# ls chargen-dgram daytime-stream echo-dgram tcpmux-server time-stream chargen-stream discard-dgram echo-stream tftp daytime-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 restart Stopping xinetd: [ OK ] Starting xinetd: [ OK ] [root@ zly xinetd.d]# ss -uanlp State Recv-Q Send-Q Local Address:Port Peer Address:Port UNCONN 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))
6、配置PXE部署OS所需的安裝源
一、從所下載的鏡像文件中添加PXE部署OS所須要的相關文件;
[root@ zly ~]# ls anaconda-ks.cfg install.log isolinux pxelinux.0 p_w_picpaths install.log.syslog package.xml [root@ zly ~]# cd isolinux/ [root@ zly isolinux]# ls boot.cat grub.conf isolinux.bin memtest TRANS.TBL vmlinuz boot.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]# ls boot.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
二、建立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]# ls default [root@ zly pxelinux.cfg]# vim default label 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
7、配置PXE所需的kickstart文件
一、建立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-kickstart Xlib: 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-packagekit Loading mirror speeds from cached hostfile .................
*(基圖形化界面建立)
須要修改內容以下,其餘選項使用默認配置
1)修改安裝方法
2)修改磁盤分區
3)修改安裝後腳本,並保存
至此kickstart文件建立完畢保存則可使用
二、修改已經建立的kickstart文件,並重啓vsftpd服務
[root@ zly ~]# ls anaconda-ks.cfg p_w_picpaths install.log.syslog package.xml centos6.cfg install.log isolinux pxelinux.0 [root@ zly ~]# cat anaconda-ks.cfg [root@ zly ~]# vim centos6.cfg url --url="ftp://192.168.20.3/pub/centos-6.4-x86_64" ............ clearpart --all --initlabel part /boot --fstype=ext4 --size=200 part pv.008002 --size=61440 volgroup vg0 --pesize=8192 pv.008002 logvol / --fstype=ext4 --name=root --vgname=vg0 --size=20480 logvol swap --name=swap --vgname=vg0 --size=2048 logvol /usr --fstype=ext4 --name=usr --vgname=vg0 --size=10240 logvol /var --fstype=ext4 --name=var --vgname=vg0 --size=20480 ........... [root@ zly ~]# service vsftpd restart Shutting down vsftpd: [ OK ] Starting vsftpd for vsftpd: [ OK ]
8、使用PXE完成自動化部署OS
新建虛擬機安裝OS,將其網卡鏈接模式改成內網模式vmnet2,啓動安裝流程,部署過程以下:
1)OS安裝選擇界面
2)等待NetworkManager配置網卡
3)從屬檢測
4)安裝OS包
5)安裝完畢後,重啓主機便可使用已安裝的系統
至此,基於PXE的自動化部署OS已經基本完成,後續自動化部署敬請關注!