美文网首页
Cobbler 自动装机

Cobbler 自动装机

作者: 走在成长的道路上 | 来源:发表于2019-04-16 14:23 被阅读0次

    基础环境

    # 使用 more 命令查看基础系统版本信息
    $ more /etc/redhat-release 
    CentOS Linux release 7.5.1804 (Core)
    
    # 通过 getenforce 命令查看 selinux 是否禁用了
    # 通过 vi /etc/selinux/config 命令进行修改,然后重启才生效
    $ getenforce
    Disabled
    
    # 修改 hostname 临时
    $ hostname srv.cobbler.com
    # 修改 hostname, 永久生效, 通过 vim /etc/hostname 修改其内容即可。 
    
    # 设置 aliyun 源
    $ wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    

    安装基础软件

    1. 执行安装命令
    # 安装 cobbler 相关内容
    $ yum -y install cobbler cobbler-web dhcp tftp-server pykickstart httpd
    
    # 安装 django 库, 实际版本以 cobbler-web 依赖为主
    $ yum -y install python2-pip
    $ pip install Django==1.8.9
    
    1. 查看安装文件信息
    # rpm -ql cobbler  # 查看安装的文件,下面列出部分。
    /etc/cobbler                  # 配置文件目录
    /etc/cobbler/settings         # cobbler主配置文件,这个文件是YAML格式,Cobbler是python写的程序。
    /etc/cobbler/dhcp.template    # DHCP服务的配置模板
    /etc/cobbler/tftpd.template   # tftp服务的配置模板
    /etc/cobbler/rsync.template   # rsync服务的配置模板
    /etc/cobbler/iso              # iso模板配置文件目录
    /etc/cobbler/pxe              # pxe模板文件目录
    /etc/cobbler/power            # 电源的配置文件目录
    /etc/cobbler/users.conf       # Web服务授权配置文件
    /etc/cobbler/users.digest     # 用于web访问的用户名密码配置文件
    /etc/cobbler/dnsmasq.template # DNS服务的配置模板
    /etc/cobbler/modules.conf     # Cobbler模块配置文件
    
    # 可以在 /etc/cobbler/settings 中进行修改
    /var/lib/cobbler              # Cobbler数据目录
    /var/lib/cobbler/config       # 配置文件
    /var/lib/cobbler/kickstarts   # 默认存放kickstart文件
    /var/lib/cobbler/loaders      # 存放的各种引导程序
    
    /var/www/cobbler              # 系统安装镜像目录
    /var/www/cobbler/ks_mirror    # 导入的系统镜像列表
    /var/www/cobbler/images       # 导入的系统镜像启动文件
    /var/www/cobbler/repo_mirror  # yum源存储目录
    
    /var/log/cobbler              # 日志目录
    /var/log/cobbler/install.log  # 客户端系统安装日志
    /var/log/cobbler/cobbler.log  # cobbler日志
    
    1. 启动服务
    # 服务启动并设置为开机自启动模式
    [root@srv ~]# systemctl start httpd
    [root@srv ~]# systemctl enable httpd
    [root@srv ~]# systemctl start cobblerd
    [root@srv ~]# systemctl enable cobblerd
    # 检查当前状态
    [root@srv ~]# 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.
    # 设置PXE文件
    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.
    # 设置tftp
    3 : change 'disable' to 'no' in /etc/xinetd.d/tftp
    # 把网络的boot-loaders通过cobbler get-loaders后放在/var/lib/cobbler/loaders
    4 : 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.
    # 启动rsync服务
    5 : enable and start rsyncd.service with systemctl
    # 安装 debmirror 包
    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
    # 安装 cman 或 fence-agents 工具包
    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.
    

    上述问题逐一按如下方式解决:

    • 备份并修改 /etc/cobbler/settings 文件
    # 备份配置文件
    [root@srv ~]# cp /etc/cobbler/settings{,.ori}
    # 查看ip
    [root@srv ~]# ip addr
    [root@srv ~]# sed -i 's/server: 127.0.0.1/server: 172.19.114.130/' /etc/cobbler/settings
    # tftp 服务位置
    [root@srv ~]# sed -i 's/next_server: 127.0.0.1/next_server: 172.19.114.130/' /etc/cobbler/settings
    # 是否开启 dhcp 服务
    [root@srv ~]# sed -i 's/manage_dhcp: 0/manage_dhcp: 1/' /etc/cobbler/settings
    # 预防将机器中的安装循环配置为始终从网络引导
    [root@srv ~]# sed -i 's/pxe_just_once: 0/pxe_just_once: 1/' /etc/cobbler/settings
    
    # 构建装机密码
    [root@srv ~]# openssl passwd -1 -salt 'cobboy' 'cobbler'
    $1$cobboy$nm7lnHQbrcuc5J4z0Df2a/
    
    # 修改装机后默认密码
    [root@srv ~]# cat /etc/cobbler/settings | grep "default_password_crypted"
    default_password_crypted: "$1$cobboy$nm7lnHQbrcuc5J4z0Df2a/"
    
    • 修改 dhcp 配置

    /etc/cobbler/dhcp.template 文件中如下内容进行修改:

    # 修改为子网网段
    subnet 172.xxx.xxx.0 netmask 255.255.255.0 {
         # 网关信息
         option routers             172.xxx.xxx.254;
         # DNS 服务
         option domain-name-servers 114.114.114.114;
         option subnet-mask         255.255.255.0;
         # 可分配的网段
         range dynamic-bootp        172.xxx.xxx.130 172.xxx.xxx.140;
    

    多网卡主机配置如下:

    $ cp /usr/lib/systemd/system/dhcpd.service /etc/systemd/system/
    $ vi /etc/systemd/system/dhcpd.service
     ExecStart=/usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid <your_interface_name(s)>
    $ systemctl --system daemon-reload
    $ systemctl restart dhcpd.service
    
    • 安装周边设施
    # 下载网络引导文件
    $ cobbler get-loaders
    
    # 安装  fence-agents
    yum install fence-agents -y
    
    • 安装 debmirror
    $ yum install debmirror -y
    

    注释掉 /etc/debmirror.conf 文件中的 'dists' 及 'arches', 如下:

    # @dists="sid";
    @sections="main,main/debian-installer,contrib,non-free";
    # @arches="i386";
    
    • 检测状态,同步配置
    # 检查配置内容
    [root@repo ~]# cobbler check
    No configuration problems found.  All systems go.
    # 同步配置相关的修改,并生成相应的配置文件
    [root@repo ~]# cobbler sync
    task started: 2019-05-08_111445_sync
    task started (id=Sync, time=Wed May  8 11:14:45 2019)
    running pre-sync triggers
    cleaning trees
    removing: /var/lib/tftpboot/grub/images
    copying bootloaders
    trying hardlink /var/lib/cobbler/loaders/pxelinux.0 -> /var/lib/tftpboot/pxelinux.0
    trying hardlink /var/lib/cobbler/loaders/menu.c32 -> /var/lib/tftpboot/menu.c32
    trying hardlink /var/lib/cobbler/loaders/yaboot -> /var/lib/tftpboot/yaboot
    trying hardlink /usr/share/syslinux/memdisk -> /var/lib/tftpboot/memdisk
    trying hardlink /var/lib/cobbler/loaders/grub-x86_64.efi -> /var/lib/tftpboot/grub/grub-x86_64.efi
    trying hardlink /var/lib/cobbler/loaders/grub-x86.efi -> /var/lib/tftpboot/grub/grub-x86.efi
    copying distros to tftpboot
    copying images
    generating PXE configuration files
    generating PXE menu structure
    rendering DHCP files
    generating /etc/dhcp/dhcpd.conf
    rendering TFTPD files
    generating /etc/xinetd.d/tftp
    cleaning link caches
    running post-sync triggers
    running python triggers from /var/lib/cobbler/triggers/sync/post/*
    running python trigger cobbler.modules.sync_post_restart_services
    running: dhcpd -t -q
    received on stdout: 
    received on stderr: 
    running: service dhcpd restart
    received on stdout: 
    received on stderr: Redirecting to /bin/systemctl restart dhcpd.service
    
    running shell triggers from /var/lib/cobbler/triggers/sync/post/*
    running python triggers from /var/lib/cobbler/triggers/change/*
    running python trigger cobbler.modules.manage_genders
    running python trigger cobbler.modules.scm_track
    running shell triggers from /var/lib/cobbler/triggers/change/*
    *** TASK COMPLETE ***
    
    • 关闭防火墙及使能cobblerd服务
    systemctl stop firewalld
    systemctl disable firewalld
    
    systemctl restart rsyncd
    systemctl enable rsyncd
    
    systemctl start  dhcpd
    systemctl enable dhcpd
    
    systemctl restart  cobblerd
    systemctl enable cobblerd
    
    systemctl start  httpd
    systemctl enable httpd
    
    systemctl start  tftp
    systemctl enable tftp
    
    1. WebUI 使用
    • WebUI 访问出现 403 问题

    仅支持 https 方式访问,因此 https://<ip>/cobbler_web

    • WebUI 出现 500 Internal Server Error

    通过查看 httpd 运行日志,可知新安装机器中不包含 django 包,安装即可

    # 安装 django 包
    yum -y install python2-pip
    pip install Django==1.8.9
    # 重启 httpd
    [root@web ~]# systemctl restart httpd
    [root@web ~]# systemctl status httpd
    
    • WebUI 用户密码
    # 查看用户信息
    $ cat /etc/cobbler/users.conf
    [admins]
    admin = ""
    cobbler = ""
    
    # 修改密码为 cobbler
    $ htdigest /etc/cobbler/users.digest "Cobbler" cobbler
    

    重新登录 https://<ip>/cobbler_web 用户为 cobbler/cobbler 即可登录。

    镜像管理

    1. 定制 PXE 显示内容

    通过 vim /etc/cobbler/pxe/pxedefault.template 修改 MENU TITLE Cobbler | ... 内容即可。

    1. 添加镜像
    [root@srv data]# mount -t iso9660 -o loop,ro  ./CentOS-7-x86_64-Minimal-1804.iso /mnt/
    # 上述也可之间简单 `mount -o loop CentOS-7-x86_64-Minimal-1804.iso /mnt`
    [root@srv data]# cobbler import --path=/mnt/ --arch=x86_64 --name=centos7-mini --breed=redhat
    

    导入成功后,会输出:*** TASK COMPLETE ***

    1. 查看镜像列表及其信息
    # 镜像列表
    [root@srv data]# cobbler list
    distros:
       centos7-mini-x86_64
    profiles:
       centos7-mini-x86_64
    ...省略...
    
    # 仅查看 profile 列表
    [root@srv data]# cobbler profile list
       centos7-mini-x86_64
    
    # 查看详细信息
    [root@srv data]# cobbler report
    distros:
    ==========
    Name                           : centos7-mini-x86_64
    Architecture                   : x86_64
    ...省略...
    Initrd                         : /var/www/cobbler/ks_mirror/centos7-mini-x86_64/images/pxeboot/initrd.img
    Kernel                         : /var/www/cobbler/ks_mirror/centos7-mini-x86_64/images/pxeboot/vmlinuz
    ...省略...
    
    profiles:
    ==========
    Name                           : centos7-mini-x86_64
    ...省略...
    DHCP Tag                       : default
    Distribution                   : centos7-mini-x86_64
    Enable gPXE?                   : 0
    Enable PXE Menu?               : 1
    ...省略...
    Kickstart                      : /var/lib/cobbler/kickstarts/sample_end.ks
    ...省略...
    
    # 仅查看 profile 列表
    [root@srv data]# cobbler profile report centos7-mini-x86_64 | grep Kickstart
    Kickstart                      : /var/lib/cobbler/kickstarts/sample_end.ks
    Kickstart Metadata             : {}
    

    从上述内容可以才看到,导入的系统相关配置文件路径为 /var/lib/cobbler/kickstarts/sample_end.ks 在该文件中进行修改。

    1. 重装系统
    # 安装 koan 客户端
    [root@srv data]# yum install koan
    
    # 查看系统列表
    [root@srv data]# koan --server=bootserver.example.com --list=profiles
    
    # 重新替换安装
    [root@srv data]# koan --replace-self --server=bootserver.example.com --profile=centos7-mini-x86_64
    
    # 重启安装系统
    [root@srv data]# reboot
    

    这时,在启动项中会增加 kickXXXXXXXX 该项即为刚加入的启动项目。具体内容可以查看 /boot/grub2/grub.cfg 文件。

    1. 定制启动流程
    • 创建 kickstart 文件
    #Kickstart Configurator for cobbler by Jason Zhao
    #platform=x86, AMD64, or Intel EM64T
    #System  language
    lang en_US
    #System keyboard
    keyboard us
    #Sytem 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 16384 --ondisk sda
    part / --fstype xfs --size 1 --grow --ondisk sda
    
    #System authorization infomation
    auth  --useshadow  --enablemd5
    #Network information
    $SNIPPET('network_config')
    #network --bootproto=dhcp --device=em1 --onboot=on
    # Reboot after installation
    reboot
    #Firewall configuration
    firewall --disabled
    #SELinux configuration
    selinux --disabled
    #Do not configure XWindows
    skipx
    #Package install information
    %pre
    $SNIPPET('log_ks_pre')
    $SNIPPET('kickstart_start')
    $SNIPPET('pre_install_network_config')
    # Enable installation monitoring
    $SNIPPET('pre_anamon')
    %end
     
    %packages
    @ base
    @ core
    #@chinese-support
    wget
    # sysstat
    # iptraf
    # ntp
    # lrzsz
    # ncurses-devel
    # openssl-devel
    # zlib-devel
    # mysql
    # nmap
    # screen
    %end
     
    %post
    # adds the yum repositories to the installed system
    $yum_config_stanza
    systemctl disable postfix.service
    systemctl disable firewalld.service
    
    # 执行初始化脚本
    rpm -ihv https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
    
    # cobbler final steps
    $SNIPPET('kickstart_done')
    %end
    
    1. 内网 Yum 仓库
    • 增加仓库源
    [root@srv ~]# cobbler repo add --name=Centos6-UPDATES --mirror=http://mirrors.aliyun.com/centos/6/updates/x86_64/ --arch=x86_64 --breed=yum
    [root@srv ~]# cobbler repo add --name=Centos6-salt201611 --mirror=https://repo.saltstack.com/yum/redhat/6/x86_64/2016.11/ --arch=x86_64 --breed=yum
    [root@srv ~]# cobbler repo add --name=Centos7-Base --mirror= https://mirrors.aliyun.com/centos/7/os/x86_64/ --arch=x86_64 --breed=rsync
    [root@srv ~]# cobbler repo add --name=Centos7-extras  --mirror=https://mirrors.aliyun.com/centos/7/extras/x86_64/ --arch=x86_64 --breed=yum
    [root@srv ~]# cobbler repo add --name=epel6-x86_64-testing --mirror=http://mirrors.aliyun.com/epel/testing/6/x86_64/ --arch=x86_64 --breed=yum
    [root@srv ~]# cobbler repo add --name=Centos7-salt201707  --mirror=https://repo.saltstack.com/yum/redhat/7Server/x86_64/2017.7/ --arch=x86_64 --breed=yum
    [root@srv ~]# cobbler repo add --name=Centos6-Base --mirror=http://mirrors.aliyun.com/centos/6/os/x86_64/ --arch=x86_64 --breed=yum
    [root@srv ~]# cobbler repo add --name=epel6-x86_64 --mirror=http://mirrors.aliyun.com/epel/6/x86_64/ --arch=x86_64 --breed=yum
    [root@srv ~]# cobbler repo add --name=Centos7-epel --mirror=https://mirrors.aliyun.com/epel/7Server/x86_64/ --arch=x86_64 --breed=yum
    [root@srv ~]# cobbler repo add --name=Centos6-webtatic --mirror=http://sp.repo.webtatic.com/yum/el6/x86_64/ --arch=x86_64 --breed=yum
    [root@srv ~]# cobbler repo add --name=Centos7-webtatic --mirror=https://sp.repo.webtatic.com/yum/el7/x86_64/ --arch=x86_64 --breed=yum
    [root@srv ~]# cobbler repo add --name=Centos6-percona  --mirror=http://repo.percona.com/release/6Server/os/x86_64/ --arch=x86_64 --breed=yum
    [root@srv ~]# cobbler repo add --name=Centos7-salt201611 --mirror=https://repo.saltstack.com/yum/redhat/7/x86_64/2016.11/ --arch=x86_64 --breed=yum
    [root@srv ~]# cobbler repo add --name=Centos7-zabbix3 --mirror=http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/ --arch=x86_64 --breed=yum
    [root@srv ~]# cobbler repo add --name=Centos6-extras  --mirror=http://mirrors.aliyun.com/centos/6/extras/x86_64/ --arch=x86_64 --breed=yum
    [root@srv ~]# cobbler repo add --name=Centos6-zabbix3 --mirror=http://repo.zabbix.com/zabbix/3.4/rhel/6/x86_64/ --arch=x86_64 --breed=yum
    [root@srv ~]# cobbler repo add --name=Centos7-percona --mirror=http://repo.percona.com/release/7Server/os/x86_64/ --arch=x86_64 --breed=yum
    

    上述增加源的内容可以通过 cobbler repo list 进行查看。

    • 同步源镜像
    [root@srv ~]# cobbler reposync --only=Centos7-zabbix3
    
    1. 指定mac安装
    # 查看启动脚本
    [root@srv ~]# ls /var/lib/cobbler/kickstarts/CentOS-7-x86_64.cfg
    /var/lib/cobbler/kickstarts/CentOS-7-x86_64.cfg
    # 添加新机器及其基础网络配置
    [root@srv ~]# cobbler system add --name=n1.cobbler.com --mac=00:0c:29:f6:4a:00 --profile=centos7-mini-x86_64 --ip-address=172.xxx.xxx.131 --subnet=255.255.255.0 --gateway=172.xxx.xxx.254 --interface=eth0 --static=1 --hostname=n1.cobbler.com --name-servers="172.xxx.xxx.201" --kickstart=/var/lib/cobbler/kickstarts/CentOS-7-x86_64.cfg
    # 查看机器名
    [root@srv ~]# cobbler system list
       n1.cobbler.com
    

    然后开机网络启动即可。上述修改后通过 cobbler sync 命令进行同步,

    相关文章

      网友评论

          本文标题:Cobbler 自动装机

          本文链接:https://www.haomeiwen.com/subject/zyyuwqtx.html