實驗環(huán)境:
客戶端:Windows
服務(wù)器:虛擬機運行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
準備:建議使用一個干凈的操作系統(tǒng),關(guān)掉selinux昙楚,清空iptables轮洋。自行搭建好yum源衰猛、安裝Ansible福稳。儒恋。
官網(wǎng)下載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黄刚、根據(jù)拓撲圖捎谨,規(guī)劃各種roles
- keepalived
我們要實現(xiàn)雙主雙實例模式民效,因此就設(shè)定keepalived1和keepalived2兩種角色好了 - LB
由于是高可用憔维,兩臺LB配置一模一樣,因此設(shè)定LB為一種角色 - varnish
同LB一樣畏邢,設(shè)定varinsh一種角色 - Web服務(wù)器:
web1搭建一個wordpress业扒,web2通過nfs共享web1的wordpress。因此劃分兩種角色 - php
就它一個了 - mysql
主從復制舒萎, mysql-master程储、mysql-slave
3、roles配置
調(diào)試了好久臂寝,直接上配置吧
有空再優(yōu)化一下配置章鲤,補充上注釋
[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、調(diào)用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
安裝好是這樣的: