一、簡介
1. 需要實現操作系統的部署
foreman提供了一個基於kickstart的部署工具,輸入一台服務器的部署網卡的mac地址和hostname、ip等信息,就能自動的幫我們部署完,並且,還可以生成一個部署iso,用於該服務器的重新部署。
foreman通過代理DNS、DHCP、TFTP完成了kickstart、cobbler、jumpstart等各種自動化安裝系統工具的圖形統一管理串口,實現的結果是只需要在foreman上指定各種模板(pxe、ks),不同的模板還可以嵌套各種片段(snippet)達到統一、簡化的目的。完成之后,便可以添加節點,關聯定義的各種模板生成各種pxe和ks文件實現自動化安裝。
2、foreman工作流程

Steps
- On the New Host page, the default VM configuration is shown and compute profiles can be applied.
- An unused IP address is requested from the DHCP smart proxy associated with the subnet.
- The IP address field is filled in on the New Host page.
- n/a
- The New Host page is submitted.
- Foreman contacts the compute resource to create the virtual machine.
- The compute resource creates a virtual machine on a hypervisor.
- The VM’s MAC address is returned from the compute resource and stored on the host.
- A reservation is created on the DHCP smart proxy associated with the subnet.
- DNS records are set up:
- A forward DNS record is created on the smart proxy associated with the domain.
- A reverse DNS record is created on the DNS smart proxy associated with the subnet.
- A PXELinux menu is created for the host in the TFTP smart proxy associated with the subnet.
- Foreman contacts the compute resource to power on the VM.
- The compute resource powers up the virtual machine.
- The host requests a DHCP lease from the DHCP server.
- The DHCP lease response is returned with TFTP options (next-server, filename) set.
- The host requests the bootloader and menu from the TFTP server.
- The PXELinux menu and OS installer for the host is returned over TFTP.
- The installer requests the “provision” template/script from Foreman.
- Foreman renders the template and returns the resulting kickstart/preseed to the host.
- Autosigning configuration for Salt (or Puppet) is added on the Salt or Puppet CA smart proxy.
- The installer notifies Foreman of a successful build in the postinstall script.
- The PXELinux menu is reverted to a “local boot” template.
- The host requests its configuration from Salt or Puppet.
- The host receives appropriate configuration using data defined in Foreman.
- Configuration reports and facts are sent from Salt or Puppet to Foreman and stored.
3、具體部署圍着如下流程:
安裝軟件源---》安裝foreman---》web界面設置 Provisioning Setup---》手動配置智能代理(dhcp、dns、tftp等)---》web界面部署第一個節點---》開啟虛擬機開始自動安裝os
web界面設置包括如下:(其中第一個節點加入foreman后,foreman會利用這個台節點創建一些信息,我們也可以按照需求新建一些信息,這個示例利用了這些信息,我們主要是配置安裝源及模板的設置。)
architectures:定義軟件架構
hardware models:軟件架構
installation media:安裝源
partition tables:分區方法
provisioning templates:部署模板
operating systems:最后一項配置,依賴之前的幾項配置,選各種配置如軟硬件架構,各種模板
4、foreman的安裝可以參考
二、設置資源及host菜單
1、解決安裝源
1)先掛載光盤
2)用httpd做源
我們前面已經部署了foreman所以系統已經部署了apache服務,
把掛載的光盤內容放在httpd目錄
mkdir -p /mnt/repo/centos7
mount /dev/cdrom /media
查看http目錄關注標紅的行即可,知道其默認目錄為
cat /etc/httpd/conf.d/05-foreman.conf
root@www pxelinux.cfg]# cat /etc/httpd/conf.d/05-foreman.conf # ************************************ # Vhost template in module puppetlabs-apache # Managed by Puppet # ************************************ <VirtualHost *:80> ServerName www.puppet-foreman.com ## Vhost docroot DocumentRoot "/usr/share/foreman/public" ## Directories, there should at least be a declaration for /usr/share/foreman/public <Directory "/usr/share/foreman/public"> Options SymLinksIfOwnerMatch AllowOverride None Require all granted </Directory>。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
。。。。。。。。。。。。。。。。。。。
。。。。。。。 <Directory ~ /usr/share/foreman/public/(assets|webpack)> # Use standard http expire header for assets instead of ETag <IfModule mod_expires.c> Header unset ETag FileETag None ExpiresActive On ExpiresDefault "access plus 1 year" </IfModule>。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
。。。。。。。。。。。。。。。。。。。。
。。。。。 </VirtualHost> PassengerPreStart http://www.puppet-foreman.com:80 [root@www pxelinux.cfg]#
得知目錄進行mv
mv /mnt/repo/ /usr/share/foreman/public/
3)創建安裝源文件
vim /etc/yum.repo.d/all_deps.repo
[root@www pxelinux.cfg]# cat /etc/yum.repos.d/all_deps.repo [centos7] name=centos7 baseurl=http://192.168.216.234/repo/centos7 gpgcheck=0 enabled=1
添加好源后記得yum clean all yum makecache
2、了解host導航菜單

