一键部署软件

一键部署软件,第1张

PXE+kickstart一键部署 *** 作系统

 

第1章 简介

作为中小公司的运维,经常会遇到一些机械式的重复工作,例如:有时公司同时上线几十甚至上百台服务器,而且需要我们在短时间内完成系统安装。

常规的办法有什么?

·        光盘安装系统===>一个服务器DVD内置光驱百千块,百台服务器都配光驱就浪费了,因为一台服务器也就开始装系统能用的上,以后用的机会屈指可数。用USB外置光驱,插来插去也醉了。

·        U盘安装系统===>还是同样的问题,要一台一台服务器插U盘。

·        网络安装系统(ftp,http,nfs) ===>这个方法不错,只要服务器能联网就可以装系统了,但还是需要一台台服务器去敲键盘点鼠标。时刻想偷懒的我们,有没有更好的方法!

高逼格的方法:

·        Kickstart

·        Cobbler

在进入主题前,首先会向大家介绍一下什么是pxepxe能干什么,Kickstart是什么,Cobbler又有什么特别。

第2章 什么是PXE

·        PXE,全名Pre-boot Execution Environment,预启动执行环境;

·        通过网络接口启动计算机,不依赖本地存储设备(如硬盘)或本地已安装的 *** 作系统;

·        IntelSystemsoft公司于1999920日公布的技术;

·        Client/Server的工作模式;

·        PXE客户端会调用网际协议(IP)、用户数据报协议(UDP)、动态主机设定协议(DHCP)、小型文件传输协议(TFTP)等网络协议;

·        PXE客户端(client)这个术语是指机器在PXE启动过程中的角色。一个PXE客户端可以是一台服务器、笔记本电脑或者其他装有PXE启动代码的机器(我们电脑的网卡)。

第3章  PXE的工作过程

图片来源:张导博客

1.   PXE ClientDHCP发送请求
PXE Client
从自己的PXE网卡启动,通过PXE BootROM(自启动芯片)会以UDP(简单用户数据报协议)发送一个广播请求,向本网络中的DHCP服务器索取IP

2.   DHCP服务器提供信息
DHCP
服务器收到客户端的请求,验证是否来至合法的PXE Client的请求,验证通过它将给客户端一个提供响应,这个提供响应中包含了为客户端分配的IP地址、pxelinux启动程序(TFTP)位置,以及配置文件所在位置。

3.   PXE客户端请求下载启动文件
客户端收到服务器的回应后,会回应一个帧,以请求传送启动所需文件。这些启动文件包括:pxelinux.0pxelinux.cfg/defaultvmlinuzinitrd.img等文件。

4.   Boot Server响应客户端请求并传送文件
当服务器收到客户端的请求后,他们之间之后将有更多的信息在客户端与服务器之间作应答, 用以决定启动参数。BootROMTFTP通讯协议从Boot Server下载启动安装程序所必须的文件(pxelinux.0pxelinux.cfg/default)default文件下载完成后,会根据该文件中定义的引导顺序,启动Linux安装程序的引导内核。

5.   请求下载自动应答文件
客户端通过pxelinux.cfg/default文件成功的引导Linux安装内核后,安装程序首先必须确定你通过什么安装介质来安装linux,如果是通过网络安装(NFS, FTP, HTTP),则会在这个时候初始化网络,并定位安装源位置。接着会读取default文件中指定的自动应答文件ks.cfg所在位置,根据该位置请求下载该文件。

6.     这里有个问题,在第2步和第5步初始化2次网络了,这是由于PXE获取的是安装用的内核以及安装程序等,而安装程序要获取的是安装系统所需的二进制包以及配置文件。因此PXE模块和安装程序是相对独立的,PXE的网络配置并不能传递给安装程序,从而进行两次获取IP地址过程,但IP地址在DHCP的租期内是一样的。

7.   客户端安装 *** 作系统
ks.cfg文件下载回来后,通过该文件找到OS Server,并按照该文件的配置请求下载安装过程需要的软件包。
OS Server
和客户端建立连接后,将开始传输软件包,客户端将开始安装 *** 作系统。安装完成后,将提示重新引导计算机。

第4章 批量装机软件介绍

Redhat系主要有两种KickstartCobbler

