十二、时间同步服务及自动化安装系统

一、时间同步介绍

       多主机协作工作时,各个主机的时间同步很重要,时间不一致会造成很多重要应用的故障,利用NTP(Network Time Protocol) 协议使网络中的各个计算机时间达到同步。

       时间同步实现:ntp,chrony

1.1 chrony使用

       实现NTP协议的的自由软件
       优势:
       - 更快的同步只需要数分钟而非数小时时间
       - 能够更好地响应时钟频率的快速变化
       - 在初始同步后不会停止时钟,以防对需要系统时间保持单调的应用程序造成影响
       - 提供更好的稳定性
       - 无需对服务器进行定期轮询,因此具备间歇性网络连接的系统仍然可以快速同步时钟

       两个主要程序:chronyd和chronyc
       - chronyd:后台运行的守护进程
       - chronyc:命令行用户工具

       常见用法示例:配置chrony服务,实现服务器时间自动同步
       服务端:

vi /etc/chrony.conf 
在以server开头的行首加上#注释符
在第27行添加内容:allow 172.16.77.0/24  #指定该网段允许访问本服务器
在第31行添加内容:local stratum 10 #即使server指令中时间服务器不可用,也允许将本地时间作为标准时间授时给其它客户端
保存退出

systemctl restart chronyd #重启服务使其更改生效

       客户端:

vi /etc/chrony.conf 
在以server开头的行首加上#注释符
在第7行添加内容:server 172.16.77.131 iburst #指定时钟服务器

systemctl restart chronyd #重启服务使其更改生效

       验证:

#客户端
chronyc sources -v
210 Number of sources = 1

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 172.16.77.131                10   6    17    44    +13us[  +16us] +/-  231us

#服务端
chronyc clients
Hostname                      NTP   Drop Int IntL Last     Cmd   Drop Int  Last
===============================================================================
172.16.77.132                   6      0   5   -    21       0      0   -     -

二、PXE介绍

       (Preboot Excution Environment)简称PXE,预启动执行环境。基于Client/Server的网络模式,支持远程主机通过网络从远端服务器下载映像,并由此支持通过网络启动操作系统
支持引导和安装Windows,linux等多种操作系统。

2.1 PXE工作原理

工作原理示意图
  1. Client向PXE Server上的DHCP发送IP地址请求消息,DHCP检测Client是否合法(主要是检测Client的网卡MAC地址),如果合法则返回Client的IP地址,同时将启动文件pxelinux.0的位置信息一并传送给Client。
  2. Client向PXE Server上的TFTP发送获取pxelinux.0请求消息,TFTP接收到消息之后再向Client发送pxelinux.0大小信息,试探Client是否满意,当TFTP收到Client发回的同意大小信息之后,正式向Client发送pxelinux.0
  3. Client执行接收到的pxelinux.0文件
  4. Client向TFTP Server发送针对本机的配置信息文件(在TFTP 服务的pxelinux.cfg目录下),TFTP将配置文件发回Client,继而Client根据配置文件执行后续操作。
  5. Client向TFTP发送Linux内核请求信息,TFTP接收到消息之后将内核文件发送给Client
  6. Client向TFTP发送根文件请求信息,TFTP接收到消息之后返回Linux根文件系统
  7. Client启动Linux内核
  8. Client下载安装源文件,读取自动化安装脚本

2.2 kickstart文件

       按特定语法给出的配置选项,通过读取事先给定的配置文件自动完成配置。

  • 命令段:指明各种安装前配置,如键盘类型等
  • 程序包段:指明要安装的程序包组或程序包,不安装的程序包等
    %packages
    @group_name
    package
    -package
    %end
  • 脚本段:
    %pre: 安装前脚本
    %post: 安装后脚本
    运行环境:安装完成的系统

必备命令
authconfig: 认证方式配置
authconfig --useshadow --passalgo=sha512

bootloader:bootloader的安装位置及相关配置
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"

keyboard: 设定键盘类型
lang: 语言类型
part: 创建分区
rootpw: 指明root的密码
timezone: 时区

依据某模板修改并生成新配置:
/root/anaconda-ks.cfg
示例:制作一个适用最小安装CentOS 7操作系统的kickstart文件

vi ks-min7.cfg
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --plaintext 000000
# System language
lang en_US
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use text mode install
text
firstboot --disable
# SELinux configuration
selinux --permissive


# Firewall configuration
firewall --disabled
# Network information
network  --bootproto=dhcp --device=eth0
# Reboot after installation
reboot
# System timezone
timezone Africa/Abidjan
# Use network installation
url --url="http://172.16.77.131/centos7"
# 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 / --asprimary --fstype="xfs" --size=17500
part /boot --asprimary --fstype="xfs" --size=512
part swap --fstype="swap" --size=2048

%packages
@^minimal
%end

%post
end

2.3 cobbler 介绍

       快速网络安装linux操作系统的服务,支持众多的Linux发行版:Red Hat、Fedora、CentOS、Debian、Ubuntu和SuSE,也可以支持网络安装windows。

2.3.1 cobbler 工作流程

cobbler工作流程
  1. client裸机配置了从网络启动后,开机后会广播包请求DHCP服务器(cobbler server)发送其分配好的一个IP
  2. DHCP服务器(cobbler server)收到请求后发送responese,包括其ip地址
  3. client裸机拿到ip后再向cobbler server发送请求OS引导文件的请求
  4. cobbler server告诉裸机OS引导文件的名字和TFTP server的ip和port
  5. client裸机通过上面告知的TFTP server地址通信,下载引导文件
  6. client裸机执行执行该引导文件,确定加载信息,选择要安装的os,期间会再向cobbler server请求kickstart文件和os image
  7. cobbler server发送请求的kickstart和os iamge
  8. client裸机加载kickstart文件
  9. client裸机接收os image,安装该os image

2.3.2 cobbler 基本使用

  • 安装软件包,配置服务
yum install cobbler dhcp  ##cobbler来自epel源
systemctl start cobblerd httpd tftp dhcpd
  • 检查环境配置
cobbler check
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 : 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.
4 : change ‘disable’ to ‘no’ in /etc/xinetd.d/rsync
5 : comment ‘dists’ on /etc/debmirror.conf for proper debian support
6 : comment ‘arches’ on /etc/debmirror.conf for proper debian support
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
  • 根据上面提示修改配置
    /etc/cobbler/settings关键参数:
    default_password_crypted: "1gEc7ilpP$pg5iSOj/mlxTxEslhRvyp/" #设置新安装机器的默认密码,通过openssl passwd -1命令生成
    manage_dhcp:1
    manage_tftpd:1
    next_server:<tftp服务器的 IP 地址>
    server:<cobbler服务器的 IP 地址>

/etc/cobbler/settings关键参数修改效果示例:

default_password_crypted: "$1$aJBnwt9m$q.FyarGk8XYOFFLsV1iUH/"
manage_dhcp: 1
manage_tftpd: 1
next_server: 172.16.77.131
server: 172.16.77.131

/etc/cobbler/dhcp.template关键参数:

subnet 172.16.77.0 netmask 255.255.255.0 {
     option routers             172.16.77.2;
     option domain-name-servers 172.16.77.2;
     option subnet-mask         255.255.255.0;
     range dynamic-bootp        172.16.77.100 172.16.77.254;

cobbler sync ##同步配置到数据目录
systemctl restart cobblerd dhcpd ##重启服务,使更改生效

  • 下载启动相关文件菜单(需连接外网)
cobbler get-loaders
cobbler sync
  • 导入安装源(使用光盘iso镜像)
cobbler import --path=/mnt --name=CentOS7.7-x86_64 --arch=x86_64
cobbler distro list
   CentOS7.7-x86_64
  • 准备kickstart文件并导入
cp ks7_mini.cfg /var/lib/cobbler/kickstarts/

vim /var/lib/cobbler/kickstarts/ks7_mini.cfg
url --url=$tree #将网络安装源地址修改为内置变量

cobbler profile add --name ks_mini7 --distro=CentOS7.7-x86_64 --kickstart=/var/lib/cobbler/kickstarts/ks7_mini.cfg
cobbler profile list
ks_mini7
  • 准备一台客户机,测试安装
           将第一引导项设为网卡启动


    设置网络启动

       选择对应的菜单选项,进入自动化安装过程


安装系统欢迎菜单

       待安装完毕重启之后,即可进入系统。


登录提示符
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268