您的位置:首页 > 其它

Puppet 案例介绍

2015-11-11 10:59 197 查看
这里我也就不去注重文章的布局,就按照我显示的情况进行了一些简单的配置,前提是一些简单的前期配置我们都已经部署好了。
一、授权比较麻烦,配置自动授权
1、创建自动签署文件
# cat /etc/puppet/autosign.conf
*.comratings.com         //对所以主机名*.comratings.com自动签署
cat /etc/puppet/puppet.conf
2、填写配置文件使其生效
# cat /etc/puppet/puppet.conf
[master]
autosign = true
autosign = /etc/puppet/autosign.conf


二、其实我是比较不喜欢主机配置ipv6地址的,我可以设定默认节点取消ipv6

1、首先应该创建好配置文件

# cat /etc/puppet/files/ipv6.conf
alias net-pf-10 off
alias ipv6 off
2、添加授权,要不客户端没有权限去获取文件
# cat /etc/puppet/fileserver.conf
[files]
path /etc/puppet/files
allow *.comratings.com
3、创建该资源的类
我们可以把类直接放在site.pp里面,然后调用,或者放在模块里面调用,放在模块创建起来稍微麻烦一些,我们这里统一放在site.pp里面
# cat /etc/puppet/manifests/site.pp
node default {
include ipv6
}

class ipv6 {
file {'/etc/modprobe.d/ipv6.conf':
source => "puppet:///files/ipv6.conf",
}
}
客户端配置好hosts,配置文件就可以获取配置文件了,客户端重启之后就取消ipv6了,原理都是想通的,想要增加其他文件都可以这样操作,修改文件也可以,对文件内容进行追加也是可以的,具体.pp文件怎么写需要查看资源file的用法了。

也可以直接在ipv6.pp里面填写如下内容:
class ipv6 {
$mycontent = "alias net-pf-10 off\nalias ipv6 off\n"
file{'/root/a.txt':
content => "$mycontent",
}
}


三、我想增加一个定时任务,对集群所有机器进行时间同步
1、创建时间同步类资源

# cat /etc/puppet/manifests/site.pp
node default {
include ipv6
include ntpdate
}

class ipv6 {
file {'/etc/modprobe.d/ipv6.conf':
source => "puppet:///files/ipv6.conf",
}
}

class ntpdate {
cron {'ntpdate':
ensure  => present,
command => '(/usr/sbin/ntpdate time.nist.gov && /sbin/hwclock -w) &> /dev/null',
user    => 'root',
hour    => '*/12',
}
}


可以通过crontab -u root -l查看对应用户的定时任务情况,除了这个方法,我们也可以通过file资源对/etc/crontab里面进行追加添加定时内容。

四、很多时间我都是最小化安装linux系统,一些常用的软件包是没有安装的,我要安装绝大部分常用的软件
1、创建软件包资源文件
# cat /etc/puppet/manifests/site.pp
node default {
include ipv6
include ntpdate
include software
}

class ipv6 {
file {'/etc/modprobe.d/ipv6.conf':
source => "puppet:///files/ipv6.conf",
}
}

class ntpdate {
cron {'ntpdate':
ensure  => present,
command => '(/usr/sbin/ntpdate time.nist.gov && /sbin/hwclock -w) &> /dev/null',
user    => 'root',
hour    => '*/12',
}
}

class software {
package { ["vim-enhanced",
"openssh-clients",
"ntpdate",
"man",
"lrzsz" ]:
ensure => installed,
}
}


五、因为大部分机器都是内网的,为了避免一些问题的产生,我喜欢把防火墙配置规则清空,然后执行关闭,设置开机不自起。
1、这次我们创建一个类资源
# cat /etc/puppet/manifests/site.pp
node default {
include ipv6
include ntpdate
include software
include firewall
}

class ipv6 {
file {'/etc/modprobe.d/ipv6.conf':
source => "puppet:///files/ipv6.conf",
}
}

class ntpdate {
cron {'ntpdate':
ensure  => present,
command => '(/usr/sbin/ntpdate time.nist.gov && /sbin/hwclock -w) &> /dev/null',
user    => 'root',
hour    => '*/12',
}
}

class software {
package { ["vim-enhanced",
"openssh-clients",
"ntpdate",
"man",
"lrzsz" ]:
ensure => installed,
}
}

class firewall {
exec {'iptables':
command => '/sbin/iptables-save > /etc/sysconfig/iptables',
onlyif  => "/etc/init.d/iptables status",
}
service {'iptables':
ensure  => stopped,
enable  => false,
before => Exec['iptables'],
}
}


六、每次配置都是Agent去向Master同步,我想实现修改某一配置后主动推送到Agent端,这就需要用到puppet kick
puppet客户端默认每30分钟跟服务器通讯一次,但是有时,我们希望服务端能给客户端紧急推送一些任务,于是就有了puppet kick
puppet kick是一个远程管理控制工具,通过它可以主动连接agent的进程,并要求这些被连接的机器主动更新配置。
通过puppet kick命令主动更新Agent状态前,需要确定Agent是否已经监听了TCP 8139端口,可以修改/etc/puppet/puppet.conf文件,追加如下内容或设置--listen参数。
# cat /etc/puppet/puppet.conf
[agent]
listen = true
在Agent端编辑或新建文件/etc/puppet/namespaceauth.conf,包含下面内容
[puppetrunner]
allow *.comratings.com
在Agent端编辑文件auth.conf
path /run
method save
allow monitor.comratings.com
# puppet master

# this one is not stricly necessary, but it has the merit
# to show the default policy which is deny everything else
path /
auth any
推送方法,在服务端运行命令
puppet kick -p 10 --host 客户端

puppetrun -p 10 --host 客户端
# puppet kick -p 10 --host shabi.comratings.com
Triggering shabi.comratings.com
Getting status
status is success
shabi.comratings.com finished with exit code 0
Finished


七、通过ERB模板配置Apache虚拟主机
1、在Master上创建配置文件和目录
# mkdir -p /etc/puppet/modules/httpd/{manifests,files,templates}
2、编辑init.pp文件
init.pp文件是模块的入库文件
# cat /etc/puppet/modules/httpd/manifests/init.pp
class apache::parameter{
$listenaddress = "${ipaddress}"
$server_admin  = "admin@comratings.com"
$server_name   = "www.comratings.com"
$document_root = "/var/www/html/puppet"
}
class httpd inherits apache::parameter{
package {"httpd":
ensure => installed,
} ->
file {"/etc/httpd/conf/httpd.conf":
mode    =>'777',
content => template('httpd/httpd.conf.vhost.erb'),
notify  => Service['httpd'],
}
service { "httpd":
ensure     => running,
hasrestart => true,
hasstatus  => true,
}
}
apache::parameter类存放了虚拟主机的变量值。

->代表资源先后顺序,先安装好httpd之后我们才修改配置文件。
~>代表资源之间的通知,相当于上面的notify。
template用法如下:
template('ModuleName/TemplateName'),中间一些目录可以省去,这里就省去了template目录。
3、准备虚拟主机模板

# cat /etc/puppet/modules/httpd/templates/httpd.conf.vhost.erb
由于内容比较多,我们只截取了虚拟主机的配置部分

<VirtualHost <%= @listenaddress%>:80>
ServerAdmin <%= @server_admin %>
DocumentRoot "<%= @document_root %>"
ServerName <%= @server_name %>
</VirtualHost>
修改好httpd.conf.vhost.erb文件后,就可以通过ruby提供的ERB工具来确认文件的语法是否正确。
# erb -P -x -T '-' httpd.conf.vhost.erb |ruby -c
Syntax OK
4、调用模板

# cat /etc/puppet/manifests/site.pp
node 'shabi.comratings.com' {
include httpd
}
当这个节点访问Master的时候,会自动加载httpd模块。

八、我只想修改某一台设备的root密码
1、首先生成密码,密码是md5加密的
# openssl passwd -1 -salt `openssl rand -hex 4`
Password:
$1$b992de1e$MOk9NWbErVcazA/BBD.ea/
2、编辑.pp文件
# cat /etc/puppet/manifests/site.pp
node "shabi.comratings.com" {
include root_passwd
}

class root_passwd {
user {'root':
password => '$1$b992de1e$MOk9NWbErVcazA/BBD.ea/',
}
}
根据node节点,我们只修改了主机名为shabi.comratings.com的这台主机的密码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  主机 文章 files