Kickstart是一种无人值守的安装方式。它的工作原理是在安装过程中记录人工干预填写的各种参数,并生成一个名为ks.cfg的文件。如果在自动安装过程中出现要填写参数的情况,安装程序首先会去查找ks.cfg文件,如果找到合适的参数,就采用所找到的参数;如果没有找到合适的参数,便会d出对话框让安装者手工填写。所以,如果ks.cfg文件涵盖了安装过程中所有需要填写的参数,那么安装者完全可以只告诉安装程序从何处下载ks.cfg文件,然后就去忙自己的事情。等安装完毕,安装程序会根据ks.cfg中的设置重启/关闭系统,并结束安装。

Cobbler集中和简化了通过网络安装 *** 作系统需要使用到的DHCPTFTPDNS服务的配置。Cobbler不仅有一个命令行界面,还提供了一个Web界面,大大降低了使用者的入门水平。Cobbler内置了一个轻量级配置管理系统,但它也支持和其它配置管理系统集成,如Puppet,暂时不支持SaltStack

简单的说,Cobbler是对kickstart的封装,简化安装步骤、使用流程,降低使用者的门槛。

4.1 查看系统环境
[root@hz ~]# rename -m
call: rename from to files...
[root@hz ~]# uname -r
2.6.32-642.el6.x86_64
[root@hz ~]# uname -m
x86_64
[root@hz ~]# iptables -L
Chain INPUT (policy ACCEPT)
target    prot opt source              destination         
Chain FORWARD (policy ACCEPT)
target    prot opt source              destination        
Chain OUTPUT (policy ACCEPT)
target    prot opt source              destination        
[root@hz ~]# getenforce
Disabled
[root@hz ~]# hostname
hz
4.2 安装dhcp服务
[root@hz ~]# yum -y install dhcp
 
[root@hz ~]# rpm -ql dhcp |grep dhcpd.conf
/etc/dhcp/dhcpd.conf
/usr/share/doc/dhcp-4.1.1/dhcpd-conf-to-ldap
/usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample
/usr/share/man/man5/dhcpd.conf.5.gz
[root@hz ~]# cp /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample  /etc/dhcp/dhcpd.conf # 
#拷贝dhcp配置文件
#修改配置文件/etc/dhcpd/dhcpd.conf;添加如下
[root@hz ~]# vim /etc/dhcp/dhcpd.conf
 subnet10.0.0.0 netmask 255.255.255.0 {
        range 10.0.0.100 10.0.0.200;           #分配的地址池
        option subnet-mask 255.255.255.0;      #设置子网掩码
        default-lease-time 21600;              #设置默认的ip租约
        max-lease-time 43200;                  #设置最大的ip租约
        next-server 10.0.0.134;                #tftp服务器地址
        filename "/pxelinux.0";                #tftp根目录下载pxelinux.0文件
 }
4.3 启动dhcp服务
[root@hz ~]# /etc/init.d/dhcpd start
Starting dhcpd:                                           [  OK  ]
[root@hz ~]#
[root@hz ~]#
[root@hz ~]# netstat -lantp|grep dhcp
[root@hz ~]# netstat -luntp|grep dhcp
udp       0      0 0.0.0.0:67                  0.0.0.0:*                      1261/dhcpd

         

4.4 设置dhcp监听网卡
[root@hz ~]# vim /etc/sysconfig/dhcpd      
  1 # Commandline options here
  2DHCPDARGS=eth0                                                           
~                                                              
[root@hz ~]# /etc/init.d/dhcpd restart
Shutting down dhcpd:                                       [  OK  ]
Starting dhcpd:                                           [  OK  ]
[root@hz ~]# tailf /var/log/messages
Sep 16 01:59:03 hz dhcpd: For info, please visithttps://www.isc.org/software/dhcp/
Sep 16 01:59:03 hz dhcpd: Not searching LDAP sinceldap-server, ldap-port and ldap-base-dn were not specified in the config file
Sep 16 01:59:03 hz dhcpd: Internet SystemsConsortium DHCP Server 4.1.1-P1
Sep 16 01:59:03 hz dhcpd: Copyright 2004-2010Internet Systems Consortium.
Sep 16 01:59:03 hz dhcpd: All rights reserved.
Sep 16 01:59:03 hz dhcpd: For info, please visithttps://www.isc.org/software/dhcp/
Sep 16 01:59:03 hz dhcpd: Wrote 0 leases to leasesfile.
Sep 16 01:59:03 hz dhcpd: Listening onLPF/eth0/00:0c:29:a9:09:d2/10.0.0.0/24
Sep 16 01:59:03 hz dhcpd: Sending on   LPF/eth0/00:0c:29:a9:09:d2/10.0.0.0/24
Sep 16 01:59:03 hz dhcpd: Sending on   Socket/fallback/fallback-net
4.5 安装tftp服务

TFTPTrivial File Transfer Protocol,简单文件传输协议)是TCP/IP协议族中的一个用来在客户机与服务器之间进行简单文件传输的协议,提供不复杂、开销不大的文件传输服务。端口号为69

[root@hz ~]# yum -y install tftp-server    
 
[root@hz ~]# vim /etc/xinetd.d/tftp
  1 # default:off
  2 #description: The tftp server serves files using the trivia
    l filetransfer \
  3 #       protocol.  The tftp protocol is often used to boot d
    iskless \
  4 #       workstations, download configurationfiles to networ
    k-awareprinters, \
  5 #       and to start the installation processfor some opera
    tingsystems.
  6 servicetftp
  7 {
  8         socket_type             = dgram
  9         protocol                = udp
 10         wait                    = yes
 11         user                    = root
 12         server                  = /usr/sbin/in.tftpd
 13         server_args             = -s /var/lib/tftpboot
 14         disable                 = yes               #将no改为yes
 15         per_source              = 11
 16         cps                     = 100 2
 17         flags                   = IPv4

 18 }     

4.6 启动tftp服务
[root@hz ~]# /etc/init.d/xinetd restart
Stopping xinetd:                                          [FAILED]
Starting xinetd:                                           [  OK  ]
[root@hz ~]# netstat -luntp|grep 69
udp       0      0 0.0.0.0:69                  0.0.0.0:*                               1333/xinetd

       

第5章 安装httpd服务

可以用ApacheNginx提供HTTP服务。Python的命令web服务不行,会有报错。

 

[root@hz ~]# yum -y install httpd
[root@hz ~]# sed -i "277i ServerName 127.0.0.1:80"/etc/httpd/conf/httpd.conf#修改dhcp服务配置文件
[root@hz ~]# /etc/init.d/httpd start
Starting httpd:                                           [  OK  ]
[root@hz ~]# mkdir /var/www/html/CentOS-6.8                 #创建挂载目录
[root@hz ~]# mount /dev/cdrom/var/www/html/CentOS-6.8/     #挂载镜像文件
mount: block device /dev/sr0 is write-protected,mounting read-only
[root@hz ~]# df -h
Filesystem     Size  Used Avail Use% Mounted on
/dev/sda3      8.9G  1.5G  7.0G 17% /
tmpfs           238M    0  238M   0% /dev/shm
/dev/sda1       190M  33M  147M  19% /boot
/dev/sr0       3.7G  3.7G     0 100% /var/www/html/CentOS-6.8
第6章 PXE配置引导

syslinux是一个功能强大的引导加载程序,而且兼容各种介质。SYSLINUX是一个小型的Linux *** 作系统,它的目的是简化首次安装Linux的时间,并建立修护或其它特殊用途的启动盘。如果没有找到pxelinux.0这个文件,可以安装一下。

[root@hz yum.repos.d]# yum -y install syslinux     
[root@hz yum.repos.d]# ls
CentOS6-Base-163.repo    CentOS-Debuginfo.repo  CentOS-Media.repo
CentOS-Base.repo.backup  CentOS-fasttrack.repo  CentOS-Vault.repo
[root@hz yum.repos.d]# cp /usr/share/syslinux/pxelinux.0/var/lib/tftpboot/
[root@hz yum.repos.d]# cp -a/var/www/html/CentOS-6.8/isolinux/* /var/lib/tftpboot/
[root@hz yum.repos.d]# ls /var/lib/tftpboot/
boot.cat  initrd.img    memtest     TRANS.TBL
boot.msg  isolinux.bin  pxelinux.0  vesamenu.c32
grub.conf isolinux.cfg  splash.jpg  vmlinuz

 

6.1 配置pxedefault文件
[root@hz ~]# mkdir -p/var/lib/tftpboot/pxelinux.cfg
[root@hz ~]# vim/var/lib/tftpboot/pxelinux.cfg/default
  defaultvesamenu.c32  #默认加载一个菜单
#prompt 1   #开启会显示命令行’boot:’ 提示符。prompt值为0时则不提示,将会直接启动’default’参数中指定的内容
timeout 600#timeout时间是引导等待用户手动选择的时间,设为1可直接引导,单位为1/10秒。
 
