运维自动化之系统部署

装机启动菜单

solinux.bin:光盘引导程序,在mkisofs的选项中需要明确给出文件路径,这个文件属于SYSLINUX项目
isolinux.cfg:isolinux.bin的配置文件,当光盘启动后(即运行isolinux.bin),会自动去找isolinux.cfg文件
vesamenu.c32:是光盘启动后的安装图形界面,也属于SYSLINUX项目,menu.c32版本是纯文本的菜单
Memtest:内存检测,这是一个独立的程序
splash.jgp:光盘启动界面的背景图
vmlinuz是内核映像
initrd.img是ramfs (先cpio,再gzip压缩)

[root@centos6 isolinux]# cat isolinux.cfg
label linux
  menu label ^Install or upgrade an existing system    #^为图形界面光标跳转指针
  menu default
  kernel vmlinuz
  append initrd=initrd.img
label vesa
  menu label Install system with ^basic video driver
  kernel vmlinuz
  append initrd=initrd.img nomodeset
label rescue
  menu label ^Rescue installed system
  kernel vmlinuz
  append initrd=initrd.img rescue
label local
  menu label Boot from ^local drive
  localboot 0xffff
label memtest86
  menu label ^Memory test
  kernel memtest
  append -
###rescue模式就是在内核参数加rescue
label linux 为基本的配置,按ESC后输入 linux rescue  也能达到label rescue的目的
[root@CentOS6 ~]# cat anaconda-ks.cfg    #安装后生成的模板,可以根据此模板制作,也可使用kickstart安装
kickstart制作自动安装脚本
host B  创建自动安装脚本ks.cfg
[root@CentOS6 www]# yum install system-config-kickstart  #安装
在虚拟机上打开
[root@CentOS6 www]# system-config-kickstart   #打开
设置
修改网卡名net.ifnames=0,基于key验证,yum源配置,保存后可以根据本主机上生成的anaconda-ks.cfg修改
centos7安装包不出现可以修改yum源名称为development,或者保存后根据anaconda-ks.cfg格式添加
[root@CentOS6 www]# ksvalidator /root/Desktop/ks.cfg  语法检查
缺点:还需要光盘


host A(192.168.8.40)  创建yum服务器,ks.cfg服务   ks.cfg可以根据服务生成
[root@centos7 ~]# systemctl start httpd  
[root@centos7 ~]# systemctl enable httpd
[root@centos7 html]# mkdir -pv centos/{6,7}/os/x86_64
[root@centos7 html]# systemctl stop firewalld
[root@centos7 html]# systemctl disable firewalld
[root@centos7 ~]# mount /dev/sr0 /var/www/html/centos/7/os/x86_64/
[root@centos7 ~]# mount /dev/sr1 /var/www/html/centos/6/os/x86_64/
[root@centos7 ~]# mkdir ksdir
[root@CentOS6 www]# scp /root/Desktop/ks.cfg 192.168.8.40:/var/www/html/ksdir
[root@centos7 ~]# vim ksdir/ks.cfg      #修改
[root@centos7 html]# cat ksdir/ks.cfg 
firewall --disabled         #防火墙关闭
# Install OS instead of upgrade   #upgrade 更新
install      #安装
# Use CDROM installation media
url --url=http://192.168.8.40/centos/7/os/x86_64/   #指定安装源(网络)
# Root password
rootpw --plaintext A!111111
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use text mode install
text                     #文本安装界面
firstboot --disable
# System keyboard
keyboard us
# System language
lang en_US
# SELinux configuration
selinux --disabled            #禁用
# Installation logging level
logging --level=info
# Reboot after installation
reboot       #安装后重启
# System timezone
timezone  Asia/Shanghai
# Network information
network  --bootproto=dhcp --device=eth0 --onboot=on
# System bootloader configuration
bootloader --append="net.ifnames=0" --location=mbr
# Partition clearing information
clearpart --all  

# Disk partitioning information
part / --fstype="ext4" --size=100000
part /boot --fstype="ext4" --size=1000
part swap --fstype="swap" --size=2000

%packages       #安装包,为空默认为最小安装

%end

%post              #安装后脚本
mkdir /root/.ssh                     #基于key验证
chmod 700 /root/.ssh
cat > /root/.ssh/authorized_keys <<EOF
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDDUiYK7Px/n3hShURFmS0Z0RX2OqExJXuhm2uh5S3XZu3R6SjJ6BluLqH948m6ZSe7qpsJYUODFH/hdF1/eJ8XqCnKHwcwBEEyFoRQj41M74MKb0KQHO+4YlWdRsaHVCB/Kptobuw6vLdM5UGHrvQI1cj96NYHHPch7GPaD5Qgw1H1p7sDWEGxNbVPzc+9kWtR4G1e1ohHzrrCi6h5J9fSPIXZti5/8F7V2ztmhXAjxqSWWmgZxBiLZtSIAGd1NXLY4tCw46QXA8o3sPvgVqNtwKzFcPhlvGIWn3o3JwCJRzvXouFODzhddHK3JD+v7pwZWtyCXxP7akyaHnS2TyKt root@centos7.localdomain
EOF
chmod 600 /root/.ssh/authorized_keys       
mkdir /etc/yum.repos.d/bak                     #创建yum源
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
cat > /etc/yum.repos.d/test.repo << EOF
[test]
name=aliyun repo
baseurl=https://mirrors.aliyun.com/centos/\$releasever/os/x86_64/
gpgcheck=0
EOF
%end

host C
插入光盘,进入安装界面
按ESC进入boot界面
输入linux ks=http://192.168.8.40/ksdir/ks.cfg
回车即自行安装 

但每次都需要加载光盘,还是不太自动,属于半自动化,因此,若要完全自动化安装,需要借助DHCP和PXE(现在所有主机都支持)

DHCP
host A
[root@centos7 html]# yum install dhcp -y
[root@centos7 html]# rpm -ql dhcp     
/etc/dhcp/dhcpd.conf     #配置文件
/usr/sbin/dhcpd        #命令
[root@centos7 html]# cat /etc/dhcp/dhcpd.conf 
#   see /usr/share/doc/dhcp*/dhcpd.conf.example    #该文件为空,但说查看文件dhcpd.conf.example ,可以把dhcpd.conf.example 拷贝过来改名即可
[root@centos7 html]# cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf 
cp: overwrite ‘/etc/dhcp/dhcpd.conf’? y
[root@centos7 html]# vim /etc/dhcp/dhcpd.conf    #标点符号后不能带空格
default-lease-time 600;     #时长
subnet 192.168.8.0 netmask 255.255.255.0 {     #子网
  range 192.168.8.50 192.168.8.100;      #地址池
  option routers 192.168.8.2;     #网关
  option domain-name-servers 8.8.8.8,180.76.76.76;    #DNS服务器
  option domain-name "magedu.com";     #主机域名
}
[root@centos7 html]# systemctl start dhcpd


host B
[root@CentOS6 www]# dhclient    #获取地址
[root@CentOS6 www]# dhclient -d   #前台执行
[root@CentOS6 www]# cat /etc/resolv.conf   #查看dns
[root@CentOS6 www]# ls /var/lib/dhclient/   #有相关DHCP信息

TFTP

TFTP:Trivial File Transfer Protocol ,是一种用于传输文件的简单高级协议,是文件传输协议(FTP)的简化版本。用来传输比文件传输协议(FTP)更易于使用但功能较少的文件
FTP和TFTP的区别
1、安全性区别
FTP支持登录安全,具有适当的身份验证和加密协议,在建立连接期间需要与FTP身份验证通信
TFTP是一种开放协议,缺乏安全性,没有加密机制,与TFTP通信时不需要认证
2、传输层协议的区别
FTP使用TCP作为传输层协议,TFTP使用UDP作为传输层协议
3、使用端口的区别
FTP使用2个端口:TCP端口21,是个侦听端口;TCP端口20或更高TCP端口1024以上用于源连接
TFTP仅使用一个具有停止和等待模式的端口:端口69/udp
4、RFC的区别
FTP是基于RFC 959文档,带有其他RFC涵盖安全措施;TFTP基于RFC 1350文档
5、执行命令的区别
FTP有许多可以执行的命令(get,put,ls,dir,lcd)并且可以列出目录等
TFTP只有5个指令可以执行(rrq,wrq,data,ack,error)

PXE

基于Client/Server的网络模式,支持远程主机通过网络从远端服务器下载映像,并由此支持通过网络启动操作系统

[root@centos7 ~]# yum install tftp-server -y
[root@centos7 ~]# rpm -ql tftp-server 
/etc/xinetd.d/tftp
/usr/lib/systemd/system/tftp.service
/usr/lib/systemd/system/tftp.socket
/usr/sbin/in.tftpd

[root@centos7 ~]# systemctl start tftp.socket 
[root@centos7 ~]# cd /var/lib/tftpboot     #tftp共享目录
[root@centos7 tftpboot]# vim /etc/dhcp/dhcpd.conf   #编辑
  filename "pxelinux.0";     #tftp启动文件
  next-server 192.168.8.40;     #tftp服务器地址
[root@centos7 tftpboot]# yum install syslinux
[root@centos7 tftpboot]# rpm -ql syslinux
/usr/share/syslinux/pxelinux.0
[root@centos7 tftpboot]# cp /usr/share/syslinux/pxelinux.0 .
[root@centos7 tftpboot]# cp /var/www/html/centos/7/os/x86_64/isolinux/isolinux.cfg .
[root@centos7 tftpboot]# cp /var/www/html/centos/7/os/x86_64/isolinux/vmlinuz .
[root@centos7 tftpboot]# cp /var/www/html/centos/7/os/x86_64/isolinux/initrd.img .
[root@centos7 tftpboot]# cp /usr/share/syslinux/menu.c32 .
[root@centos7 tftpboot]# mkdir pxelinux.cfg
[root@centos7 tftpboot]# cp /var/www/html/centos/7/os/x86_64/isolinux/isolinux.cfg pxelinux.cfg/default
[root@localhost /var/www/html]# cat /var/lib/tftpboot/pxelinux.cfg/default 
default menu.c32
timeout 60

menu title PXE INSTALL MENU

label auto
  menu label ^Auto Install CentOS 7
  kernel vmlinuz
  append initrd=initrd.img ks=http://192.168.10.17/ksdir/cen7-ks.cfg

label manual
  menu label manual install CentOS 7
  kernel vmlinuz
  append initrd=initrd.img ks=http://192.168.10.17/ksdir/cen7-ks.cfg

label local defalut
  menu label Boot from ^local drive
  localboot 0xffff

menu end
[root@localhost /var/www/html]# tree /var/lib/tftpboot/
/var/lib/tftpboot/
├── initrd.img
├── menu.c32
├── pxelinux.0
├── pxelinux.cfg
│   └── default
└── vmlinuz

[root@localhost /var/www/html]# vim /etc/dhcp/dhcpd.conf
 subnet 192.168.10.0 netmask 255.255.255.0 {
  range 192.168.10.50 192.168.10.100;
  option domain-name-servers 8.8.8.8;
  option domain-name "mage.com";
  option routers 192.168.10.2;
  filename "pxelinux.0";
  next-server 192.168.10.17;

[root@localhost /var/www/html]# systemctl restart dhcpd
挂载光盘在centos/7/os/x86_64/下
实验:centos6自动安装
[root@localhost ~]# mkdir /var/lib/tftpboot/centos{6,7}
[root@localhost ~]# cat /var/lib/tftpboot/pxelinux.cfg/default 
default menu.c32
timeout 60

menu title PXE INSTALL MENU

label auto
  menu label ^Auto Install CentOS 7
  kernel /cnetos7/vmlinuz
  append initrd=/cnetos7/initrd.img ks=http://192.168.10.17/ksdir/cen7-ks.cfg

label auto
  menu label ^Auto Install CentOS 6
  kernel /cnetos6/vmlinuz
  append initrd=/cnetos6/initrd.img ks=http://192.168.10.17/ksdir/cen6-ks.cfg
  
label manual
  menu label manual install CentOS 7
  kernel vmlinuz
  append initrd=initrd.img ks=http://192.168.10.17/ksdir/cen7-ks.cfg

label local
  menu label Boot from ^local drive
  localboot 0xffff

menu end
centos6上部署服务器

安装前准备:关闭防火墙和SELINUX,DHCP服务器静态IP
1 安装相应软件包
yum install dhcp httpd tftp-server syslinux
chkconfig tftp on
chkconfig xinetd on
chkconfig httpd on
chkconfig dhcpd on
service httpd start
service xneted start
2 准备Yum 源和相关目录
mkdir -pv /var/www/html/centos/{6,ks}
mount /dev/sr0 /var/www/html/centos/6
3 准备kickstart文件
/var/www/html/centos/ks/centos6.cfg
注意权限:
chmod 644 /var/www/html/centos/ks/centos6.cfg
4 准备相关的启动文件
mkdir /var/lib/tftpboot/pxelinux.cfg/
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
cd /misc/cd/images/pxeboot/
cp vmlinuz initrd.img /var/lib/tftpboot
Cd /misc/cd/isolinux/
cp boot.msg vesamenu.c32 splash.jpg /var/lib/tftpboot
5 准备启动菜单文件
cp /misc/cd/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
vim /var/lib/tftpboot/pxelinux.cfg/default
default vesamenu.c32 指定菜单风格
#prompt 1
timeout 600
display boot.msg
menu background splash.jpg
menu title Welcome to wang CentOS 6
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
label auto
menu label ^Automatic Install Centos6
kernel vmlinuz
append initrd=initrd.img ks=http://192.168.100.100/centos/ks/centos6.cfg
label manual
menu label ^Manual Install Centos
kernel vmlinuz
append initrd=initrd.img inst.repo=http://192.168.100.100/centos/6
label local
menu default
menu label Boot from ^local drive
localboot 0xffff
目录结构如下:
tree /var/lib/tftpboot/
/var/lib/tftpboot/
├── boot.msg
├── initrd.img
├── pxelinux.0
├── pxelinux.cfg
│ └── default
├── splash.jpg
├── vesamenu.c32
└── vmlinuz
6 配置dhcp服务
cp /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcp/dhcpd.conf
vim /etc/dhcp/dhcpd.conf
option domain-name “magedu.com”;
option domain-name-servers 192.168.100.1;
subnet 192.168.100.0 netmask 255.255.255.0 {
range 192.168.100.1 192.168.100.200;
option routers 192.168.100.1;
filename “pxelinux.0”;
next-server 192.168.100.100;
}
service dhcpd start

cobbler

快速网络安装linux操作系统的服务,支持众多的Linux发行版:Red Hat、Fedora、CentOS、Debian、Ubuntu和SuSE,也可以支持网络安装windows
提供了CLI和Web的管理形式

[root@centos7 ~]# yum install cobbler dhcp -y 

[root@CentOS7 ~]# systemctl start tftp
[root@CentOS7 ~]# systemctl start httpd
[root@CentOS7 ~]# systemctl start cobblerd

[root@CentOS7 ~]# cobbler check

[root@CentOS7 ~]# openssl passwd -1        生成centos加密密码
Password: 
Verifying - Password: 
$1$IJpgAKtK$h7bybw2Nf8.Kj5gDC1mqA.
[root@centos7 ~]# vim /etc/cobbler/settings
next_server: 192.168.8.7
server: 192.168.8.7
default_password_crypted: "$1$IJpgAKtK$h7bybw2Nf8.Kj5gDC1mqA." 
manage_dhcp: 1 
[root@centos7 ~]# systemctl restart cobblerd

[root@CentOS7 ~]# vim /etc/cobbler/dhcp.template   #修改DHCP模板
subnet 192.168.8.0 netmask 255.255.255.0 {
     option routers             192.168.8.2;
     option domain-name-servers 192.168.8.2;
     option subnet-mask         255.255.255.0;
     range dynamic-bootp        192.168.8.100 192.168.8.254;
     default-lease-time         21600;
     max-lease-time             43200;
     next-server                $next_server;
[root@CentOS7 ~]# cobbler sync       #生成DHCP配置
[root@CentOS7 ~]# systemctl start dhcpd

[root@CentOS7 ~]# cobbler get-loaders     #下载配置文件及模板
[root@CentOS7 ~]# cobbler sync

[root@CentOS7 /var/lib/tftpboot]# mount /dev/sr0 /media/cdrom/
[root@CentOS7 ~]# cobbler import --name=CentOS7.7-x86_64 --path=/media/cdrom --arch=x86_64
[root@CentOS7 ~]# cobbler distro list
   CentOS7.7-x86_64
[root@CentOS7 ~]# cobbler profile list
   CentOS7.7-x86_64

[root@CentOS7 ~]# cp ks7-min.cfg /var/lib/cobbler/kickstarts/
[root@CentOS7 ~]# vim /var/lib/cobbler/kickstarts/ks7-min.cfg 
url --url=$tree
[root@CentOS7 ~]# cobbler profile add --name CentOS-7.7_mini --distro=CentOS7.7-x86_64 --kickstart=/var/lib/cobbler/kickstarts/ks7-min.cfg
[root@CentOS7 ~]# cat /var/lib/tftpboot/pxelinux.cfg/default
[root@CentOS7 ~]# cobbler profile remove --name=CentOS7.7-x86_64

yum cobbler-web
https://192.168.37.17/cobbler_web

#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $1$a0FLqLLM$/CkytBzhikO/RQQMAHSBG.
# System language
lang en_US
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use CDROM installation media
url --url=$tree
# Use text mode install
text
firstboot --disable
# SELinux configuration
selinux --disabled


# Firewall configuration
firewall --disabled
# Network information
network  --bootproto=dhcp 
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all
# Disk partitioning information
part /data --fstype="xfs" --ondisk=sda --size=51200
part swap --fstype="swap" --ondisk=sda --size=4096
part / --fstype="xfs" --ondisk=sda --size=102400
part /boot --fstype="xfs" --ondisk=sda --size=1024


%packages


%end

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值