Ansible自动化搭建wordpress博客
2017-09-25 本文已影响0人
姜饼人_9e7b
实验环境:
客户端:Windows
服务器:虚拟机运行CentOS7
LB Nginx1:172.16.80.100
LB Nginx2:172.16.80.101
Web Nginx1:172.16.80.102
Web Nginx2:172.16.80.103
Keepalived双实例双主模式,两个vip分别为172.16.80.200和172.16.80.201
准备:建议使用一个干净的操作系统,关掉selinux,清空iptables。自行搭建好yum源、安装Ansible。。
官网下载wordpress-4.8.1-zh_CN.tar包
1、配置无密钥登录
[root@lb-nginx1 ~]#ssh-keygen -t rsa -P ''
#生成rsa密钥
[root@lb-nginx1 ~]ssh-copy-id -i ~/.ssh/id_rsa root@172.16.80.100
[root@lb-nginx1 ~]ssh-copy-id -i ~/.ssh/id_rsa root@172.16.80.101
[root@lb-nginx1 ~]ssh-copy-id -i ~/.ssh/id_rsa root@172.16.80.102
[root@lb-nginx1 ~]ssh-copy-id -i ~/.ssh/id_rsa root@172.16.80.103
#把密钥拷贝到各主机
[root@lb-nginx1 ~]ssh 172.16.80.100 'ifconfig';ssh 172.16.80.101 'ifconfig';ssh 172.16.80.102 'ifconfig';ssh 172.16.80.103 'ifconfig'
#验证是否能正常访问各主机
2、根据拓扑图,规划各种roles
- keepalived
我们要实现双主双实例模式,因此就设定keepalived1和keepalived2两种角色好了 - LB
由于是高可用,两台LB配置一模一样,因此设定LB为一种角色 - varnish
同LB一样,设定varinsh一种角色 - Web服务器:
web1搭建一个wordpress,web2通过nfs共享web1的wordpress。因此划分两种角色 - php
就它一个了 - mysql
主从复制, mysql-master、mysql-slave
3、roles配置
调试了好久,直接上配置吧
有空再优化一下配置,补充上注释
[root@centos7a ~]mkdir -pv /etc/ansible/roles/{keepalived1,keepalived2,lb,mysql-master,mysql-slave,nfs,php,varnish,web1,web2}/{files,templates,tasks,handlers,vars,meta,default}
[root@centos7a ~]#cd /etc/ansible/roles/
[root@centos7a roles]#ls
keepalived1 keepalived2 lb mysql-master mysql-slave nfs php varnish web1 web2
[root@centos7c roles]#tree
.
├── keepalived1
│ ├── default
│ ├── files
│ ├── handlers
│ ├── meta
│ ├── tasks
│ ├── templates
│ └── vars
├── keepalived2
│ ├── default
│ ├── files
│ ├── handlers
│ ├── meta
│ ├── tasks
│ ├── templates
│ └── vars
├── lb
│ ├── default
│ ├── files
│ ├── handlers
│ ├── meta
│ ├── tasks
│ ├── templates
│ └── vars
├── mysql-master
│ ├── default
│ ├── files
│ ├── handlers
│ ├── meta
│ ├── tasks
│ ├── templates
│ └── vars
├── mysql-slave
│ ├── default
│ ├── files
│ ├── handlers
│ ├── meta
│ ├── tasks
│ ├── templates
│ └── vars
├── nfs
│ ├── default
│ ├── files
│ ├── handlers
│ ├── meta
│ ├── tasks
│ ├── templates
│ └── vars
├── php
│ ├── default
│ ├── files
│ ├── handlers
│ ├── meta
│ ├── tasks
│ ├── templates
│ └── vars
├── varnish
│ ├── default
│ ├── files
│ ├── handlers
│ ├── meta
│ ├── tasks
│ ├── templates
│ └── vars
├── web1
│ ├── default
│ ├── files
│ ├── handlers
│ ├── meta
│ ├── tasks
│ ├── templates
│ └── vars
└── web2
├── default
├── files
├── handlers
├── meta
├── tasks
├── templates
└── vars
LB配置:
[root@lb-nginx1 roles]#tree lb/
lb/
├── default
├── files
│ └── lb.conf
├── handlers
│ └── main.yml
├── meta
├── tasks
│ └── main.yml
├── templates
└── vars
####################################################################
[root@lb-nginx1 roles]#cat lb/tasks/main.yml
- name: install nginx
yum: name=nginx state=present
- name: install conf
copy: src=lb.conf dest=/etc/nginx/nginx.conf
tags: conf
notify: restart nginx
- name: start nginx
service: name=nginx state=started enabled=yes
####################################################################
[root@lb-nginx1 roles]#cat lb/handlers/main.yml
- name: restart nginx
service: name=nginx state=restarted
####################################################################
[root@lb-nginx1 roles]#cat lb/files/lb.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream www.server.pools{
server 172.16.80.200:6081;
server 172.16.80.201:6081;
}
server {
listen 80;
server_name www.nginx.com;
location / {
proxy_pass http://www.server.pools;
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
keepalived1配置
[root@lb-nginx1 roles]#tree keepalived1/
keepalived1/
├── default
├── files
│ └── keepalived1.conf
├── handlers
│ └── main.yml
├── meta
├── tasks
│ └── main.yml
├── templates
└── vars
####################################################################
[root@lb-nginx1 roles]#cat keepalived1/tasks/main.yml
- name: install keepalived
yum: name=keepalived state=present
- name: install conf
copy: src=keepalived1.conf dest=/etc/keepalived/keepalived.conf
tags: conf
notify: restart keepalived
- name: start keepalived
service: name=keepalived state=started enabled=yes
####################################################################
[root@lb-nginx1 roles]#cat keepalived1/files/keepalived1.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keadmin@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id CentOS7B.luo.com
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 15
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass hahahaha
}
virtual_ipaddress {
172.16.80.200
}
}
vrrp_instance VI_2 {
state MASTER
interface ens33
virtual_router_id 22
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass hehehehe
}
virtual_ipaddress {
172.16.80.201
}
}
####################################################################
[root@lb-nginx1 roles]#cat keepalived1/handlers/main.yml
- name: restart keepalived
service: name=keepalived state=restarted
varnish配置
[root@lb-nginx1 roles]#tree varnish/
varnish/
├── default
├── files
│ └── varnish.vcl
├── handlers
│ └── main.yml
├── meta
├── tasks
│ └── main.yml
├── templates
└── vars
####################################################################
[root@lb-nginx1 roles]#cat varnish/tasks/main.yml
- name: install varnish
yum: name=varnish state=present
- name: install conf
copy: src=varnish.vcl dest=/etc/varnish/default.vcl
tags: conf
notify: restart varnish
- name: start varnish
service: name=varnish state=started enabled=yes
####################################################################
[root@lb-nginx1 roles]#cat varnish/files/varnish.vcl
vcl 4.0;
import directors;
backend web1 {
.host = "172.16.80.102";
.port = "80";
}
backend web2 {
.host = "172.16.80.103";
.port = "80";
}
sub vcl_init {
new WEB = directors.round_robin();
WEB.add_backend(web1);
WEB.add_backend(web2);
}
sub vcl_recv {
set req.backend_hint = WEB.backend();
}
sub vcl_backend_response {
}
sub vcl_deliver {
}
####################################################################
[root@lb-nginx1 roles]#cat varnish/handlers/main.yml
- name: restart varnish
service: name=varnish state=restarted
web1配置
[root@lb-nginx1 roles]#tree web1/
web1/
├── default
├── files
│ ├── wordpress-4.8.1-zh_CN.tar.gz
│ └── web.conf
├── handlers
│ └── main.yml
├── meta
├── tasks
│ └── main.yml
├── templates
└── vars
[root@lb-nginx1 roles]#cat web1/tasks/main.yml
- name: add user nginx
user: name=nginx group=nginx uid=666
- name: install nginx
yum: name=nginx state=present
- name: install conf
copy: src=web.conf dest=/etc/nginx/nginx.conf
tags: conf
notify: restart nginx
- name: copy blog
unarchive: src=blog.tar.gz dest=/usr/share/nginx/html/
- name: set mode
file: name=/usr/share/nginx/html/wordpress recurse=yes owner=nginx group=nginx
- name: start nginx
service: name=nginx state=started enabled=yes
[root@lb-nginx1 roles]#cat web1/handlers/main.yml
- name: restart nginx
service: name=nginx state=restarted
[root@lb-nginx1 roles]#cat web1/files/web.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.static.com;
location / {
root html;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
Web2配置
[root@lb-nginx1 roles]#tree web2
web2
├── default
├── files
│ └── web.conf
├── handlers
│ └── main.yml
├── meta
├── tasks
│ └── main.yml
├── templates
└── vars
[root@lb-nginx1 roles]#cat web2/tasks/main.yml
- name: add user nginx
user: name=nginx group=nginx uid=666
- name: install nginx
yum: name=nginx state=present
- name: install conf
copy: src=web.conf dest=/etc/nginx/nginx.conf
tags: conf
notify: restart nginx
- name: mkdir
file: name=/usr/share/nginx/html/wordpress state=directory
- name: mount
mount: src=172.16.80.102:/usr/share/nginx/html/wordpress/ name=/usr/share/nginx/html/wordpress fstype=nfs state=mounted
- name: start nginx
service: name=nginx state=started enabled=yes
[root@lb-nginx1 roles]#cat web2/handlers/main.yml
- name: restart nginx
service: name=nginx state=restarted
[root@lb-nginx1 roles]#cat web2/files/web.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.static.com;
location / {
root html;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
[root@lb-nginx1 roles]#tree nfs/
nfs/
├── default
├── files
│ └── exports
├── handlers
├── meta
├── tasks
│ └── main.yml
├── templates
└── vars
[root@lb-nginx1 roles]#cat nfs/tasks/main.yml
- name: install nfs
yum: name=nfs-utils state=present
- name: install conf
copy: src=exports dest=/etc/exports
- name: start nfs
service: name=nfs state=started enabled=yes
[root@lb-nginx1 roles]#cat nfs/files/exports
/usr/share/nginx/html/wordpress 172.16.80.103(rw,all_squash,anonuid=666)
php配置:
[root@lb-nginx1 roles]#tree php/
php/
├── default
├── files
├── handlers
├── meta
├── tasks
│ └── main.yml
├── templates
└── vars
[root@lb-nginx1 roles]#cat php/tasks/main.yml
- name: install php
yum: name={{ item }} state=present
with_items:
- php-mysql
- php-fpm
- name: start php-fpm
service: name=php-fpm state=started enabled=yes
mysql-master
[root@lb-nginx1 roles]#tree mysql-master/
mysql-master/
├── default
├── files
│ └── my.cnf
├── handlers
├── meta
├── tasks
│ └── main.yml
├── templates
└── vars
[root@lb-nginx1 roles]#cat mysql-master/tasks/main.yml
- name: install mariadb
yum: name={{ item }} state=present
with_items:
- mariadb
- mariadb-server
- name: install conf
copy: src=my.cnf dest=/etc/my.cnf
- name: start mariadb
service: name=mariadb state=started enabled=yes
- name: mysql
command: mysql -e "create database wordpress;grant all on wordpress.* to wordpress@'172.16.80.%' identified by '123456';"
- name: command
command: mysql -e "grant replication slave,replication client on *.* to 'backuper'@'172.16.%.%' identified by 'backuper';"
[root@lb-nginx1 roles]#cat mysql-master/files/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log_bin=master_bin
server_id=1
innodb_file_per_table=ON
skip_name-resolve=ON
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
mysql-slave
[root@lb-nginx1 roles]#tree mysql-slave/
mysql-slave/
├── default
├── files
│ └── my.cnf
├── handlers
├── meta
├── tasks
│ └── main.yml
├── templates
└── vars
[root@lb-nginx1 roles]#cat mysql-slave/tasks/main.yml
- name: install mariadb
yum: name={{ item }} state=present
with_items:
- mariadb
- mariadb-server
- name: install conf
copy: src=my.cnf dest=/etc/my.cnf
- name: start mariadb
service: name=mariadb state=started enabled=yes
- name: command
command: mysql -e "change master to master_host='172.16.80.102',master_user='backuper',master_password='backuper',master_log_file='master_bin.000001',master_log_pos=30364;"
- name: command
command: mysql -e "start slave;"
[root@lb-nginx1 roles]#cat mysql-slave/files/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
relay-log=relay-log
server-id=22
innodb_file_per_table=ON
skip_name_resolve=ON
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d
4、调用role
[root@lb-nginx1 ~]#vim /etc/ansible/hosts
[proxy]
172.16.80.100
172.16.80.101
[web]
172.16.80.102
172.16.80.103
#在/etc/ansible/hosts加上上面几行
[root@centos7a ~]#cat role.yml
- hosts: proxy
remote_user: root
roles:
- lb
- varnish
- hosts: 172.16.80.100
remote_user: root
roles:
- keepalived1
- hosts: 172.16.80.101
remote_user: root
roles:
- keepalived2
- hosts: 172.16.80.102
remote_user: root
roles:
- php
- web1
- nfs
- hosts: 172.16.80.103
remote_user: root
roles:
- php
- nfs
- web2
- hosts: 172.16.80.102
remote_user: root
roles:
- mysql-master
- hosts: 172.16.80.103
remote_user: root
roles:
- mysql-slave
测试
[root@lb-nginx1 ~]#ansible-playbook -C role.yml
如果没有问题,部署
[root@lb-nginx1 ~]#ansible-playbookrole.yml
安装好是这样的: