s14.运维自动化之系统部署 -- 实战案例:kickstart文件制作过程

341 阅读2分钟

本文已参与「新人创作礼」活动, 一起开启掘金创作之路。

1.kickstart文件制作过程

范例:使用system-config-kickstart 一边来看看生成的kickstart 配置文件,我们一页一页观察

[root@centos7 isolinux]# system-config-kickstart
-bash: system-config-kickstart: command not found
[root@centos7 isolinux]# yum -y install system-config-kickstart
​
[root@centos7 isolinux]# system-config-kickstart
Could not open display because no X server is running.
Try running 'system-config-kickstart --help' for a list of options.
​

linux运行图形工具需要安装Xmanager

002.jpg

安装了Xmanager ,打开桌面的Xmanager 文件夹,进入tools目录,打开Xmanager - Passive文件

003.jpg

右下角任务栏,点上Xmanager - Passive的图标,会出现Server:0.0

[root@centos7 ~]# who am i
root     pts/0        2021-11-30 14:51 (172.31.0.1) #查看windows连接xshell的ip是172.31.0.1
​
[root@centos7 isolinux]# export DISPLAY=172.31.0.1:0.0 #这里172.31.0.1,是windows连接xshell的IP
​
[root@centos7 ~]# system-config-kickstart
/usr/share/system-config-kickstart/kickstartGui.py:104: GtkWarning: GtkSpinButton: setting an adjustment with non-zero page size is deprecated
  xml = gtk.glade.XML ("/usr/share/system-config-kickstart/system-config-kickstart.glade", domain="system-config-kickstart")
Loaded plugins: fastestmirror

1.1 Basic Configuration 基本配置

004.jpg

配置文件如下

┌────────────────────────────────────────────────────────┐
│#Basic Configuration
│========================================================
│#platform=x86, AMD64, or Intel EM64T#Default Language默认语言
│lang en_US
│# Keyboard 键盘
│keyboard 'us'# timezone 时区(勾选了"Use UTC clock" 会追加[--isUtc])
│timezone Asia/Shanghai
│# Root password
│rootpw --iscrypted $1$DBk7xfJp$Agxd303XUAfRKIf7gB8DG/
└──────────────────────────────────────────────────────────┘
​
┌────────────────────────────────────────────────────────┐
│#Advanced Configuration
│勾选就有,不勾没有
│========================================================
│# Reboot after installation
│reboot
│# Use text mode install
│text
└────────────────────────────────────────────────────────┘

1.2 Installation Method 安装方法

005.jpg

[root@centos7 ~]# yum -y install httpd;systemctl enable --now httpd
[root@centos7 ~]# mkdir -pv /var/www/html/centos/{6,7}
mkdir: created directory ‘/var/www/html/centosmkdir: created directory ‘/var/www/html/centos/6mkdir: created directory ‘/var/www/html/centos/7[root@centos7 ~]# mkdir -pv /var/www/html/rocky/8
mkdir: created directory ‘/var/www/html/rockymkdir: created directory ‘/var/www/html/rocky/8’
​
[root@centos7 ~]# mount /dev/sr0 /var/www/html/centos/7
mount: /dev/sr0 is write-protected, mounting read-only

http://172.31.0.7/centos/7/

006.jpg

007.jpg

配置文件如下

┌────────────────────────────────────────────────────────┐
│#Installation Method
│========================================================
│# Install OS instead of upgrade
│install
│# Upgrade existing installation
│upgrade
└────────────────────────────────────────────────────────┘
​
┌────────────────────────────────────────────────────────┐
│#Installation source
│选了哪项就写哪项
│========================================================
│# Use CDROM installation media
│cdrom
│# Use NFS installation media
│nfs --server=服务器 --dir=目录
│# Use network installation
│url --url="ftp://用户名:密码@服务器/目录"# Use network installation
│url --url="http://服务器/目录"# Use hard drive installation media
│harddrive --dir=目录 --partition=分区
└────────────────────────────────────────────────────────┘

1.3 Boot Loader Options 引导加载程序选项

008.jpg

#net.ifnames=0,系统安装后网卡是eth0、eth1网卡名

配置文件如下

┌────────────────────────────────────────────────────────────────┐
│#Installation Method&GRUB options&Install Options
│=================================================================
│ ┌────────────────────────────────────┐
│ │ 选择了Do not install a boot loader │
│ └────────────────────────────────────┘
│ # System bootloader configuration
│ bootloader --location=none
│
│ ┌────────────────────────────────────┐
│ │ 选择了install new boot loader │
│ └────────────────────────────────────┘
│ bootloader --append="ker" --location=mbr --password="123"#append是内核参数,location是bootloader安装位置,password是GRUB密码
└────────────────────────────────────────────────────────────────┘

1.4 Partition Information 分区信息

009.jpg

010.jpg

011.jpg

012.jpg

配置文件如下

┌─────────────────────────────────────────────────────┐
│# Master Boot Record#Master Boot Record选择了clear... 否则就没有
│======================================================
│# Clear the Master Boot Record
│zerombr
└─────────────────────────────────────────────────────┘
​
┌─────────────────────────────────────────────────────┐
│# Partitions&Disk Label
│======================================================
│# Partition clearing information
│clearpart --linux --initlabel
└─────────────────────────────────────────────────────┘
​
┌─────────────────────────────────────────────────────┐
│# Layout 分区
│part 挂载点 --fstype=文件系统 --size=大小(单位M)
│======================================================
│# Disk partitioning information
│part / --fstype="xfs" --size=10240
│part /boot --fstype="ext4" --size=1024
│part swap --fstype="swap" --size=2048
└─────────────────────────────────────────────────────┘

1.5 Network Configuration 网络配置

013.jpg

014.jpg

015.jpg

配置文件如下

┌─────────────────────────────────────────────────────┐
│# Network Configuration
│Centos7如果要写eth0,要加内核参数net.ifnames=0
│======================================================
│# Network information
│network --bootproto=dhcp --device=eth0
└─────────────────────────────────────────────────────┘

1.6 Authentication 身份验证

016.jpg

配置文件如下

┌───────────────────────────────────────────────────────────────┐
│# Authentication
│如果勾选Enable Fingerprint reader则追加参数 --enablefingerprint
│===============================================================
│# System authorization information
│auth --useshadow --passalgo=md5
└───────────────────────────────────────────────────────────────┘

1.7 Firewall Configuration 防火墙配置

017.jpg

配置文件如下

┌───────────────────────────────────────────────────────────────┐
│# Firewall Configuration
│===============================================================
│# SELinux configuration
│selinux --disabled或permissive或enforcing
│
│# Firewall configuration
│firewall --disabled或enabled
│#如果是enable,可以在追加:--http --ftp --telnet --smtp --ssh
│#还可以追加端口:--port=555:tcp,444:udp
└───────────────────────────────────────────────────────────────┘

1.8 Display Configuration 显示配置

018.jpg

配置文件如下

┌───────────────────────────────────────────────────────────────┐
│# Display Configuration
│===============================================================
│如果选了安装图形界面,就没有下面这句话
│# Do not configure the X Window System
│skipx
│# Run the Setup Agent on first boot
│firstboot --enabledisable
└───────────────────────────────────────────────────────────────┘

1.9 Package Selection 包选择

019.jpg

如果你的界面是什么都没有,那么需要把你现使用的yum仓库配置文件改一下

[root@centos7 ~]# vim /etc/yum.repos.d/base.repo
#把[base]改成[development]
[development]
​
[root@centos7 ~]# system-config-kickstart
/usr/share/system-config-kickstart/kickstartGui.py:104: GtkWarning: GtkSpinButton: setting an adjustment with non-zero page size is deprecated
  xml = gtk.glade.XML ("/usr/share/system-config-kickstart/system-config-kickstart.glade", domain="system-config-kickstart")
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile

020.jpg

现在就可以选择软件包了

配置文件如下

%packages
@development
-byacc
-cscope
-ctags
-diffstat
-doxygen
-elfutils
-gcc-gfortran
-git
-indent
-intltool
-patchutils
-rcs
-subversion
-swig
-systemtap
%end

1.10 Pre-Installation Script 安装前脚本

021.jpg

配置文件如下

%pre
i am pre
%end

1.11 Post-Installation Script 安装后脚本

[root@centos7 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:bv537rQA+rerONNjC479HKkVcy18Xz259TRkdO3fuks root@centos7
The key's randomart image is:
+---[RSA 2048]----+
|               .o|
|              . o|
|               + |
|           . .o +|
|        S + + o+B|
|       . . * o oX|
|        =.+ . Eoo|
|       *oB+.o+o. |
|      . **B*o**o |
+----[SHA256]-----+
[root@centos7 ~]# ls .ssh/
id_rsa  id_rsa.pub
[root@centos7 ~]# ssh-copy-id 127.0.0.1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:zb/8mo/ptS0h8eHVY1FDRuvh6aQj1opzpsD7khnYjSo.
ECDSA key fingerprint is MD5:f1:ee:83:81:26:e8:ed:a5:39:d5:fd:41:bc:a8:23:38.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@127.0.0.1's password: 
​
Number of key(s) added: 1
​
Now try logging into the machine, with:   "ssh '127.0.0.1'"
and check to make sure that only the key(s) you wanted were added.
​
[root@centos7 ~]# ls .ssh/
authorized_keys  id_rsa  id_rsa.pub  known_hosts
[root@centos7 ~]# cat .ssh/authorized_keys 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCmzWpLcgL5GXBcdikYgiHdUnnxUkKPPy1HCR1bGyR+pnKAjirE5aKgn5+wPrNVPAMHTz+v09rigKKX/RHQSSXF76UdaRwl9RCkgL5c7lePvdG+zUXhXO+lGDI7T+vgTWlIHeO3y/MSPNtbG4FcMcpegSi1G2DyzVZcL31izDnm3gK7QDLlOyL/781GVQM1MHzTTtnz6M1GyNdbhuJfguDcQRmHN+xflMZQYchOHLK6GTDGQIydbYVcQjFGmLPqvLNOsm1SKz3Jdw0+Gb0rGgq5mwAeAxMH1PDg7+/iuZV5w2sVZHf7s1hbOUd/9nTifCD7aH/AcFeJg9s1erCgoy0p root@centos7
​
mkdir -m 700 /root/.ssh
cat > /root/.ssh/authorized_keys <<EOF
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCmzWpLcgL5GXBcdikYgiHdUnnxUkKPPy1HCR1bGyR+pnKAjirE5aKgn5+wPrNVPAMHTz+v09rigKKX/RHQSSXF76UdaRwl9RCkgL5c7lePvdG+zUXhXO+lGDI7T+vgTWlIHeO3y/MSPNtbG4FcMcpegSi1G2DyzVZcL31izDnm3gK7QDLlOyL/781GVQM1MHzTTtnz6M1GyNdbhuJfguDcQRmHN+xflMZQYchOHLK6GTDGQIydbYVcQjFGmLPqvLNOsm1SKz3Jdw0+Gb0rGgq5mwAeAxMH1PDg7+/iuZV5w2sVZHf7s1hbOUd/9nTifCD7aH/AcFeJg9s1erCgoy0p root@centos7
EOF
chmod 600 /root/.ssh/authorized_keys

022.jpg

配置文件如下

%post --nochroot
i am post
%end

1.12 以上system-config-kickstart 执行后最终生成的kickstart文件如下

023.jpg

024.jpg

[root@centos7 ~]# ksvalidator ks7.cfg #检查语法格式
​
[root@centos7 ~]# cd /var/www/html/
[root@centos7 html]# ls
centos  rocky
[root@centos7 html]# mkdir ks
[root@centos7 html]# cp /root/ks7.cfg ks/centos7.cfg
[root@centos7 html]# ls ks/
centos7.cfg

http://172.31.0.7/ks/

025.jpg

[root@centos7 ~]# vim /var/www/html/ks/yum7.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-11-30
#FileName:      yum7.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup
cat > /etc/yum.repos.d/base.repo <<EOF
[base]
name=base
baseurl=https://mirrors.cloud.tencent.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-$releasever[extras]
name=extras
baseurl=https://mirrors.cloud.tencent.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-$releasever[updates]
name=updates
baseurl=https://mirrors.cloud.tencent.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-$releasever[centosplus]
name=centosplus
baseurl=https://mirrors.cloud.tencent.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-$releasever
EOF
cat > /etc/yum.repos.d/epel.repo <<EOF
[epel]
name=epel
baseurl=https://mirrors.cloud.tencent.com/epel/$releasever/$basearch/
gpgcheck=1
gpgkey=https://mirrors.cloud.tencent.com/epel/RPM-GPG-KEY-EPEL-$releasever
EOF
​
[root@centos7 ~]# cat /var/www/html/ks/centos7.cfg 
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $1$r6bueld4$D66DkBZeburidFINHY6Pf1
# System language
lang en_US
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use text mode install
text
firstboot --disable
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx
​
​
# Firewall configuration
firewall --disabled
# Network information
network  --bootproto=dhcp --device=eth0
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# Use network installation
url --url="http://172.31.0.7/centos/7"
# System bootloader configuration
bootloader --append="net.ifnames=0" --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="ext4" --size=1024
part swap --fstype="swap" --size=2048
part / --fstype="ext4" --grow --size=1
​
%post --interpreter=/bin/bash
curl http://172.31.0.7/ks/yum7.sh |bash
mkdir -m 700 /root/.ssh
cat > /root/.ssh/authorized_keys <<EOF
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCmzWpLcgL5GXBcdikYgiHdUnnxUkKPPy1HCR1bGyR+pnKAjirE5aKgn5+wPrNVPAMHTz+v09rigKKX/RHQSSXF76UdaRwl9RCkgL5c7lePvdG+zUXhXO+lGDI7T+vgTWlIHeO3y/MSPNtbG4FcMcpegSi1G2DyzVZcL31izDnm3gK7QDLlOyL/781GVQM1MHzTTtnz6M1GyNdbhuJfguDcQRmHN+xflMZQYchOHLK6GTDGQIydbYVcQjFGmLPqvLNOsm1SKz3Jdw0+Gb0rGgq5mwAeAxMH1PDg7+/iuZV5w2sVZHf7s1hbOUd/9nTifCD7aH/AcFeJg9s1erCgoy0p root@centos7
EOF
chmod 600 /root/.ssh/authorized_keys
%end

新建一个虚拟机

光盘启动

026.jpg

在这里按ESC

027.jpg

输入 linux ks=http://172.31.0.7/ks/centos7.cfg

028.jpg

正在开始安装

029.jpg

[root@centos7 ~]# ssh root@172.31.7.2
The authenticity of host '172.31.7.2 (172.31.7.2)' can't be established.
ECDSA key fingerprint is SHA256:7TnP0IvpkgAGUXH0rfUEYJhZ1neeB5XJ+Q/VWGsTUgQ.
ECDSA key fingerprint is MD5:24:de:20:16:4f:b0:cc:10:8d:18:e4:a8:f6:d3:e4:89.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.31.7.2' (ECDSA) to the list of known hosts.
Last login: Wed Nov 24 21:07:11 2021
[root@172 ~]# ls
anaconda-ks.cfg  original-ks.cfg
[root@172 ~]# cat anaconda-ks.cfg 
#platform=86, AMD64, or Intel EM64T#version=DEVEL
# System authorization information
auth --useshadow  --passalgo=sha512
# Install OS instead of upgrade
install
# Use text mode install
text
# Firewall configuration
firewall --disabled
firstboot --disable
ignoredisk --only-use=sda
# Keyboard layouts
# old format: keyboard us
# new format:
keyboard --vckeymap=us --xlayouts=''
# System language
lang en_US.UTF-8
​
# Network information
network  --bootproto=dhcp --device=eth0 --activate
network  --hostname=localhost.localdomain
# Reboot after installation
reboot
# Use network installation
url --url="http://172.31.0.7/centos/7"
# Root password
rootpw --iscrypted $1$r6bueld4$D66DkBZeburidFINHY6Pf1
# SELinux configuration
selinux --disabled
# System services
services --enabled="chronyd"
# Do not configure the X Window System
skipx
# System timezone
timezone Asia/Shanghai
# System bootloader configuration
bootloader --append="net.ifnames=0 crashkernel=auto" --location=mbr --boot-drive=sda
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part / --fstype="ext4" --size=48127
part swap --fstype="swap" --size=2048
part /boot --fstype="ext4" --size=1024
​
%post --interpreter=/bin/bash
mkdir -m 700 /root/.ssh
cat > /root/.ssh/authorized_keys <<EOF
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCmzWpLcgL5GXBcdikYgiHdUnnxUkKPPy1HCR1bGyR+pnKAjirE5aKgn5+wPrNVPAMHTz+v09rigKKX/RHQSSXF76UdaRwl9RCkgL5c7lePvdG+zUXhXO+lGDI7T+vgTWlIHeO3y/MSPNtbG4FcMcpegSi1G2DyzVZcL31izDnm3gK7QDLlOyL/781GVQM1MHzTTtnz6M1GyNdbhuJfguDcQRmHN+xflMZQYchOHLK6GTDGQIydbYVcQjFGmLPqvLNOsm1SKz3Jdw0+Gb0rGgq5mwAeAxMH1PDg7+/iuZV5w2sVZHf7s1hbOUd/9nTifCD7aH/AcFeJg9s1erCgoy0p root@centos7
EOF
chmod 600 /root/.ssh/authorized_keys
%end
​
%packages
@core
chrony
kexec-tools
​
%end
​
%addon com_redhat_kdump --enable --reserve-mb='auto'
​
%end
[root@172 ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
devtmpfs          487312       0    487312   0% /dev
tmpfs             497836       0    497836   0% /dev/shm
tmpfs             497836    7816    490020   2% /run
tmpfs             497836       0    497836   0% /sys/fs/cgroup
/dev/sda3       48377556 1304932  44592140   3% /
/dev/sda1         999320  103500    827008  12% /boot
tmpfs              99568       0     99568   0% /run/user/0
[root@172 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   50G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0    2G  0 part [SWAP]
└─sda3   8:3    0   47G  0 part /
sr0     11:0    1  9.5G  0 rom  
[root@172 ~]# exit
logout
Connection to 172.31.7.2 closed.