<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/ DTD/wml_1.1.xml">

<wml>
<head>
<meta http-equiv="cache-control" content="max-age=180,private" />
</head>
<card title="Cobbler自动化部署linux操作系统">
<p>
作者:<a href="index.php?action=showuser&amp;userid=1&amp;hash=">admin</a><br />时间:2014-03-19 13:00<br />分类:<a href="index.php?action=list&amp;cid=3&amp;hash=">电脑技术</a><br />内容:
Cobbler自动化部署linux操作系统

之前安装操作系统，使用的是kickstart，如果是两年或更早以前你用这个，还会显得很高端，现在如果还在使用kickstart，就有点落伍了。简单总结了下kickstart的缺点：

1、不能夸网段安装，每个网段都得部署一个网络安装源；

2、安全性太差，如果你把服务器第一启动顺序设置为从网络启动，那么每次重启服务器，你的系统可能就要重新安装了，不设置第一启动为网络的话，每次重装就必须得进机房接上显示器，等待着F12的出现（DELL服务器是开机时按F12从网络引导，其他服务器不太清楚，开机都会有提示的）而这个等待的过程是相当痛苦无聊的。即使你不把服务器第一启动项设置为从网络启动，一旦你的系统挂了，无法正确引导了，那么正好这时你的DHCP也开着，那么你的噩运也就降临了！

3、Kickstart的模板文件功能单一，无法对新系统做过多初始化配置。

以上仅是简单列了三点，将cobbler结合IPMI使用，功能还是相当强大的，对运维人员来说，基本上不需要进机房了。

安装前简单介绍下cobbler。


Cobbler介绍


cobbler是Redhat支持的一个开源项目，用来部署和安装系统。所以对Redhat系列os支持是比较好的。不过对ubuntu支持是非常差的。cobbler里不少设置，只针对redhat有效，同时也由于不同发行版的安装是有差异。cobbler感觉好像和ubuntu关系比较紧张.

cobbler不仅仅是一个pxe服务器，他还可以管理dns（bind），dhcp。一般数据中心里或者生产环境,是不允许dhcp, 但是pxe,必须需要dhcp,所以我们需要根据mac地址来分配IP,这样dhcp就不会影响到网络。


目标


1、自动化安装centos和ubuntu

2、IP,主机名自动配置

3、网络安装，无风险


准备服务器


一台普通PC机就可以，我的环境安装的是centos6.2_x86_64,最小化安装，没什么特别的要求，配置个固定IP就OK。


设置源


采用网易的源和USTC的源:


 



[html] view plaincopy







    yum -y install wget vim-enhanced  

      

    mv /bin/vi /bin/vi.save  

      

    ln -s /usr/bin/vim /bin/vi  

      

    cd /etc/yum.repos.d/  

      

    mkdir backup  

      

    mv *.repo ./backup  

      

    wget http://mirrors.163.com/.help/CentOS6-Base-163.repo  

      

    rpm -ivh http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-7.noarch.rpm  

      

    yum list  






设置hostname

 

 



[html] view plaincopy







    cat /etc/hosts  

      

    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4  

      

    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6  

      

    192.168.80.8     cobbler.test.com          




 


cobbler关闭selinux


sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config

这个需要重启机器才能生效,所以建议修改完重启.


 



[html] view plaincopy







    [root@cobbler ~]# sestatus  

      

    SELinux status:                 disabled  




 


关闭iptables


cobbler会使用到好几个端口，为了简化，直接把iptables关闭:


 



[html] view plaincopy







    chkconfig ip6tables off  

      

    chkconfig iptables off  

      

    /etc/init.d/ip6tables stop  

      

    /etc/init.d/iptables stop  






安装cobbler

1、安装cobbler相关的包

如果希望cobbler可以管理dhcp，dns，需要把dns和dhcp都安装在同一台服务器上。如果你不管理dns,可以不需要安装bind.


 



[html] view plaincopy







    yum -y install cman tftp-server dhcp cobbler cobbler-web bind pykickstart  






 

2、安装debmirror

需要用debmirror来下载ubuntu 12.04的源,做本地源.目前debmirrors, 只能使用rpm包安装, 先装依赖包


 



[html] view plaincopy







    yum -y install ed patch perl perl-Compress-Zlib perl-Cwd perl-Digest-MD5 \  

      

    perl-Digest-SHA1 perl-LockFile-Simple perl-libwww-perl  




 

下载debmirrors


 



[html] view plaincopy







    wget http://mirrors.sohu.com/fedora-epel/5Server/x86_64/debmirror-20090807-1.el5.noarch.rpm  






安装


 



[html] view plaincopy







    rpm -ivh debmirror-20090807-1.el5.noarch.rpm  






设置cobbler

1、tftp服务和rsync服务


 



[html] view plaincopy







    sed -i '/disable/c\\tdisable\t\t\t= no' /etc/xinetd.d/tftp  

      

    sed -i -e 's/\=\ yes/\=\ no/g' /etc/xinetd.d/rsync   

      

    service xinetd restart  






2、web可以登陆

 


 



[html] view plaincopy







    sed -i 's/authn_denyall/authn_configfile/g' /etc/cobbler/modules.conf  






3、 设置cobbler  web登陆,用户登陆密码


 



[html] view plaincopy







    htdigest /etc/cobbler/users.digest &quot;Cobbler&quot; cobbler  






 

4、 设置cobbler登陆服务器地址


 



[html] view plaincopy







    sed -i 's/server: 127.0.0.1/server: 192.168.80.8/g' /etc/cobbler/settings  






5、ks脚本关闭pxe，这样就不会重复安装


 



[html] view plaincopy







    sed -i 's/pxe_just_once: 0/pxe_just_once: 1/g' /etc/cobbler/settings  






6、TFTP服务器 IP地址


 



[html] view plaincopy







    sed -i 's/next_server: 127.0.0.1/next_server: 192.168.80.8/g' /etc/cobbler/settings  






7、cobbler管理rsync


 



[html] view plaincopy







    sed -i 's/manage_rsync: 0/manage_rsync: 1/g' /etc/cobbler/settings  






8、cobbler管理dhcp


 



[html] view plaincopy







    sed -i 's/manage_dhcp: 0/manage_dhcp: 1/g' /etc/cobbler/settings  






 

dhcp服务是由cobbler来管理 /etc/cobbler/dhcp.template

 

修改下面的内容就可以。注释掉 range dynamic-bootp 。不然你内网就会出现一个dhcp服务器。

 


 



[html] view plaincopy







    subnet 192.168.80.0 netmask 255.255.255.0 {  

      

         option routers             192.168.80.1;  

      

         option domain-name-servers  192.168.80.8;  

      

         option subnet-mask         255.255.255.0;  

      

         #range dynamic-bootp        192.168.80.100 192.168.80.150;  

      

         filename                   &quot;/pxelinux.0&quot;;  

      

         default-lease-time         21600;  

      

         max-lease-time             43200;  

      

         next-server                $next_server;  

      

    }  




 

注意：这里你可以设置多个DHCP地址范围，然后在交换机上做DHCP中继，即可实现跨网段安装了，我的环境已经实现！

9、设置root默认密码

这个设置只针对centos有效。我默认的root密码是：hadoop


 



[html] view plaincopy







    # openssl passwd -1 -salt 'random-phrase-here' 'hadop'  

      

    $1$random-p$99lAzJZEl9uunSaThe9oM0修改 /etc/cobbler/settings  

      

       

      

    #default_password_crypted: &quot;$1$mF86/UHC$WvcIcX2t6crBz2onWxyac.&quot;  

      

    default_password_crypted: &quot;$1$random-p$99lAzJZEl9uunSaThe9oM0&quot;   




 

 

10、 启动相关服务


 



[html] view plaincopy







    chkconfig httpd on   

      

    chkconfig cobblerd on  

      

    chkconfig dhcpd on  

      

    /etc/init.d/httpd restart  

      

    /etc/init.d/cobblerd restart  






11、设置debmirror


 



[html] view plaincopy







    sed -i -e 's|@dists=.*|#@dists=|'  /etc/debmirror.conf  

      

    sed -i -e 's|@arches=.*|#@arches=|'  /etc/debmirror.conf  






12、启动菜单


 



[html] view plaincopy







    cobbler get-loaders   






检查cobbler

cobbler提供一个工具，检查你的设置。看到提示，就表示全部工作都已经完成。

对cobbler所有修改，需要cobbler sync检查

 


 



[html] view plaincopy







    # cobbler check  

      

    No configuration problems found.  All systems go.  






web登陆

登录：https://192.168.80.8/cobbler_web/

user:cobbler 密码就是上面设置的

 

 

cobbler的使用，主要是集中在上面几个菜单里。这里简单解析一下。

Distros：这个其实就是发行版，类似Centos，ubuntu，suse。centos6.2和centos6.3，是不同的distros。假设我们导入一个centos6.3的iso，就是增加了一个Distros

Profiles：针对Distros的设置，一个distros，可以保护多个profiles，包括不同的kickstart文件。源的设置，都是在这里设置。

Systems：针对是每个节点，这里可以指定节点的ip地址，dns name，还有就是ipmi的用户名和密码，实现远程开机，关机。这是一个重点，日后对机器的操作，全部在sytem的菜单里操作。system里，会指定节点使用哪个profile。

Repos：这个主要是针对Redhat和Centos有效，可以管理源，并且这些源，可以在profile里添加。比较方便。需要注意的是，对于ubuntu的源，只能在kickstart 脚本里指定。

Images：这个我没用到，看说明主要是针对不能pxe的机器，采用iso启动。

Kicsstart Templates：这是核心，cobbler内置了几个ks文件，你如果导入一个centos，系统会默认关联一个ks文件，你不需要做任何设置，就可以把os自动装完。对于ubuntu，我们就需要单独创建一个preseed文件，这些文件可以通过web管理和修改。非常方便。

Snippets：这是cobbler的精华。一些常用的设置，写成一个模块，让ks文件调用。这样更加灵活。例如centos的网络固定IP地址的设置，就是通过这里实现。


Squid安装


如果多个节点同时安装，用squid可以加快速度，至少对ubuntu是非常有效的。目前centos的ks文件里，我还没搞明白如何设置squid，ubuntu的preseed文件里已经启用squid。所以我们需要安装squid。

 


 



[html] view plaincopy







    yum -y install squid  

      

    /etc/init.d/squid restart  

      

    chkconfig squid on  






 


设置apache根目录访问


我们希望直接访问IP，就可以看到源的目录，尤其对于ubuntu的源来说，这样看起来更加规范。

 


 



[html] view plaincopy







    # cat /etc/httpd/conf.d/welcome.conf  

      

    &lt;LocationMatch &quot;^/+$&quot;&gt;  

      

     Options Indexes FollowSymLinks  

      

        Order allow,deny  

      

        Allow from all  

      

    &lt;/LocationMatch&gt;  






重启apache，就可以 http://192.168.80.8 看到目录。实际目录是 /var/www/html/


安装Centos系统


我的环境里部署了centos5.6，centos6.2和centos6.4三个版本，这里选择centos6.2为列来说明下，其他版本操作相同。

1、导入centos 6.3 iso

加载iso


 



[html] view plaincopy







    mount -t auto -o loop /root/CentOS-6.2-x86_64-bin-DVD1.iso /mnt   






估计是由于centos里光盘信息比较多，所以导入的时候，会同时创建一个distro，profile。并且还设置的repo。不过这个repo，只包括DVD1，如果希望包括DVD2，还需要做设置才行。

 


 



[html] view plaincopy







    # cobbler import --path=/mnt --name=CentOS6.2 --arch=x86_64  

      

    task started: 2013-05-06_140452_import  

      

    task started (id=Media import, time=Mon Aug  6 14:04:52 2012)  

      

    Found a redhat compatible signature: Packages  

      

    adding distros  

      

    creating new distro: CentOS6.2-x86_64  

      

    creating new profile: CentOS6.2-x86_64  

      

    associating repos  

      

    traversing distro CentOS6.2-x86_64  

      

    descent into /var/www/cobbler/ks_mirror/CentOS6.2-x86_64  

      

    processing repo at : /var/www/cobbler/ks_mirror/CentOS6.2-x86_64  

      

    need to process repo/comps: /var/www/cobbler/ks_mirror/CentOS6.2-x86_64  

      

    looking for /var/www/cobbler/ks_mirror/CentOS6.2-x86_64/repodata/*comps*.xml  

      

    running: createrepo -c cache -s sha --groupfile /var/www/cobbler/ks_mirror/CentOS6.2-x86_64/repodata/0dae8d32824acd9dbdf7ed72f628152dd00b85e4bd802e6b46e4d7b78c1042a3-c6-x86_64-comps.xml /var/www/cobbler/ks_mirror/CentOS6.2-x86_64  

      

    1878/3848 - Packages/libipa_hbac-1.8.0-32.el6.i686.rpm                            

      

    iso-8859-1 encoding on Ville Skytt&lt;ville.skytta@iki.fi&gt; - 2.8.2-2  

      

       

      

    3848/3848 - Packages/gpm-1.20.6-12.el6.x86_64.rpm                                 

      

    Saving Primary metadata  

      

    Saving file lists metadata  

      

    Saving other metadata  

      

       

      

    received on stderr:   

      

    associating kickstarts  

      

    *** TASK COMPLETE ***  






2、设置profile kickstart文件

profile默认是采用 /var/lib/cobbler/kickstarts/sample.ks  作为ks文件。

 

可以考虑使用我的ks文件来替换：centos.ks

 


 



[html] view plaincopy







    cd /var/lib/cobbler/kickstarts  

      

    wget http://192.168.80.8/centos.ks  




 


 



[html] view plaincopy







    cobbler profile edit --name=CentOS6.2-x86_64 --kickstart=/var/lib/cobbler/kickstarts/centos.ks  






注意，我的centos.ks 文件，默认已经是采用proxy，所以你一定要安装好squid。

这个时候，你可以通过下面命令查看修改的界面

 


 



[html] view plaincopy







    cobbler profile report  






 

3、命令行添加一个客户机

我是采用DELL的机器。我已经启用了IPMI。对于cobbler来说，必须启用ipmi，才能发挥它的优势。

 

IPMI IP：192.168.80.120

user：root

pass：calvin

 

 



[html] view plaincopy







    cobbler system  add \  

      

    --name=node12 \  

      

    --hostname=cobbler-test  \  

      

    --profile=CentOS6.2-x86_64 \  

      

    --interface=eth0 \  

      

    --mac=00:25:90:67:CE:1A \  

      

    --interface=eth0 \  

      

    --ip-address=192.168.80.20 \  

      

    --subnet=255.255.255.0 \  

      

    --gateway=192.168.80.1 \  

      

    --static=1 \  

      

    --power-type=ipmilan \  

      

    --power-user=root \  

      

    --power-pass=calvin \  

      

    --power-address=192.168.80.120  






这些添加，其实都是可以在web完成，不过web比较繁琐，所以命令行下更加方便。

cobbler所有设置和修改，都需要通过cobbler sync来生效，你会看到重启name，dhcp服务。

 

cobbler sync查看添加节点的情况

 


 



[html] view plaincopy







    cobbler system report --name=cobbler-test  






我们需要留意的一个参数是：netboot-enabled，当cobbler装完客户端后，这个参数会自动变成0，如果我们希望重装，就需要修改成1.

 


 



[html] view plaincopy







    cobbler system edit --name= cobbler-test  --netboot-enabled=1  






 

所有的修改，一定要记住，需要cobbler sync才能生效。

 

安装客户端

 

设置远程机器pxe启动：有些服务器支持下次启动pxe启动，可以运行这个命令，让机器下次是从pxe启动:


 



[html] view plaincopy







    ipmitool -I lan -H 192.168.80.120 -U root -P calvin chassis bootdev pxe  






 

重启远程机器:

有两个办法重启远程机器

 


 



[html] view plaincopy







    cobbler system reboot --name= cobbler-test   

      

    ipmitool -I lan -H 192.168.80.120 -U root -P calvin chassis power reset  






当系统启动后，会从pxe启动，装完os后，会自动修改 netboot-enabled=0。

 

4、web操作

 

1）netboot enable

 



 

这里我们可以选择将节点解锁。

2）sysnc

在web界面，也可以实现cobbler sync

 

3）重启远程机器

 

这些操作，都是可以批量操作。让远程机器重启。目前web还无法实现让远程机器下次从pxe启动。

 

 



到这里已经可以顺利完成系统的安装了！关于centos的源的一些设置以及同步网络源这里就不详细说了，因为centos我用的不多，主要还是ubuntu的系统，在ubuntu里面再详细说明。


安装ubuntu


Cobbler上安装ubuntu就没centos那么简单了，网上也很少有完整的资料，这里还是参照沙克的blog，顺利完成了安装。这里我顺利完成了ubuntu12.04和ubuntu11.04的部署，下面以ubuntu12.04为例。

1、导入ISO

经过测试，是可以直接使用ubuntu 12.04的导入的iso，作为安装源，完成安装。

 


 



[html] view plaincopy







    mount -t auto -o loop /root/ubuntu-12.04-server-amd64.iso /mnt  




 



[html] view plaincopy







    # cobbler import --path=/mnt --name=ubuntu12.04  

      

    task started: 2013-05-08_101200_import  

      

    task started (id=Media import, time=Wed Aug  8 10:12:00 2012)  

      

    Found a debian/ubuntu compatible signature: pool  

      

    adding distros  

      

    avoiding symlink loop  

      

    creating new distro: ubuntu12.04-x86_64  

      

    creating new profile: ubuntu12.04-x86_64  

      

    avoiding symlink loop  

      

    skipping import, as distro name already exists: ubuntu12.04-x86_64  

      

    avoiding symlink loop  

      

    avoiding symlink loop  

      

    associating repos  

      

    traversing distro ubuntu12.04-x86_64  

      

    descent into /var/www/cobbler/ks_mirror/ubuntu12.04  

      

    associating kickstarts  

      

    skipping /var/www/cobbler/ks_mirror/ubuntu12.04/dists/stable  

      

    skipping /var/www/cobbler/ks_mirror/ubuntu12.04/dists/precise  

      

    skipping /var/www/cobbler/ks_mirror/ubuntu12.04/dists/unstable  

      

    *** TASK COMPLETE ***  






2、对于ubuntu来说，就没那么幸运，导入的distro和profile，是有问题的

 


 



[html] view plaincopy







    cobbler distro report --name=ubuntu12.04-x86_64  

      

    cobbler profile report --name=ubuntu12.04-x86_642：修改distro  






 

默认的是错误，一定要修改。

 


 



[html] view plaincopy







    cobbler distro edit --name=ubuntu12.04-x86_64 \  

      

    --kernel=/var/www/cobbler/ks_mirror/ubuntu12.04/install/netboot/ubuntu-installer/amd64/linux \  

      

    --initrd=/var/www/cobbler/ks_mirror/ubuntu12.04/install/netboot/ubuntu-installer/amd64/initrd.gz  --os-version=precise  






3、脚本

我根据ubuntu的Maas，提取了他的pressed文件，修改了一下，可以直接下载使用：ubuntu.preseed

 

这个preseed文件的分区，大家可以调整。里面有两种分区的方案，我注释掉了默认的分区方案。

 


 



[html] view plaincopy







    cd /var/lib/cobbler/kickstarts  

      

    wget http://192.168.80.8/ubuntu.preseed  






由于ubuntu.preseed文件，用到几个snippets，所以需要创建

 


 



[html] view plaincopy







    Vi  /var/lib/cobbler/snippets/orchestra_disable_pxe  

      

       

      

    wget &quot;http://$http_server:$http_port/cblr/svc/op/nopxe/system/$system_name&quot; -O /dev/null \  

      

       

      

    vi /var/lib/cobbler/snippets/orchestra_proxy  

      

       

      

    # Point apt to orchestra-provisioning-server's squid  

      

    d-i     mirror/country string manual  

      

    d-i     mirror/http/hostname string $http_server  

      

    d-i     apt-setup/security_host string $http_server  

      

    d-i     apt-setup/security_path string /ubuntu  

      

    d-i     mirror/http/directory string /ubuntu  

      

    d-i     mirror/http/proxy string http://@@server@@:3128/设置静态IP地址  






 

由于cobbler没有提供脚本来设置静态IP，所以这个需要自己想办法. static.sh 脚本 ，注意，这个脚本，有一个地方你是需要修改，就是你的网段。我的网段是192.168.80.0，你改成你的网段才可以。

 


 



[html] view plaincopy







    mkdir /var/www/html/post  

      

    cd /var/www/html/post  

      

    wget http:// 192.168.80.8/static.sh_.txt  




 

我的ubuntu.preseed 已经调用了这个脚本。

 

 

 

4、设置proflie

增加kernel option,关联ubuntu preseed

 


 



[html] view plaincopy







    cobbler profile edit --name=ubuntu12.04-x86_64 \  

      

    --kopts=&quot;log_port=514 priority=critical locale=en_US log_host=192.168.80.8 netcfg/choose_interface=auto &quot; \  

      

    --kickstart=/var/lib/cobbler/kickstarts/ubuntu.preseed  






5、设置安装源

这里直接使用导入的光盘作为源，安装的时候会到/var/www/html/ubuntu目录下寻找系统安装文件，因为在orchestra_proxy文件里我们指定了系统安装是mirror的存放位置是apache根目录下的ubuntu目录，所以我们需要做个软连接：


 



[html] view plaincopy







    ln -s /var/www/cobbler/ks_mirror/ubuntu12.04 /var/www/html/ubuntu  






这样就可以顺利完成安装。

当然这样系统镜像也会作为你的本地源的默认路径，系统安装包里的软件肯定是有限的，只能安装一些基本的软件，彻底的解决办法，就是同步一个ubuntu 12.04的本地源。这是一个体力活，全部的ubuntu 12.04 的源，一共135G，同步时间，根据你的网络而定 ，我现在已经同步完了。

具体同步方法后面再介绍。

我将同步来的源存放在 /home/ubuntu 下。需要链接到web可以访问的目录

 


 



[html] view plaincopy







    ln -s /home/ubuntu /var/www/html/ubuntu  






当然这样，就和上面的连接冲突了，这样就会使用网络同步的源来安装系统，这样也是完全可行的，但是考虑到为了保持系统版本的一致性，最好还是使用光盘镜像文件来安装，因为源我会定期进行更新。方法也很简单，修改下orchestra_proxy里的参数就行了。

6、安装客户端

这个就和centos基本是一样。唯一的区别就是需要指定ubuntu


 



[html] view plaincopy







    cobbler system  add \  

      

    --name=node15 \  

      

    --hostname=node15 \  

      

    --profile=ubuntu12.04-x86_64 \  

      

    --interface=eth0 \  

      

    --mac=00:25:90:67:CE:28 \  

      

    --interface=eth0 \  

      

    --ip-address=192.168.80.120 \  

      

    --subnet=255.255.255.0 \  

      

    --gateway=192.168.80.1 \  

      

    --static=1 \  

      

    --power-type=ipmilan \  

      

    --power-user=root \  

      

    --power-pass=calvin \  

      

    --power-address=192.168.80.120  






 

这样一个节点就添加成功了，我们可以将这个命令写在一个脚本文件里，需要添加节点的时候运行一下就可以了，非常方便！
</p><p>
<a href="index.php?action=login&amp;hash=">立即登陆发表评论</a><br />
</p>
<p><a href="index.php?action=list&amp;hash=">返回日志列表</a><br /><a href="index.php?action=index&amp;hash=">返回主页</a></p>
</card>
</wml>
