自動部署系統

Linux系統批量自動安裝

實現原理:將手動安裝的全部的詳細步驟記錄到一個文件中,而後有一種軟件經過讀取這個文件就能夠實現自動化安裝系統。html

這個工具叫KickStart,KickStart是ReadHat公司的開源的工具,因此對Centos兼容性最好。注意kickstart是一個項目的名稱,沒有這個軟件。python

cobbler是對kickstart的全部組件的封裝。本質上就是網頁版本的kickstart。linux

PXE-KickStart原理

PXE,全名Pre-boot Execution Environment,預啓動執行環境。ios

經過網絡接口啓動計算機,不依賴本地存儲設備或本地已安裝的操做系統。web

PXE客戶端會調用網際協議(ip)、用戶數據報協議(UDP)、動態主機設定協議(DHCP)、小型文件傳輸協議(TFTP)等網絡協議。django

運行原理圖:

PXE客戶端向DCHP服務器請求ip地址,向TFTP服務器請求下載啓動文件、向HTTP請求自動應答文件(KickStart文件)
環境準備:準備模板機(centos7)vim

  1. 克隆以前必須關閉 NetworManager,而且開機不自啓動
    關閉systemctl status NetworkManager
    關閉運行:systemctl stop NetworkManager
    關閉開機自啓動:systemctl disable NetworkManager
    centos

  2. 處理網卡,centos7只須要刪除UUID,不須要刪除HWADDR
    sed -ri '/UUID|HWADDR/d' /etc/sysconfig/network-scripts/ifcfg-eth[01]
    sed -ri '/UUID/d' /etc/sysconfig/network-scripts/ifcfg-eth[01]緩存

  3. 關閉防火牆和selinux
    防火牆:
    關閉運行:systemctl stop firewalld.service
    關閉自啓動:systemctl disable firewalld.service
    查看狀態:
    systemctl status firewalld.service

    selinux:
    關閉配置 sed -i.bak 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
    當前關閉 setenforce 0
    查看 getenforcebash

  4. 替換爲國內yum源
    https://opsx.alibaba.com/mirror 打開點擊幫助
    執行 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    替換 epel源
    執行 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    以後運行 yum makecache 生成緩存
    查看當前源
    yum repolist

    能夠開始進行克隆了

查看是否能夠上網

ping baidu.com
ping 223.5.5.5
route -n 查看網管
ping 網管

開始部署DCHP服務器(先克隆一臺服務器)

  1. 修改hostname
    hostnamectl set-hostname oldboy-kickstart

  2. 安裝第一個軟件DHCP
    yum install -y dhcp

  3. 由於dhcp是對客戶端進行ip分發,故將虛擬機自己的dhcp服務關閉

  4. 配置dhcp
    cat >>/etc/dhcp/dhcpd.conf<<EOF
    subnet 172.16.1.0 netmask 255.255.255.0 {
    range 172.16.1.100 172.16.1.199; #可分配的起始IP-結束IP
    option subnet-mask 255.255.255.0; #設定netmask
    default-lease-time 21600; #設定默認IP租用期限
    max-lease-time 43200; #設定最大IP租用期限
    next-server 172.16.1.201; #告知客戶端TFTP服務器的ip
    filename "/pxelinux.0"; #告知客戶端從TFTP根目錄下載pxelinux.0文件
    }
    EOF

  5. 啓動dhcp
    systemctl start dhcpd.service
    systemctl disable dhcpd.service #關閉開機自啓動
    tailf /var/log/message 實時查看dhcp日誌

  6. 建立一臺虛擬機,查看是否會分發ip
    確保網卡和kickstart的LAN區段屬於同一網段

    開啓虛擬機,出現DHCP

    由於兩塊網卡,第一塊eth0不在同一網段,因此DHCP分發失敗,第二塊成功,可是TFTP服務咱們還沒開啓,故超時。

  7. 有可能會遇到的坑

  8. 當前虛擬機作快照

  9. 經過抓包查看dhcp過程
    安裝軟件 yum install -y wireshark
    tshark -ni eth1 #指定抓取eth1

部署TFTP服務

  1. 安裝tftp
    yum install -y tftp-server
    systemctl start tftp.socket 啓動

  2. 上面克隆的服務器啓動

    安裝syslinux就能夠了

  3. 將syslinux的pxelinux.0 複製到tftp根目錄下

    cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/

  4. 繼續報錯

  5. 所需文件在光盤中
    mkdir -p /var/www/html/CentOS7/isolinux/
    mount /dev/cdrom /var/www/html/CentOS7/isolinux/
    複製到var/lib/tftpboot/ 下面
    cp /var/www/html/CentOS7/isolinux/isolinux/* /var/lib/tftpboot/
    cp /var/www/html/CentOS7/isolinux/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default

  6. 配置完成後的目錄
    [root@oldboy-kickstart tftpboot]# tree /var/lib/tftpboot/
    /var/lib/tftpboot/
    ├── boot.cat
    ├── boot.msg
    ├── grub.conf
    ├── initrd.img
    ├── isolinux.bin
    ├── isolinux.cfg
    ├── memtest
    ├── pxelinux.0
    ├── pxelinux.cfg
    │ └── default
    ├── splash.png
    ├── TRANS.TBL
    ├── vesamenu.c32
    └── vmlinuz

  7. 以後從新啓動克隆的虛擬機,成功進入安裝界面

部署HTTP服務

  1. 安裝http服務
    yum -y install httpd
    systemctl start httpd.service

  2. 訪問測試10.0.0.201/Centos7,出現了硬盤信息

整合完全部的操做,開始自動部署系統!

KS 官方文檔 https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/7/html-single/installation_guide/index

  1. 編寫KS文件(包含安裝的全部步驟)也叫自動應答文件
    全部安裝完成的系統,作的每一步操做,都保存在 anaconda-ks.cfg
    ks文件語法:

  2. 準備好ks文件
    [root@oldboy-kickstart ~]# cat /var/www/html/ks_config/CentOS7-ks.cfg
# Kickstart Configurator for CentOS 7 by yao zhang
install
url --url="http://172.16.1.201/CentOS7/isolinux/"
text
lang en_US.UTF-8
keyboard us
zerombr
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
network  --bootproto=static --device=eth0 --gateway=10.0.0.254 --ip=10.0.0.202 --nameserver=223.5.5.5 --netmask=255.255.255.0 --activate
network  --bootproto=static --device=eth1 --ip=172.16.1.202 --netmask=255.255.255.0 --activate
network  --hostname=Cobbler
#network --bootproto=dhcp --device=eth1 --onboot=yes --noipv6 --hostname=CentOS7
timezone --utc Asia/Shanghai
authconfig --enableshadow --passalgo=sha512
rootpw  --iscrypted $6$X20eRtuZhkHznTb4$dK0BJByOSAWSDD8jccLVFz0CscijS9ldMWwpoCw/ZEjYw2BTQYGWlgKsn945fFTjRC658UXjuocwJbAjVI5D6/
clearpart --all --initlabel
part /boot --fstype xfs --size 1024
part swap --size 1024
part / --fstype xfs --size 1 --grow
firstboot --disable
selinux --disabled
firewall --disabled
logging --level=info
reboot

%packages
@^minimal
@compat-libraries
@debugging
@development
tree
nmap
sysstat
lrzsz
dos2unix
telnet 
wget 
vim 
bash-completion
%end

%post
systemctl disable postfix.service
%end
  1. 備份
    cp /var/lib/tftpboot/pxelinux.cfg/default{,.bak}

  2. 寫入ks文件
oldboy centos7 kickstart configure
default ks
timeout 50
prompt 0
label ks
  kernel vmlinuz
  append initrd=initrd.img inst.ks=http://172.16.1.201/ks_config/CentOS7-ks.cfg net.ifnames=0 biosdevname=0 ksdevice=eth1 #安裝系統的內核參數,因此要建立ks_config/Centos7-ks.cfg文件
  1. 建立ks所需目錄
    mkdir -p /var/www/html/ks_config

  2. 上傳CentOS7-ks.cfg文件到 /var/www/html/ks_config

  3. 訪問url查看是否能找到ks_config文件

  4. 訪問url查看是否能找/var/www/html/ks_config/CentOS7-ks.cfg裏面的Centos文件路徑

  5. 接下來能夠愉快的自動部署系統了,全程自動,不再用點點點了

Cobbler服務

Cobbler是一個Linux系統安裝的服務,能夠經過網絡啓動(PXE)的方式來快速安裝、重裝物理服務器和虛擬機,同時還能夠管理DHCP、DNS等。

Cobbler可使用命令行方式管理,也提供了基於web的界面管理工具(cobbler-web),還提供了API接口,能夠方便二次開發使用。

Cobbler是較早前的kickstart的升級版,優勢是比較容易配置,還自帶web界面比較易於管理。

  1. 使用上面kickstart自動安裝的系統來安裝cobbler環境
    域名解析

    查看網卡信息

  2. 更改epel源
    curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

  3. 安裝cobbler
    yum install -y cobbler cobbler-web dhcp tftp-server pykickstart httpd python-django

  4. 檢查是否安裝成功

  5. 啓動cobbler和httpd
    systemctl start cobblerd.service
    systemctl start httpd.service
    cobbler check 檢查cobbler配置

  6. cobbler詳細配置說明
    [root@Cobbler ~]# cobbler check
    The following are potential configuration items that you may want to fix:

1 : The 'server' field in /etc/cobbler/settings must be set to something other than localhost, or kickstarting features will not work. This should be a resolvable hostname or IP for the boot server as reachable by all machines that will use it.
2 : For PXE to be functional, the 'next_server' field in /etc/cobbler/settings must be set to something other than 127.0.0.1, and should match the IP of the boot server on the PXE network.
3 : change 'disable' to 'no' in /etc/xinetd.d/tftp
4 : Some network boot-loaders are missing from /var/lib/cobbler/loaders, you may run 'cobbler get-loaders' to download them, or, if you only want to handle x86/x86_64 netbooting, you may ensure that you have installed a recent version of the syslinux package installed and can ignore this message entirely. Files in this directory, should you want to support all architectures, should include pxelinux.0, menu.c32, elilo.efi, and yaboot. The 'cobbler get-loaders' command is the easiest way to resolve these requirements.
5 : enable and start rsyncd.service with systemctl
6 : debmirror package is not installed, it will be required to manage debian deployments and repositories
7 : The default password used by the sample templates for newly installed machines (default_password_crypted in /etc/cobbler/settings) is still set to 'cobbler' and should be changed, try: "openssl passwd -1 -salt 'random-phrase-here' 'your-password-here'" to generate new one
8 : fencing tools were not found, and are required to use the (optional) power management features. install cman or fence-agents to use them

Restart cobblerd and then run 'cobbler sync' to apply changes.

修改一:vim /etc/cobbler/settings 修改dhcp爲1,這個設置意味着容許cobbler去管理dhcp服務


修改server:172.16.1.202

修改next_server:172.16.1.202

修改二:vim /etc/cobbler/dhcp.template
:%s#192.168#172.16#g 修改內網ip

同時刪除以下信息

修改三:將disble yes 改成no

修改四: 下載cobbler引導文件
cobbler get-loaders

修改五: 啓動rsyncd服務
[root@Cobbler ~]# systemctl enable rsyncd
[root@Cobbler ~]# systemctl start rsyncd

修改七:設置密碼放入default_password_crypted
生成密碼:openssl passwd -1
將生成密碼放入

  1. 從新運行cobbler
    systemctl restart cobblerd.service
    cobbler sync 使修改生效

    cobbler check 檢查只剩兩天報錯

  2. 重啓全部服務
    systemctl restart cobblerd.service httpd.service tftp.socket rsyncd.service
    [root@Cobbler ~]# systemctl is-active cobblerd.service httpd.service tftp.socket rsyncd.service
    active
    active
    active
    active

  3. 訪問cobbler-web界面
    https://10.0.0.202/cobbler_web

    默認帳戶密碼爲cobbler

使用cobbler

  1. 確保選擇了光盤

  2. 將光盤掛載到/mnt下
    [root@Cobbler ~]# mount /dev/cdrom /mnt/
    mount: /dev/sr0 is write-protected, mounting read-only

  3. 單擊run,events顯示正在運行

  4. 導入結束

5.config配置
修改網卡名字

使用cobbler建立自定義系統配置文件

上面的配置結束,要指定本身的配置文件

# Cobbler for Kickstart Configurator for CentOS 7 by yao zhang
install
url --url=$tree
text
lang en_US.UTF-8
keyboard us
zerombr
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
#Network information
$SNIPPET('network_config')
#network --bootproto=dhcp --device=eth0 --onboot=yes --noipv6 --hostname=CentOS7
timezone --utc Asia/Shanghai
authconfig --enableshadow --passalgo=sha512
rootpw  --iscrypted $default_password_crypted
clearpart --all --initlabel
part /boot --fstype xfs --size 1024
part swap --size 1024
part / --fstype xfs --size 1 --grow
firstboot --disable
selinux --disabled
firewall --disabled
logging --level=info
reboot

%pre
$SNIPPET('log_ks_pre')
$SNIPPET('kickstart_start')
$SNIPPET('pre_install_network_config')
# Enable installation monitoring
$SNIPPET('pre_anamon')
%end

%packages
@^minimal
@compat-libraries
@core
@debugging
@development
bash-completion
chrony
dos2unix
kexec-tools
lrzsz
nmap
sysstat
telnet
tree
vim
wget
%end

%post
systemctl disable postfix.service
%end

profile指定剛纔建立的ks

system指定剛纔建立的ks

  1. 選擇systems,點擊create

  2. 修改General

  3. 修改Networking(Global)

  4. 修改Networking
    配置eth0
    mac地址位置

    配置eth1

  5. 點擊Action區域的sync生效

全篇完結。

相關文章
相關標籤/搜索