puppet的多环境实现
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了puppet的多环境实现相关的知识,希望对你有一定的参考价值。
目的:根据环境的不同设置不同的nginx进程数:生产环境配置为auto,测试环境配置2个,开发环境为1个。前提:确保所有节点的epel源可用,因为安装nginx需要epel源。
步骤:
1、Master端操作:
①创建如下目录结构
tree /puppet/environment/
/puppet/environment/
├── dev
│ ├── manifests
│ └── modules
├── prod
│ ├── manifests
│ └── modules
└── test
├── manifests
└── modules
mkdir -pv /puppet/environment/{prod,test,dev}/{modules,manifests}
②在puppet.conf的master段添加environmentpath:指向多环境配置的目录
puppet config --section=master set environmentpath /puppet/environments
如果不想这样指定,可以在/etc/puppet/下建一个软链接environments指向该路径,就不需要改配置文件了
③定义模块,准备相关文件
cd /puppet/environment/prod/
mkdir modules/nginx/{manifests,files -pv}
cp /etc/nginx/nginx.conf modules/nginx/files/
vim modules/nginx/files/nginx.conf
配置进程数为worker_processes auto
④编辑清单文件,定义类:
vim prod/modules/nginx/manifests/init.pp
class nginx {
package{'nginx':
ensure => latest,
} ->
file{'nginx':
ensure => file,
source => 'puppet:///modules/nginx/nginx.conf',
path => '/etc/nginx/nginx.conf',
owner => 'root',
group => 'root',
mode => '0644',
} ~>
service{'nginx':
ensure => running,
enable => true,
hasrestart => true,
hasstatus => true,
restart => 'usr/sbin/nginx -s reload'
}
}
⑤编辑站点清单文件:
vim prod/manifests/site.pp
node 'server1.tingjie.com'{
include nginx
}
⑥配置testing环境和development环境的文件
cp -r modules/nginx/ /puppet/environment/test/modules/
cp -r modules/nginx/ /puppet/environment/dev/modules/
cp manifests/site.pp /puppet/environment/test/manifests/
cp manifests/site.pp /puppet/environment/dev/manifests/
vim test/modules/nginx/files/nginx.conf
worker_processes 2;
vim dev/modules/nginx/files/nginx.conf
worker_processes 1;
puppet parser validate test/modules/nginx/ 可以检查语法错误
最终的目录结构如下:
tree /puppet/environment/
/puppet/environment/
├── dev
│ ├── manifests
│ │ └── site.pp
│ └── modules
│ └── nginx
│ ├── files
│ │ └── nginx.conf
│ └── manifests
│ └── init.pp
├── prod
│ ├── manifests
│ │ └── site.pp
│ └── modules
│ └── nginx
│ ├── files
│ │ └── nginx.conf
│ └── manifests
│ └── init.pp
└── test
├── manifests
│ └── site.pp
└── modules
└── nginx
├── files
│ └── nginx.conf
└── manifests
└── init.pp
⑦重启服务:systemctl restart puppetmaster.service
2、server1端测试:
systemctl stop puppetagent.service
使用下面的命令可以看过程,当然也可以直接重启服务
puppet agent -v --noop --environment=prod --no-daemonize
puppet agent -v --noop --environment=test --no-daemonize
puppet agent -v --noop --environment=dev --no-daemonize
如果想固定一台机器使用的环境为prod,那么
vim /etc/puppet/puppet.conf
environment = prod
重启服务即可
以上是关于puppet的多环境实现的主要内容,如果未能解决你的问题,请参考以下文章
jenkins-使用继承实现基于 kubernetes Pod 的多容器的多构建环境的 Jenkins Slave