cobbler介绍

Cobbler是一个Linux服务器快速网络安装的服务,由python开发,小巧轻便(15k行python代码),可以通过PXE的方式来快速安装、重装物理服务器和虚拟机,同时还可以管理DHCP,DNS,TFTP、RSYNC以及yum仓库、构造系统ISO镜像。

Cobbler可以使用命令行方式管理,也提供了基于Web的界面管理工具(cobbler-web),还提供了API接口,可以方便二次开发使用。

cobbler官网:http://cobbler.github.io/

cobbler工作流程

consul centos7 单机部署_System

cobbler组件

consul centos7 单机部署_centos_02

cobbler环境部署

1、系统环境准备

 部署cobbler操作系统为centos7.1

[root@linux-node1 ~]# cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)
[root@linux-node1 ~]# uname -rm
3.10.0-229.el7.x86_64 x86_64

 关闭selinux和firewalld服务

[root@linux-node1 ~]# cat /etc/selinux/config 
SELINUX=disabled

[root@linux-node1 ~]# systemctl  stop  firewalld
[root@linux-node1 ~]# systemctl  disable  firewalld

 安装阿里云的epel源

[root@linux-node1 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

2、部署cobbler

 安装cobbler软件包及其他辅助包

[root@linux-node1 ~]# yum install -y httpd dhcp tftp cobbler cobbler-web pykickstart xinetd

 启动httpd tftp cobbler并使其开机自启动

[root@linux-node1 ~]# systemctl start httpd
[root@linux-node1 ~]# systemctl start cobblerd
[root@linux-node1 ~]# systemctl start xinetd
[root@linux-node1 ~]# systemctl enable httpd
[root@linux-node1 ~]# systemctl enable cobblerd
[root@linux-node1 ~]# systemctl enable xinetd

 执行cobbler  check命令,它会提示我们需要做一些配置

consul centos7 单机部署_System_03

consul centos7 单机部署_centos_04

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.
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 : 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.
5 : enable and start rsyncd.service with systemctl
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
8 : fencing tools were not found, and are required to use the (optional) power management features.
install cman or fence-agents to use them

View Code

 按照cobbler  check执行命令的提示,做如下操作

#生成一个密码,作为模板里的密码使用
[root@linux-node1 ~]# openssl passwd -1  -salt 'cobbler'  'cobbler'
$1$cobbler$M6SE55xZodWc9.vAKLJs6.

[root@linux-node1 ~]# vim /etc/cobbler/settings
server: 192.168.1.180
next_server: 192.168.1.180
default_password_crypted: "$1$cobbler$M6SE55xZodWc9.vAKLJs6."
#设置cobbler来管理dhcp
manage_dhcp: 1
#设置cobbler来管理tftp
manage_tftpd: 1
#设置cobbler支持yum源安装
yum_post_install_mirror: 1

#修改tftp配置,启动tftp
[root@linux-node1 ~]# vim /etc/xinetd.d/tftp 
disable                 = no

#从cobbler的github上下载一些需要的文件
[root@linux-node1 ~]# cobbler get-loaders

#启动rsyncd服务并设置开机启动
[root@linux-node1 ~]# systemctl start rsyncd
[root@linux-node1 ~]# systemctl enable rsyncd

  重启cobblerd服务并再次检查cobbler。显示下面的状况,说明cobbler的配置正常。

[root@linux-node1 ~]# systemctl restart cobblerd
[root@linux-node1 ~]# cobbler check
The following are potential configuration items that you may want to fix:
 
1 : debmirror package is not installed, it will be required to manage debian deployments and repositories
2 : 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.

 修改cobbler定义的dhcp模板

[root@linux-node1 ~]# vim  /etc/cobbler/dhcp.template 
subnet 192.168.1.0 netmask 255.255.255.0 {
     option routers             192.168.1.1;
     option domain-name-servers 192.168.1.1;
     option subnet-mask         255.255.255.0;
     range dynamic-bootp        192.168.1.100 192.168.1.200;
     default-lease-time         21600;
     max-lease-time             43200;
     next-server                $next_server;
}

 执行cobbler sync的时候,cobbler的dhcp模板的设置会将dhcp的配置文件dhcpd.conf的配置内容覆盖

[root@linux-node1 ~]# systemctl restart cobblerd
[root@linux-node1 ~]# cobbler sync

[root@linux-node1 ~]# cat  /etc/dhcp/dhcpd.conf 
# ******************************************************************
# Cobbler managed dhcpd.conf file
# generated from cobbler dhcp.conf template (Sun Nov 19 14:02:13 2017)
# Do NOT make changes to /etc/dhcpd.conf. Instead, make your changes
# in /etc/cobbler/dhcp.template, as /etc/dhcpd.conf will be
# overwritten.
# ******************************************************************
subnet 192.168.1.0 netmask 255.255.255.0 {
     option routers             192.168.1.1;
     option domain-name-servers 192.168.1.1;
     option subnet-mask         255.255.255.0;
     range dynamic-bootp        192.168.1.100 192.168.1.200;
     default-lease-time         21600;
     max-lease-time             43200;
     next-server                192.168.1.180;
}

 导入镜像到cobbler中,先将iso光驱启动并放入centos7.iso或centos6.iso镜像

#导入centos7的镜像
[root@linux-node1 ~]# mount  /dev/cdrom  /mnt/
[root@linux-node1 ~]# cobbler  import  --name=CentOS7-x86_64 --path=/mnt/  --arch=x86_64

#导入centos6的镜像
[root@linux-node1 ~]# umount  /mnt/
[root@linux-node1 ~]# mount /dev/cdrom  /mnt/
[root@linux-node1 ~]# cobbler  import  --name=CentOS6-x86_64 --path=/mnt/  --arch=x86_64

 查看系统镜像的目录和profile文件如下

[root@linux-node1 ~]# ll /var/www/cobbler/ks_mirror/
total 8
dr-xr-xr-x 7 root root 4096 Oct 24  2014 CentOS6-x86_64
drwxr-xr-x 8 root root 4096 Apr  1  2015 CentOS7-x86_64

[root@linux-node1 ~]# cobbler profile  list
   CentOS6-x86_64
   CentOS7-x86_64

 制作centos7和centos6的ks文件如下:

#首先进入/var/lib/cobbler/kickstarts目录,因为cobbler定义的镜像关联的ks文件默认目录
[root@linux-node1 kickstarts]# /var/lib/cobbler/kickstarts

#编辑centos7的ks文件
[root@linux-node1 kickstarts]# vim  CentOS-7-x86_64.cfg
#Kickstart Configurator for cobbler by goser
#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 300 --ondisk sda
part swap --size 4096 --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
sysstat
iptraf
ntp
lrzsz
ncurses-devel
openssl-devel
zlib-devel
OpenIPMI-tools
mysql
nmap
screen
%end
 
%post
systemctl disable postfix.service
$yum_config_stanza
%end

#编辑centos6的ks文件
#Kickstart Configurator for cobbler by goser
#platform=x86, AMD64, or Intel EM64T
key --skip
#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 yes
#Partition clearing information
clearpart --all --initlabel
#Disk partitioning information
part /boot --fstype ext4 --size 300 --ondisk sda
part swap --size 4096 --ondisk sda
part / --fstype ext4 --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
%packages
@ base
@ chinese-support
@ core
sysstat
iptraf
ntp
e2fsprogs-devel
keyutils-libs-devel
krb5-devel
libselinux-devel
libsepol-devel
lrzsz
ncurses-devel
openssl-devel
zlib-devel
OpenIPMI-tools
mysql
lockdev
minicom
nmap
 
%post
#/bin/sed -i 's/#Protocol 2,1/Protocol 2/' /etc/ssh/sshd_config
/bin/sed  -i 's/^ca::ctrlaltdel:/#ca::ctrlaltdel:/' /etc/inittab
/sbin/chkconfig --level 3 diskdump off
/sbin/chkconfig --level 3 dc_server off
/sbin/chkconfig --level 3 nscd off
/sbin/chkconfig --level 3 netfs off
/sbin/chkconfig --level 3 psacct off
/sbin/chkconfig --level 3 mdmpd off
/sbin/chkconfig --level 3 netdump off
/sbin/chkconfig --level 3 readahead off
/sbin/chkconfig --level 3 wpa_supplicant off
/sbin/chkconfig --level 3 mdmonitor off
/sbin/chkconfig --level 3 microcode_ctl off
/sbin/chkconfig --level 3 xfs off
/sbin/chkconfig --level 3 lvm2-monitor off
/sbin/chkconfig --level 3 iptables off
/sbin/chkconfig --level 3 nfs off
/sbin/chkconfig --level 3 ipmi off
/sbin/chkconfig --level 3 autofs off
/sbin/chkconfig --level 3 iiim off
/sbin/chkconfig --level 3 cups off
/sbin/chkconfig --level 3 openibd off
/sbin/chkconfig --level 3 saslauthd off
/sbin/chkconfig --level 3 ypbind off
/sbin/chkconfig --level 3 auditd off
/sbin/chkconfig --level 3 rdisc off
/sbin/chkconfig --level 3 tog-pegasus off
/sbin/chkconfig --level 3 rpcgssd off
/sbin/chkconfig --level 3 kudzu off
/sbin/chkconfig --level 3 gpm off
/sbin/chkconfig --level 3 arptables_jf off
/sbin/chkconfig --level 3 dc_client off
/sbin/chkconfig --level 3 lm_sensors off
/sbin/chkconfig --level 3 apmd off
/sbin/chkconfig --level 3 sysstat off
/sbin/chkconfig --level 3 cpuspeed off
/sbin/chkconfig --level 3 rpcidmapd off
/sbin/chkconfig --level 3 rawdevices off
/sbin/chkconfig --level 3 rhnsd off
/sbin/chkconfig --level 3 nfslock off
/sbin/chkconfig --level 3 winbind off
/sbin/chkconfig --level 3 bluetooth off
/sbin/chkconfig --level 3 isdn off
/sbin/chkconfig --level 3 portmap off
/sbin/chkconfig --level 3 anacron off
/sbin/chkconfig --level 3 irda off
/sbin/chkconfig --level 3 NetworkManager off
/sbin/chkconfig --level 3 acpid off
/sbin/chkconfig --level 3 pcmcia off
/sbin/chkconfig --level 3 atd off
/sbin/chkconfig --level 3 sendmail off
/sbin/chkconfig --level 3 haldaemon off
/sbin/chkconfig --level 3 smartd off
/sbin/chkconfig --level 3 xinetd off
/sbin/chkconfig --level 3 netplugd off
/sbin/chkconfig --level 3 readahead_early off
/sbin/chkconfig --level 3 xinetd off
/sbin/chkconfig --level 3 ntpd on
/sbin/chkconfig --level 3 avahi-daemon off
/sbin/chkconfig --level 3 ip6tables off
/sbin/chkconfig --level 3 restorecond off
/sbin/chkconfig --level 3 postfix off

 更改centos6和centos7的ks文件位置,并更改centos7的内核设置,让centos7的网络接口名称类似于centos6的eth0,配置如下:

[root@linux-node1 ~]# cobbler profile  edit  --name=CentOS6-x86_64 --kickstart=/var/lib/cobbler/kickstarts/CentOS-6-x86_64.cfg 
[root@linux-node1 ~]# cobbler profile  edit  --name=CentOS7-x86_64 --kickstart=/var/lib/cobbler/kickstarts/CentOS-7-x86_64.cfg  --kopts='net.ifnames=0 biosdevname=0'

#再用cobbler  profile  report命令就可以看到centos6和centos7的profile文件的ks文件路径发生了变化,centos7的kenel options也发生了变化

 同步cobbler,删除了/var/www下面的一些文件,重新生成了一些文件。如果修改文件不执行sync,修改是不生效的

[root@linux-node1 ~]# cobbler sync

 更改cobbler装机时提示界面。修改后执行cobbler sync命令,使配置生效

[root@linux-node1 ~]# vim  /etc/cobbler/pxe/pxedefault.template 

DEFAULT menu
PROMPT 0
MENU TITLE Cobbler | 
TIMEOUT 200
TOTALTIMEOUT 6000
ONTIMEOUT $pxe_timeout_profile

LABEL local
        MENU LABEL goser company mirrors iso do
        MENU DEFAULT
        LOCALBOOT -1

$pxe_menu_items

MENU end

 测试创建一个实例,虚拟机设置如下:

consul centos7 单机部署_linux_05

 当实例启动出现下面界面说明cobbler配置生效。

consul centos7 单机部署_System_06

  当然也可以对实例预装完成后执行脚本,让其下载阿里的epel源,或做一些系统的操作,这里以centos6为例,配置如下:

 首先修改centos6的ks文件,在%post下添加一个脚本,这里注意的是,这个脚本不能放在/var/www/cobbler目录下,因为这个目录下自建的文件在执行cobbler sync的时候会被删除掉,我们将这个脚本和epel文件放在/var/www/cobbler/repo_mirror下就不会被清除掉了

 制作一个脚本optimization.sh

consul centos7 单机部署_System_03

consul centos7 单机部署_centos_04

[root@linux-node1 repo_mirror]# cat  optimization.sh 
#!/bin/bash
##############################################################
# File Name: /var/www/html/config/optimization.sh
# Version: V1.0
# Author: goser
# Organization: cnblog.com/goser
# Created Time : 2017-10-03
# Description: Linux system initialization
##############################################################
. /etc/init.d/functions
Ip=192.168.1.180
Port=80
ConfigDir=cobbler/repo_mirror
# Judge Http server is ok?
PortNum=`nmap $Ip  -p $Port 2>/dev/null|grep open|wc -l`
[ $PortNum -lt 1 ] && {
echo "Http server is bad!"
exit 1
}
# Defined result function
function Msg(){
if [ $? -eq 0 ];then
          action "$1" /bin/true
else
action "$1" /bin/false
fi
}
# Defined Yum source Functions
function yum(){
YumDir=/etc/yum.repos.d
[ -f "$YumDir/CentOS-Base.repo" ] && cp $YumDir/CentOS-Base.repo{,.ori} &&\
wget -O $YumDir/CentOS-Base.repo https://$Ip/$ConfigDir/CentOS-Base.repo --no-check-certificate &>/dev/null &&\
wget -O $YumDir/epel.repo https://$Ip/$ConfigDir/epel.repo --no-check-certificate &>/dev/null &&\
Msg "YUM source"
}
# Defined Hide the system version number Functions
function HideVersion(){
[ -f "/etc/issue" ] && >/etc/issue
Msg "Hide issue"
[ -f "/etc/issue.net" ] && > /etc/issue.net
Msg "Hide issue.net"
}
# Defined OPEN FILES Functions
function openfiles(){
[ -f "/etc/security/limits.conf" ] && {
echo '* - nofile 65535' >> /etc/security/limits.conf
Msg "open files"
}
}
# Defined System Startup Services Functions
function boot(){
for args in `chkconfig --list|grep "3:on"|awk '{print $1}'|grep -vE "crond|network|rsyslog|sshd|sysstat"`
do
   chkconfig $args off
done
Msg "BOOT config"
}
# Defined Time Synchronization Functions
function Time(){
echo "#time sync by goser at $(date +%F)" >>/var/spool/cron/root
echo '*/5 * * * * /usr/sbin/ntpdate time.nist.gov &>/dev/null' >>/var/spool/cron/root
Msg "Time Synchronization"
}
# Defined main Functions
function main(){
yum
HideVersion
openfiles
boot
Time
}
main

optimization.sh

 修改CentOS-6-x86_64.cfg

consul centos7 单机部署_System_03

consul centos7 单机部署_centos_04

[root@linux-node1 kickstarts]# vim  CentOS-6-x86_64.cfg 
#Kickstart Configurator for cobbler by goser
#platform=x86, AMD64, or Intel EM64T
key --skip
#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 yes
#Partition clearing information
clearpart --all --initlabel
#Disk partitioning information
part /boot --fstype ext4 --size 300 --ondisk sda
part swap --size 4096 --ondisk sda
part / --fstype ext4 --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
%packages
@ base
@ chinese-support
@ core
sysstat
iptraf
ntp
e2fsprogs-devel
keyutils-libs-devel
krb5-devel
libselinux-devel
libsepol-devel
lrzsz
ncurses-devel
openssl-devel
zlib-devel
OpenIPMI-tools
mysql
lockdev
minicom
nmap
 
%post
wget -O /tmp/optimization.sh https://192.168.1.180/cobbler/repo_mirror/optimization.sh --no-check-certificate &>/dev/null
/bin/sh /tmp/optimization.sh
#/bin/sed -i 's/#Protocol 2,1/Protocol 2/' /etc/ssh/sshd_config
/bin/sed  -i 's/^ca::ctrlaltdel:/#ca::ctrlaltdel:/' /etc/inittab

修改的CentOS-6-x86_64.cfg

 将epel源和optimization.sh脚本放到/var/www/cobbler/repo_mirror/目录下,并执行cobbler  sync同步操作。

 注意:这里的脚本和ks文件中的wget 命令后一定要加上--no-check-certificate选项,不然创建的实例无法从cobbler服务端下载,因为下载访问的url是https的方式,要认证证书的,加上--no-check-certificate各个选项为了在下载的时候忽略这个证书认证,直接下载。

 待上面的实例安装成功后,安装koan,能实现重装,注意是在新建的实例上安装koan。koan是kickstart-over-a-network的缩写,它是cobbler的客户端帮助程序,koan允许你通过网络提供虚拟机,也允许你重装已经存在的客户端。当运行时,koan会从远端的cobbler server获取安装信息,然后根据获取的安装信息进行安装

#在新建的实例上下载阿里云的epel源
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

#在新建的实例上安装koan
[root@localhost ~]# yum install  koan  -y

#在新建的实例上查看它能进行重装的系统
[root@localhost ~]# koan  --server=192.168.1.180  --list=profiles
- looking for Cobbler at http://192.168.1.180:80/cobbler_api
CentOS7-x86_64
CentOS6-x86_64

#把系统重装装成centos6,执行完毕,重启就会自动重装为centos6的系统
[root@localhost ~]# koan  --replace-self  --server=192.168.1.180  --profile=CentOS6-x86_64
[root@localhost ~]# reboot

 等待重装完毕后,验证安装centos6成功后脚本是否成功执行:

consul centos7 单机部署_System_03

consul centos7 单机部署_centos_04

[root@linux-node1 ~]# ssh  192.168.1.5

[root@localhost ~]# ll /etc/yum.repos.d/
-rw-r--r--. 1 root root 2573 Dec 19 17:18 CentOS-Base.repo
-rw-r--r--. 1 root root 1991 Oct 23  2014 CentOS-Base.repo.ori
-rw-r--r--. 1 root root 1084 Dec 19 17:06 epel.repo

[root@localhost ~]# chkconfig --list|grep 3:on
crond           0:off   1:off   2:on    3:on    4:on    5:on    6:off
network         0:off   1:off   2:on    3:on    4:on    5:on    6:off
rsyslog         0:off   1:off   2:on    3:on    4:on    5:on    6:off
sshd            0:off   1:off   2:on    3:on    4:on    5:on    6:off
sysstat         0:off   1:on    2:on    3:on    4:on    5:on    6:off

[root@localhost ~]# crontab -l
#time sync by goser at 2017-12-19
*/5 * * * * /usr/sbin/ntpdate time.nist.gov &>/dev/null

[root@localhost ~]# cat /etc/security/limits.conf 
* - nofile 65535

验证脚本是否成功执行

3、cobbler-web

 cobbler部署完成后,可以使用web界面来管理cobbler的,在浏览器中输入https://192.168.1.180/cobbler_web即可。

 但是在实验中用浏览器访问的时候报错,无法访问web界面,分析/var/log/httpd/ssl_error_log 的时候错误如下:

[Tue Dec 19 21:09:53.330707 2017] [:error] [pid 2068] [remote 192.168.1.2:4484] mod_wsgi (pid=2068): Exception occurred processing WSGI script '/usr/share/cobbler/web/cobbler.wsgi'.
[Tue Dec 19 21:09:53.330821 2017] [:error] [pid 2068] [remote 192.168.1.2:4484] Traceback (most recent call last):
[Tue Dec 19 21:09:53.330866 2017] [:error] [pid 2068] [remote 192.168.1.2:4484]   File "/usr/share/cobbler/web/cobbler.wsgi", line 25, in application
[Tue Dec 19 21:09:53.330930 2017] [:error] [pid 2068] [remote 192.168.1.2:4484]     from django.core.wsgi import get_wsgi_application
[Tue Dec 19 21:09:53.330968 2017] [:error] [pid 2068] [remote 192.168.1.2:4484] ImportError: No module named django.core.wsgi

 根据错误提示,应该是python的django web架构模块缺失问题,可能是django在安装的时候不完全,解决办法为:重新安装django

[root@linux-node1 ~]# yum list|grep  django
python-django.noarch                    1.6.11.6-1.el7                 @epel 

[root@linux-node1 ~]# yum  install  -y  python-django

 这时在用浏览器访问cobbler就没问题了,cobbler的web界面如下:默认的用户名和密码为:cobbler

consul centos7 单机部署_consul centos7 单机部署_13

 当然也可对web登录的修改密码。在/etc/cobbler/目录下user.conf是用户文件   ,users.digest是密码文件

[root@linux-node1 cobbler]# htdigest /etc/cobbler/users.digest "Cobbler" cobbler
Changing password for user cobbler in realm Cobbler
New password:
Re-type new password:

 通过web界面,管理员就可以很方便的管理cobbler了,有了这个web管理功能再做自动化运维就很方便了,而且cobbler还提供cobbler-api,这样就可以和其他产品做继承,比如和zabbix,saltstack,openstack等等

 其实cobbler还可以管理dns(dnsmasq.template),其实生产中一般都是单独的dns服务器。不使用cobbler

使用cobbler搭建自己的yum源

 可以通过自建yum源来提高本地部署的效率,如果没有自己的yum源的话,就要从公网上下载yum源,下载的速度很慢,部署业务的时候效率就很低。

 下面时候cobbler来添加yum源,比如添加openstack的yum源

[root@linux-node1 ~]# cobbler repo  add  --name=openstack-ocata --mirror=https://mirrors.aliyun.com/centos/7/cloud/x86_64/openstack-ocata/ --arch=x86_64 breed=yum

[root@linux-node1 ~]# cobbler repo  add  --name=openstack-pike --mirror=https://mirrors.aliyun.com/centos/7/cloud/x86_64/openstack-pike/ --arch=x86_64 breed=yum

[root@linux-node1 ~]# cobbler repo list
   openstack-ocata
   openstack-pike

 同步yum源。待同步完成后,会将同步过来的rpm包存放到/var/www/cobbler/repo_mirror/目录下

[root@linux-node1 ~]# cobbler reposync

 对profile文件添加repo选项,让创建实例可以生成repo文件,这些repo文件链接会指向cobbler服务器ip,在添加repo选项之前,要确保/etc/cobbler/settings中的参数yum_post_install_mirror值为1,同时还要确保ks文件中%post选项下有$yum_config_stanza

[root@linux-node1 ~]# vim  /etc/cobbler/settings 
yum_post_install_mirror: 1

[root@linux-node1 kickstarts]# vim  CentOS-7-x86_64.cfg 
%post
systemctl disable postfix.service
$yum_config_stanza
%end

#添加repo选项,这里可以添加一个repo,也可以添加多个repo
[root@linux-node1 kickstarts]# cobbler profile  edit  --name=CentOS7-x86_64 --repo='openstack-ocata openstack-pike'

 同步cobber  执行cobbler  sync使修改生效

cobbler自定义系统安装

 通过它能进行定制的需求,比如装机时mac地址和ip地址的对应,让指定的mac地址的机器获取指定的IP

consul centos7 单机部署_centos_14

 实际生产中会有很多kickstart文件,即使相同系统的kickstart文件也有不同,比如根据业务,对应不同的分区方案(web服务器和数据库服务器对磁盘分区要求就不同)

 下面根据一个实例的mac地址来自动安装系统  mac地址为:00:50:56:20:EA:6F

consul centos7 单机部署_centos_15

 根据这个mac地址,来自定义配置实例的ip  hostname  gateway  subnet 等等

[root@linux-node1 kickstarts]# cobbler system add  --name=linux-node88 --mac=00:50:56:20:EA:6F --profile=CentOS7-x86_64 --ip-address=192.168.1.88 --subnet=255.255.255.0 --gateway=192.168.1.1 --interface=eth0 --static=1 --hostname=linux-node88.goser.com --name-servers=192.168.1.180 --kickstart=/var/lib/cobbler/kickstarts/CentOS-7-x86_64.cfg

 查看是否添加成功

[root@linux-node1 kickstarts]# cobbler system list
   linux-node88

 执行cobbler  sync使配置生效

 查看dhcp配置文件。末尾部分可以看到自定义的配置cat /etc/dhcp/dhcpd.conf

[root@linux-node1 kickstarts]# cat  /etc/dhcp/dhcpd.conf 
group {
    host generic1 {
        hardware ethernet 00:50:56:20:EA:6F;
        fixed-address 192.168.1.88;
        option host-name "linux-node88.goser.com";
        option subnet-mask 255.255.255.0;
        option routers 192.168.1.1;
        filename "/pxelinux.0";
        next-server 192.168.1.180;
    }

 接下来启动这个mac地址00:50:56:20:EA:6F对应的实例,来自动安装操作系统,不需要手动干预自动安装完成。这种方式的自动安装系统在生产环境中经常被采用。

 安装完成后,可以查看实例的ip地址  主机名等来验证cobbler system部署自定义安装是否成功

[root@linux-node88 ~]# hostname
linux-node88.goser.com

[root@linux-node88 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.88  netmask 255.255.255.0  broadcast 192.168.1.255

[root@linux-node88 ~]# cat /etc/yum.repos.d/cobbler-config.repo 

# error: could not read repo source: /var/www/cobbler/ks_mirror/config/CentOS7-x86_64-0.repo

[openstack-ocata]
name=openstack-ocata
baseurl=http://192.168.1.180/cobbler/repo_mirror/openstack-ocata
enabled=1
priority=99
gpgcheck=0


[openstack-pike]
name=openstack-pike
baseurl=http://192.168.1.180/cobbler/repo_mirror/openstack-pike
enabled=1
priority=99
gpgcheck=0

cobbler-api

 我们知道通过程序的api可以调用它的各种参数来使用,比如利用cobbler-api提供各种参数调用给zabbix使用,可以让zabbix来监控cobbler,或者cobbler利用saltstack-api来获取主机的mac地址来自定义部署操作系统等。

 新建一个cobbler_list.py 的脚本

[root@linux-node1 kickstarts]# mkdir /mywork/tools -p
[root@linux-node1 kickstarts]# cd  /mywork/tools/

[root@linux-node1 tools]# vim  cobbler_list.py
#!/usr/bin/python
import xmlrpclib
server = xmlrpclib.Server("http://192.168.1.180/cobbler_api")
print server.get_distros()
print server.get_profiles()
print server.get_systems()
print server.get_images()
print server.get_repos()

 执行这个python脚本,输出一个列表

[root@linux-node1 tools]# python  cobbler_list.py

 下面通过python编写的一个脚本,来让api实现指定mac地址的机器自动装机,python脚本如下:

[root@linux-node1 tools]# vim  cobbler_system_api.py 
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import xmlrpclib
class CobblerAPI(object):
    def __init__(self,url,user,password):
        self.cobbler_user= user
        self.cobbler_pass = password
        self.cobbler_url = url
    def add_system(self,hostname,ip_add,mac_add,profile):
        '''
        Add Cobbler System Infomation
        '''
        ret = {
            "result": True,
            "comment": [],
        }
        #get token
        remote = xmlrpclib.Server(self.cobbler_url)
        token = remote.login(self.cobbler_user,self.cobbler_pass)
        #add system
        system_id = remote.new_system(token)
        remote.modify_system(system_id,"name",hostname,token)
        remote.modify_system(system_id,"hostname",hostname,token)
        remote.modify_system(system_id,'modify_interface', {
            "macaddress-eth0" : mac_add,
            "ipaddress-eth0" : ip_add,
            "dnsname-eth0" : hostname,
        }, token)
        remote.modify_system(system_id,"profile",profile,token)
        remote.save_system(system_id, token)
        try:
            remote.sync(token)
        except Exception as e:
            ret['result'] = False
            ret['comment'].append(str(e))
        return ret
def main():
    cobbler = CobblerAPI("http://192.168.1.180/cobbler_api","cobbler","cobbler")
    ret = cobbler.add_system(hostname='cobbler-api-test',ip_add='192.168.1.111',mac_add='00:50:56:2F:73:CF',profile='CentOS7-x86_64')
    print ret
if __name__ == '__main__':
    main()

 上面脚本中的mac地址00:50:56:2F:73:CF就是准备执行这个python脚本要自动创建的实例对应的mac地址

[root@linux-node1 tools]# python cobbler_system_api.py 
{'comment': [], 'result': True}
[root@linux-node1 tools]# cobbler system  list
   cobbler-api-test
   linux-node88

 待实例安装完成后一样可以验证它的主机名  ip地址等信息

Cobbler目录详细介绍

1、Cobbler配置文件目录:/etc/cobbler
/etc/cobbler/settings #cobbler主配置文件
/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模块配置文件
2、Cobbler数据目录:/var/lib/cobbler
/var/lib/cobbler/config #配置文件
/var/lib/cobbler/triggers #Cobbler命令
/var/lib/cobbler/kickstarts #默认存放kickstart文件
/var/lib/cobbler/loaders #存放的各种引导程序
3、系统安装镜像目录:/var/www/cobbler
/var/www/cobbler/ks_mirror #导入的系统镜像列表
/var/www/cobbler/images #导入的系统镜像启动文件
/var/www/cobbler/repo_mirror #yum源存储目录
4、日志目录:/var/log/cobbler
/var/log/cobbler/install.log #客户端系统安装日志
/var/log/cobbler/cobbler.log #cobbler日志