ansible一键部署lnvamp

lnvamp架构部署

     lnvamp的意思是Linux(centos7.2),Nginx,varnish,Apache,MySQL,PHP。我在这个平台之上构建的discuz论坛,我写了一个roles来实现,这个脚本很不健壮幂等性不足,需要大量的判断继续完善,由于时间原因我就不继续完善了,先把草稿贴出来。同时这个脚本的复用性也不好,基本上没有使用变量,后期会慢慢改善。

     架构图如下

spacer.gifwKioL1ibD5GzZ08XAABe9_n5evI680.png-wh_50

nginx提供反向代理,安装nginx

varnish提供缓存,安装varnish

server提供页面处理,安装httpd,php,php-mysql,nfs-utils

file提供数据库和网页文件,安装mysql-server,mysql-python,nfs-utils


file的ansible内容写法

注意

    因为手头没有主机了,这里file主机我使用的是centos6

     mysql-python没有对应的rpm包,由于考虑幂等性的原因这里我没有使用ansible来完成,我在宿主机上执行的,这里我们只好手动编译,手动编译脚本如下

1
2
3
4
5
6
yum  install  python-devel mysql-devel zlib-devel openssl-devel -y
yum groupinstall development tools -y
wget https: //pypi .python.org /packages/9a/81/924d6799494cf7fb24370335c2f782088d6ac4f79e4137d4df94cea3582c/MySQL-python-1 .2.3. tar .gz
tar  xf MySQL-python-1.2.3. tar .gz
cd  MySQL-python-1.2.3
python setup.py  install

vim roles/filesystem/tasks/main.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
- name: copy wordpress files
   copy: src=Discuz_X3.2_SC_UTF8.zip dest=/
- name: unzip wordpress files
   command : unzip  /Discuz_X3 .2_SC_UTF8.zip -d /
- name:  chmod  wordpress files
   command chown  -R apache:apache  /wordpress
- name: copy nfs  file
#- name: share wordpress files
#  file: dest=/wordpress mode=766 owner=root group=root state=directory
- name: copy wordpress files
   copy: src=Discuz_X3.2_SC_UTF8.zip dest=/
- name: unzip wordpress files
   command : unzip  /Discuz_X3 .2_SC_UTF8.zip -d /
- name:  chmod  wordpress files
   command chown  -R apache:apache  /wordpress
- name: copy nfs  file
   copy: src=exports dest= /etc/exports
- name:  install  nfs-utils
   yum: name=nfs-utils state=present
- name: start nfs server
   service: name=nfs state=started enabled= true
- name:  install  mysql-server
   yum: name=mysql-server state=present
- name: start mysql server
   service: name=mysqld state=started enabled= true
#- name: install mysql-python
#  yum: name=mysql-server state=present
- name: Create a new database with name  'wpdb'
   mysql_db: name=wpdb state=present
- name: Copy database sqlfile
   copy: src=grant.sql dest= /tmp
- name: Restore database
   mysql_db:
     name: wpdb
     state:  import
     target:  /tmp/grant .sql

vim roles/filesystem/files/exports

1
/wordpress  172.16.0.0 /16 (rw,no_root_squash)

vim roles/filesystem/files/grant.sql

1
grant all privileges on wpdb.* to wpuser@ '172.16.29.%' identified by  "wpprass" ;

server的ansible内容写法

vim roles/amp/tasks/main.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- name:  install  httpd
   yum: name=httpd state=present
- name:  install  php
   yum: name=php state=present
- name:  install  php-mysql
   yum: name=php-mysql state=present
- name:  install  nfs-utils
   yum: name=nfs-utils state=present
file :
     path:  /wordpress
     owner: apache
     group: apache
     state: directory
     mode: 0750
- name: copy fstab  file
   copy: src=fstab dest= /etc/fstab
- name: copy http  file
   copy: src=httpd.conf dest= /etc/httpd/conf/httpd .conf
- name:  mount  wordpress files
   command mount  -a
- name: start httpd server
   service: name=httpd state=started enabled= true

vim roles/amp/files/httpd.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
ServerRoot  "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
<Directory />
     AllowOverride none
     Require all denied
< /Directory >
DocumentRoot  "/wordpress/upload"
<Directory  "/var/www" >
     AllowOverride None
     Require all granted
< /Directory >
<Directory  "/wordpress/upload" >
     Options Indexes FollowSymLinks
     AllowOverride None
     Require all granted
< /Directory >
<IfModule dir_module>
     DirectoryIndex index.php index.html
< /IfModule >
<Files  ".ht*" >
     Require all denied
< /Files >
ErrorLog  "logs/error_log"
LogLevel warn
<IfModule log_config_module>
     LogFormat  "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\""  combined
     LogFormat  "%h %l %u %t \"%r\" %>s %b"  common
     <IfModule logio_module>
       LogFormat  "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O"  combinedio
     < /IfModule >
     CustomLog  "logs/access_log"  combined
< /IfModule >
<IfModule alias_module>
     ScriptAlias  /cgi-bin/  "/var/www/cgi-bin/"
< /IfModule >
<Directory  "/var/www/cgi-bin" >
     AllowOverride None
     Options None
     Require all granted
< /Directory >
<IfModule mime_module>
     TypesConfig  /etc/mime .types
     AddType application /x-compress  .Z
     AddType application /x-gzip  .gz .tgz
     AddType text /html  .shtml
     AddOutputFilter INCLUDES .shtml
< /IfModule >
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
     MIMEMagicFile conf /magic
< /IfModule >
EnableSendfile on
IncludeOptional conf.d/*.conf

vim roles/amp/files/fstab

1
2
3
4
UUID=098eb04e-16b2-4c66-8a02-943994f669cb /                       xfs     defaults        0 0
UUID=ddf83638-3fbf-48a6-8633-05e869a5dad1  /boot                    xfs     defaults        0 0
/dev/sda3    swap swap defaults 0 0
172.16.29.1: /wordpress  /wordpress  nfs defaults 0 0

nginx的ansible内容写法


vim roles/nginx/tasks/main.yml

1
2
3
4
5
6
- name:  install  nginx
   yum: name=nginx state=present
- name: copy nignx.conf  file
   copy: src=nginx.conf dest= /etc/nginx/nginx .conf
- name: start nginx server
   service: name=nginx state=started enabled= true

 

vim roles/nginx/files/nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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 auto;
error_log  /var/log/nginx/error .log;
pid  /run/nginx .pid;
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 ;
     # 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;
     upstream websrvs {
         server 172.16.29.10:80;
         server 172.16.29.20:80;
         ip_hash;
     }
     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 / {
             proxy_pass http: //172 .16.29.30;
             proxy_set_header Host $host;
             proxy_set_header   X-Real-IP    $remote_addr;
             proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
         }
         error_page 404  /404 .html;
             location =  /40x .html {
         }
         error_page 500 502 503 504  /50x .html;
             location =  /50x .html {
         }
     }
}


varnish的ansible内容写法


vim roles/varnish/tasks/main.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- name:  install  varnish
   yum: name=varnish state=present
- name: copy varnish.params  file
   copy: src=varnish.params dest= /etc/varnish/varnish .params
- name: copy default.vcl  file
   copy: src=default.vcl dest= /etc/varnish/default .vcl
- name: start varnish server
   service: name=varnish state=started enabled= true
vim roles /varnish/files/varnish .params
RELOAD_VCL=1
VARNISH_VCL_CONF= /etc/varnish/default .vcl
VARNISH_LISTEN_PORT=80
VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1
VARNISH_ADMIN_LISTEN_PORT=6082
VARNISH_SECRET_FILE= /etc/varnish/secret
VARNISH_STORAGE= "malloc,512m"
VARNISH_TTL=120
VARNISH_USER=varnish
VARNISH_GROUP=varnish
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
vim roles /varnish/files/default .vcl
 
vcl 4.0;
import  directors;
backend default {
     .host =  "127.0.0.1" ;
     .port =  "8080" ;
}
probe check {
     .url =  "/robots.txt" ;
     .window = 5;
     .threshold = 3;
     .interval = 2s;
     .timeout = 1s;
}
backend server1 {
     .host =  "172.16.29.10" ;
     .port =  "80" ;
     .probe = check;
}
backend server2 {
     .host =  "172.16.29.20" ;
     .port =  "80" ;
     .probe = check;
}
sub vcl_init {
     new static = directors.round_robin();
     static.add_backend(server1);
     static.add_backend(server2);
}
sub vcl_recv {
     set  req.backend_hint = static.backend();
}
sub vcl_backend_response {
}
sub vcl_deliver {
}



调用roles的ansible内容写法


file主机的ansible调用方法

1
2
3
4
5
vim  file .yml
---
- hosts:  file
   roles:
     - filesystem

server主机的ansible调用方法

1
2
3
4
5
vim amp.yml
---
- hosts: lamp
   roles:
     - amp

nginx主机的ansible调用方法

1
2
3
4
5
vim nginx.yml
---
- hosts: nginx
   roles:
     - nginx

varnish主机的ansible调用方法

1
2
3
4
5
vim varnish.yml
---
- hosts: varnish
   roles:
     - varnish



总结

     这个实验基本上没有说原理和操作步骤,只是简单罗列文件,最后我把我编写的完整roles的下载路径http://down.51cto.com/data/2286987

spacer.gif





      本文转自Ailu0li 51CTO博客,原文链接:




### 使用Ansible实现一键高可用部署 为了构建一个基于Ansible一键高可用(High Availability, HA)环境部署方案,可以采用以下方法。这种方法不仅能够简化复杂的配置过程,还能提高自动化程度并减少人为错误。 #### 高可用架构概述 高可用性通常涉及多个组件的冗余设计,例如负载均衡器、数据库集群以及应用服务器群集。通过使用Ansible Playbooks,可以定义这些组件及其依赖关系,并将其应用于目标主机。此过程中,所有操作均可以通过单一命令完成执行[^1]。 #### 准备工作 在开始之前,需确保已准备好如下事项: - **操作系统**: 所有节点应运行相同版本的操作系统(如Ubuntu 20.04 LTS),以便统一管理[^3]。 - **SSH密钥分发**: Ansible依靠SSH连接到远程机器;因此,在控制机与被控节点之间建立无密码认证至关重要。 - **Inventory文件**: 定义网络中的各个节点角色(比如webserver、dbserver等)。下面是一个简单的inventory示例: ```ini [load_balancers] lb1 ansible_host=192.168.1.10 lb2 ansible_host=192.168.1.11 [app_servers] app1 ansible_host=192.168.1.20 app2 ansible_host=192.168.1.21 [databases] db1 ansible_host=192.168.1.30 db2 ansible_host=192.168.1.31 ``` #### 创建Playbook 编写一系列playbooks用于不同阶段的任务处理。以下是几个关键部分的例子: ##### 1. Docker安装 (如果需要容器化服务)[^2]: ```yaml --- - name: Install Docker on all servers hosts: all become: yes tasks: - apt: update_cache: yes name: docker.io state: present ... ``` ##### 2. Jenkins设置 (可选,适用于CI/CD管道集成)[^2]: 利用官方提供的指南或者自定义脚本初始化jenkins实例,并启用必要的插件支持持续交付流程。 ##### 3. 主要应用程序和服务部署: 根据实际业务需求调整相应模块参数... ##### 4. 数据库复制机制设定: 对于MySQL/MariaDB之类的RDBMS来说,主从同步模式是一种常见做法... 最后一步则是测试整个系统的功能性和稳定性,确认没有任何单点故障存在之后即可投入使用。 #### 运行Ansible Playbook 一旦所有的剧本都准备就绪,则只需简单地调用`ansible-playbook`命令加上指定好的`.yml`文件名就能触发完整的HA解决方案实施过程。 ```bash ansible-playbook site.yml --ask-become-pass ``` 以上就是关于如何借助Ansible达成一键式的高可用环境搭建的大致思路介绍[^2].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值