linux学习33-自动化运维cobbler

自动化安装工具cobbler

1 概述

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

  • PXE的二次封装,将多种安装参数封装到一个菜单

  • Python编写

  • 管理方式

    1. CLI:命令行管理
    2. Web:web网站管理
  • 需要安装的包

    1. cobbler(基于EPEL源),DHCP
    2. 其它cobbler 服务需要,但会自动依赖安装的包
      1. PXE
      2. rsync
      3. Http
      4. DNS
      5. Kickstart
      6. IPMI 电源管理
      7. 检查cobbler环境
      8. cobbler check
  • cobbler 相关术语

    1. 发行版:
      表示一个操作系统版本,它承载了内核和 initrd 的信息,以及内核参数等其他数据
    2. 配置文件:
      包含一个发行版、一个 kickstart 文件以及可能的存储库,还包含更多特定的内核参数等其他数据
    3. 系统:
      表示要配置的主机,它包含一个配置文件或一个镜像,还包含 IP 和 MAC 地址、电源管理(地址、凭据、类型)以及更为专业的数据等信息
    4. 存储库:
      保存一个 yum 或 rsync 存储库的镜像信息
    5. 镜像:
      可替换一个包含不属于此类别的文件的发行版对象(例如,无法分为内核和 initrd 的对象)
  • 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
  • cobbler 各种配置目录说明

    文件 配置内容
    /etc/cobbler/settings cobbler 主配置文件
    /etc/cobbler/settings cobbler 主配置文件
    /etc/cobbler/iso/ iso模板配置文件
    /etc/cobbler/pxe pxe模板文件
    /etc/cobbler/power 电源配置文件
    /etc/cobbler/user.conf web服务授权配置文件
    /etc/cobbler/users.digest web访问的用户名密码配置文件
    /etc/cobbler/dhcp.template dhcp服务器的的配置模板
    /etc/cobbler/dnsmasq.template dns服务器的配置模板
    /etc/cobbler/tftpd.template tftp服务的配置模板
    /etc/cobbler/modules.conf 模块的配置文件
  • cobbler 目录介绍

  1. 数据目录
    件目录 配置内容
    var/lib/cobbler/config/ 用于存放distros,system,profiles 等信息配置文件
    var/lib/cobbler/triggers/ 用于存放用户定义的cobbler命令
    var/lib/cobbler/kickstart/ 默认存放kickstart文件
    var/lib/cobbler/loaders/ 存放各种引导程序
  2. 镜像目录
    件目录 配置内容
    var/www/cobbler/ks_mirror/ 导入的发行版系统的所有数据
    var/www/cobbler/images/ 导入发行版的kernel和initrd镜像用于远程网络启动
    var/www/cobbler/repo_mirror/ yum 仓库存储目录
  • 日志目录

    文件目录 配置内容
    /var/log/cobbler/installing 客户端安装日志
    /var/log/cobbler/cobbler.log cobbler日志
  • cobbler 命令介绍

    文件目录 配置内容
    cobbler check 核对当前设置是否有问题
    cobbler list 列出所有的cobbler元素
    cobbler report 列出元素的详细信息
    cobbler sync 同步配置到数据目录,更改配置最好都要执行下
    cobbler reposync 同步yum仓库
    cobbler distro 查看导入的发行版系统信息
    cobbler system 查看添加的系统信息
    cobbler profile 查看配置信息
    1. 查看profiles,包含许多子命令
      [root@localhost  ~]$cobbler profile
      usage
      =====
      cobbler profile add
      cobbler profile copy
      cobbler profile dumpvars
      cobbler profile edit
      cobbler profile find
      cobbler profile getks
      cobbler profile list
      cobbler profile remove
      cobbler profile rename
      cobbler profile report
      
    2. 列出对应元素的详细信息
      cobbler distro report --name=centos-7-x86_64
    3. 查看引导文件
      cat /var/lib/tftpboot/pxelinux.cfg/default
    4. 同步cobbler配置
      cobbler sync
    5. 针对发行版生成的应答文件
      cobbler profile list
    6. 查看发行版的版本号
      cobbler distro list
  • /etc/cobbler/settings中重要的参数设置

    1. root登录口令,MD5算法,生成后修改
      default_password_crypted: "$1$gEc7ilpP$pg5iSOj/mlxTxEslhRvyp/"
    2. dhcp委托管理,1为委托cobbler管理,0为手动创建
      manage_dhcp:1
    3. tftpd委托管理,1为委托cobbler管理,0为手动创建
      manage_tftpd:1
    4. 该选项作用设为1,防止机器循环安装配置始终从网络引导,机器回传Cobbler安装完成,Cobbler将系统对象的netboot标志更改为false,强制要求机器从本地磁盘引导。
      pxe_just_once:1
    5. tftp服务器的 IP 地址
      next_server:
    6. cobbler服务器的 IP 地址
      server:

2 cobbler 实现步骤

  • 安装包,并设置服务

    1. 安装cobbler,需要epel源
    2. 安装DHCP,其他的有依赖性的包会自动安装
    [root@localhost  ~]$yum install cobbler dhcp
    
  • 启动服务

    [root@localhost ~]$systemctl start cobblerd  httpd  tftp
    
  • 检查配置,执行命令cobbler check,会提示那些工作没有做

    [root@localhost  /etc/yum.repos.d]$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.
    '指定cobbler服务器,可解析的主机名或启动服务器的Ip,所有使用它的机器都可以访问它'
    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
    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.
    '缺少boot-loaders必要文件,例如pxelinux.0, menu.c32等'
    5 : enable and start rsyncd.service with systemctl
    '这个服务用不着,不需要启动'
    6 : debmirror package is not installed, it will be required to manage debian deployments and repositories
    '与debian系统相关,这里使用的是centos,用不着,不用管'
    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
    '应答文件中root账号的默认值,需要修改,搜索default_password_crypted'
    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.
    
    1. 报错内容1解决办法,编辑文件/etc/cobbler/settings,找到如下行server,更改服务器ip
      [root@localhost  ~]$vim /etc/cobbler/settings
      # (dual homed, etc), you need to read the --server-override section
      # of the manpage for how that works.
      server: 127.0.0.1         <==修改前
      server: 172.20.50.100     <==更改后,本机的IP地址
      
    2. 报错内容2解决办法,编辑文件/etc/cobbler/settings,通过搜索next_server找到tftp服务地址
      [root@localhost  ~]$vim /etc/cobbler/settings    <==要修改内容在72行
      # if you do not set this correctly, this will be manifested in TFTP open timeouts.
      next_server: 127.0.0.1       <==修改器
      next_server: 172.20.50.100   <==更改后,本机IP地址
      
    3. 报错内容4解决办法,获得boot-loaders文件,执行命令cobbler get-loader
      方法1:可以联网的情况
      [root@localhost ~]$cobbler get-loaders
      '上述命令完成后,查看目录下的文件'
      [root@localhost  ~]$ls /var/lib/cobbler/loaders
      COPYING.elilo     COPYING.yaboot  grub-x86_64.efi  menu.c32    README
      COPYING.syslinux  elilo-ia64.efi  grub-x86.efi     pxelinux.0  yaboot
      'cobbler程序将这些启动必要程序,在/var/lib/tftpboot/下建立'
      [root@localhost  ~]$ls /var/lib/tftpboot/
      boot  etc  grub  images  images2  initrd.img  memdisk  menu.c32  ppc  pxelinux.0  	pxelinux.cfg  	s390x  vmlinuz
      
      方法2:不能联网
      需要安装syslinux程序包
      [root@localhost  ~]$cp /usr/share/syslinux/{pxelinux.0,menu.c32} /var/lib/tftpboot
      
    4. 报错内容7解决办法,重设root应答文件的口令centos7使用命令openssl passwd -1生成md5的口令,修改配置文件对应项
      '1. 生成MD5的加密口令'
      [root@localhost  ~]$openssl passwd -1
      Password: 
      Verifying - Password: 
      $1$BpAPCm/E$D9UJkY5N93hVNtH2gaPA20
      '2. 搜索default_password_crypted,修改为生成的口令   在101行'
      [root@localhost ~]$vim /etc/cobbler/settings
      # and put the output between the "" below.
      default_password_crypted: "$1$BpAPCm/E$D9UJkY5N93hVNtH2gaPA20"
      
    5. 建议修改项,设置为1让cobbler管理dhcp自动生成配置文件,就不需要手工写dhcp配置文件了
      [root@localhost ~]$vim /etc/cobbler/settings
      # set to 1 to enable Cobbler's DHCP management features.
      # the choice of DHCP management engine is in /etc/cobbler/modules.conf
      manage_dhcp: 0    <==修改为1
      
  • 完成检查配置后,重启服务并执行同步命令

