ansible自动化部署高可用Web服务

365 阅读3分钟

综合项目:自动化部署高可用Web服务

具体要求如下:

创建Role,通过Role完成项目

部署Nginx调度器

部署2台LNP服务器

部署MariaDB数据库

方案: 综合项目所需主机清单如表-1所示。

表-1

步骤一:部署两台后端LNP服务器(没有mariadb)

1)创建role角色

[root@control ansible]# ansible-galaxy init ~/ansible/roles/lnmp

2)准备2台LNP动态网站的素材

拷贝Nginx源码包,编写一个源码编译安装nginx的shell脚本。

[root@control ansible]# cp lnmp_soft/nginx-1.17.6.tar.gz \

~/ansible/roles/lnmp/files/

[root@control ansible]# vim ~/ansible/roles/lnmp/files/nginx_install.sh

#!/bin/bash

yum -y install gcc pcre-devel openssl-devel make

cd /tmp

#之所以cd到/tmp目录,是因为等会会包nginx源码包拷贝到这个目录,所以需要到这里找源码

tar -xf /tmp/nginx-1.17.6.tar.gz

cd nginx-1.17.6

./configure --with-http_ssl_module

make

make install

新建一个Nginx配置文件模板。

[root@control ansible]# vim ~/ansible/roles/lnmp/files/nginx.conf

#user nobody;

worker_processes 2;

#error_log logs/error.log;

events {

worker_connections  65535;

}

http {

include       mime.types;
default_type  application/octet-stream;
#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';
sendfile        on;
tcp_nopush     on;
keepalive_timeout  65;
#gzip  on;
server {
    listen       80;
    server_name  localhost;
    location / {
        root   html;
        index  index.html index.htm;
    }
    error_page  404              /404.html;
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        include        fastcgi.conf;
    }
}

}

准备测试首页文件模板。

[root@control ansible]# vim ~/ansible/roles/lnmp/templates/index.html

Welcom to {{ansible_hostname}}.

2)修改role配置文件。

[root@control ansible]# vim roles/lnmp/tasks/main.yml


  • name: copy nginx-1.17.6.tar.gz to webserver.

    copy:

    src: nginx-1.17.6.tar.gz

    dest: /tmp/

#拷贝源码包软件

  • name: install nginx through shell script.

    script: nginx_install.sh

    args:

    creates: /usr/local/nginx/sbin/nginx #执行源码编译安装脚本,如果已经安装nginx,则不再执行安装脚本.

#args是关键词,设置script模块的参数,通过creates参数做判断,creates也是关键词

#creates后面跟文件名或目录,如果creates判断文件存在的话就不再执行script模块对应的脚本。

  • name: copy nginx.conf to destination host. copy: src: nginx.conf dest: /usr/local/nginx/conf/nginx.conf
  • name: copy index.html to destination host. template: src: index.html dest: /usr/local/nginx/html/index.html
  • name: install php yum: name: - php - php-fpm - php-mysqlnd #rhel8中使用php-mysqlnd替代了php-mysql - mariadb-devel
  • name: run all service. block:
    • service: name: php-fpm state: started
    • shell: /usr/local/nginx/sbin/nginx args: creates: /usr/local/nginx/logs/nginx.pid #nginx.pid存在,说明nginx已经启动。如果该文件存在,则不再启动nginx。

#args是关键词,设置shell模块的参数,通过creates参数做判断,creates也是关键词

#creates后面跟文件名,如果creates判断文件存在的话就不再执行shell模块对应的命令。

3)编写Playbook调用role,并执行Playbook。

[root@control ansible]# vim lnmp.yml

  • hosts: webserver roles:
    • lnmp [root@control ansible]# ansible-playbook lnmp.yml

步骤二:部署nginx代理服务器

1)创建role角色

[root@control ansible]# ansible-galaxy init ~/ansible/roles/proxy

2)准备代理服务器需要的素材

拷贝Nginx源码包,编写一个源码编译安装nginx的shell脚本。

[root@control ansible]# cp lnmp_soft/nginx-1.17.6.tar.gz \

~/ansible/roles/proxy/files/

[root@control ansible]# vim ~/ansible/roles/proxy/files/nginx_install.sh

#!/bin/bash

yum -y install gcc pcre-devel openssl-devel make

cd /tmp

tar -xf /tmp/nginx-1.17.6.tar.gz

cd nginx-1.17.6

./configure --with-http_ssl_module

make

make install

新建一个Nginx代理服务器的配置文件模板。

[root@control ansible]# vim ~/ansible/roles/proxy/files/nginx.conf

#user nobody;

worker_processes 2;

#error_log logs/error.log;

events {

worker_connections  65535;

} http {

include       mime.types;
default_type  application/octet-stream;
#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';
sendfile        on;
tcp_nopush     on;
keepalive_timeout  65;
#gzip  on;

upstream webs {

server 192.168.4.13;

server 192.168.4.14;

} server {

    listen       80;
    server_name  localhost;
    location / {
        proxy_pass http://webs;
        root   html;
        index  index.html index.htm;
    }
    error_page  404              /404.html;
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

} 3)修改role配置文件。

[root@control ansible]# vim roles/proxy/tasks/main.yml


  • name: copy nginx-1.17.6.tar.gz to proxy. copy: src: nginx-1.17.6.tar.gz dest: /tmp/ #拷贝源码包软件
  • name: install nginx through shell script. script: nginx_install.sh args: creates: /usr/local/nginx/sbin/nginx #执行源码编译安装脚本,如果已经安装nginx,则不再执行安装脚本.

#args是关键词,设置script模块的参数,通过creates参数做判断,creates也是关键词

#creates后面跟文件名,如果creates判断文件存在的话就不再执行script模块对应的命令。

  • name: copy nginx.conf to destination host. copy: src: nginx.conf dest: /usr/local/nginx/conf/nginx.conf
  • name: run nginx service. shell: /usr/local/nginx/sbin/nginx args: creates: /usr/local/nginx/logs/nginx.pid #nginx.pid存在,说明nginx已经启动。如果该文件存在,则不再启动nginx。

4)编写Playbook调用role,并执行Playbook。

[root@control ansible]# vim proxy.yml

  • hosts: proxy roles:
    • proxy [root@control ansible]# ansible-playbook proxy.yml

步骤三:部署数据库

[root@control ansible]# vim mariadb.yml

  • hosts: database tasks:
    • name: install mariadb server yum: name: - mariadb - mariadb-server - mariadb-devel
    • name: run mariadb service service: name: mariadb state: started [root@control ansible]# ansible-playbook mariadb.yml