A-A+

使用salt集中安装/配置/管理Tomcat

2014年09月07日 Other Tech 暂无评论 阅读 3,905 次

首先我们有2台机器需要部署tomcat如下:

[root@mysqlhost ~]# salt-key -L
Accepted Keys:
192.168.137.154
192.168.137.155
Unaccepted Keys:
Rejected Keys:

查看2台机器OS版本

[root@mysqlhost ~]# salt '*' cmd.run "uname -a;cat /etc/redhat-release"
192.168.137.155:
    Linux mysqlhost 2.6.32-71.el6.x86_64 #1 SMP Wed Sep 1 01:33:01 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux
    Red Hat Enterprise Linux Server release 6.0 (Santiago)
192.168.137.154:
    Linux rhl6.0x64 2.6.32-71.el6.x86_64 #1 SMP Wed Sep 1 01:33:01 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux
    Red Hat Enterprise Linux Server release 6.0 (Santiago)

查看2台机器磁盘信息

[root@mysqlhost ~]# salt '*' cmd.run "df -h"
192.168.137.154:
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/sda3              36G   15G   20G  43% /
    tmpfs                 1.9G  204K  1.9G   1% /dev/shm
    /dev/sda1             194M   30M  155M  16% /boot
192.168.137.155:
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/sda3              36G  7.0G   27G  21% /
    tmpfs                 1.9G     0  1.9G   0% /dev/shm
    /dev/sda1             194M   30M  155M  16% /boot	

我们在根目录下建一个tomcat目录

[root@mysqlhost ~]# salt '*' cmd.run "mkdir -p /tomcat"
192.168.137.155:
    
192.168.137.154:

将安装介质放到指定目录并解压

[root@mysqlhost ~]# salt-cp '*' /tmp/apache-tomcat-8.0.12.tar.gz /tomcat/apache-tomcat-8.0.12.tar.gz
{}
注意:在传输大文件时会超时报错
[root@mysqlhost ~]# salt '*' cmd.run "cd /tomcat;tar -zxvf apache-tomcat-8.0.12.tar.gz" 
[root@mysqlhost ~]# salt '*' cmd.run "ls -ltr /tomcat"
192.168.137.155:
    total 9004
    -rw-r--r--. 1 root root 9212991 Sep  4 04:36 apache-tomcat-8.0.12.tar.gz
    drwxr-xr-x. 9 root root    4096 Sep  4 04:37 apache-tomcat-8.0.12
192.168.137.154:
    total 9004
    -rw-r--r--. 1 root root 9212991 Aug 14 16:53 apache-tomcat-8.0.12.tar.gz
    drwxr-xr-x. 9 root root    4096 Aug 14 16:55 apache-tomcat-8.0.12

配置环境变量

[root@mysqlhost ~]# salt '*' cmd.run "echo JAVA_HOME=/usr/java/jdk1.7.0_25                 >> /etc/profile"
[root@mysqlhost ~]# salt '*' cmd.run "echo CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib     >> /etc/profile"
[root@mysqlhost ~]# salt '*' cmd.run "echo PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin    >> /etc/profile"
[root@mysqlhost ~]# salt '*' cmd.run "echo export PATH CLASSPATH JAVA_HOME	               >> /etc/profile"
[root@mysqlhost ~]# salt '*' cmd.run "source /etc/profile;tail -4 /etc/profile"
192.168.137.154:
    JAVA_HOME=/usr/java/jdk1.7.0_25
    CLASSPATH=/lib:/jre/lib
    PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/u01/mysql/bin:/root/bin:/bin:/jre/bin
    export PATH CLASSPATH JAVA_HOME
192.168.137.155:
    JAVA_HOME=/usr/java/jdk1.7.0_25
    CLASSPATH=/lib:/jre/lib
    PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/u01/mysql/bin:/root/bin:/bin:/jre/bin
    export PATH CLASSPATH JAVA_HOME

