自動化運維工具Puppet

    開發puppet模塊,nginx負載均衡并反代動態請求至httpd,httpd用ajp連接器將反代請求至tomcat,并部署tomcat-session-memcached

架構圖為

自動化運維工具Puppet

在master主機上開發的模塊為:

1、chrony模塊;

├── chrony
│   ├── files
│   │   └── chrony.conf
│   ├── lib
│   ├── manifests
│   │   └── init.pp
│   ├── spec
│   ├── templates
│   └── test

vim init.pp

class chrony {
                    package{‘chrony’:
                     ensure  => latest,
        } ->

        file{‘chrony.conf’:
                    path    => ‘/etc/chrony.conf’,
                    source  => ‘puppet:///modules/chrony/chrony.conf’,
        } ~>

        service{‘chronyd’:
                    ensure  => running,
                    enable  => true,
            }
    }
vim chrony/files/chrony.conf
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst

#在這里輸入我們要根據那個主機同步時間
server 172.16.252.187 iburst
# Ignore stratum in source selection.
stratumweight 0

# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

# Enable kernel RTC synchronization.
rtcsync

# In first three updates step the system clock instead of slew
# if the adjustment is larger than 10 seconds.
makestep 10 3

# Allow NTP client access from local network.
#allow 192.168/16

# Listen for commands only on localhost.
bindcmdaddress 127.0.0.1
bindcmdaddress ::1

# Serve time even if not synchronized to any NTP server.
#local stratum 10

keyfile /etc/chrony.keys

# Specify the key used as password for chronyc.
commandkey 1

# Generate command key if missing.
generatecommandkey

# Disable logging of client accesses.
noclientlog

# Send a message to syslog if a clock adjustment is larger than 0.5 seconds.
logchange 0.5

logdir /var/log/chrony
#log measurements statistics tracking

2、nginx模塊:

── nginx
│   ├── files
│   ├── lib
│   ├── manifests
│   │   └── init.pp
│   ├── spec
│   ├── templates
│   │   └── nginx-proxy.conf.erb
│   └── tests
init.pp;

class nginx {
        package{‘nginx’:
                ensure => latest,
                } ->
        file{‘nginx-proxy.conf’:
                path => ‘/etc/nginx/nginx.conf’,
                content => template(‘/etc/puppet/modules/nginx/templates/nginx-proxy.conf.erb’),
           } ~>
        service{‘nginx’:
                ensure => running,
                enable => true,
                restart => ‘systemctl restart nginx.service’,
                }

}
 vim nginx/templates/nginx-proxy.conf.erb
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes <%= @processorcount %>;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  ‘$remote_addr – $remote_user [$time_local] “$request” ‘
                      ‘$status $body_bytes_sent “$http_referer” ‘
                      ‘”$http_user_agent” “$http_x_forwarded_for”‘;

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    upstream tcs {
        server tomcat0.zcylinux.io:80;
        server tomcat1.zcylinux.io:80;
}
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }
        location ~* \.jsp$ {
                proxy_pass http://tcs;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

3、httpd模塊:

├── httpd
│   ├── files
│   ├── lib
│   ├── manifests
│   │   └── init.pp
│   ├── spec
│   ├── templates
│   │   └── zcylinux-ajp.conf.erb
│   └── tests

vim init.pp


class httpd {
        package{‘httpd’:
                ensure => latest,
             } ->
        file{‘httpd-ajp.conf’:
                path => ‘/etc/httpd/conf.d/zcylinux.conf’,
                content => template(‘/etc/puppet/modules/httpd/templates/zcylinux-ajp.conf.erb’),
           } ~>
        service{‘httpd’:
                ensure => running,
                enable => true,
                restart => ‘systemctl restart httpd.service’,
          }
}
vim httpd/templates/zcylinux-ajp.conf.erb
<VirtualHost *:80>
        ServerName <%= @fqdn %>
        ProxyRequests off
        ProxyPreserveHost on
        ProxyVia on
         <Proxy *>
                Require all granted
         </Proxy>
        ProxyPass / ajp://<%= @ipaddress %>:8009/
        ProxyPassReverse / ajp://<%= @ipaddress %>:8009/
         <Location />
                Require all granted
         </Location>
</VirtualHost>

4、jdk:

── jdk
│   ├── files
│   │   └── java.sh
│   ├── lib
│   ├── manifests
│   │   └── init.pp
│   ├── spec
│   ├── templates
│   └── tests

init.pp:

class jdk {
        package{‘java-1.8.0-openjdk-devel’:
                ensure => latest,
           }
        file{‘java.sh’:
                ensure => file,
                path => ‘/etc/profile.d/java.sh’,
                source => ‘puppet:///modules/jdk/java.sh’,
                mode => 0766,
          }
}
   vim jdk/files/java.sh

   export JAVA_HOME=/usr

5、tomcat
    ├── files
    │   ├── index-testA.jsp
    │   ├── index-testB.jsp
    │   ├── javolution-5.4.3.1.jar
    │   ├── memcached-session-manager-2.1.1.jar
    │   ├── memcached-session-manager-tc7-2.1.1.jar
    │   ├── msm-javolution-serializer-2.1.1.jar
    │   ├── server.xml
    │   ├── spymemcached-2.11.1.jar
    │   └── tomcat-users.xml
    ├── lib
    ├── manifests
    │   └── init.pp
    ├── spec
    ├── templates
    └── tests
init.pp

class tomcat {
        if $fqdn =~ /(?i-mx:tomcat0.zcylinux.io)/ {
            $index=’index-testA.jsp’
        } else {
            $index=’index-testB.jsp’
        }

        package{[‘tomcat’,’tomcat-webapps’,’tomcat-admin-webapps’,’tomcat-docs-webapp’]:
                ensure => latest,
               } ->

        file{‘server.xml’:
                path => ‘/etc/tomcat/server.xml’,
                source => ‘puppet:///modules/tomcat/server.xml’,
                owner => ‘root’,
                group => ‘tomcat’,
           } ->
        exec{‘mkdir’:
                command => ‘mkdir -p /var/lib/tomcat/webapps/test/{classes,lib,WEB-INF,META-INF}’,
                path => ‘/bin:/sbin:/usr/bin:/usr/sbin’,
                creates => ‘/var/lib/tomcat/webapps/test’,
          } ->
        file{“$index”:
                path => ‘/var/lib/tomcat/webapps/test/index.jsp’,
                source => “puppet:///modules/tomcat/$index”,
          } ->
        exec{‘chown’:
                command => ‘chown -R tomcat:tomcat /var/lib/tomcat/webapps/test/’,
                path => ‘/bin:/sbin:/usr/bin:/usr/sbin’,
          } ->
        file{‘javolution-5.4.3.1.jar’:
                path => ‘/usr/share/tomcat/lib/javolution-5.4.3.1.jar’,
                source => ‘puppet:///modules/tomcat/javolution-5.4.3.1.jar’,
        }
        file{‘memcached-session-manager-tc7-2.1.1.jar’:
                path => ‘/usr/share/tomcat/lib/memcached-session-manager-tc7-2.1.1.jar’,
                source => ‘puppet:///modules/tomcat/memcached-session-manager-tc7-2.1.1.jar’,
        }
        file{‘spymemcached-2.11.1.jar’:
                path => ‘/usr/share/tomcat/lib/spymemcached-2.11.1.jar’,
                source => ‘puppet:///modules/tomcat/spymemcached-2.11.1.jar’,
        }
        file{‘memcached-session-manager-2.1.1.jar’:
                path => ‘/usr/share/tomcat/lib/memcached-session-manager-2.1.1.jar’,
                source => ‘puppet:///modules/tomcat/memcached-session-manager-2.1.1.jar’,
        }
        file{‘msm-javolution-serializer-2.1.1.jar’:
                path => ‘/usr/share/tomcat/lib/msm-javolution-serializer-2.1.1.jar’,
                source => ‘puppet:///modules/tomcat/msm-javolution-serializer-2.1.1.jar’,
        }
        service{‘tomcat’:
                ensure => running,
                enable => true,
                restart => ‘systemctl restart tomcat.service’,
                subscribe => File[‘server.xml’],
         }

}
自動化運維工具Puppet

vim server.xml

    加入一個contest,使得能夠將session保存在memcached中

自動化運維工具Puppet


編輯測試頁面

vim index-testA.jsp:

<%@ page language=”java” %>
<html>
  <head><title>TomcatA</title></head>
  <body>
    <h1><font color=”red”>TomcatA.magedu.com</font></h1>
    <table align=”centre” border=”1″>
      <tr>
        <td>Session ID</td>
    <% session.setAttribute(“magedu.com”,”magedu.com”); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>
~        
vim index-testB.jsp:

<%@ page language=”java” %>
<html>
  <head><title>TomcatB</title></head>
  <body>
    <h1><font color=”red”>TomcatB.magedu.com</font></h1>
    <table align=”centre” border=”1″>
      <tr>
        <td>Session ID</td>
    <% session.setAttribute(“magedu.com”,”magedu.com”); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>


6、 memcached
│   ├── files
│   ├── lib
│   ├── manifests
│   │   └── init.pp
│   ├── spec
│   ├── templates
│   └── tests

init.pp

class memcached {
        package{‘memcached’:
                ensure => latest,
        } ->
        service{‘memcached’:
                ensure => running,
                enable => true,
         }
}

編輯列表清單:

  vim /etc/puppet/manifests/site.pp
        node ‘ngx.zcylinux.io’ {
                include chrony
                 include nginx
            }

        node /tomcat[0-1]\.zcylinux\.io/ {
                include chrony
                include jdk
                include tomcat
                include httpd
        }
        node /mem[1-2]\.zcylinux\.io/ {
                include chrony
                include memcached
        }

master中安裝:
     注意版本:

            facter, puppet, puppet-server
我這里用的是:

自動化運維工具Puppet


agent主機上:

facter, puppet


初始化(即生成CA簽署agent主機過程);

        puppet master –no-daemonize -v  可視化,觀察過程

也可直接開啟服務:systemctl start puppetmaster.service


agnet端請求簽證:puppet agent –server master.zcylinux.io –no-daemonize -v

請求后,master端需要簽署:puppet cert sign Agent端主機

之后可開啟服務:systemctl start puppetagent.service

    開啟后,agent端會每隔一段時間想master詢問,是否有更改

也可以手動觸發修改

puppet kick:手動觸發修改

在agent端進行設置(默認監聽在8139端口
vim puppet.conf
[agent]
listen = true
3.8版本要加在main中

自動化運維工具Puppet


vim auth.conf
path /run
method save
auth any
allow master.magedu.com
在最后的上邊加入以上內容,只允許master觸發

自動化運維工具Puppet


master端:
        puppet kick [–host <HOST>] [–all]



測試;

自動化運維工具Puppet自動化運維工具Puppet

成功


































































原創文章,作者:Immortals、zcy,如若轉載,請注明出處:http://www.www58058.com/82861

(0)
Immortals、zcyImmortals、zcy
上一篇 2017-07-28
下一篇 2017-07-29

相關推薦

  • linux文件系統分類

    1、linux文件系統分配策略: 塊分配(blockallocation)和擴展分配(extentallocation): 塊分配:磁盤上的文件塊根據需要分配給文件,避免了存儲空間的浪費。但當文件擴充時,會造成文件中文件塊的不連續,從而導致過多的磁盤尋道時間。 每一次文件擴展時,塊分配算法就需要寫入文件塊的結構信息,也就是meta-dada。meta-dat…

    Linux干貨 2017-05-02
  • Linux文件查找

    什么是文件查找     在文件系統中查找符合條件的文件; 文件查找分為:     實時查找:遍歷所有文件進行條件匹配(find)     非實時查找:根據索引查找(locate) locate ? 查詢系統上預建的文件索引數據庫 …

    Linux干貨 2016-08-15
  • rpm程序包管理器

    linux程序包管理器: 協作用戶管理應用程序:安裝、升級、查詢、校驗、卸載等 軟件程序包生成過程:     源代碼——》目標二進制格式–》組織成為一個或有限幾個包文件     源代碼:程序員寫好的純文本文檔格式的代碼     …

    Linux干貨 2016-08-19
  • shell腳本中變量與運算及簡單編程示例

    一、變量         在Linux shell腳本的變量中,分為系統定義的變量和用戶定義的變量。這些變量是用來調用一個數值或字符值。定義變量時,不需要聲明變量類型。 1、系統變量         …

    Linux干貨 2016-08-15
  • 我的學習宣言,不忘初心

    Dear 馬哥: 我相信選擇馬哥教育是正確的。 我將用洪荒之力的努力來成為一名合格的馬幫門徒! 一定不學中國足球。 敬禮 自學生 朱宏

    Linux干貨 2016-10-30
  • bash功能特性六 bash操作環境

    一、bash查找命令的順序     1、以相對路徑或絕對路徑執行的命令;     2、由alias找到該命令來執行;     3、由bash內置的命令來執行;     4、通過$PATH這個變量的順序找到的第一個命令來執…

    Linux干貨 2015-04-22
欧美性久久久久