设为首页 收藏本站
查看: 842|回复: 0

[经验分享] linux下用puppet搭建lamp和lnmp-LINUXU

[复制链接]

尚未签到

发表于 2018-8-2 10:07:32 | 显示全部楼层 |阅读模式
  puppet是一种Linux、Unix、windows平台的集中配置管理系统,使用自有的puppet描述语言,可管理配置文件、用户、cron任务、软件包、系统服务等。puppet把这些系统实体称之为资源,puppet的设计目标是简化对这些资源的管理以及妥善处理资源间的依赖关系。
  puppet采用C/S星状的结构,所有的客户端和一个或几个服务器交互。每个客户端周期的(默认半个小时)向服务器发送请求,获得其最新的配置信息,保证和该配置信息同步。每个puppet客户端每半小时(可以设置)连接一次服务器端,下载最新的配置文件并且严格按照配置文件来配置服务器。 配置完成以后,puppet客户端可以反馈给服务器端一个消息。如果出错,也会给服务器端反馈一个消息。
  实验目的:
  1、使用puppet搭建lamp
  2、使用puppet搭建nnmp
  节点:
  node1.stu30.com 172.16.30.1 master
  node2.stu30.com 172.16.30.2 agent lamp
  node3.stu30.com 172.16.30.3 agent lnmp
  通过master管理两个节点分别安装lamp和lnmp环境,lamp使用php模块,lnmp使用php-fpm,nginx通过rpm包安装,并为vim添加nginx的语法高亮模块,并添加cron任务实现,每隔三分钟同步一次时间
  modules目录树状图:执行下面的模块安装时,此树状图中的文件必须存在,不然会报错。

  安装过程:
  node1节点安装puppet-server:
[root@node1 2.7.25]# ls  
facter-1.7.5-1.el6.x86_64.rpm   puppet-dashboard-1.2.23-1.el6.noarch.rpm
  
puppet-2.7.25-1.el6.noarch.rpm  puppet-server-2.7.25-1.el6.noarch.rpm
  
[root@node1 2.7.25]# yum install -y facter-1.7.5-1.el6.x86_64.rpm puppet-2.7.25-1.el6.noarch.rpm puppet-server-2.7.25-1.el6.noarch.rpm
  启动测试:
[root@node1 ~]# puppet master -v -d –noop –no-daemonize  命令说明:
  -v:显示额外信息
  -d:显示debug信息
  –noop:不真正执行,只是测试
  –no-daemonize:不以守护进程方式运行,即前台运行,可以看到运行时的详细信息
  第一次运行会有如下信息:表明node1自己生成了私钥,并生成证书签署请求,自签证书,自动生成一个CA服务器,因为master与agent之间是通过ssl通信的,所以必须有证书才行,如果有其他客户端,master会签署其他客户端的签署请求生成证书。
info: Creating a new SSL key for ca  
info: Creating a new SSL certificate request for ca
  
info: Certificate Request fingerprint (md5): 25:08:0F:4F:4C:08:CB:0C:51:1D:9E:26:6E:E7:FF:85
  
notice: Signed certificate request for ca
  
notice: Rebuilding inventory file
  
debug: Using cached certificate for ca
  
info: Creating a new certificate revocation list
  
info: Creating a new SSL key for node1.stu30.com
  
debug: Using cached certificate for ca
  
info: Creating a new SSL certificate request for node1.stu30.com
  
info: Certificate Request fingerprint (md5): 6F:90:90:1B:CC:BC:FD:02:32:69:53:F3:E9:20:84:AC
  
notice: node1.stu30.com has a waiting certificate request
  
debug: Using cached certificate for ca
  
debug: Using cached certificate_request for node1.stu30.com
  
notice: Signed certificate request for node1.stu30.com
  
notice: Removing file Puppet::SSL::CertificateRequest node1.stu30.com at '/var/lib/puppet/ssl/ca/requests/node1.stu30.com.pem'
  
notice: Removing file Puppet::SSL::CertificateRequest node1.stu30.com at '/var/lib/puppet/ssl/certificate_requests/node1.stu30.com.pem'
  这样master就没有问题了,Ctrl+c终止运行,使用命令server puppetmaster start以守护进程运行在后台,监听端口为8140:
http://linuxu.qiniudn.com/wp-content/uploads/2015/02/wpid-2f795faf146ad4fac0744c4c168f5852_621b2e8f-c370-4dbe-930d-50843fc948f4.png下面开始配置agent节点node3:只需要安装agent就可以了,不需要安装server端

[root@node3 2.7.25]# yum  install -y puppet-2.7.25-1.el6.noarch.rpm  facter-1.7.5-1.el6.x86_64.rpm  和master一样,先进行测试运行:
[root@node3 2.7.25]# puppet agent –server=node1.stu30.com -v -d –noop –no-daemonize  此时需要添加另外一个参数–server,告诉agent谁是master,如果以守护进程使用需要去配置文件[agent]段中添加server = master_hostname,必须是master主机名,因为CA服务器是以主机名判断的,像本实验做了如下配置:

  如果运行没有爆出error错误,就可以运行了,为了能够看到运行的状态,我们将–noop参数去掉,同样以非守护进程运行
[root@node3 2.7.25]# puppet agent --server=node1.stu30.com -v -d  --no-daemonize  
debug: Finishing transaction 70043945071220
  
info: Creating a new SSL key for node3.stu30.com
  
info: Caching certificate for ca
  
info: Creating a new SSL certificate request for node3.stu30.com
  
info: Certificate Request fingerprint (md5): 3A:AE:32:5B:31:FB:CD:09:E3:96:E6:4E:FA:D0:0F:7C
  
debug: Using cached certificate for ca
  
debug: Using cached certificate for ca
  此时会从信息中看出,node3生成了一个证书签署请求等待master请求
  现在我们去node1节点查看
[root@node1 ~]# puppet cert list  
  "node3.stu30.com" (87:DD:C2:66:41:9F:69:34:47:2A:9E:78:30:F6:37:63)
  已经生成了,puppet cert list是显示等待签署的证书,下面签署证书
[root@node1 ~]# puppet cert sign node3.stu30.com  
notice: Signed certificate request for node3.stu30.com
  
notice: Removing file Puppet::SSL::CertificateRequest node3.stu30.com at '/var/lib/puppet/ssl/ca/requests/node3.stu30.com.pem
  证书签署完毕,并且告诉我们存放的位置
  Ctrl+c终止程序,并再次运行,agent便会向master发送catalog日志了
debug: Using cached certificate for node3.stu30.com  
info: Caching certificate_revocation_list for ca
  
info: Caching catalog for node3.stu30.com
  
debug: Creating default schedules
  
info: Applying configuration version '1422608294'
  node3的agent便安装好了,node2的安装方法一样,这里不再说明
  下面开始配置我们的模块,按照上面的树状图创建所需要的目录
  模块对应的init.pp文件
  mysql:
class mysql{  
    package{'mysql-server':
  
        ensure => installed, ##安装的状态
  
    }
  
    file {'my.cnf':
  
        ensure => file,
  
        source => 'puppet:///modules/mysql/my.cnf', ##配置文件,此路径是master相应模块里的files里的文件,注意,路径里不需要填写files路径,直接在模块下填写文件名就可以了
  
        path => '/etc/my.cnf', ##此路径是说明将配置文件存放在agent上的位置
  
        require =>  Package['mysql-server'], ## 执行此资源需要的前提条件,即必须安装上mysql才能将配置文件粗制到agent
  
        notify => Service['mysqld'],## 通知机制,如果配置文件发生了变化,则通知service资源,service是对mysql进行重启的资源
  
    }
  

  
    service{'mysqld':
  
        ensure => running, ##确保mysql安装上之后的状态为running
  
        enable => false,  ##不开机启动
  
        require => Package['mysql-server'], ##需要package安装完成才能执行
  
        require => File['my.cnf'], ##需要配置文件复制完毕之后才能执行
  
    }
  
}
  httpd_php:此处和上面一样就不一一说明了
class httpd_php{  
    package{['httpd','php']:  ##可以同时安装两个包
  
            ensure => installed,
  
    }
  
    file{'httpd.conf':
  
        source => 'puppet:///modules/httpd_php/httpd.conf',
  
        path => '/etc/httpd/conf/httpd.conf',
  
        require => Package['httpd','php'],
  
        notify => Service['httpd'],
  
        }
  
    file{'php.ini':
  
         ensure => file,
  
         source => 'puppet:///modules/httpd_php/php.ini',
  
         path => '/etc/php.ini',
  
         require => Package['php'],
  
         notify => Service['httpd'],
  
        }
  
    file{'index.php':
  
         ensure => file,
  
         source => 'puppet:///modules/httpd_php/index.php',
  
         path => '/var/www/html/index.php',
  
         require => Package['httpd'],
  

  
    }
  
    service{'httpd':
  
            ensure => running,
  
            enable => false,
  
            require => Package['httpd','php'],
  

  
    }
  
}
  nginx:
class nginx{  
    package{'nginx':
  
            ensure => latest, ##安装状态
  
            provider => rpm, ##因为是以rpm包安装,所以这里使用rpm安装
  
            source => '/tmp/nginx-1.6.2-1.el6.ngx.x86_64.rpm', ##rpm包的路径,此处的路径需要和下面复制的rpm的路径一致
  
    }
  

  
    file{'default.conf':
  
        ensure => file,
  
        source => 'puppet:///modules/nginx/default.conf',
  
        path => '/etc/nginx/conf.d/default.conf',
  
        require => Package['nginx'],
  
        notify => Service['nginx'],
  
        }
  
    file{'index.php':
  
         ensure => file,
  
         source => 'puppet:///modules/nginx/index.php',
  
         path => '/usr/share/nginx/html/index.php',
  
         require => Package['nginx'],
  
        }
  
    file{'nginxrpm':
  
         ensure => file,
  
         source => 'puppet:///modules/nginx/nginx-1.6.2-1.el6.ngx.x86_64.rpm',
  
         path => '/tmp/nginx-1.6.2-1.el6.ngx.x86_64.rpm',
  
         before => Package['nginx'],
  

  
    }
  
    service{'nginx':
  
            ensure => running,
  
            enable => false,
  
            require => Package['nginx'],
  
        }
  
}
  nginx_syntax:nginx语法高亮模块
class nginx_syntax{  
       exec{'mkdir -pv .vim/syntax': ##执行创建目录的命令
  
            cwd => '/root', ## 创建目录的命令是在那个目录执行的,说的好理解点,就像是先cd到哪个目录,在执行创建命令,即当前工作目录
  
            path    => ['/bin'], ##mkdir的路径,此处只写到父目录即可
  
            creates => '/root/.vim/syntax', ## 创建完成之后的目录,如果此目录不存在则会创建,如果存在则不会创建,必须全部存在才不会创建,比如.vim目录存在,而syntax不存在,则只会创建syntax而不会创建.vim了
  
            before => File['filetype.vim','nginx.vim'],##此操作必须在复制文件之前,因为文件必须复制到此目录里
  
           }
  
        file{'filetype.vim':
  
             ensure => file,
  
             source => 'puppet:///modules/nginx_syntax/filetype.vim',
  
             path => '/root/.vim/filetype.vim',
  
       }
  
       file{'nginx.vim':
  
            ensure => file,
  
            source => 'puppet:///modules/nginx_syntax/nginx.vim',
  
            path => '/root/.vim/syntax/nginx.vim',
  
           }
  
}
  ntp:cron任务模块
class ntp {  ##父类  
}
  
        class ntp::synctime inherits ntp { ## 子类,继承上面的父类ntp,此处只是说明继承这个用法,完全可以只声明一个类
  
        cron{'synctime':
  
                ensure  => present, ## 状态,和package的installed作用一样,就是必须得有
  
                minute  => '*/5', ##定义cron里的时间,省略的默认为*
  
                command => '/usr/sbin/ntpdate 172.16.0.1 &> /dev/null', ##cron中添加的命令
  
                name    => 'sync time from ntp server', ##此cron的名字
  
        }
  
}
  php-fpm:nginx将php的请求代理至php-fpm
class nginx_syntax{  
       exec{'mkdir -pv .vim/syntax': ##执行创建目录的命令
  
            cwd => '/root', ## 创建目录的命令是在那个目录执行的,说的好理解点,就像是先cd到哪个目录,在执行创建命令,即当前工作目录
  
            path    => ['/bin'], ##mkdir的路径,此处只写到父目录即可
  
            creates => '/root/.vim/syntax', ## 创建完成之后的目录,如果此目录不存在则会创建,如果存在则不会创建,必须全部存在才不会创建,比如.vim目录存在,而syntax不存在,则只会创建syntax而不会创建.vim了
  
            before => File['filetype.vim','nginx.vim'],##此操作必须在复制文件之前,因为文件必须复制到此目录里
  
           }
  
        file{'filetype.vim':
  
             ensure => file,
  
             source => 'puppet:///modules/nginx_syntax/filetype.vim',
  
             path => '/root/.vim/filetype.vim',
  
       }
  
       file{'nginx.vim':
  
            ensure => file,
  
            source => 'puppet:///modules/nginx_syntax/nginx.vim',
  
            path => '/root/.vim/syntax/nginx.vim',
  
           }
  
}
  每个模块的配置文件都说明完毕,我们再来看看/etc/puppet/manifest目录下的文件:

  node2.lamp.pp:

  node3.lnmp.pp:

  因为agent只会读取site.pp所以我们需要编辑site.pp文件:

  现在master已经配置好了,我们重启以下master服务,并确定端口已经监听
  [root@node1 ~]# server puppetmaster restart

  node2,node3启动agent:
  node2:
[root@node2 ~]# puppet agent -v --no-daemonize --server=node1.stu30.com  
info: Caching certificate for node2.stu30.com
  
notice: Starting Puppet client version 2.7.25
  
info: Caching certificate_revocation_list for ca
  
info: Caching catalog for node2.stu30.com
  
info: Applying configuration version '1422677379'
  
notice: /Stage[main]/Httpd_php/Package[php]/ensure: created
  
notice: /Stage[main]/Mysql/Package[mysql-server]/ensure: created
  
info: FileBucket adding {md5}8ace886bbe7e274448bc8bea16d3ead6
  
info: /Stage[main]/Mysql/File[my.cnf]: Filebucketed /etc/my.cnf to puppet with sum 8ace886bbe7e274448bc8bea16d3ead6
  
notice: /Stage[main]/Mysql/File[my.cnf]/content: content changed '{md5}8ace886bbe7e274448bc8bea16d3ead6' to '{md5}dcb78c908cf0077856ebd212792e9ab2'
  
info: /Stage[main]/Mysql/File[my.cnf]: Scheduling refresh of Service[mysqld]
  
notice: /Stage[main]/Mysql/Service[mysqld]/ensure: ensure changed 'stopped' to 'running'
  
notice: /Stage[main]/Mysql/Service[mysqld]: Triggered 'refresh' from 1 events
  
notice: /Stage[main]/Ntp::Synctime/Cron[synctime]/ensure: created
  
notice: /Stage[main]/Httpd_php/File[index.php]/ensure: defined content as '{md5}eb7009d32f9ae4035f2973b7fb8ae29c'
  
notice: /Stage[main]/Httpd_php/Service[httpd]/ensure: ensure changed 'stopped' to 'running'
  
info: Creating state file /var/lib/puppet/state/state.yaml
  
notice: Finished catalog run in 42.28 seconds
  安装之后检查监听端口,并访问测试:


  node3:
[root@node3 ~]# puppet agent -v --no-daemonize --server=node1.stu30.com  
info: Caching certificate for node3.stu30.com
  
notice: Starting Puppet client version 2.7.25
  
info: Caching certificate_revocation_list for ca
  
info: Caching catalog for node3.stu30.com
  
info: Applying configuration version '1422677379'
  
notice: /Stage[main]/Nginx_syntax/File[nginx.vim]/ensure: defined content as '{md5}10395c7a028cc58030f82ab296f13ff3'
  
notice: /Stage[main]/Nginx_syntax/File[filetype.vim]/ensure: defined content as '{md5}dee2611ad012952aa899705cb8821b59'
  
notice: /Stage[main]/Mysql/Package[mysql-server]/ensure: created
  
info: FileBucket adding {md5}8ace886bbe7e274448bc8bea16d3ead6
  
info: /Stage[main]/Mysql/File[my.cnf]: Filebucketed /etc/my.cnf to puppet with sum 8ace886bbe7e274448bc8bea16d3ead6
  
notice: /Stage[main]/Mysql/File[my.cnf]/content: content changed '{md5}8ace886bbe7e274448bc8bea16d3ead6' to '{md5}dcb78c908cf0077856ebd212792e9ab2'
  
info: /Stage[main]/Mysql/File[my.cnf]: Scheduling refresh of Service[mysqld]
  
notice: /Stage[main]/Mysql/Service[mysqld]/ensure: ensure changed 'stopped' to 'running'
  
notice: /Stage[main]/Mysql/Service[mysqld]: Triggered 'refresh' from 1 events
  
notice: /Stage[main]/Ntp::Synctime/Cron[synctime]/ensure: created
  
notice: /Stage[main]/Nginx/File[nginxrpm]/ensure: defined content as '{md5}e6f65f654b90f2356a9a3c52f37b53eb'
  
notice: /Stage[main]/Nginx/Package[nginx]/ensure: created
  
notice: /Stage[main]/Nginx/File[index.php]/ensure: defined content as '{md5}28433e7b2073e6222f3882e3a9d86c82'
  
info: FileBucket adding {md5}4dce452bf8dbb01f278ec0ea9ba6cf40
  
info: /Stage[main]/Nginx/File[default.conf]: Filebucketed /etc/nginx/conf.d/default.conf to puppet with sum 4dce452bf8dbb01f278ec0ea9ba6cf40
  
notice: /Stage[main]/Nginx/File[default.conf]/content: content changed '{md5}4dce452bf8dbb01f278ec0ea9ba6cf40' to '{md5}3eda5027b81f41800de51afdc0a6b111'
  
info: /Stage[main]/Nginx/File[default.conf]: Scheduling refresh of Service[nginx]
  
notice: /Stage[main]/Nginx/Service[nginx]/ensure: ensure changed 'stopped' to 'running'
  
notice: /Stage[main]/Nginx/Service[nginx]: Triggered 'refresh' from 1 events
  
notice: /Stage[main]/Php-fpm/Package[php-fpm]/ensure: created
  
notice: /Stage[main]/Php-fpm/Service[php-fpm]/ensure: ensure changed 'stopped' to 'running'
  
info: Creating state file /var/lib/puppet/state/state.yaml
  安装之后检查监听端口,并访问测试:


  到此使用puppet分别部署lamp和lnmp已经完成了,上面的语言描述可能有不准确的地方,可能有些地方有错误,还希望指正~
  问题总结:
  1、签署过证书之后,为了做测试还原虚拟机之后,再次运行agent会提示找不到ssl文件

  按照提示分别在master和puppet执行操作;
  2、如果提示pid文件无法创建,需将pid删除,并ps aux 找到pid kill掉;
  3、添加nginx配置文件的语法高亮时,不会判断是否存在nginx是否存在,因为,是作为一个单独模块存在的,可以将其整合到nginx模块中,并添加限制require;
  4、以下问题出现的原因分析

  在/etc/puppet/manifests/目录下面创建了很多的对应模块的pp文件以及site.pp文件,而site.pp文件内容为import “*.pp”,意思为导入当前目录下的所有以pp结尾的文件,这会将自己同时导入,造成错误,只需修改site.pp文件中的*.pp为你要使用的pp文件,比如import “nginx.pp”,或者在site.pp的目录下新建目录来存放非site.pp的文件,并保持site.pp为import “创建的目录/*.pp”,就不会出错了;
  5、以下问题原因分析http://linuxu.qiniudn.com/wp-content/uploads/2015/02/wpid-2f795faf146ad4fac0744c4c168f5852_e6ebd53c-36c4-46d3-8751-bc007cffa0f5.png
  出现这个错误是因为之前将modules目录和manifests目录备份到了虚拟机的共享文件夹里,造成了无权限读取,所有文件权限为770,一定要注意权限的问题也会造成无法读取pp文件。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-545171-1-1.html 上篇帖子: Puppet错误及解决方法(三) 下篇帖子: Linux 运维批量部署工具
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表