puppet布署公司开发的产品

回复 收藏
加入猿课这么久了第一次发帖子{:4_118:}{:4_118:}

公司下个月要用puppet部署新产品,这一个多月一直在啃puppet...{:4_102:}{:4_102:}
今天终于基本搞定了...
下面是我做的笔记,有什么不足的地方还请大家帮忙指正...{:4_94:}{:4_94:}

安装netcute


思路
使用file资源,从服务端获取安装文件、启动脚本、配置文件
使用exec资源,执行安装包解压缩、添加启动脚本到init、
使用ERB模板创建默认配置文件,在nood节点中定义不同nood的id和key,供ERB模版调用,为不同的节点创建配置文件
使用service资源,启动并确保netcute(serveer-daemon)服务正常运行,当配置文件更改后自动执行重启操作


各资源说明及相互间依赖关系
file ['/usr/local/netcute.1.tar.gz']                  ##安装文件  

exec ['tar xvf /usr/local/netcute.1.tar.gz']          ##执行对安装文件解压缩,当安装文件更改时触发执行操作

file ['/etc/init.d/netcuted']                         ##服务的启动脚本

exec ['chkconfig --add netcuted']                     ##将脚本添加到init,依赖file ['/etc/init.d/netcuted']且触发执行

file ['/usr/local/netcute/config/data-agent.json']    ##配置文件

service ['netcuted']                                  ##启动服务,依赖file ['/etc/init.d/netcuted'],
                                                               ##当file ['/usr/local/netcute/config/data-agent.json'] 更改后执行restart操作
具体实现
创建netcute启动脚本 (从网上找了个能看懂的,照葫芦画瓢,写的超级烂,欢迎拍砖{:4_118:}{:4_118:})

#!/bin/bash
#chkconfig:2345 55 25
#processname:service-daemon
#description:source data-agent

prog=/usr/local/src/vixtel/netcute/bin

start(){

   pid_daemon=`ps -ef |grep service-daemon | grep -v grep | awk '{print $2}'`
   pid_agent=`ps -ef |grep data-agent | grep -v grep | awk '{print $2}'`

   if [ "$pid_daemon" != "" ] && [ "$pid_agent" != "" ] ;then
        echo "服务已在运行..."
   else
        echo "正在启动服务...."
        $prog/service-daemon &
   fi
}

stop(){

    pid_daemon=`ps -ef |grep service-daemon | grep -v grep | awk '{print $2}'`
    pid_agent=`ps -ef |grep data-agent | grep -v grep | awk '{print $2}'`

    if [ "$pid_daemon" != "" ] && [ "$pid_agent" != "" ] ;then
         echo "正在停止服务...."
         kill $pid_agent
         kill $pid_daemon
    else
         echo "服务未运行..."
    fi
}

status(){

   pid_daemon=`ps -ef |grep service-daemon | grep -v grep | awk '{print $2}'`
   pid_agent=`ps -ef |grep data-agent | grep -v grep | awk '{print $2}'`

   if [ "$pid_daemon" != "" ] && [ "$pid_agent" != "" ] ;then
        echo "服务正在运行...."
   else
       echo "服务已停止运行!!!"
   fi
}


restart(){
    stop
    sleep 10
    start
}

case "$1" in
   "start")
       start
       ;;
   "stop")
       stop
       ;;
   "status")
       status
       ;;
    "restart")
       restart
       ;;
    *)
     echo "用法:$0 start|stop|status|restart"

esac



在pupetmaster 创建test模块
结构如下:

[root@puppet test]# !tr
tree /etc/puppet/modules/test/
/etc/puppet/modules/test/
├── files
│   ├── netcute.1.tar.gz
│   └── netcute-shell.sh
├── manifests
│   └── init.pp
└── templates
    └── data-agent.json.erb

3 directories, 4 files



编辑init.pp 文件


class test  {

  file {'/usr/local/netcute.1.tar.gz':
    owner    => 'root',
    group    => 'root',
    mode     => '744',
    source   => 'puppet:///modules/netcute/netcute.1.tar.gz'
  }

  exec {'tar xvf /usr/local/netcute.1.tar.gz':
    cwd         => "/usr/local",
    path        => "/bin:/usr/bin:/usr/sbin",
    subscribe   => File["/usr/local/netcute.1.tar.gz"],
    refreshonly => true,
  }

  file {'/etc/init.d/netcuted':
     owner    => 'root',
     group    => 'root',
     mode     => '755',
     source   => 'puppet:///modules/netcute/netcute-shell.sh'
  }

  exec {'chkconfig --add netcuted':
    path    => "/bin:/usr/bin:/usr/sbin:/sbin",
    subscribe   => File["/etc/init.d/netcuted"],
    refreshonly => true,
  }


  file {'/usr/local/netcute/config/data-agent.json':
    ensure   => present,
    content  => template('test/data-agent.json.erb'),
    owner    => 'root',
    group    => 'root',
    mode     => '0744',
    require  =>  Exec["tar xvf /usr/local/netcute.1.tar.gz"],
  }

  service {'netcuted':
    ensure      => running,
    enable     => true,
    hasstatus  => true,
    hasrestart => true,
    path        => "/etc/init.d/",
    #start       => "/etc/init.d/netcuted start",
    #status      => "/etc/init.d/netcuted status",
    #stop        => "/etc/init.d/netcuted stop",
    #restart     => "/etc/init.d/netcuted restart",
    require    => Exec ["chkconfig --add netcuted"],
    subscribe   => File["/usr/local/netcute/config/data-agent.json"],
  }


}


h3. 编辑ERB模版


{
        "global":
        {
                "shareName": "share/data-agent.shm",
                "notifierName": "dataagent",
                "heartbeatInterval": "10"
        },
        "log": {
                "type": "FILE|STDOUT|STDERR",
                "level": "INFO|WARNING|ERROR|FATAL",
                "append": true,
                "file": "log/data-agent.log",
                "timeCapacity": 10,
                "sizeCapacity": 2,
                "timeMilliSeconds": true
        },
        "agent": {
                "name": "Agent",
                "id": <%= scope.lookupvar('test::id_local')%>,    ##调用变量id_local
                "key": <%= scope.lookupvar('test::key')%>,        ##调用变量key
                "threadStackSize": 1024,
                "ioThreadCount": 0,
                "edgeServer": "http://172.30.1.178:8443"
        },
        "httpServer": {
                "enabled": false,
                "name": "Http API",
                "address": "0.0.0.0",
                "port": 9443,
                "maxConnections": 1000
        }
}



定义节点、变量id、key


[root@puppet templates]# vim /etc/puppet/manifests/site.pp
node 'agent.domain.com'{
  $id_local = 555
  $key = '"TEST-KEY"'
  include test
}


测试
在 agent.domain.com上操作

使用--noop 测试
[root@agent ~]# puppet agent --server puppet.domain.com --test --noop
Info: Caching certificate for agent.domain.com
Info: Caching certificate_revocation_list for ca
Info: Caching certificate for agent.domain.com
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for agent.domain.com
Info: Applying configuration version '1462934510'
Notice: /Stage[main]/Test/File[/usr/local/netcute.1.tar.gz]/ensure: current_value absent, should be file (noop)
Info: /Stage[main]/Test/File[/usr/local/netcute.1.tar.gz]: Scheduling refresh of Exec[tar xvf /usr/local/netcute.1.tar.gz]
Notice: /Stage[main]/Test/Exec[tar xvf /usr/local/netcute.1.tar.gz]: Would have triggered 'refresh' from 1 events
Notice: /Stage[main]/Test/File[/usr/local/netcute/config/data-agent.json]/ensure: current_value absent, should be present (noop)
Info: /Stage[main]/Test/File[/usr/local/netcute/config/data-agent.json]: Scheduling refresh of Service[netcuted]
Notice: /Stage[main]/Test/File[/etc/init.d/netcuted]/ensure: current_value absent, should be file (noop)
Info: /Stage[main]/Test/File[/etc/init.d/netcuted]: Scheduling refresh of Exec[chkconfig --add netcuted]
Notice: /Stage[main]/Test/Exec[chkconfig --add netcuted]: Would have triggered 'refresh' from 1 events
Notice: /Stage[main]/Test/Service[netcuted]/ensure: current_value stopped, should be running (noop)
Info: /Stage[main]/Test/Service[netcuted]: Unscheduling refresh on Service[netcuted]
Notice: Class[Test]: Would have triggered 'refresh' from 6 events
Notice: Stage[main]: Would have triggered 'refresh' from 1 events
Info: Creating state file /var/lib/puppet/state/state.yaml
Notice: Finished catalog run in 0.29 seconds

没有出现错误

实际执行

[root@agent ~]# puppet agent --server puppet.domain.com --test
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for agent.domain.com
Info: Applying configuration version '1462934510'
Notice: /Stage[main]/Test/File[/usr/local/netcute.1.tar.gz]/ensure: defined content as '{md5}15002950736a3ac95715b7c07eb57760'
Info: /Stage[main]/Test/File[/usr/local/netcute.1.tar.gz]: Scheduling refresh of Exec[tar xvf /usr/local/netcute.1.tar.gz]
Notice: /Stage[main]/Test/Exec[tar xvf /usr/local/netcute.1.tar.gz]: Triggered 'refresh' from 1 events
Notice: /Stage[main]/Test/File[/usr/local/netcute/config/data-agent.json]/content:
--- /usr/local/netcute/config/data-agent.json        2016-05-09 15:44:41.000000000 +0800
+++ /tmp/puppet-file20160511-1846-v2qzbx-0        2016-05-11 18:44:19.142420323 +0800
@@ -1,32 +1,33 @@
-{
-        "global":
-        {
-                "shareName": "share/data-agent.shm",
-                "notifierName": "dataagent",
-                "heartbeatInterval": "10"
-        },
-        "log": {
-                "type": "FILE|STDOUT|STDERR",
-                "level": "INFO|WARNING|ERROR|FATAL",
-                "append": true,
-                "file": "log/data-agent.log",
-                "timeCapacity": 10,
-                "sizeCapacity": 2,
-                "timeMilliSeconds": true
-        },
-        "agent": {
-                "name": "Agent",
-                "id": 5,
-                "key": "123",
-                "threadStackSize": 1024,
-                "ioThreadCount": 0,
-                "edgeServer": "http://172.30.1.178:8443"
-        },
-        "httpServer": {
-                "enabled": false,
-                "name": "Http API",
-                "address": "0.0.0.0",
-                "port": 9443,
-                "maxConnections": 1000
-        }
-}
+{
+        "global":
+        {
+                "shareName": "share/data-agent.shm",
+                "notifierName": "dataagent",
+                "heartbeatInterval": "10"
+        },
+        "log": {
+                "type": "FILE|STDOUT|STDERR",
+                "level": "INFO|WARNING|ERROR|FATAL",
+                "append": true,
+                "file": "log/data-agent.log",
+                "timeCapacity": 10,
+                "sizeCapacity": 2,
+                "timeMilliSeconds": true
+        },
+        "agent": {
+                "name": "Agent",
+                "id": 555,
+                "key": "TEST-KEY",
+                "threadStackSize": 1024,
+                "ioThreadCount": 0,
+                "edgeServer": "http://172.30.1.178:8443"
+        },
+        "httpServer": {
+                "enabled": false,
+                "name": "Http API",
+                "address": "0.0.0.0",
+                "port": 9443,
+                "maxConnections": 1000
+        }
+}
+

Info: Computing checksum on file /usr/local/netcute/config/data-agent.json
Info: /Stage[main]/Test/File[/usr/local/netcute/config/data-agent.json]: Filebucketed /usr/local/netcute/config/data-agent.json to puppet with sum 32dd3c5991a21dafe4b989936a696680
Notice: /Stage[main]/Test/File[/usr/local/netcute/config/data-agent.json]/content: content changed '{md5}32dd3c5991a21dafe4b989936a696680' to '{md5}af6127ce2e5b29eaf3cac307c1b47356'
Info: /Stage[main]/Test/File[/usr/local/netcute/config/data-agent.json]: Scheduling refresh of Service[netcuted]
Notice: /Stage[main]/Test/File[/etc/init.d/netcuted]/ensure: defined content as '{md5}7bcaf98eac579e0f14f3bec0a6fa8c19'
Info: /Stage[main]/Test/File[/etc/init.d/netcuted]: Scheduling refresh of Exec[chkconfig --add netcuted]
Notice: /Stage[main]/Test/Exec[chkconfig --add netcuted]: Triggered 'refresh' from 1 events
Notice: /Stage[main]/Test/Service[netcuted]: Triggered 'refresh' from 1 events
Notice: Finished catalog run in 15.83 seconds

执行成功
查看服务状态,验证布署
>>>>
[root@agent ~]# ps aux | grep service-daemon
root       5352  0.4  0.3  18448  1620 ?        S    18:44   0:00 /usr/local/netcute/bin/service-daemon
root       5360  0.0  0.1 103316   868 pts/0    D+   18:45   0:00 grep service-daemon
[root@agent ~]# ps aux | grep data-agent
root       5355  0.1  0.4  89692  2236 ?        Ssl  18:44   0:00 bin/data-agent --daemon --resume
root       5362  0.0  0.1 103316   900 pts/0    S+   18:45   0:00 grep data-agent

>>>> 服务正常启动

---------------------------------------------------------------------------------------------------------------------------------------------------
问题:当手动在agent端将服务kill掉后,puppet不会重新启动服务
         感觉问题出在启动脚本,或者是服务缺少某个文件,导致puppet无法监控服务的运行状态

         需要了解puppet监控服务运行状态的原理,网上没找到,有哪位大牛知道的请方便告诉一下{:4_114:}{:4_114:}

明天公司给我一台服务器和几台硬件agent让我做测试,会继续跟进测试中出现的问题...希望大牛们可以多帮帮忙{:4_98:}(唉,没人带{:4_102:}{:4_102:}全公司就我一个渣渣运维{:4_97:})


2016-05-11 17:40 举报
已邀请:
0

乐橙306

赞同来自:

加油!
0

hzsnone

赞同来自:

感谢分享

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: