Jusene's Blog

ansible Playbook导演系统部署

字数统计: 1.5k阅读时长: 9 min
2017/05/28 Share

playbook

playbook是由yaml组织的一系列指导系统操作的指令集,通过playbook我们可以固化下系统操作,达到一次性部署一个小集群的目的。

编写剧本

由上图可以我们想要部署的是lnamp集群,并且我们还需要高可用使用keepalived高可用nginx。

整个过程我们不需要到每台机器上部署,我们只需要在ansible上编写ansible roles即可。

nginx+keepalived-1 主机名:proxy1 ip:10.211.55.38 centos
nginx+keepalived-2 主机名:proxy2 ip:10.211.55.39 centos
httpd+php-1 主机名:www1 ip:10.211.55.40 centos
httpd+php-2 主机名:www2 ip:10.211.55.41 centos
mysql 主机名:db ip:10.211.55.42 centos

vip:10.211.55.24

nginx roles

ansible部署机与各主机双机互信

ansible:

1
~]# yum install -y ansible
2
~]# cd /etc/ansible
3
~]# cat hosts
4
[proxy]
5
10.211.55.38
6
10.211.55.39
7
8
[www]
9
10.211.55.40
10
10.211.55.41
11
12
[db]
13
10.211.55.42
14
~]# cd roles
15
~]# mkdir nginx keepalived httpd php mysql epel
16
~]# cd nginx
17
~]# mkdir tasks
18
~]# cd tasks
19
~]# cat main.yml
20
- name: install nginx
21
  yum: name=nginx state=latest
22
- name: install config
23
  template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
24
  notify: restart nginx
25
  tag: nginxconf
26
- name: start nginx
27
  service: name=nginx state=started
28
~]# mkdir ../handlers
29
~]# cd ../handlers
30
~]# cat main.yml
31
- name: restart nginx
32
  service: name=nginx state=restarted
33
~]# mkdir ../templates
34
~]# cd ../templates
35
~]# cat nginx.conf.j2
36
user nginx;
37
worker_processes {{ ansible_processor_vcpus }};
38
error_log /var/log/nginx/error.log;
39
pid /run/nginx.pid;
40
41
42
include /usr/share/nginx/modules/*.conf;
43
44
events {
45
    worker_connections 1024;
46
}
47
48
http {
49
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
50
                      '$status $body_bytes_sent "$http_referer" '
51
                      '"$http_user_agent" "$http_x_forwarded_for"';
52
53
    access_log  /var/log/nginx/access.log  main;
54
55
    sendfile            on;
56
    tcp_nopush          on;
57
    tcp_nodelay         on;
58
    keepalive_timeout   65;
59
    types_hash_max_size 2048;
60
61
    include             /etc/nginx/mime.types;
62
    default_type        application/octet-stream;
63
upstream www_pool {
64
	server 10.211.55.40:80;
65
	server 10.211.55.41:80;
66
	ip_hash;
67
}
68
69
server {
70
	listen 80;
71
	server_name www.jusene.me;
72
	location / {
73
		proxy_pass http://www_pool;
74
		proxy_set_header Host $host
75
		proxy_set_header X_Forward_For $remote_addr;
76
		proxy_connect_timeout 60;
77
		proxy_send_timeout 60;
78
		proxy_read_timeout 60;
79
		proxy_buffer_size 4k;
80
		proxy_buffers 4 32k;
81
		proxy_busy_buffers_size 64k;
82
		proxy_temp_file_write_size 64k;
83
	}
84
85
}
86
}

keepalived role

1
~]# cd /etc/ansible/roles/keepalived
2
~]# mkdir tasks
3
~]# cd tasks
4
~]# cat main.yml
5
- name: install keepalived
6
  yum: name=keepalived state=latest
7
- name: install config
8
  template: src=keepalived.conf.j2 dest=/etc/keepalived/keepalived.conf
9
  notify: restart keepalived
10
  tag: keepalivedconf
11
- name: start keepalived
12
  service: name=keepalived state=started
13
~]# mkdir ../handlers
14
~]# cd ../handlers
15
~]# cat main.yml
16
- name: restart keepalived
17
  service: name=keepalived state=restarted
18
~]# mkdir ../templates
19
~]# cd ../templates
20
~]# cat keepalived.conf.j2
21
global_defs {
22
	notification_email {
23
		root@{{ ansible_hostname }}
24
	}
25
	notification_email_from keepalived@{{ ansible_hostname}}
26
	smtp_server 127.0.0.1
27
	smtp_connect_timeout 30
28
	router_id {{ ansible_hostname }}
29
	vrrp_mcast_group4 224.0.100.18
30
}
31
vrrp_script chk_nginx {
32
	script 'killall -0 nginx'
33
	interval 2
34
	weight -10
35
}
36
vrrp_instance VI_1 {
37
	{% if ansible_eth0[ipv4][address] == '10.211.55.38' %}
38
	state MASTER
39
	{% if ansible_eth0[ipv4][address] == '10.211.55.39' %}
40
	state BACKUP
41
	{% endif %}
42
	interface eth0
43
	virtual_router_id 100
44
	{% if ansible_eth0[ipv4][address] == '10.211.55.38' %}
45
	priority 100
46
	{% if ansible_eth0[ipv4][address] == '10.211.55.39' %}
47
	priority 98
48
	{% endif %}
49
	advert_int 1
50
	authentication {
51
		auth_type PASS
52
		auth_pass jusene
53
	}
54
	virtual_ipaddress {
55
		10.211.44.24 dev eth0 label eth0:0
56
	}
57
	track_script {
58
		chk_nginx
59
	}
60
}

httpd role

1
~]# cd /etc/ansible/roles/httpd
2
~]# mkdir tasks
3
~]# cd tasks
4
~]# cat main.yml
5
- name: install httpd
6
  yum: name=httpd state=latest
7
- name: install config
8
  template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
9
  notify: restart httpd
10
  tag: httpdconf
11
- name: start httpd
12
  service: name=httpd state=started
13
~]# mkdir ../handlers
14
~]# cd ../handlers
15
~]# cat main.yml
16
- name: restart httpd
17
  service: name=httpd state=restarted
18
~]# mkdir ../templates
19
~]# cd ../templates
20
~]# cat httpd.conf.j2
21
ServerRoot "/etc/httpd"
22
Listen 80
23
Include conf.modules.d/*.conf
24
User apache
25
Group apache
26
ServerAdmin root@localhost
27
<Directory />
28
    AllowOverride none
29
    Require all denied
30
</Directory>
31
ServerName www.jusene.me
32
DocumentRoot "/www"
33
<Directory "/www">
34
    Options None
35
    AllowOverride None
36
    Require all granted
37
</Directory>
38
<IfModule dir_module>
39
    DirectoryIndex index.html index.php
40
</IfModule>
41
<Files ".ht*">
42
    Require all denied
43
</Files>
44
ErrorLog "logs/error_log"
45
LogLevel warn
46
<IfModule log_config_module>
47
    LogFormat "%(X_Forward_For)i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
48
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
49
    <IfModule logio_module>
50
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
51
    </IfModule>
52
    CustomLog "logs/access_log" combined
53
</IfModule>
54
<IfModule mime_module>
55
    TypesConfig /etc/mime.types
56
    AddType application/x-compress .Z
57
    AddType application/x-gzip .gz .tgz
58
    AddType text/html .shtml
59
    AddOutputFilter INCLUDES .shtml
60
</IfModule>
61
AddDefaultCharset UTF-8
62
<IfModule mime_magic_module>
63
    MIMEMagicFile conf/magic
64
</IfModule>
65
EnableSendfile on

php role

1
~]# cd /etc/ansible/roles/php
2
~]# mkdir tasks
3
~]# cd tasks
4
~]# cat main.yml
5
- name: install php as mod
6
  yum: name=php state=latest
7
  notify: restart httpd

mysql role

1
~]# cd /etc/ansible/roles/mysql
2
~]# mkdir tasks
3
~]# cat main.yml
4
- name: install mysqld
5
  yum: name=mysql-server state=latest
6
- name: install config
7
  template: src=my.cnf.j2 dest=/etc/my.cnf
8
  notify: restart mysqld
9
  tag: mysqldconf
10
- name: create datadir
11
  file: path=/data/mysqldata state=directory ower=mysql group=mysql
12
- name: init datadir
13
  command: mysql_install_db --datadir=/data/mysqldata --user=mysql
14
- name: start mysqld
15
  service: name=mysqld state=started
16
~]# mkdir ../handlers
17
~]# cd ../handlers
18
~]# cat main.yml
19
- name: restart mysqld
20
  service: name=mysqld state=restarted
21
~]# mkdir ../templates
22
~]# cd ../templates
23
~]# cat my.cnf.j2
24
[client]
25
port=3306
26
socket=/tmp/mysql.sock
27
[mysqld]
28
port=3306
29
socket=/tmp/mysql.sock
30
datadir=/data/mysqldata
31
skip-extrnal-locking
32
query_cache_size=32M
33
thread_concurrency = 8
34
key_buffer = 512M
35
max_allowed_packet = 2048M
36
myisam_sort_buffer_size = 128M
37
query_cache_size= 128M
38
thread_concurrency = 32
39
wait_timeout=2592000
40
interactive_timeout=2592000
41
group_concat_max_len=4096
42
back_log=500
43
key_buffer_size=512M
44
max_heap_table_size=128M
45
thread_cache_size=128
46
sort_buffer_size=8M
47
read_buffer_size=8M
48
read_rnd_buffer_size = 8M
49
open_files_limit=200000
50
max_connections=4000
51
expire_logs_days = 3
52
event_scheduler = on
53
log-bin=mysql-bin
54
server-id       = 1
55
innodb_file_per_table =1
56
[mysqldump]
57
quick
58
max_allowed_packet = 16M
59
[mysql]
60
no-auto-rehash
61
[myisamchk]
62
key_buffer_size = 256M
63
sort_buffer_size = 256M
64
read_buffer = 2M
65
write_buffer = 2M
66
67
[mysqlhotcopy]
68
interactive-timeout

epel role

1
~]# cd /etc/ansible/roles/epel
2
~]# mkdir tasks
3
~]# cd tasks
4
~]# cat main.yml
5
- name: install epel repo
6
  template: src=epel.repo.j2 dest=/ect/epel.repo
7
~]# mkdir ../templates
8
~]# cd ../templates
9
~]# cat epel.repo.j2
10
[epel]
11
name=aliyun_epel
12
baseurl=https://mirrors.aliyun.com/epel/{{ ansible_distribution_major_version }}/x86_64/
13
enable=1
14
gpgcheck=0
15
cost=1000

lnamp.yml

1
~]# cd /etc/ansible
2
~]# cat lnamp.yml
3
---
4
- hosts: all
5
  remote_user: root
6
  forks: 5
7
  roles:
8
  - {role: epel}
9
  - {role: nginx, when group_names == 'proxy'}
10
  - {role: keepalived, when group_names == 'proxy'}
11
  - {role: httpd,when group_names == 'www'}
12
  - {role: php,when group_names == 'www'}
13
  - {role: mysql,when group_names == 'db'}
14
15
16
17
~]# ansible-playbook lnamp.yml
CATALOG
  1. 1. playbook
  2. 2. 编写剧本
  3. 3. nginx roles
  4. 4. keepalived role
  5. 5. httpd role
  6. 6. php role
  7. 7. mysql role
  8. 8. epel role
  9. 9. lnamp.yml