在服务器网络环境中,配置好PXE网络安装操作系统,可实现对服务器的自动化安装管理。在安装完成操作系统之后,可以通过saltstack对服务器运行的服务,进行快速部署。在这种理想的环境中,一个新的数据中心,只需要手工安装配置DHCP、FTP、Salt-Master角色。其余的角色仅需在Salt-Master中指定操作系统、网络、服务等信息。Salt-Master即可实现自动化安装配置、部署服务。
配置PXE服务,以自动安装服务器操作系统,需要FTP服务提供安装影像和自动应答脚本,需要DHCP服务分配IP地址并引导PXEClient,需要TFTP服务为PXEClient提供启动管理器。所以,本文档涉及到的内容基于已经有可用的DHCP、TFTP、FTP/HTTP服务的基础环境中。这几种服务使用saltstack配置过程,已经在前几篇文档中有详尽的描述。这里不再重复讨论。
PXE启动过程及原理见另一篇博文《配置PXE环境自动安装Linux》
在完成saltstack平台下的PXE服务配置过后,可实现如下的服务器管理策略:
服务器的各项属性都由pillar变量指定:
- [root@SSHGateway ~]# cat /srv/pillar/pxelinux.sls
- #设定DHCP启用PXE支持,若DHCP的pillar已经指定,则可删除此变量
- dhcpd_with_pxe_support: True
- #设定PXE使用的FTP和TFTP服务地址
- pxe_use_ftp_server: 10.34.20.68
- pxe_use_tftp_server: 10.34.25.17
- #指定UEFI和Legacy-BIOS两类PXE配置文件路径
- pxe_conf_path:
- UEFI: /var/lib/tftpboot/UEFI
- Legacy: /var/lib/tftpboot/Legacy/pxelinux.cfg
- #操作系统代码和ks文件对应关系
- pxe_ks_file_list:
- Legacy_CentOS_5_i386: http://10.34.20.68/linux/ks/CentOS-5-i386-100GLVM-ks.cfg
- Legacy_CentOS_5_x86_64: http://10.34.20.68/linux/ks/CentOS-5-x86_64-100GLVM-ks.cfg
- Legacy_CentOS_6_i386: http://10.34.20.68/linux/ks/CentOS-6-i386-100GLVM-ks.cfg
- Legacy_CentOS_6_x86_64: http://10.34.20.68/linux/ks/CentOS-6-x86_64-100GLVM-ks.cfg
- Legacy_CentOS_7_x86_64: http://10.34.20.68/linux/ks/CentOS-7-x86_64-100GLVM-ks.cfg
- UEFI_CentOS_6_x86_64: http://10.34.20.68/linux/ks/CentOS-6-x86_64-UEFI-100GLVM-ks.cfg
- UEFI_CentOS_7_x86_64: http://10.34.20.68/linux/ks/CentOS-7-x86_64-UEFI-100GLVM-ks.cfg
- #设备第一个Ethernet接口的MAC地址,设备UUID的最后12个字符
- # hw_maca= last 12 chars of hw_uuid
- #使用IP地址的十六进制表示方式作为PXE配置文件名
- # pxeconf= /usr/bin/gethostip -x address
- #设备对应的各项信息
- #原本打算用UUID作为PXE配置文件名。
- #在UEFI和BIOS各自启动过程,由于计算方式不同,得到的UUID值不同
- #在UEFI和BIOS各自使用了大小写不同的MAC地址,且获取配置文件时大小写敏感。
- #所以,只能使用IP地址的十六进制表示方式。
- #Finished标志为true时,会删除其对应的配置文件,以避免系统重复安装
- pxe_new_server_info:
- pxe-test.xiyang-liu.com:
- address: 10.1.1.201
- pxeconf: 0A0105C9
- hw_mac0: 00:50:56:a0:e4:ea
- hw_uuid: 12345678-9abc-def1-2345-005056A0E4EA
- hw_type: UEFI
- osinfo:
- name: CentOS
- release: 6
- arch: x86_64
- finished: False
- pxe-test-f.xiyang-liu.com:
- address: 10.1.1.202
- pxeconf: 0A0105CA
- hw_mac0: 00:50:56:a0:e4:eb
- hw_uuid: 12345678-9abc-def1-2345-005056A0E4EB
- hw_type: Legacy
- osinfo:
- name: CentOS
- release: 6
- arch: x86_64
- finished: False
在理清管理思路后,即可在准备相关配置文件的同事,动手写作pxelinux的服务状态定义。如下为最终的pxelinux服务状态配置:
- [root@SSHGateway~]#cat / srv / salt / service / pxelinux.sls#将相关的pxelinux文件复制到被管端#理想情况下,应当自动从ftp服务器下载最新的vmlinuz和initrd文件pxe - bios - loader: file.managed: -name: /var/lib / tftpboot / legacy - pxelinux.zip - source: salt: //resource/legacy-pxelinux.zip
- pxe - uefi - loader: file.managed: -name: /var/lib / tftpboot / uefi - bootx64.zip - source: salt: //resource/uefi-bootx64.zip
- #将解压缩脚本复制到被管端,并执行。unzip - pxe - loaders: file.managed: -name: /var/lib / tftpboot / unzip - pxe - loaders.sh - source: salt: //conf/pxelinux/unzip-pxe-loaders.sh
- - mode: 755 cmd.run: -name: /var/lib / tftpboot / unzip - pxe - loaders.sh - require: -file: unzip - pxe - loaders#设置默认的PXE配置文件pxelinux -
- default:
- file.managed:
- -name: /var/lib / tftpboot / Legacy / pxelinux.cfg /
- default - source: salt: //conf/pxelinux/default
- uefi -
- default:
- file.managed:
- -name: /var/lib / tftpboot / UEFI / efidefault - source: salt: //conf/pxelinux/efidefault
- #将完整配置复制到被管端,以方便查阅pxelinux - full - conf: file.managed: -name: /var/lib / tftpboot / Legacy / pxelinux.cfg / pxe - full - source: salt: //conf/pxelinux/pxe-full
- gpxelinux - full - conf: file.managed: -name: /var/lib / tftpboot / Legacy / pxelinux.cfg / gpxe - full - source: salt: //conf/pxelinux/gpxe-full
- uefi - full - conf: file.managed: -name: /var/lib / tftpboot / UEFI / efi - full - source: salt: //conf/pxelinux/efi-full
- #循环处理每个服务器定义信息。#对finished为真的项目删除其配置文件,否则创建其配置文件 { %
- for hostname,
- hwinfo in pillar['pxe_new_server_info'].iteritems() %
- } { %
- if hwinfo.finished %
- }
- pxe - {
- {
- hostname
- }
- } - conf: file.absent: -name: {
- {
- pillar['pxe_conf_path'].get("hwinfo.hw_type")
- }
- }
- /{{ hwinfo.pxeconf }}
- {% else %}
- pxe-{{ hostname }}-conf:
- file.managed:
- - name: {{pillar['pxe_conf_path'].get(hwinfo.hw_type) }}/ {
- {
- hwinfo.pxeconf
- }
- } - source: salt: //conf/pxelinux/{{ hwinfo.hw_type }}-CentOS-Template
- - template: jinja - context: osinfo: {
- {
- hwinfo.osinfo
- }
- }
- kscode: {
- {
- hwinfo.hw_type
- }
- }
- _ {
- {
- hwinfo.osinfo.name
- }
- }
- _ {
- {
- hwinfo.osinfo.release
- }
- }
- _ {
- {
- hwinfo.osinfo.arch
- }
- } { % endif %
- } { % endfor %
- }
通过上述服务定义可以看出,除了必备的PXELinux和CentOS引导影像。还需要如下几类文件:
其中PXE启动文件压缩包为/var/lib/tftpboot目录下Legacy和UEFI两个目录中的文件,其文件列表如下:
- [root@SSHGateway resource]# tree Legacy/
- Legacy/
- ├── gpxelinux.0
- ├── memdisk
- ├── pxelinux.0
- └── pxelinux.cfg
- ├── 5
- │ ├── i386
- │ │ ├── initrd.img
- │ │ └── vmlinuz
- │ └── x86_64
- │ ├── initrd.img
- │ └── vmlinuz
- ├── 6
- │ ├── i386
- │ │ ├── initrd.img
- │ │ └── vmlinuz
- │ └── x86_64
- │ ├── initrd.img
- │ └── vmlinuz
- ├── 7
- │ └── x86_64
- │ ├── initrd.img
- │ └── vmlinuz
- ├── boot.msg
- ├── grub.conf
- ├── isolinux.bin
- ├── memtest
- ├── splash.jpg
- └── vesamenu.c32
- 9 directories, 19 files
- [root@SSHGateway resource]# tree UEFI/
- UEFI/
- ├── 6
- │ ├── initrd.img
- │ └── vmlinuz
- ├── 7
- │ ├── initrd.img
- │ └── vmlinuz
- ├── BOOTX64.efi
- └── splash.xpm.gz
- 2 directories, 6 files
PXE启动文件解压缩时,要保证只在第一次部署时执行脚本。然而,每次同步状态,脚本都会执行一次。所以,脚本判断在不存在相关目录的情况下,才解压缩软件包。也可以修改脚本,判断如果压缩包有更新的情况下,强制解压缩覆盖现有文件(或递归删除相应目录,重新解压缩)。现有解压缩脚本内容如下:
- [root@SSHGateway ~]# cat /srv/salt/conf/pxelinux/unzip-pxe-loaders.sh
- #!/bin/bash
- exitcode=0
- if [ -d /var/lib/tftpboot/Legacy ]
- then
- echo "/var/lib/tftpboot/Legacy seems existance. remove it to unzip your newer package"
- elif [ -f /var/lib/tftpboot/legacy-pxelinux.zip ]
- then
- unzip /var/lib/tftpboot/legacy-pxelinux.zip -d /var/lib/tftpboot/
- else
- echo "I can't find your legacy-pxelinux.zip"
- let $exitcode++
- fi
- if [ -d /var/lib/tftpboot/UEFI ]
- then
- echo "/var/lib/tftpboot/UEFI seems existance. remove it to unzip your newer package"
- elif [ -f /var/lib/tftpboot/uefi-bootx64.zip ]
- then
- unzip /var/lib/tftpboot/uefi-bootx64.zip -d /var/lib/tftpboot/
- else
- echo "I can't find your uefi-bootx64.zip, too"
- let $exitcode++
- fi
- exit $exitcode
DHCP配置文件中的PXE相关配置部分主要涉及到通过dhcp 60选项指定tftp服务器地址和启动文件路径。详见博文《配置PXE环境自动安装Linux》和博文《SaltStack自动化运维-配置DHCP服务》中的相关部分。示例文件如下所示:
- [root@SSHGateway ~]# cat /srv/salt/conf/dhcpd/subnet/dhcpd.10.1.1.0
- shared-network xxzx {
- subnet 10.1.1.0 netmask 255.255.255.0 {
- option routers 10.1.1.254;
- option subnet-mask 255.255.255.0;
- option domain-name "xiyang-liu.com";
- option domain-name-servers {{ DNS }};
- default-lease-time 86400;
- max-lease-time 172800;
- pool {
- failover peer "xiyang-dhcp";
- range 10.1.1.1 10.1.1.20;
- {%- if pillar.has_key('dhcpd_with_pxe_support') and pillar['dhcpd_with_pxe_support'] %}
- class "pxeclients" {
- match if substring (option vendor-class-identifier09) = "PXEClient";
- next-server 10.34.25.17;
- if option arch = 00:07 {
- filename "/UEFI/BOOTX64.efi";
- } else {
- filename "/Legacy/pxelinux.0";
- }
- }
- {%- for hostname,hwinfo in pillar['pxe_new_server_info'].iteritems() %}
- host {{hostname}} {
- option host-name "{{hostname}}";
- hardware ethernet {{hwinfo.hw_mac0}};
- {%- if ( not hwinfo.finished ) and hwinfo.osinfo.name == 'Windows' %}
- filename "\boot\x86\wdsnbp.com";
- next-server 10.34.25.20;
- {%- elif ( not hwinfo.finished ) and hwinfo.hw_type == 'UEFI' %}
- filename "/UEFI/BOOTX64.efi";
- {%- elif ( not hwinfo.finished ) and hwinfo.hw_type == 'Legacy' %}
- filename "/Legacy/pxelinux.0";
- {%- endif %}
- fixed-address {{hwinfo.address}};
- }
- {% endfor %}
- {% endif %}
- }
- }
- }
在给一台新服务器指定其PXE启动用的配置文件后,其引导后,加电将自动进入操作系统安装过程,会删除一切数据。所以,在一台服务器安装完成后需要设置finished标记,删除为其指定的配置。若不慎,进入PXE启动也将使用默认配置文件,从本机启动。特殊配置文件模板内容如下所示:
- [root@SSHGateway ~]# cat /srv/salt/conf/pxelinux/Legacy-CentOS-Template
- default pxelinux.cfg/vesamenu.c32
- timeout 60
- label autoinstallcentos
- menu label Legacy {{osinfo.name}} {{osinfo.release}} {{osinfo.arch}} AutoInstall
- menu default
- kernel pxelinux.cfg/{{osinfo.release}}/{{osinfo.arch}}/vmlinuz
- append ks={{pillar['pxe_ks_file_list'].get(kscode)}} initrd=pxelinux.cfg/{{osinfo.release}}/{{osinfo.arch}}/initrd.img
- [root@SSHGateway ~]# cat /srv/salt/conf/pxelinux/UEFI-CentOS-Template
- default=0
- timeout 10
- hiddenmenu
- title UEFI {{osinfo.name}} {{osinfo.release}} {{osinfo.arch}} AutoInstall
- root (nd)
- kernel /{{osinfo.release}}/vmlinuz ks={{ pillar['pxe_ks_file_list'].get(kscode)}}
- initrd /{{osinfo.release}}/initrd.img
默认PXE配置文件内容如下所示:
- [root@SSHGateway ~]# cat /srv/salt/conf/pxelinux/default
- default pxelinux.cfg/vesamenu.c32
- #prompt 1
- timeout 60
- label rescue
- menu label Rescue installed system
- kernel pxelinux.cfg/i386/vmlinuz
- append initrd=pxelinux.cfg/i386/initrd.img rescue
- label memtest86
- menu label Memory test
- kernel pxelinux.cfg/memtest
- append -
- label local
- menu label Boot from local drive
- menu default
- localboot 0xffff
- [root@SSHGateway ~]# cat /srv/salt/conf/pxelinux/efidefault
- splashimage=(nd)/splash.xpm.gz
- timeout 600
- title rescue
- root (nd)
- kernel /vmlinuz rescue askmethod
- initrd /initrd.img
- #UEFI模式的从本地磁盘启动,暂时还没找到实现方法。或许UEFI模式PXE网络启动总是排在最后吧。
完整PXE配置文件efi-full、pxe-full和gpxe-full文件内容参见博文《配置PXE环境自动安装Linux》
分别修改/srv/pillar/top.sls和/srv/salt/top.sls文件如下,以启用上面的配置:
- [root@SSHGateway ~]# cat /srv/pillar/top.sls
- base:
- '*':
- - base_info
- - base_srv
- 'dhcp-master.xiyang.com':
- - dhcpd.network-info
- - dhcpd.master
- - pxelinux
- 'dhcp-slave.xiyang.com':
- - dhcpd.network-info
- - dhcpd.slave
- - pxelinux
- [root@SSHGateway ~]# cat /srv/salt/top.sls
- base:
- '*':
- - base.core
- - base.iptables
- - service.nagios.linux-client
- - service.ntp.client
- 'dhcp-master.xiyang.com':
- - service.dhcpd
- - service.tftp.ro-server
- - service.pxelinux
- 'dhcp-slave.xiyang.com':
- - service.dhcpd
- - service.tftp.ro-server
- - service.pxelinux
本文介绍了在服务器网络环境中,通过saltstack配置pxelinux网络启动并安装操作系统的方法。也涉及到了服务器管理理念的一些内容。
来源: https://juejin.im/entry/59fc193a51882575d1445677