系統自動部署

16.1 使用Centos 6部署PXE+Kickstart,安裝centos6系統,ks文件自定義

16.1.1 準備工作

關閉selinux重啓

[root@pxe ~]# setenforce 0
[root@pxe ~]# service iptables stop

安裝軟件

[root@pxe ~]# yum install -y tftp-server dhcp syslinux vsftpd
[root@pxe ~]# yum groupinstall -y "X Window System" "Desktop"
[root@pxe ~]# yum install -y system-config-kickstart	

掛載光盤

[root@pxe ~]# mkdir  /var/ftp/pub/cd
[root@pxe ~]# mount -t iso9660 /dev/cdrom /var/ftp/pub/cd

注:最好寫到/etc/fstab中

16.1.2 配置tftp服務

[root@pxe ~]# vim /etc/xinetd.d/tftp

disable = no

[root@pxe ~]# service xinetd restart
Stopping xinetd: [  OK  ]
Starting xinetd: [  OK  ]

16.1.3 配置dhcp服務

[root@pxe ~]# vim /etc/dhcp/dhcpd.conf

subnet 192.168.217.0 netmask 255.255.255.0 {
  range 192.168.217.200 192.168.217.210;
  option domain-name-servers 114.114.114.114;
  option routers 192.168.217.165;
  next-server 192.168.217.165;
  filename "pxelinux.0";  
}

[root@pxe ~]# service dhcpd restart
Starting dhcpd: [  OK  ]

16.1.4 配置ftp服務

[root@pxe ~]# service vsftpd restart
Shutting down vsftpd: [  OK  ]
Starting vsftpd for vsftpd: [  OK  ]

16.1.5 準備相關文件

[root@pxe ~]# mkdir /var/lib/tftpboot/pxelinux.cfg
[root@pxe ~]# cp /usr/share/syslinux/pxelinux.0  /var/lib/tftpboot/
[root@pxe ~]# cp /var/ftp/pub/cd/images/pxeboot/initrd.img /var/ftp/pub/cd/images/pxeboot/vmlinuz  /var/lib/tftpboot/
[root@pxe ~]# cp /var/ftp/pub/cd/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default

內容/var/lib/tftpboot/pxelinux.cfg/default

default linux
label linux
  menu label ^Install CentOS6.9
  menu default
  kernel vmlinuz
  append initrd=initrd.img ks=ftp://192.168.217.165/pub/ks.cfg

16.1.6 部署ks文件

打開圖形化界面

[root@pxe ~]# init 5

打開/usr/bin/system-config-kickstart

設置密碼

在這裏插入圖片描述

指定安裝源

可以是FTP的,也可以是HTTP的

在這裏插入圖片描述

如果是HTTP的,就要把掛載的目錄複製到/var/www/html/目錄下

開啓http服務

cp -r cp -rp /var/ftp/pub/cd /var/www/html

在這裏插入圖片描述

手動分區

在這裏插入圖片描述

設置網卡

在這裏插入圖片描述

選擇安裝兩個基礎包

在這裏插入圖片描述

保存ctrl +s

路徑要選擇

在這裏插入圖片描述

新建虛擬機

可以看到這裏分配的ip爲192.168.217.200,

在這裏插入圖片描述

16.1.7 完成

在這裏插入圖片描述

完成

16.2 使用Centos 7部署Cobbler,安裝Centos7與Ubuntu

16.2.1 Cobbler安裝

基礎環境部署

關閉selinux:

