ansible一键部署lnvamp

  1. 云栖社区>
  2. 博客>
  3. 正文

ansible一键部署lnvamp

技术小胖子 2017-11-03 15:45:00 浏览797
展开阅读全文

     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
  commandchown -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
  commandchown -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
  commandmount -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









网友评论

登录后评论
0/500
评论
技术小胖子
+ 关注