display boot.msg    #菜单背景图片、标题、颜色。
menu background splash.jpg
menu title Welcome to CentOS 6.8!
menu color border 0 #ffffffff #00000000
menu color sel 7 #ffffffff #ff000000
menu color title 0 #ffffffff #00000000
menu color tabmsg 0 #ffffffff #00000000
menu color unsel 0 #ffffffff #00000000
menu color hotsel 0 #ff000000 #ffffffff
menu color hotkey 7 #ffffffff #ff000000
menu color scrollbar 0 #ffffffff #00000000
#lable指定在boot:提示符下输入的关键字,比如boot:linux[ENTER],这个会启动lablelinux下标记的kernel和initrd.img文件
label linux #一个标签就是前面图片的一行选项。
  menu label^Install or upgrade an existing system
  menudefault
  kernelvmlinuz    #指定要启动的内核。同样要注意路径,默认是/tftpboot目录
  appendinitrd=initrd.img  #指定追加给内核的参数,initrd.img是一个最小的linux系统
label vesa
  menu labelInstall system with ^basic video driver
  kernelvmlinuz
  appendinitrd=initrd.img nomodeset
label rescue
  menu label^Rescue installed system
  kernelvmlinuz
  append initrd=initrd.imgrescue
label local
  menu labelBoot from ^local drive
  localboot0xffff
label memtest86
  menu label^Memory test
  kernelmemtest
  append -
 
default ks
prompt 0
label ks
  kernelvmlinuz
  appendinitrd=initrd.img ks=http://10.0.0.134/ks_config/CentOS-6.8-ks.cfg #??????ks.cfg?????
# append initrd=initrd.imgks=http://10.0.0.134/ks_config/CentOS-6.8-ks.cfg ksdevice=eth0

 

6.2 系统安装完成之后系统自动生成的ks.cfg文件,路径/root/ anaconda-ks.cfg
[root@hz ~]# cat anaconda-ks.cfg
# Kickstart file automatically generated byanaconda.
#version=DEVEL
install
cdrom
lang en_US.UTF-8
keyboard us
network --onboot no --device eth0 --bootproto dhcp--noipv6
O4aRrtA7s2sy/MOMOFoGAhm0LJi24egarXfYtZe.5ZVpQLo76IJOfAkCFKG4F/
firewall --service=ssh
authconfig --enableshadow --passalgo=sha512
selinux --enforcing
timezone Asia/Shanghai
=auto rhgb quiet"
# The following is the partition information yourequested
# Note that any partitions you deleted are notexpressed
# here so unless you clear all partitions first,this is
# not guaranteed to work
#clearpart --none
 
#part /boot --fstype=ext4 --asprimary --size=200
#part swap --asprimary --size=700
#part / --fstype=ext4 --grow --asprimary --size=200
 
 
repo --name="CentOS"  --baseurl=cdrom:sr0 --cost=100
 