架構,定義x86_64,i386,unix,windows等
硬件型號,定義硬件具體型號
安裝介質,定義安裝源媒介或者路徑
操作系統,定義操作系統具體型號比如centos,redhat,bsdfree等
模板
分區表
置備模板
3、設置安裝介質
我們現在添加安裝介質,可以是URL或者nfs,我們用剛剛添加源的URL
http://192.168.216.234/repo/centos7
由於架構和硬件型號及操作系統,我們裝玩foreman后本機就已經識別好,所以先添加安裝介質
流程:主機---》安裝介質---》創建媒體---》添加:name、path、操作系統


4、克隆置備模板並自定義編寫ks
編輯模板需要具有ks知識,我這里隨便克隆一下,也沒改,先測試一下
流程:主機---》置備模板---》選則kickstart default pxelinux 模板 克隆按鈕---》編輯相應的信息---》name,code---》類型-這里選pxelinux---》組合-這里操作系統選centos7---》提交
圖示如下:
1)主機---》置備模板---》

2)選則kickstart default pxelinux 模板 克隆按鈕---》
3)編輯相應的信息---》name,code---》

4)類型-這里選pxelinux---》

5)組合-這里操作系統選centos7---》提交

5、設置操作系統(operating system)
流程:主機---》操作系統--->點擊主機”CentOS 7.6.1810“ ---》操作系統參數---》分區表---》安裝介質---》模板---》提交
1)主機---》操作系統--->點擊主機”CentOS 7.6.1810“ ---》

2)操作系統參數---》

3)分區表---》

4)安裝介質

5)模板---》提交

注意:創建主機可以添加三個模板,一個是pxelinux模板,一個是置備模板,一個是完全模板,並且不是鎖定狀態,所以需要克隆一個新的模板,
1、三個原始模板分別是:完成模板:kickstart default finish
置備模板:kickstart default
pxelinux模板:kickstart default pxelinux
2、可以運行一下看看效果
3、克隆時我們分別在原名稱上添加test字樣即可,隨便改名字
(上面小結“4”已經說明如何克隆模板,這里不做介紹了)
具體請參考另外一篇文章,具體介紹模板,文章更新后在加上連接
三、設置代理及tftp文件
新版本我沒有找到Infrastructure->Provisioning setup導航,這個導航可以就像一個初始化指導一樣如圖下,輸入可用信息,然后生成配置步驟

1、手動設置代理服務及設置
os部署需要tftp及dhcp,dns可以暫時不部署
[root@www repodata]# foreman-installer \ > --enable-foreman-proxy \ > --foreman-proxy-tftp=true \ > --foreman-proxy-tftp-servername=192.168.216.234 \ > --foreman-proxy-dhcp=true \ > --foreman-proxy-dhcp-interface=ens33 \ > --foreman-proxy-dhcp-gateway=192.168.216.234 \ > --foreman-proxy-range="192.168.216.155 192.168.216.165"
如果是沒聯網的機器關閉下面這個源
> --foreman-configure-epel-repo=false 關閉
2、部署tftp還需要copy那個幾個文件
tftp需要提供bootloader,及內核文件,具體什么作用可以參考pxe+http+kickstart實驗原理
文檔上也有寫:https://www.theforeman.org/manuals/1.21/index.html#4.3.9TFTP
作用:
vmlinuz kenrnel file
vesamenu.32 圖形菜單
isolinux.cfg 開機菜單設定
initrd.img 開啟過程中核心組件參數
cd /usr/share/foreman/public/repo/centos7 cp isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default #這個其實不用拷了,后面說說 cp images/pxeboot/{vmlinuz,initrd.img} /var/lib/tftpboot/boot cp isolinux/{vesamenu.c32,boot.msg,splash.png} /var/lib/tftpboot/
這里手動設置其實非常簡單幾個要點
1、tftp默認目錄必須存在
2、pxe引導先決條件:(/usr/share/syslinux中)
pxelinux.0
menu.c32
chain.c32
3、內核文件放在/var/lib/tftpboot/boot/
4、需要准備/var/lib/tftpboot/pxelinux.cfg/default
其實這個文件不需要手動寫,我們在定義host得時候其實可以pxe和ks一起部署了,只是之前沒有找到方法耽誤了很長時間。下面介紹如何操作
四、開始部署節點
1、主機導航菜單創建主機
流程:主機---》創建主機---》主機---》操作系統---》接口---》puppet類---》參數---》附加信息
1)主機---》創建主機

2)主機---》

3)操作系統---》
4)接口---》
點擊編輯添加箭頭指向的信息,也就是你需要創建主機的mac/ip/域名

虛擬機得mac在下圖可以看到

5)puppet類---》參數---》
這里我們暫時不用設置
6)附加信息

2、提交后

3、創建好后自動創建pxe文件
之前還納悶沒有pxe文件怎么執行安裝菜單那,原理foreman創建主機后就已經生成了以mac為名稱的文件
下面是示例,因為之前又克隆一個主機所以是新的主機,所喲mac地址不同
[root@www pxelinux.cfg]# ll total 16 -rw-r--r-- 1 foreman-proxy foreman-proxy 557 Jun 4 11:53 01-00-50-56-2f-4e-fe -rw-r--r-- 1 foreman-proxy foreman-proxy 1058 Jun 3 17:49 default drwxr-xr-x 2 root root 142 Jun 3 15:33 test [root@www pxelinux.cfg]# cat 01-00-50-56-2f-4e-fe # This file was deployed via 'Kickstart default PXELinux test' template DEFAULT menu MENU TITLE Booting into OS installer (ESC to stop) TIMEOUT 100 ONTIMEOUT installer LABEL installer MENU LABEL Kickstart default PXELinux test KERNEL boot/centos7-test1-pPa2hWu9wvb7-vmlinuz APPEND initrd=boot/centos7-test1-pPa2hWu9wvb7-initrd.img ks=http://www.puppet-foreman.com/unattended/provision?token=241cc771-e27d-4f81-99a9-30e4cbc7628b network ksdevice=bootif ks.device=bootif BOOTIF=00-00-50-56-2f-4e-fe kssendmac ks.sendmac inst.ks.sendmac IPAPPEND 2
3、啟動虛擬機
創建虛擬機的步驟這里就省略了,可以自行創建即可
啟動虛擬機
看到菜單

回車下一步就自行安裝了,上面已經部署了分區及置備的ks 需要什么添加什么即可,開始自動部署之旅吧

五、排錯
1、出現如下錯誤的情況
![]()

注意出錯的信息:failed to fetch kickstart 獲取ks失敗
因為實驗環境沒有設置dns,所以我們可以把pxe文件的里面的ks=http://www.puppet-foreman.com域名換成你的ip即可。
參考1:https://www.theforeman.org/manuals/1.20/index.html#7.3GettingHelp
參考2:https://www.cnblogs.com/zhangxingeng/p/9560221.html
參考3:https://www.cnblogs.com/zhangxingeng/p/10248601.html
參考4:https://projects.theforeman.org/projects/foreman/wiki/TemplateWriting#PXE-Menus
參考5:https://projects.theforeman.org/projects/foreman/wiki
參考6:https://projects.theforeman.org/projects/foreman/wiki/Dynamic_disk_partitioning
轉載請注明出處:https://www.cnblogs.com/zhangxingeng/p/10268171.html