[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld

yum配置:

#講舊倉庫配置轉移到某目錄中 
[root@localhost ~]# mkdir /etc/yum.repos.d/old && mv /etc/yum.repos.d/C* /etc/yum.repos.d/old/ 

#安裝wget,添加aliyun Base倉庫及epel倉庫 
[root@localhost ~]# yum install -y wget 
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 
[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

16.2.2 Cobbler服務部署

安裝服務:

[root@localhost ~]# yum install cobbler cobbler-web dhcp tftp-server pykickstart httpd xinetd python-ctypes -y

服務自啓動:

[root@localhost ~]# systemctl enable httpd 
[root@localhost ~]# systemctl enable xinetd 
[root@localhost ~]# systemctl enable rsyncd 
[root@localhost ~]# systemctl enable tftp 
[root@localhost ~]# systemctl enable cobblerd

啓動服務:

[root@localhost ~]# systemctl restart httpd 
[root@localhost ~]# systemctl restart xinetd 
[root@localhost ~]# systemctl restart tftp 
[root@localhost ~]# systemctl restart cobblerd

執行cobbler check檢查運行環境:

[root@localhost ~]# 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 : SELinux is enabled. Please review the following wiki page for details on ensuring cobbler works correctly in your SELinux environment:
    https://github.com/cobbler/cobbler/wiki/Selinux
4 : change 'disable' to 'no' in /etc/xinetd.d/tftp
5 : 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.
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.

問題1,2,7:

默認密碼,setting,和dhcp

生成新的默認密碼

[root@localhost ~]# openssl passwd -1 -salt `openssl rand 15 -base64` '123' 
$1$Yo1o0fBf$pm5RkWqGdviwsu4YiWhF9/

注意: 此密碼是系統安裝後的默認密碼,建議使用強密碼

修改setting配置文件

[root@localhost ~]# sed -i 's%^server: 127.0.0.1%server: 192.168.217.155%g' /etc/cobbler/settings 
[root@localhost ~]# sed -i 's%^next_server: 127.0.0.1%next_server: 192.168.217.155%g' /etc/cobbler/settings 
[root@localhost ~]# sed -i 's%manage_dhcp: 0%manage_dhcp: 1%g' /etc/cobbler/settings 
[root@localhost ~]# sed -i 's%^default_password_crypted.*%default_password_crypted: "$1$Yo1o0fBf$pm5RkWqGdviwsu4YiWhF9/"%g' /etc/cobbler/settings

配置DHCP /etc/dhcp/dhcpd.conf(根據需求更改)

[root@localhost ~]# vim /etc/dhcp/dhcpd.conf
subnet 192.168.217.0 netmask 255.255.255.0 { 
	option routers 192.168.217.155; #網關地址 
	option domain-name-servers 114.114.114.114; #dns地址 
	option subnet-mask 255.255.255.0; #掩碼 
	range dynamic-bootp 192.168.217.10 192.168.217.100; #地址分配範圍 		default-lease-time 21600; 
	max-lease-time 43200; 
	next-server $next_server; #引用上面setting配置的nex-server值 
	filename "/pxelinux.0"; 
}

上面的不用改,只改這個就好了。/etc/cobbler/dhcp.template

還要修改/etc/cobbler/dhcp.template 文件

[root@localhost ~]# vim /etc/cobbler/dhcp.template
subnet 192.168.217.0 netmask 255.255.255.0 {
     option routers             192.168.217.155;
     option domain-name-servers 114.114.114.114;
     option subnet-mask         255.255.255.0;
     range dynamic-bootp        192.168.217.10 192.168.217.100;
     default-lease-time         21600;
     max-lease-time             43200;
     next-server                $next_server;
     class "pxeclients" {

問題3:

關閉selinux

[root@localhost ~]# setenforce 0 
#編輯/etc/selinux/config,並重啓生效 
SELINUX=disabled
init 6

問題4

啓動tftp服務

編輯tftp服務配置文件 /etc/xinetd.d/tftp

[root@localhost ~]# vim  /etc/xinetd.d/tftp 
service tftp 
{ 
	socket_type = dgram 
	protocol = udp 
	wait = yes 
	user = root 
	server = /usr/sbin/in.tftpd 
	server_args = -s /var/lib/tftpboot 
	disable = no #修改此處值爲
	no per_source = 11
	cps = 100 2 
	flags = IPv4 
}

問題5:

獲取引導文件

[root@localhost ~]# cobbler get-loaders

截圖

在這裏插入圖片描述

問題6:

debian相關文件獲取(如不需要部署debian系統可省略)

[root@localhost ~]# yum -y install debmirror 
#修改/etc/debmirror.conf配置文件,註釋掉@dists和@arches兩行 
[root@localhost ~]# vi /etc/debmirror.conf 
... 
#@dists="sid"; 
@sections="main,main/debian-installer,contrib,non-free"; 
#@arches="i386"; 
...

問題8:

電源管理工具(可選)

[root@localhost ~]# yum -y install fence-agents

重啓服務並同步狀態

[root@localhost ~]# systemctl restart cobblerd 
[root@localhost ~]# cobbler check
No configuration problems found.  All systems go.

這就完成

16.2.3 cobbler基礎配置Ubuntu

同步配置是配置生效

[root@localhost ~]# cobbler sync

配置distro:

主要用來定義和設置系統發行版本,裏包含了系統的內核,引導文件,以及安裝包等內容,就相當於一個LINUX系統的ISO鏡像。如:多個發行版本,同一發行版不同版本號。

1.首先上傳系統iso鏡像文件,此處以ubuntu12.04鏡像爲例,其他鏡像做法類似

wget http://releases.ubuntu.com/precise/ubuntu-12.04.5-server-amd64.iso

[root@localhost ~]# mkdir -p /system/ubuntu1204 ; mount -t iso9660 ~/ubuntu-12.04.5-server-amd64.iso /system/ubuntu1204

2.導入鏡像

[root@localhost ~]# cobbler import --name=ubuntu1204 --path=/system/ubuntu1204 --arch=x86_64 
task started: 2020-04-30_102441_import
task started (id=Media import, time=Thu Apr 30 10:24:41 2020)
Found a candidate signature: breed=freebsd, version=10.1
Found a candidate signature: breed=freebsd, version=10.0
Found a candidate signature: breed=freebsd, version=8.3
Found a candidate signature: breed=freebsd, version=8.2
Found a candidate signature: breed=freebsd, version=8.4
Found a candidate signature: breed=freebsd, version=11.0
Found a candidate signature: breed=freebsd, version=10.3
Found a candidate signature: breed=freebsd, version=11.2
Found a candidate signature: breed=freebsd, version=11.1
Found a candidate signature: breed=freebsd, version=12.0
Found a candidate signature: breed=freebsd, version=10.2
Found a candidate signature: breed=freebsd, version=11.3
Found a candidate signature: breed=freebsd, version=9.0
Found a candidate signature: breed=freebsd, version=9.1
Found a candidate signature: breed=freebsd, version=9.2
Found a candidate signature: breed=freebsd, version=9.3
Found a candidate signature: breed=ubuntu, version=precise
running: /usr/bin/file /var/www/cobbler/ks_mirror/ubuntu1204-x86_64/dists/precise/Release.gpg
received on stdout: /var/www/cobbler/ks_mirror/ubuntu1204-x86_64/dists/precise/Release.gpg: PGP signature

received on stderr: 
running: /usr/bin/file /var/www/cobbler/ks_mirror/ubuntu1204-x86_64/dists/precise/Release
received on stdout: /var/www/cobbler/ks_mirror/ubuntu1204-x86_64/dists/precise/Release: ASCII text

received on stderr: 
Found a matching signature: breed=ubuntu, version=precise
Adding distros from path /var/www/cobbler/ks_mirror/ubuntu1204-x86_64:
creating new distro: ubuntu1204-x86_64
trying symlink: /var/www/cobbler/ks_mirror/ubuntu1204-x86_64 -> /var/www/cobbler/links/ubuntu1204-x86_64
creating new profile: ubuntu1204-x86_64
associating repos
checking for apt repo(s)
adding apt repo for ubuntu1204-x86_64
Added repos for ubuntu1204-x86_64
*** TASK COMPLETE ***

3.查看distro配置

[root@localhost ~]# cobbler distro report 
Name                           : ubuntu1204-x86_64
Architecture                   : x86_64
TFTP Boot Files                : {}
Breed                          : ubuntu
Comment                        : 
Fetchable Files                : {}
Initrd                         : /var/www/cobbler/ks_mirror/ubuntu1204-x86_64/install/netboot/ubuntu-installer/amd64/initrd.gz
Kernel                         : /var/www/cobbler/ks_mirror/ubuntu1204-x86_64/install/netboot/ubuntu-installer/amd64/linux
Kernel Options                 : {}
Kernel Options (Post Install)  : {}
Kickstart Metadata             : {'tree': 'http://@@http_server@@/cblr/links/ubuntu1204-x86_64'}
Management Classes             : []
OS Version                     : precise
Owners                         : ['admin']
Red Hat Management Key         : <<inherit>>
Red Hat Management Server      : <<inherit>>
Template Files                 : {}

查看ubuntu相關文件:

[root@localhost ~]# cobbler list 
distros: 
	ubuntu1204-x86_64 #鏡像 

profiles: 
	ubuntu1204-x86_64 #配置,用於配置關聯自動應答ks文件 

systems: #可以爲不同的主機根據MAC設置地址及安裝參數 

repos: 
	ubuntu1204-x86_64 #倉庫,用於自定義倉庫 

images: 

mgmtclasses: 

packages: 

files:

**(自定義)配置profifile:**定義自動安裝應答文件

主要用來定義kickstart配置文件,安裝時依據配置文件進行定製化安裝,配置此項時要和distro進行關聯。

[root@localhost ~]# cobbler profile report 
Name                           : ubuntu1204-x86_64
TFTP Boot Files                : {}
Comment                        : 
DHCP Tag                       : default
Distribution                   : ubuntu1204-x86_64
Enable gPXE?                   : 0
Enable PXE Menu?               : 1
Fetchable Files                : {}
Kernel Options                 : {}
Kernel Options (Post Install)  : {}
Kickstart                      : /var/lib/cobbler/kickstarts/sample.seed #關聯的kickstart文件 (ubuntu和centos的ks文件格式有很大區別,不能混用)
Kickstart Metadata             : {}
Management Classes             : []
Management Parameters          : <<inherit>>
Name Servers                   : []
Name Servers Search Path       : []
Owners                         : ['admin']
Parent Profile                 : 
Internal proxy                 : 
Red Hat Management Key         : <<inherit>>
Red Hat Management Server      : <<inherit>>
Repos                          : []
Server Override                : <<inherit>>
Template Files                 : {}
Virt Auto Boot                 : 1
Virt Bridge                    : xenbr0
Virt CPUs                      : 1
Virt Disk Driver Type          : raw
Virt File Size(GB)             : 5
Virt Path                      : 
Virt RAM (MB)                  : 512
Virt Type                      : kvm

配置repo:定義本地或遠程倉庫源

查看默認repo

[root@localhost ~]# cobbler repo report 
Name                           : ubuntu1204-x86_64
Apt Components (apt only)      : ['main', 'universe']
Apt Dist Names (apt only)      : ['precise', 'precise-updates', 'precise-security']
Arch                           : x86_64
Breed                          : apt
Comment                        : 
Createrepo Flags               : <<inherit>>
Environment Variables          : {}
Keep Updated                   : True
Mirror                         : http://archive.ubuntu.com/ubuntu
Mirror locally                 : True
Owners                         : ['admin']
Priority                       : 99
External proxy URL             : 
RPM List                       : []
Yum Options                    : {'--verbose': '~', '--ignore-release-gpg': '~'}

自定義repo

cobbler repo add --help #創建
repo cobbler repo edit --help #修改repo

(自定義)配置system

主要用來定義內核參數,電源管理,計算機名,網絡接口參數等(客戶機),如:根據mac地址配置IP地址,通過和

profifile,distro關聯實現自動化安裝。

cobbler system add --help #創建
system cobbler system edit --help #修改system

測試

新建一臺虛擬機,啓動看到如下界面

在這裏插入圖片描述
選擇第二個菜單,是之前製作的distro,接下來服務器會自動按照之前的ks文件自動安裝

然後就等等等。

在這裏插入圖片描述

嫌慢可以修改配置文件/var/lib/cobbler/kickstarts/sample.seed

在這裏插入圖片描述

改成

192.168.217.155

/system/ubuntu1204

16.2.4 cobbler基礎配置Centos 7

創建掛載點,並進行掛載

[root@localhost ~]# mkdir /system/centos7
[root@localhost ~]# mount -o loop /dev/cdrom /centos7

查看掛載後的目錄

[root@localhost ~]# ls /system/centos7/
CentOS_BuildTag  GPL       LiveOS    RPM-GPG-KEY-CentOS-7
EFI              images    Packages  RPM-GPG-KEY-CentOS-Testing-7
EULA             isolinux  repodata  TRANS.TBLuildTag  EULA  images    LiveOS    repodata              RPM-GPG-KEY-CentOS-Testing-7
EFI              GPL   isolinux  Packages  RPM-GPG-KEY-CentOS-7  TRANS.TBL

導入鏡像

[root@localhost ~]# cobbler import --path=/system/centos7 --name=centos7 --arch=x86_64

查看導入後鏡像信息

[root@localhost ~]# cobbler distro report --name=centos7-x86_64
Name                           : centos7-x86_64
Architecture                   : x86_64
TFTP Boot Files                : {}
Breed                          : redhat
Comment                        : 
Fetchable Files                : {}
Initrd                         : /var/www/cobbler/ks_mirror/centos7-x86_64/images/pxeboot/initrd.img
Kernel                         : /var/www/cobbler/ks_mirror/centos7-x86_64/images/pxeboot/vmlinuz
Kernel Options                 : {}
Kernel Options (Post Install)  : {}
Kickstart Metadata             : {'tree': 'http://@@http_server@@/cblr/links/centos7-x86_64'}
Management Classes             : []
OS Version                     : rhel7
Owners                         : ['admin']
Red Hat Management Key         : <<inherit>>
Red Hat Management Server      : <<inherit>>
Template Files                 : {}

查看profile信息

[root@localhost ~]# cobbler profile report --name=centos7-x86_64
Name                           : centos7-x86_64
TFTP Boot Files                : {}
Comment                        : 
DHCP Tag                       : default
Distribution                   : centos7-x86_64
Enable gPXE?                   : 0
Enable PXE Menu?               : 1
Fetchable Files                : {}
Kernel Options                 : {}
Kernel Options (Post Install)  : {}
Kickstart                      : /var/lib/cobbler/kickstarts/sample_end.ks
Kickstart Metadata             : {}
Management Classes             : []
Management Parameters          : <<inherit>>
Name Servers                   : []
Name Servers Search Path       : []
Owners                         : ['admin']
Parent Profile                 : 
Internal proxy                 : 
Red Hat Management Key         : <<inherit>>
Red Hat Management Server      : <<inherit>>
Repos                          : []
Server Override                : <<inherit>>
Template Files                 : {}
Virt Auto Boot                 : 1
Virt Bridge                    : xenbr0
Virt CPUs                      : 1
Virt Disk Driver Type          : raw
Virt File Size(GB)             : 5
Virt Path                      : 
Virt RAM (MB)                  : 512
Virt Type                      : kvm

ks 文件

[root@localhost ~]# vim /var/lib/cobbler/kickstarts/centos7.ks
# Cobbler for Kickstart Configurator for CentOS 7.4 
# System language 
lang en_US 
# System keyboard 
keyboard us 
# System timezone 
timezone Asia/Shanghai 
#Root password 
rootpw --iscrypted $default_password_crypted 
#Use text mode install 
text 
#Install OS instead of upgrade 
install 
#Use NFS installation Media 
url --url=$tree 
#System bootloader configuration 
bootloader --location=mbr 
#Clear the Master Boot Record 
zerombr 
#Partition clearing information 
clearpart --all --initlabel 
#Disk partitioning information 
part /boot --fstype xfs --size 1024 --ondisk sda 
part swap --size 1024 --ondisk sda 
part / --fstype xfs --size 1 --grow --ondisk sda 
#System authorization --enablemd5 
auth --useshadow --enablemd5 
# Network information 
$SNIPPET('network_config') 
reboot 
firewall --disabled 
selinux --disabled 
skipx 

%pre 
$SNIPPET('log_ks_pre') 
$SNIPPET('kickstart_start') 
$SNIPPET('pre_install_network_config') 
$SNIPPET('pre_anamon') 
%end

%packages 

%end 

%post 
systemctl disable postfix.service 
@base 
@core 
tree 
nmap 
sysstat 
lrzsz 
dos2unix 
telnet 
iptraf 
ncurses-devel 
openssl-devel 
zlib-devel 
OpenIPMI-tools 
screen 
%end

編輯centos7鏡像所使用的kickstart文件

[root@localhost ~]# cobbler profile edit --name=centos7-x86_64 --kickstart=/var/lib/cobbler/kickstarts/centos7.ks
[root@localhost ~]# cobbler profile report --name=centos7-x86_64 |grep Kickstart
Kickstart                      : /var/lib/cobbler/kickstarts/centos7.ks
Kickstart Metadata             : {}

測試

在這裏插入圖片描述

完成

在這裏插入圖片描述

問題總結:https://blog.51cto.com/14012942/2436394

1、要關掉NAT的dhcp

2、ks文件中別有中文註釋

3、開始安裝前重啓下httpd,tftp,dhcpd,cobblerd

4、ks文件中的url別寫url --url=$tree,寫成http連接:url --url="http://192.168.217.155/cobbler/ks_mirror/CentOS7.6/"

5、CentOS7機器的內存爲1G時安裝失敗

6、安裝CentOS7 mini安裝包步驟都找不到,所以全部放到post裏,安裝後yum裝包

7、ks文件中的rootpw可以通過shadow文件來獲取

8、如果無特殊情況,一定要將/var/lib/tftpboot/pxelinux.cfg/default 默認啓動項設置爲local
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章