%packages
@base
@compat-libraries
@core
@debugging
@development
@server-policy
@workstation-policy
python-dmidecode
sgpio
device-mapper-persistent-data
systemtap-client
6.3 配置ks.cfg文件
%end[root@hz ~]# grub-crypt         #配置root使用密码写入ks.cfg文件
Password:
Retype password:
BfukeEC6DBNKkhpC2t4QJ9mbb1ebzRG.Iv9kdlYOn/
[root@oldboy ~]# cat/var/www/html/ks_config/CentOS-6.8-ks.cfg
# Kickstart Configurator for CentOS 6.8 by huazai
install                             #安装
url --url=http://10.0.0.134/CentOS-6.8/     #系统文件地址,这里使用dhcp
text                                #文本界面安装
lang en_US.UTF-8    #语言
keyboard us #键盘形式
zerombr #MBR清空
bootloader --location=mbr --driveorder=sda--append="crashkernel=auto rhgb quiet"#引导加载程序参数
network --bootproto=dhcp --device=eth0 --onboot=yes--noipv6 --hostname=CentOS6#配置eth0
timezone --utc Asia/Shanghai#时区
authconfig --enableshadow --passalgo=sha512#验证配置
rootpw --iscrypted $h9bfiYHCy93MZRV1$ptHYuPFAdaDOLvR4rxSw.5YqJl8.vfewgWeHFnoyHgm2BfukeEC6DBNKkhpC2t4QJ9mbb1ebzRG.Iv9kdlYOn/                        #root用户的密码
clearpart --all --initlabel #清空分区
part /boot --fstype=ext4 --asprimary --size=200 #boot分区
part swap --size=1024   #swap分区
part / --fstype=ext4 --grow --asprimary --size=200  #根分区
firstboot --disable #关闭一系列服务
selinux --disabled  #关闭selinux
firewall --disabled #关闭防火墙
logging --level=info    #日志记录等级info级别
reboot  #重启
%packages #安装包
@base
@compat-libraries
@debugging
@development
tree
nmap
sysstat
lrzsz
dos2unix
telnet
%post #安装完执行的脚本
wget -O /tmp/optimization.shhttp://10.0.0.134/ks_config/optimization.sh &>/dev/null
/bin/sh /tmp/optimization.sh             #下载指定的系统优化脚本
%end
6.4 系统优化脚本
[root@hz ~]# vim/var/www/html/ks_config/optimization.sh
#!/bin/bash
##############################################################
# File Name:/var/www/html/ks_config/optimization.sh
# Version: V1.0
# Author: yao zhang
# Organization: www.zyops.com
# Created Time : 2015-12-03 15:23:08
# Description: Linux system initialization
##############################################################
. /etc/init.d/functions
Ip=10.0.0.7
Port=80
ConfigDir=ks_config
# Judge Http server is ok?
PortNum=`nmap $Ip -p $Port 2>/dev/null|grep open|wc -l`
[ $PortNum -lt 1 ] && {
        echo"Http server is bad!"
        exit1
}
# Defined result function
function Msg(){
        if [$? -eq 0 ];then
         action "" /bin/true
        else
         action "" /bin/false
        fi
}
# Defined IP function
function ConfigIP(){
       Suffix=`ifconfig eth0|awk -F "[ .]+" 'NR==2 {print }'`
        cat>/etc/sysconfig/network-scripts/ifcfg-eth0 <<-END
       DEVICE=eth0
       TYPE=Ethernet
       ONBOOT=yes
       NM_CONTROLLED=yes
       BOOTPROTO=none
       IPADDR=10.0.0.$Suffix
       PREFIX=24
       GATEWAY=10.0.0.2
        DNS1=10.0.0.2
       DEFROUTE=yes
       IPV4_FAILURE_FATAL=yes
       IPV6INIT=no
       NAME="System eth0"
        END
        Msg"config eth0"
}
# Defined Yum source Functions
function yum(){
       YumDir=/etc/yum.repos.d
        [ -f"$YumDir/CentOS-Base.repo" ] && cp$YumDir/CentOS-Base.repo{,.ori}
        wget-O $YumDir/CentOS-Base.repo http://$Ip:$Port/$ConfigDir/CentOS-Base.repo&>/dev/null &&\
        wget-O $YumDir/epel.repo http://$Ip:$Port/$ConfigDir/epel.repo &>/dev/null&&\
        Msg"YUM source"
}
# Defined Hide the system version number Functions
function HideVersion(){
        [ -f"/etc/issue" ] && >/etc/issue
        Msg"Hide issue"
        [ -f"/etc/issue.net" ] && > /etc/issue.net
        Msg"Hide issue.net"
}
# Defined OPEN FILES Functions
function openfiles(){
        [ -f"/etc/security/limits.conf" ] && {
        echo'*  - nofile  65535' >>/etc/security/limits.conf
        Msg"open files"
        }
}
# Defined Kernel parameters Functions
function kernel(){
       KernelDir=/etc
        [ -f"$KernelDir/sysctl.conf" ] && /bin/mv$KernelDir/sysctl.conf{,.ori}
        wget-O $KernelDir/sysctl.conf http://$Ip:$Port/$ConfigDir/sysctl.conf&>/dev/null
        Msg"Kernel config"
}
# Defined System Startup Services Functions
function boot(){
        for hzin `chkconfig --list|grep "3:on"|awk '{print }'|grep -vE"crond|network|rsyslog|sshd|sysstat"`
          do
          chkconfig $hz off
        done
        Msg"BOOT config"
}
# Defined Time Synchronization Functions
function Time(){
        echo"#time sync by zhangyao at $(date +%F)" >>/var/spool/cron/root
        echo'*/5 * * * * /usr/sbin/ntpdate time.nist.gov &>/dev/null'>>/var/spool/cron/root
        Msg"Time Synchronization"
}
# Defined main Functions
function main(){
       ConfigIP
        yum
       HideVersion
       openfiles
       kernel
        boot
        Time
}
main
# rz??CentOS-Base.repo?epel.repo?sysctl.conf

 

 

第7章 启动全新服务器

需要和pxe服务器内网同网段,开机通过dhcp自动获取,然后自动开始装机

注意将本地的其他dhcp服务器自动关闭,以防产生干扰。

开始我一直出现错误就是因为虚拟网卡的dhcp服务没有关闭,导致一直失败。


系统开始安装:

 



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/778353.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-03
下一篇 2022-05-03

发表评论

登录后才能评论

评论列表(0条)

保存