[root@localhost ~]$systemctl restart cobblerd
[root@localhost ~]$cobblersync
  • 配置DHCP服务
    1. 修改配置文件,修改manage_dhcp: 1后,自行生成的文件
      [root@localhost  ~]$vim /etc/cobbler/dhcp.template 
      subnet 192.168.1.0 netmask 255.255.255.0 {                      <==修改项,本机网段
      option routers             192.168.1.5;                    <==修改项,本机网关
      option domain-name-servers 192.168.1.1;                    <==修改项,dns
      option subnet-mask         255.255.255.0;                  <==修改项,子网掩码
      range dynamic-bootp        192.168.1.100 192.168.1.254;    <==修改项,ip范围
      	 default-lease-time         21600;
      	 max-lease-time             43200;
         next-server                $next_server;
      
    2. 同步,查看,重启服务
      '同步修改后的数据'
      [root@localhost ~]$cobbler sync
      '查看是否生成了dhcp配置文件'
      [root@localhost  ~]$vim /etc/dhcp/dhcpd.conf	
      '重启服务'
      [root@localhost  ~]$systemctl restart cobblerd
      
  • 分别导入centos的安装源,并查看
    1. 导入光盘中的内容到系统cobbler里面,会自动生成仓库,生成一个发行版本以及对应的应答文件,默认为最小化安装

      [root@localhost  ~]$cobbler import --path=/misc/cd/  --name=Centos7.5-x86_64  --	arch=x86_64
      --name= 表示生成的仓库名称
      --path= 表示光盘路径
      --arch= 表示操作系统架构
      
    2. 查看生成的启动菜单,可根据需要修改

      [root@localhost  ~]$vim /var/lib/tftpboot/pxelinux.cfg/default
      
  • 系统默认的应答文件如果不能满足要求,可以自己关联应答文件
    1. 准备应答文件,修改仓库路径为cobbler的变量$tree,会自动识别并填写仓库路径
      编辑应答文件
      [root@localhost  ~]$ks_centos7.cfg
       # Use network installation
      url --url="http://192.168.32.6/centos/7"      <==找到此行
      url --url=$tree         					  <==修改为变量
      
    2. 修改密码,MD5加密
      # Root password
      rootpw --iscrypted   $1$BpAPCm/E$D9UJkY5N93hVNtH2gaPA20
      
    3. 将准备好的应答文件拷贝到/var/lib/cobbler/kickstarts下,这里重命名为ks7.cfg
      [root@localhost  ~]$cp ks_centos7.cfg /var/lib/cobbler/kickstarts/ks7.cfg
      
    4. 关联cobbler,执行以下内容
      [root@localhost ~]$cobbler distro list
       Centos7.5-x86_64
      [root@localhost ~]$cobbler  profile add --name=Centos7.5-x86_64_desktop  --distro=Centos7.5-x86_64   --kickstart=/var/lib/cobbler/kickstarts/ks7.cfg
      
           --name=:为新的应答文件起名,用户可以看到的启动菜单项
         --distro=:表示关联发行版
      --kickstart=:应答文件的绝对路径
              add :表示增加
      
    5. 查看修改效果
      [root@localhost ~]$cobbler profile list
      Centos7.5-x86_64
      Centos7.5-x86_64_desktop    <==生成项
      [root@localhost  ~]$vim /var/lib/tftpboot/pxelinux.cfg/default
      
    6. 删除菜单项
      [root@localhost  ~]$cobbler profile remove --name=Centos7.5-x86_64
      
  • 安装系统,测试效果

3. cobbler的web管理实现

在实现cobbler的基础之上,安装对应的cobbler的web服务组件,提供cobbler的基于web管理界面

  • 访问web界面
    通过https://cobblerserver/cobbler_web访问
    例如:https://172.20.50.100/cobbler_web

  • 安装,epel源
    增加了web服务的子配置文件,安装成功后要重新启动httpd服务。启动成功的话,查看ss -ntl可以看到443端口(https)
    yum install cobbler-web

  • 认证方式

    1. 认证方法配置文件:/etc/cobbler/modules.conf
    2. 支持多种验证方法,这里只介绍一下两种
      authn_configfile – use /etc/cobbler/users.digest (for basic setups)
      authn_pam – use PAM facilities
  • 使用authn_configfile模块认证cobbler_web用户,基于用户名和密码

    1. 配置文件[authentication]默认项为authn_configfile

      [root@localhost  ~]$vim /etc/cobbler/modules.conf
      [authentication]
      module=authn_configfile
      
    2. 登录用户和密码存放点,如下

      [root@localhost  ~]$cat /etc/cobbler/users.digest 
      |cobbler:|Cobbler:|a2d6bae81669d707b72c0bd9806e01f3|
      |    1   |    2   |               3                |
      1. 代表用户名
      2. realm(领域),意思是此用户账号是用于Cobbler服务自身的,C为大写
      3. 加密的口令,默认也是cobbler
      
    3. 如果没有文件/etc/cobbler/users.digest,可创建并添加所需的用户

      htdigest -c /etc/cobbler/users.digest Cobbler admin
      

      注意:添加第一个用户时,使用“-c”选项,后续添加其他用户时不要再使用,cobbler_web的realm只能为Cobbler

  • 使用authn_pam模块认证cobbler_web用户

    1. 配置文件[authentication]默认项改为authn_pam
      [root@localhost  ~]$vim /etc/cobbler/modules.conf
      [authentication]
      module = authn_pam  
      
    2. 用户信息存放在以下目录,需要创建linux账号,并授权管理cobbler
      '创建账号'
      [root@localhost  ~]$useradd -s /sbin/nologin cobbler2
      '设置密码'
      [root@localhost  ~]$echo magedu | passwd --stdin cobbler2
      Changing password for user cobbler2.
      passwd: all authentication tokens updated successfully.
      '授权管理cobbler'
      [root@localhost  ~]$vim /etc/cobbler/users.conf
      [admins]
      admin = "cobbler2"      <==填新创建的用户
      cobbler = ""            <==密码不需要填,为账户密码
      '重启cobblerd服务'
      [root@localhost  ~]$systemctl restart cobblerd
      

猜你喜欢

转载自blog.csdn.net/free050463/article/details/82851046