将tomcat添加到系统服务中

salt '*' cmd.run "cp /tomcat/apache-tomcat-8.0.12/bin/catalina.sh /etc/init.d/Tomcat"
salt '*' cmd.run "sed -i '2i\#chkconfig: 2345 10 90' /etc/init.d/Tomcat"
salt '*' cmd.run "sed -i '3i\#description:Tomcat service' /etc/init.d/Tomcat"	
salt '*' cmd.run "sed -i '4i\JAVA_HOME=/usr/java/jdk1.7.0_25' /etc/init.d/Tomcat"	
salt '*' cmd.run "sed -i '5i\CATALINA_HOME=/tomcat/apache-tomcat-8.0.12' /etc/init.d/Tomcat"	
salt '*' cmd.run "chkconfig --add Tomcat"	
salt '*' cmd.run "service Tomcat stop"
salt '*' cmd.run "service Tomcat start"

获取当前运行tomcat的版本信息

[root@mysqlhost salt]# salt '*' cmd.run "service Tomcat version"
192.168.137.155:
    Server version: Apache Tomcat/8.0.12
    Server built:   Aug 29 2014 03:46:23
    Server number:  8.0.12.0
    OS Name:        Linux
    OS Version:     2.6.32-71.el6.x86_64
    Architecture:   amd64
    JVM Version:    1.7.0_25-b15
    JVM Vendor:     Oracle Corporation
192.168.137.154:
    Server version: Apache Tomcat/8.0.12
    Server built:   Aug 29 2014 03:46:23
    Server number:  8.0.12.0
    OS Name:        Linux
    OS Version:     2.6.32-71.el6.x86_64
    Architecture:   amd64
    JVM Version:    1.7.0_25-b15
    JVM Vendor:     Oracle Corporation	

配置tomcat集中管理

[root@mysqlhost salt]# pwd
/srv/salt
[root@mysqlhost salt]# ls -l
total 12
drwxr-xr-x. 2 root root 4096 Sep  4 06:33 apache
drwxr-xr-x. 2 root root 4096 Sep  4 09:35 tomcat
-rw-r--r--. 1 root root   33 Sep  4 09:17 top.sls
[root@mysqlhost salt]# cat top.sls 
base:
  '*':
    - tomcat.tomcat
[root@mysqlhost tomcat]# pwd
/srv/salt/tomcat
[root@mysqlhost tomcat]# ls -l
total 12
-rw-------. 1 root root 6373 Sep  4 08:51 server.xml
-rw-r--r--. 1 root root  272 Sep  4 09:30 tomcat.sls
[root@mysqlhost tomcat]# cat tomcat.sls 
tomcat:
  service:
    - name: Tomcat
    - running
    - reload: True
    - watch:
      - file: /tomcat/apache-tomcat-8.0.12/conf/server.xml

/tomcat/apache-tomcat-8.0.12/conf/server.xml:
  file:
    - managed
    - source: salt://tomcat/server.xml
    - backup: minion

同步配置

[root@mysqlhost salt]# salt -E '192.168.137.15*' state.highstate 
192.168.137.154:
----------
          ID: /tomcat/apache-tomcat-8.0.12/conf/server.xml
    Function: file.managed
      Result: True
     Comment: File /tomcat/apache-tomcat-8.0.12/conf/server.xml is in the correct state
     Changes:   
----------
          ID: tomcat
    Function: service.running
        Name: Tomcat
      Result: True
     Comment: Started Service Tomcat
     Changes:   
              ----------
              Tomcat:
                  True

Summary
------------
Succeeded: 2
Failed:    0
------------
Total:     2
192.168.137.155:
----------
          ID: /tomcat/apache-tomcat-8.0.12/conf/server.xml
    Function: file.managed
      Result: True
     Comment: File /tomcat/apache-tomcat-8.0.12/conf/server.xml is in the correct state
     Changes:   
----------
          ID: tomcat
    Function: service.running
        Name: Tomcat
      Result: True
     Comment: Started Service Tomcat
     Changes:   
              ----------
              Tomcat:
                  True

Summary
------------
Succeeded: 2
Failed:    0
------------
Total:     2
[root@mysqlhost salt]# salt '*' cmd.run "service Tomcat stop"
192.168.137.155:
    
192.168.137.154:
    
[root@mysqlhost salt]# salt '*' cmd.run "service Tomcat start"
192.168.137.155:
    Tomcat started.
192.168.137.154:
    Tomcat started.

测试集中管理
目前154的机器的tomcat运行在默认的8080端口

[root@mysqlhost tomcat]# telnet 192.168.137.154 8084
Trying 192.168.137.154...
telnet: connect to address 192.168.137.154: Connection refused
[root@mysqlhost tomcat]# telnet 192.168.137.154 8080
Trying 192.168.137.154...
Connected to 192.168.137.154.
Escape character is '^]'.
^]
telnet> q
Connection closed.

我们在集中管理端将tomcat配置文件server.xml的默认端口从8080改为8084

<Connector port="8084" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

同步我们的配置

[root@mysqlhost tomcat]# salt '192.168.137.154' state.highstate
192.168.137.154:
----------
          ID: /tomcat/apache-tomcat-8.0.12/conf/server.xml
    Function: file.managed
      Result: True
     Comment: File /tomcat/apache-tomcat-8.0.12/conf/server.xml updated
     Changes:   
              ----------
              diff:
                  ---  
                  +++  
                  @@ -65,7 +65,7 @@
                            APR (HTTP/AJP) Connector: /docs/apr.html
                            Define a non-SSL HTTP/1.1 Connector on port 8080
                       -->
                  -    <Connector port="8080" protocol="HTTP/1.1"
                  +    <Connector port="8084" protocol="HTTP/1.1"
                                  connectionTimeout="20000"
                                  redirectPort="8443" />
                       <!-- A "Connector" using the shared thread pool-->
                  
----------
          ID: tomcat
    Function: service.running
        Name: Tomcat
      Result: True
     Comment: Started Service Tomcat
     Changes:   
              ----------
              Tomcat:
                  True

Summary
------------
Succeeded: 2
Failed:    0
------------
Total:     2

重启tomcat并测试端口

[root@mysqlhost tomcat]# salt '192.168.137.154' cmd.run "service Tomcat stop"
192.168.137.154:
    
[root@mysqlhost tomcat]# salt '192.168.137.154' cmd.run "service Tomcat start"
192.168.137.154:
    Tomcat started.			   

[root@mysqlhost tomcat]# telnet 192.168.137.154 8080
Trying 192.168.137.154...
telnet: connect to address 192.168.137.154: Connection refused
[root@mysqlhost tomcat]# telnet 192.168.137.154 8084
Trying 192.168.137.154...
Connected to 192.168.137.154.
Escape character is '^]'.
^]
telnet> q
Connection closed.	

在154机器上可以找到修改前的配置文件

[root@rhl6 conf]# pwd
/var/cache/salt/minion/file_backup/tomcat/apache-tomcat-8.0.12/conf
[root@rhl6 conf]# ls -ltr
total 32
-rw-r--r--. 1 root root 6373 Aug 14 21:47 server.xml_Thu_Aug_14_21:47:58_116759_2014
-rw-r--r--. 1 root root 6373 Aug 14 22:18 server.xml_Thu_Aug_14_22:18:40_820031_2014
-rw-r--r--. 1 root root 6373 Aug 14 22:24 server.xml_Thu_Aug_14_22:24:01_400928_2014
-rw-r--r--. 1 root root 6373 Aug 14 22:27 server.xml_Thu_Aug_14_22:27:43_710707_2014
标签:

给我留言

Copyright © YallonKing 保留所有权利.   Theme  Ality

用户登录

分享到: