2017-3-21 saltstack部署Nginx,反向代理到tomcat实现动静分离

 

    最近在学习salt,入手比较简单,但是深入就难了,sls文件比较难写,这回使用命令行部署,下次研究研究salt web界面,其中Nginx采用源码包部署比较复杂,故还是采用yum安装。

 

1、需求规划

192.168.1.20 : salt-master

192.168.1.23 : Nginx

192.168.1.26: Tomcat

目标:master先推送tomcat到192.168.1.26上,然后推送nginx到192.168.1.23上,最后整合Nginxt+tomcat实现动静分离,让Nginx处理静态页面,tomcat处理动态JSP请求。注:salt的Master和Minion安装非常简单,我就不多写了。

[root@salt-master ~]# salt '*' test.ping   ##先决条件,minion id是Nginx和Tomcat
Nginx:
  True
Tomcat:
  True

2、推送tomcat源码包的步骤,下列步骤均在master端

[root@node-20 opt]# cd /opt
[root@node-20 opt]# wget http://download.oracle.com/otn-pub/java/jdk/8u121-b13/e9e7ea248e2c4826b92b3f075a80e441/jdk-8u121-linux-x64.tar.gz?AuthParam=1486351013_d24154d4328f6c76a3f531ac4dcd7858
##下载JDK,百度JDK的Linux版即可
[root@node-20 opt]# wget http://mirrors.cnnic.cn/apache/tomcat/tomcat-8/v8.5.11/bin/apache-tomcat-8.5.11.tar.gz ##下载tomcat,在apachel.org找
[root@node-20 opt]# mkdir /srv/salt/jdk -p ##创建相应的推送目录
[root@node-20 opt]# mkdir /srv/salt/tomcat -p
[root@node-20 opt]# mkdir /srv/salt/tomcat/files -p
[root@node-20 opt]# mkdir /srv/salt/jdk/files -p
[root@node-20 opt]# cp apache-tomcat-8.5.11.tar.gz /srv/salt/tomcat/files/ ##把相应包放在软件里面
[root@node-20 opt]# cp /opt/jdk-8u121-linux-x64.tar.gz /srv/salt/jdk/files/
[root@node-20 salt]# cd /srv/salt/jdk/ ##进入该目录编写sls文件
[root@node-20 jdk]# cat install.sls ##编写jdk的安装sls

jdk-install:
  file.managed:
    - name: /usr/local/src/jdk-8u121-linux-x64.tar.gz
    - source: salt://jdk/files/jdk-8u121-linux-x64.tar.gz
    - user: root
    - group: root
    - mode: 755
  cmd.run:
    - name: cd /usr/local/src && tar zxf jdk-8u121-linux-x64.tar.gz && mv jdk1.8.0_121 /usr/local/jdk && chown -R root:root /usr/local/jdk
    - unless: test -d /usr/local/jdk
    - require:
      - file: jdk-install

jdk-config:
  file.append:
    - name: /etc/profile
    - text:
      - export JAVA_HOME=/usr/local/jdk
      - export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
      - export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar

  

[root@node-20 tomcat]# cd /srv/salt/tomcat/ ##编写tomcat的sls
[root@node-20 tomcat]# cat install.sls

include:
  - jdk.install

tomcat-install:
  file.managed:
    - name: /usr/local/src/apache-tomcat-8.5.11.tar.gz
    - source: salt://tomcat/files/apache-tomcat-8.5.11.tar.gz
    - user: root
    - group: root
    - mode: 755
  cmd.run:
    - name: cd /usr/local/src && tar zxf apache-tomcat-8.5.11.tar.gz && mv apache-tomcat-8.5.11 /usr/local/tomcat && chown -R root:root /usr/local/tomcat
    - unless: test -d /usr/local/tomcat
    - require:
      - file: tomcat-install

tomcat-config:
  file.append:
    - name: /etc/profile
    - text:
      - export TOMCAT_HOME=/usr/local/tomcat

  

[root@node-20 tomcat]# cd /srv/salt/
[root@node-20 salt]# cat top.sls ##编写topfile文件

base:
  '*':
    - tomcat.install

[root@node-20 salt]# salt 'Tomcat' state.highstate

报错:

++++++++++++++++++++++++

----------
ID: install
Function: cmd.run
Name: cd /usr/local/src && tar zxf jdk-8u121-linux-x64.tar.gz && mv jdk1.8.0_121 /usr/local/jdk && chown -R root:root /usr/local/jdk
Result: False
Comment: The following requisites were not found:
require:
file: jdk.install
Changes:

解决:很奇怪不知道为什么非要手工解决?

[root@salt-master salt]# salt 'Tomcat' cmd.run "cd /usr/local/src && tar zxf jdk-8u121-linux-x64.tar.gz && mv jdk1.8.0_121 /usr/local/jdk && chown -R root:root /usr/local/jdk"
Tomcat:

----------------------------------------------------------

2.1 登录到tomcat :

[root@tomcat ~]# source /etc/profile
[root@tomcat ~]# java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

[root@tomcat ~]# /usr/local/tomcat/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.

[root@tomcat ~]# systemctl stop firewalld  ##单独访问8080端口

2017-3-21 saltstack部署Nginx,反向代理到tomcat实现动静分离_第1张图片

 

 

 3、部署Nginx到192.168.1.23机器

[root@salt-master salt]# pwd
/srv/salt

[root@salt-master salt]# cat tomcat.conf

server {
    listen 80 ;
    server_name _ ;
    location / {
        root /usr/share/nginx/html ;
        index index.html index.htm ;
    }
    
    location ~ \.jsp$ {
        proxy_pass http://192.168.1.26:8080 ;
    }
}

[root@salt-master salt]# cat nginx_install.sls

nginx-install:
  pkg.installed:
    - name:  nginx

command:
  cmd.run:
    - names:
      - /usr/sbin/chkconfig --add nginx
      - /usr/sbin/chkconfig  nginx on
    - unless: /usr/sbin/chkconfig --list nginx

nginx_service:
  file.managed:
    - name: /etc/nginx/conf.d/tomcat.conf
    - user: nginx
    - mode: 644
    - source: salt://tomcat.conf
    - template: jinja
  service.running:
    - name: nginx
    - enable: True
    - reload: True

  

[root@salt-master salt]# salt 'Nginx' state.sls nginx_install

4、推送jsp测试文件和index.html

[root@salt-master salt]# mkdir tuiSong && cd tuiSong

[root@salt-master tuiSong]# cat test.jsp 

<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
Cluster App Test

Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"
");%> <% out.println("
ID " + session.getId()+"
"); String dataName = request.getParameter("dataName"); if (dataName != null && dataName.length() > 0) { String dataValue = request.getParameter("dataValue"); session.setAttribute(dataName, dataValue); } out.print("Session list"); Enumeration e = session.getAttributeNames(); while (e.hasMoreElements()) { String name = (String)e.nextElement(); String value = session.getAttribute(name).toString(); out.println( name + " = " + value+"
"); System.out.println( name + " = " + value); } %> %
%name:type=text size=20 name="dataName"> %
%key:type=text size=20 name="dataValue"> %
%type=submit> %
% %

[root@salt-master tuiSong]# cat index.html

This is Nginx server,which located in 192.168.1.23

[root@salt-master ~]# salt 'Nginx' cp.get_file salt://tuiSong/index.html /usr/share/nginx/html/index.html
Nginx:
    /usr/share/nginx/html/index.html

[root@salt-master ~]# salt 'Tomcat' cp.get_file salt://tuiSong/test.jsp /usr/local/tomcat/webapps/ROOT/test.jsp
Tomcat:
    /usr/local/tomcat/webapps/ROOT/test.jsp

 

2017-3-21 saltstack部署Nginx,反向代理到tomcat实现动静分离_第2张图片

报错:++++++++++++++++++++++++++++

 接着访问192.168.1.23/test.jsp居然没有看到理想的JSP页面?返回404

 解决:(1)单独访问192.168.1.26/test.jsp正常 (2)把Nginx.conf的server默认配置字段注释掉,怀疑和虚拟主机配置文件有冲突

2017-3-21 saltstack部署Nginx,反向代理到tomcat实现动静分离_第3张图片

 

总结:整个实验还是比较麻烦的,SLS文件比较难以搞定,nginx的配置忘了差不多了,但是有一个问题,当访问192.168.1.23/index.jsp和192.168.1.26:8080效果差了很多,应该就是Nginx没有去读取后端tomcat的css文件,这个问题,就是location的问题了。

location ~ \.jsp$|\.css$|\.gif$ {
  proxy_pass http://192.168.1.26:8080 ;
}

 

转载于:https://www.cnblogs.com/yue-hong/p/6597102.html

你可能感兴趣的:(2017-3-21 saltstack部署Nginx,反向代理到tomcat实现动静分离)