期末架構-自動部署系統


本節PPT鏈接

我們之前安裝系統都是一台一台的裝

現在我們學習並行安裝---網絡安裝

使用KickStart 項目的軟件cobbler(網頁版的kickstart)軟件安裝(見ppt圖片)

 

PXE-Kickstart的運行原理

PXE,是指預啟動執行環境,解決了網絡啟動

部署之前確定一下環境是否設置好了,是否和教程中一致

kickstart的原理(白海明講)

客戶機內存至少2個G

客戶端想要裝系統,他的電腦必須要支持網絡啟動功能,要支持這個功能必須要有一個網卡,

網卡有一個功能PXE,具體來說這是一個芯片,這個芯片的作用就是在主板加電以后,網卡也會跟着加電,加點以后,這個芯片就會向網絡發送4個dhcp包,來獲得IP

...

 

 

克隆之前,模板機的操作

第一個操作(一關閉)

要關閉NetworkManager,否則會報錯

systemctl  stop  NetworkManager

systemctl  disabled  NetworkManager

# 然后再ifup  eth0 #  這個命令不用

[root@Luffy-pizza ~]# systemctl is-active NetworkManager
inactive
[root@Luffy-pizza ~]# systemctl is-enabled NetworkManager
disabled

 

第二個操作(一刪除)

之前在使用6的時候我們講過要一清兩刪

7不用這么麻煩,只用刪除/etc/sysconfig/network-scripts/ifcfg-eth0和eth1 里面的UUID 就可以了

第三個操作(配置yum源)

配置yum源,在下載系統的網站中

https://opsx.alibaba.com/mirror在centos列后面可以找到幫助

點擊幫助,復制命令即可

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

修改默認的 yum源

如果沒法上網,從以下方法依次排查

   78  ping www.baidu.com     ping 外網
   79  ping 223.5.5.5    ping 設置的DNS 看DNS對不對
   80  route -n             查看路由
   81  ping 10.0.0.254   ping網關

 

還要修改epel源,依賴

 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

使用命令yum makecache 在服務器上生成緩存

注意:如果阿里雲的不好使,一般使用清華的,

yum repolist 查看yum源

第四個操作

確認做完了,關機halt ,做快照

部署其他服務,部署kickstart服務器

根據PXE-KickStart 原理詳細見PPT中本節PPT鏈接

克隆做好的模板機一台,修改主機名

[root@hah ~]# hostnamectl set-hostname kickstart
[root@hah ~]# cat /etc/hostname 
kickstart

 

安裝第一個軟件DHCP(配合PPT)

dhcp是給我們的客戶端分發IP地址的,

首先關掉虛擬機的dhcp

[root@kickstart ~]# rpm -qa dhcp
dhcp-4.2.5-68.el7.centos.1.x86_64
[root@kickstart ~]# rpm -ql dhcp
..

 

/etc/dhcp/dhcpd.conf  就是需要配置的配置文件

[root@kickstart ~]# cat /etc/dhcp/dhcpd.conf 
#
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.example
#   see dhcpd.conf(5) man page
#
[root@kickstart ~]# 告訴我們這個文件是空的,我們去 一個文件中找例子

 

PPT中寫好了,詳細的解釋看PPT

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;
option subnet-mask 255.255.255.0;
default-lease-time 21600;
max-lease-time 43200;
next-server 172.16.1.201;
filename "/pxelinux.0";
}
EOF

 

上面地址從100-199,有100個地址,但是實際我們只能用一半(在第1和第5步都會消耗一個IP)

但是有幾百,幾千台服務器,我們怎么來部署系統,一般是,利用交換機分批次的安裝

最好不要讓dhcp開機自啟動,我們可不希望一開機就裝系統

[root@kickstart ~]# systemctl start dhcpd.service 
[root@kickstart ~]# systemctl is-enable dhcpd.service 
Unknown operation 'is-enable'.
[root@kickstart ~]# systemctl is-enabled dhcpd.service 
disabled

 

開一個窗口查看日志

[root@kickstart ~]# tail /var/log/messages -f
Mar 14 03:50:44 Luffy-pizza dhcpd: Listening on LPF/eth1/00:0c:29:95:f5:98/172.16.1.0/24
Mar 14 03:50:44 Luffy-pizza dhcpd: Sending on   LPF/eth1/00:0c:29:95:f5:98/172.16.1.0/24
已經開始監聽了

 

啟動了之后,再克隆一台虛擬機,記得添加一個網卡,並且在一個LAN區段

確保內存、硬盤、LAN區段設置好,啟動虛擬機

啟動后,首先‘掃描eth0’,沒有等到,然后就關閉了,然后在eth1等到了,

分配了IP172.16.1.100 從DHCP172.16.1.201

最后因為TFTP沒有配置,提示超時(在PPT中也有講解)

查看我們的日志tailf /var/log/messages

 

Mar 14 03:58:31 Luffy-pizza dhcpd: DHCPDISCOVER from 00:0c:29:94:c5:9b via eth1
# DHCP 發現了 
Mar 14 03:58:32 Luffy-pizza dhcpd: DHCPOFFER on 172.16.1.100 to 00:0c:29:94:c5:9b via eth1
# 發IP
Mar 14 03:58:33 Luffy-pizza dhcpd: DHCPREQUEST for 172.16.1.100 (172.16.1.201) from 00:0c:29:94:c5:9b via eth1
# 客戶端說收到了
Mar 14 03:58:33 Luffy-pizza dhcpd: DHCPACK on 172.16.1.100 to 00:0c:29:94:c5:9b via eth1
Mar 14 04:01:01 Luffy-pizza systemd: Started Session 5 of user root.
Mar 14 04:01:01 Luffy-pizza systemd: Starting Session 5 of user root.

 

 

抓包查看DHCP與next server

dhcp服務完成之后,客戶端有了IP,我們怎么知道客戶端向誰要對應的配置,和對應的文件?

一個是通過日志,一個是通過抓包來看。我們常用的抓包軟件一個叫wireshark

但是傳輸的數據在LAN區段中,通過vm8是抓不到的,需要在LINUX下安裝 wireshark

yum install -y wireshark
確認有兩個網卡,並且可以ping 通 ping 172.16.1.61

默認監聽的是eth0,加上參數修改

tshark  -ni  eth1      -n的作用是禁止將端口解析成服務名,回車,就開始監聽LAN區段了

重啟新建的要裝系統的客戶端虛擬機,就可以看到日志

 

配置TFTP服務

TFTP服務的作用是給客戶端下載啟動文件,PXE的鏡像和軟件

安裝

[root@kickstart ~]# yum -y install tftp-server

 

啟動

[root@kickstart ~]# systemctl start tftp.socket 

 

這個時候在查看日志或者看客戶端啟動過程,提示的是找不到文件

我們切換到TFTP服務的根目錄下

[root@kickstart ~]# cd /var/lib/tftpboot/
[root@kickstart tftpboot]# ll
total 0
[root@kickstart tftpboot]# 

 

裝完軟件syslinux就有了

[root@kickstart tftpboot]# yum install syslinux -y

 

找一下prelinux.0文件的位置

[root@kickstart tftpboot]# rpm -ql syslinux |grep pxelinux.0
/usr/share/syslinux/gpxelinux.0
/usr/share/syslinux/pxelinux.0

 

將文件復制到/var/lib/tftpboot/

[root@kickstart tftpboot]# cp /usr/share/syslinux/pxelinux.0  /var/lib/tftpboot/

 

繼續重復上面的重啟客戶端的操作,出現了新的問題,即ppt中(那些年我們才過的坑(3))

因為缺少pxelinux.0這個小系統的配置文件(default文件決定了安裝界面是什么樣子),這個文件在系統安裝盤中可以找到

解決方案見PPT

下圖表示出了tftboot文件下的文件都是來自哪里

 

apache安裝?

ppt里面叫安裝HTTP服務

見PPT,作用也在ppt中標明

OK!可以裝系統了

PXE-kickstart部署原理重現-ks配置文件詳解

1、新建虛擬機的內存至少2個G,否則會報錯(磁盤空間不足),客戶機啟動之后,添加了兩塊網卡,其中一塊是LAN區段的,和我們的DHCP服務器在一個局域網,客戶機就發UDP廣播請求IP地址,

2、DHCP服務器在分配IP的同時,給了客戶機一個next server 告訴他去tftp服務下載引導系統啟動的文件(如pxelinux.0),會經常改的文件叫pxelinux.cfg/default,配置我們安裝系統的樣式,

[root@kickstart html]# vim /var/lib/tftpboot/pxelinux.cfg/default 

 

 

3、選擇完成之后,下載好了文件,那么安裝系統的軟件包在哪里找呢?在http服務找軟件包

在http服務下載自動應答文件(就不用手動選擇語言和分區等操作)

注:

1)、因為我們沒有修改內核的參數,所以新裝的系統的IP網卡的名字是ens34.即自動生成的

通過在default配置文件中添加參數可以修改網卡名

append initrd=initrd.img inst.repo=http://172.16.1.201/CentOS7/ net.ifnames=0 biosdevname=0

 

2)、沒有IP地址,因為DHCP服務沒有開啟

[root@kickstart html]# systemctl start dhcpd.service 
[root@kickstart html]# systemctl start tftp.socket 
[root@kickstart html]# systemctl start httpd.service

 

 

如何自動部署(編寫KS文件)

即自動應答文件,裝系統的過程都在這個文件里面放着呢

見PPT中3中不同的編輯ks文件的方法

系統默認的ks文件

 

[root@kickstart ~]# cat anaconda-ks.cfg 
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Use graphical install
graphical
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=sda
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8

# Network information
network  --bootproto=static --device=eth0 --gateway=10.0.0.254 --ip=10.0.0.201 --nameserver=223.5.5.5 --netmask=255.255.255.0 --ipv6=auto --activate
network  --bootproto=static --device=eth1 --ip=172.16.1.0 --netmask=255.255.255.0 --ipv6=auto --activate
network  --hostname=Luffy-pizza

# Root password
rootpw --iscrypted $6$g.4ZvAHfTTcmO4Pj$LV0z.V8qYH0DP89NxCJS2SejOfcSvQZDq.TuFT6SsffDVsp7Z.tyDQEYXRSt3KGcsxzqvjoLtVNfLLL/9lxQE0
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc --nontp
user --groups=wheel --name=pizza --password=$6$Zm0fYVfS3AY3MIvL$tvzUY8g2Vx883oFL9lL35LHvvkdlrFh4kxRXht6FbJ9BTjJD846SFD7AGCZt/POvzMKmPOOWQs8BCtQUkx8Ri/ --iscrypted --gecos="pizza"
# System bootloader configuration
bootloader --location=mbr --boot-drive=sda
# Partition clearing information
clearpart --none --initlabel
# Disk partitioning information
part swap --fstype="swap" --ondisk=sda --size=1024
part /boot --fstype="xfs" --ondisk=sda --size=1024
part / --fstype="xfs" --ondisk=sda --size=100351

%packages
@^minimal
@compat-libraries
@core
@debugging
@development

%end

%addon com_redhat_kdump --disable --reserve-mb='auto'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
[root@kickstart ~]# 

 

白海明手動配置好的ks文件

#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
# old format: keyboard us
# new format:
keyboard --vckeymap=us --xlayouts='us'
# Root password
rootpw --iscrypted $1$0ZMwWP.q$wjRkxcadGHkUBDxxzVUYK.
# Use network installation
url --url="http://192.168.10.42/dvd"
# System language
lang en_US
repo --name="Red Hat Enterprise Linux" --baseurl=http://192.168.10.42/dvd --cost=100
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use text mode install
text
firstboot --disable
# SELinux configuration
selinux --disabled

# System services
services --enabled="chronyd"
ignoredisk --only-use=sda
# Firewall configuration
firewall --disabled
# Network information
network  --bootproto=dhcp --device=ens33
# Reboot after installation
reboot
# System timezone
timezone America/New_York
# System bootloader configuration
bootloader --append="crashkernel=auto" --location=mbr --boot-drive=sda
#autopart --type=lvm
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="xfs" --size=500
part swap --fstype="swap" --size=2048
part / --fstype="xfs" --grow --size=1

%packages
@core
%end

 

李導配置好的

# Kickstart Configurator for CentOS 7 by yao zhang
install
url --url="http://172.16.1.201/CentOS7/"
text
lang en_US.UTF-8
keyboard us
zerombr # 把mbr清空
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/
#rootpw 123456 明文的密碼
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 # 腳本 和ks文件放在一起
%end

查找幫助文件,去官網http://access.redhat.com/documenttion/en-us/red.......有中文

生成暗文密碼的命令在PPT中

 

default文件決定了怎么安裝系統,去哪里找系統,暫時了解這幾行就可以了,就這幾行起作用

# 注釋...說明一下centos7 kickstart configure
default ks
timeout 50 prompt 0 label ks kernel vmlinuz append initrd
=initrd.img ks=http://172.16.1.201/ks_config/CentOS7-ks.cfg net.ifnames=0 biosdevname=0 ksdevice=eth1

 

注:ksdevice指定網卡
將default保存
在服務器創建目錄並上傳ks文件,或者是編寫
腳本也放在這個目錄下

做完之后,試一下能不能訪問到default中的ks文件的目錄
[root@kickstart ks_config]# curl http://172.16.1.201/ks_config/CentOS7-ks.cfg

 

試一下能不能下載軟件
[root@kickstart ks_config]# curl http://172.16.1.201/CentOS7/

 

可以歡快的裝系統了!!
注:kickstart重啟后沒有將光盤掛載放在啟動任務中,開機需要再次掛載,才能正常安裝系統

Cobbler安裝系統(kickstart的升級版)

 把主機名改為cobbler

hosts文件添加10.0.0.202   cobbler

修改網卡(0和1),改為靜態的

因為我的配置文件中是已經設置成了靜態的,所以不用修改,但是無法聯網

所有的設置都是正確的,直到我先重啟cobbler,在關閉kickstart后,可以上網了!

在開啟kickstart后,kickstart沒有收到影響!

---------------------------------------------------------------------------------------------------------------

