Ansible部署nginx+tomcat+jenkins

Ansible+tomcat+jenkins

准备环境

  • 准备四台干净的机器,关闭防火墙和SELinux

  • 分别给四台机器修改主机名并做解析

  • 192.168.226.128 ansible
    192.168.226.129 nginx-proxy
    192.168.226.130 tomcat-jenkins1
    192.168.226.131 tomcat-jenkins2

  • 在控制节点上下载ansible工具

  • 在ansible的主机清单文件中配置主机组
    [host1]
    nginx-proxy
    [host2]
    tomcat-jenkins1
    tomcat-jenkins2

  • 各主机与控制节点秘钥传输

  • 将tomcat,jenkins包上传到控制节点


→→→安装部署ansible及主机清单文件配置←←←


准备工作,在ansible主机上

[xiaobai@ansible] tar xzf jdk-7u67-linux-x64.tar.gz -C /usr/local
[xiaobai@ansible] mv /usr/local/jdk1.7.0_67/ /usr/local/java
[xiaobai@ansible] tar xzf apache-tomcat-7.0.85.tar.gz -C /usr/local
[xiaobai@ansible] mv /usr/local/apache-tomcat-7.0.85/ /usr/local/tomcat

#编辑tomcat的启动脚本
[xiaobai@ansible] vim /usr/local/tomcat/bin/startup.sh
source /etc/profile   #加在第二行

[xiaobai@ansible] yum -y install epel-release;yum -y install nginx
[xiaobai@ansible] vim /etc/nginx/nginx.conf
keepalive_timeout   0;   #搜索长连接改为 

Ansible部署nginx+tomcat+jenkins_第1张图片

#编辑nginx代理主机的配置文件
[xiaobai@ansible] vim /etc/nginx/conf.d/proxy.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

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   0;
    types_hash_max_size 2048;

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

upstream xiaobai {
    server 192.168.226.130:80;
    server 192.168.226.131:80;
   }

server {
      listen 80;
      server_name  localhost;
      location  /  {
      proxy_pass http://xiaobai;
      proxy_set_header Host $http_host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

      proxy_connect_timeout 30;
      proxy_send_timeout 60;
      proxy_read_timeout 60;
      }
   }
}


Ansible部署nginx+tomcat+jenkins_第2张图片

roles编写

#ansible主机上操作
[xiaobai@ansible] cd /etc/ansible/roles/
[xiaobai@ansible roles] mkdir -p tomcat/{handlers,tasks,vars}
[xiaobai@ansible roles] touch tomcat/{vars,handlers,tasks}/main.yml site.yml
[xiaobai@ansible roles] mkdir -p nginx/{handlers,tasks,vars}
[xiaobai@ansible roles] touch nginx/{vars,handlers,tasks}/main.yml
[xiaobai@ansible roles] tree .
.
├── nginx
│   ├── handlers
│   │   └── main.yml
│   ├── tasks
│   │   └── main.yml
│   └── vars
│       └── main.yml
├── site.yml
└── tomcat
    ├── handlers
    │   └── main.yml
    ├── tasks
    │   └── main.yml
    └── vars
        └── main.yml

#nginx代理的剧本
[xiaobai@ansible roles] vim nginx/tasks/main.yml
---
 - name: install epel
   yum: name=epel-release state=latest
 - name: install nginx
   yum: name=nginx state=latest
 - name: copy nginx
   copy: src={{ proxy_conf }} dest={{ nginx_proxy }}
   notify: start nginx

[xiaobai@ansible roles] vim nginx/handlers/main.yml
---
 - name: start nginx
   service: name=nginx state=started

[xiaobai@ansible roles] vim nginx/vars/main.yml
proxy_conf: /etc/nginx/conf.d/proxy.conf
nginx_proxy: /etc/nginx/nginx.conf

#tomcat服务器+jenkins
[xiaobai@ansible roles] vim tomcat/tasks/main.yml
---
 - name: configure jdk
   copy: src={{ src_jdk }} dest={{ dest_jdk }}
 - name: unzip jdk
   shell: tar xzf {{ dest_jdk }} -C /usr/local
 - name: rename jdk
   shell: mv /usr/local/jdk1.7.0_67/ /usr/local/java
 - name: jdk vars1
   shell: echo "JAVA_HOME=/usr/local/java" >> /etc/profile
 - name: jdk vars2
   shell: echo 'PATH=$JAVA_HOME/bin:$PATH' >> /etc/profile
 - name: copy tomcat
   copy: src={{ src_tomcat }} dest={{ dest_tomcat }}
 - name: unzip tomcat
   shell: tar xzf {{ dest_tomcat }} -C /usr/local
 - name: rename tomcat
   shell: mv /usr/local/apache-tomcat-7.0.85/ /usr/local/tomcat
 - name: copy tomcat start file
   copy: src=/usr/local/tomcat/bin/startup.sh dest=/usr/local/tomcat/bin/startup.sh
 - name: copy jenkins
   copy: src={{ src_jenkins }} dest={{ dest_jenkins }}
   notify: start tomcat
#unzip模块无法在ansible内使用,所以这里就不用unzip了

[xiaobai@ansible roles] vim tomcat/handlers/main.yml
---
 - name: start tomcat
   shell: nohup /usr/local/tomcat/bin/startup.sh &

[xiaobai@ansible roles] vim tomcat/vars/main.yml
src_jdk: ~/jdk-7u67-linux-x64.tar.gz
dest_jdk: ~/jdk-7u67-linux-x64.tar.gz
src_tomcat: ~/apache-tomcat-7.0.85.tar.gz
dest_tomcat: ~/apache-tomcat-7.0.85.tar.gz
src_jenkins: ~/jenkins.war
dest_jenkins: /usr/local/tomcat/webapps/

#配置剧本site.yml文件
[xiaobai@ansible roles] vim site.yml
---
 - hosts: host1
   user: root
   roles:
   - nginx
 - hosts: host2
   user: root
   roles:
   - tomcat

[xiaobai@ansible roles] ansible-playbook --syntax-check site.yml 
playbook: site.yml
[xiaobai@ansible roles] ansible-playbook site.yml
nginx-proxy                : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
tomcat-jenkins1            : ok=12   changed=11   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
tomcat-jenkins2            : ok=12   changed=11   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

#到这里nginx代理负载均衡+tomcat+jenkins就部署完成了,可以通过访问ip来测试

文章总览分类目录 @小白

你可能感兴趣的:(Ansible,nginx,tomcat,负载均衡,jenkins)