有一個坑,使用Centos6部署cobbler的時候,一直安裝不上Django14.需要單獨下載文件安裝

在centos7中不存在這個問題!

--------------------------------------------------------------------------------------------------------------------

開始安裝cobbler

# cobbler在epel源中,跟新epel源,將默認的yum源改為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

yum -y install cobbler cobbler-web dhcp tftp-server pykickstart httpd python-django
# 幫助文檔在官網manuals中
# 啟動cobbler systemctl start cobblerd.service
# 啟動http systemctl start httpd.service # 檢查 cobbler check
# 按照提示一個一個修正。里邊大部分都可以解決
[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.
server后面不能寫127.0.0.1,寫本地IP地址或者主機名
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. next_server不能是172.0.0.1,改成和上面一樣的
3 : change 'disable' to 'no' in /etc/xinetd.d/tftp
將默認關閉的yes改成no
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
開啟rsrsyncd.service並設置開機自啟動
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 默認的密碼cobbler,並提供了修改命令openssl passwd -1
8 : fencing tools were not found, and are required to use the (optional) power management features. install cman or fence-agents to use them fencing tools 工具 和腦裂有關

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

cobbler是可以自己去管理dhcp服務的,先配置一下他們,開兩個窗口,一個檢查,一個配置

 

主要配置文件的位置 /etc/cobbler/settings  
默認配置文件修改 /etc/cobbler/dhcp.template

防止誤重裝,防止不停的重裝 sed -i 's/pxe_just_once: 0/pxe_just_once: 1/' /etc/cobbler/settings 配置Cobbler統一管理DHCP 將0改為1 sed -i 's/manage_dhcp: 0/manage_dhcp: 1/' /etc/cobbler/settings 配置DHCP Cobbler模版,修改IP地址,並刪除22和23兩行 sed -i.ori 's#192.168.1#172.16.1#g;22d;23d' /etc/cobbler/dhcp.template
# 解決了上面的大部分問題。只剩下兩個,不用解決也行,根據提示運行命名
# 啟動服務,同步配置 systemctl start rsyncd systemctl enable rsyncd systemctl enable tftp.socket systemctl start tftp.socket systemctl restart cobblerd.service cobbler sync # 修改完后 就啟動了 # web操作cobbler https://10.0.0.202/cobbler_web 賬號密碼默認均為cobbler --實際中已經修改成了123456 接下來就在網頁上操作,enjoy!

 

 

並沒有訪問成功,在網上找到解決方案 https://blog.51cto.com/12643266/2339793 跟新django版本

仍是使用的密碼cobbler

使用Cobbler

導入鏡像

和kickstart類似,首先需要一個鏡像

左邊的Action---import DVD

prefix是名字,一定要寫上 。默認的路徑是/var/www/cobbler/ks_mirror里面

arch 是64位,之前默認是i386

path一定要寫上,/mnt/下一定要有光盤,要將光盤掛載到/mnt/下。mount  /dev/cdrom  /mnt/

點擊run開始同步,完成之后events中顯示complete,正在同步就是running

它不像kickstart可以直接讀取光盤內容,必須要導入到系統目錄中

進行配置

Configuration---settings

Configuration---distros:發行版本,就是導入進來了什么系統,可以修改一些參數,比如

  加入net.ifnames=0 biosdevname=0 修改網卡名

  修改這一項就可以了,save

Configuration---Kickstart Templates: 放了默認的ks文件,只是 一般都不用,只是為了學習用的吧

看一個編輯好的配置文件

# Cobbler for Kickstart Configurator for CentOS 7 by yao zhang
install
url --url=$tree    # 使用了tree變量,變量在Distros中定義了
text   # 文本模式安裝
lang en_US.UTF-8   # 字符集
keyboard us  # 鍵盤語言
zerombr  # 清空mbr
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"  # 啟動的
#Network information  network_config是一個文件名,在/var/lib/snippets下
$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')  # 開啟ks
$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

 

把這個文件保存到Configuration---Kickstart Templates中

在系統中查看在不在

[root@Cobbler centos7.4-x86_64]# ll /var/lib/cobbler/kickstarts/
total 60
-rw-r--r-- 1 root root 1028 Mar 14 16:44 centos7.ks

 

Configuration---Profiles

單擊上傳的系統,或者進入編輯

將kickstart 選擇我們我剛剛創建的ks文件

kernel options 可以加上那兩個內核參數,也可以不加

save!

自定義系統

我們要自動安裝系統,它是根據system里面的配置來安裝了。所以我們可以創建一個自定義的system

 

單擊add,下面的內容才能繼續添加

mac地址在(虛擬機網卡---高級---可以找到MAC地址)

save!!

做完修改要點擊一下Action下的Sync 。相當於在命令行執行 cobbler  sync

注意:有時候在恢復快照的時候,虛擬機的MAC地址會變,要留意

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM