当前位置:   article > 正文

【运维知识进阶篇】用Ansible Roles重构LNMP架构(Linux+Nginx+Mariadb+PHP),实现4个项目一键部署_linux中ansible怎么部署lnmp架构环境

linux中ansible怎么部署lnmp架构环境

我们先前用playbook构造过lnmp架构,实现了一键部署四个项目的效果,但是我们是将所有的命令都写入了一个playbook中,我们所需的文件也只是简单的放入了playbook的同级目录,这样很混乱,而roles可以很好解决这一点,使用roles,我们可以很轻松的整理我们的配置文件,更有利于我们写好后排错,或者更改配置,我们再将变量,判断语句,循环语句加上,打造我们用Ansible部署lnmp架构的最终版本!

准备工作

主机名称主机IP(外网、内网)作用
LB0110.0.0.5、172.16.1.5七层负载均衡、keepalived高可用,https证书
LB0210.0.0.6、172.16.1.6七层负载均衡、keepalived高可用,https证书
Web0110.0.0.7、172.16.1.7Nginx、php服务、存放代码文件
Web0210.0.0.8、172.16.1.8Nginx、php服务、存放代码文件
NFS10.0.0.31、172.16.1.31存放静态资源
Backup10.0.0.41、172.16.1.41存放静态数据的备份、实时同步NFS的代码内容
MySQL10.0.0.51、172.16.1.51存放动态数据
Ansible10.0.0.61、172.16.1.61使用Ansible作为控制机

重构思路

用roles和不用roles的逻辑其实是一样的,要根据服务器的功能,先收集服务器所需要的文件,再进行安装,传输文件,启动服务或重启服务等操作。只是我们这次不必担心命名问题,因为不同的服务或不同功能的服务器所需要的配置文件会被放到不同的目录,不会冲突。

roles这个角色,可以根据同类服务器的功能定义,也可以通过服务去定义,因为我们是一键部署所有服务和项目,也不存在指定部署服务的需求,如果通过服务来定义,也容易出现需要很多when判断的情况,如果用同类功能的服务器定义角色,可能会出现同一条命令需要反复编写的情况,自行选择,我采取根据同类功能服务器去定义我们的roles角色。

管理机操作

1、添加目标客户机至主机列表

  1. [root@Ansible roles]# cat hosts
  2. [lb_group]
  3. lb01 ansible_ssh_host=172.16.1.5
  4. lb02 ansible_ssh_host=172.16.1.6
  5. [web_group]
  6. web01 ansible_ssh_host=172.16.1.7
  7. web02 ansible_ssh_host=172.16.1.8
  8. [nfs]
  9. 172.16.1.31
  10. [backup]
  11. 172.16.1.41
  12. [mysql]
  13. 172.16.1.51

2、将角色与主机对应

  1. [root@Ansible roles]# cat site.yml
  2. - hosts: all
  3. roles:
  4. - role: basic
  5. - role: lb_group
  6. when: ansible_hostname is match "LB*"
  7. - role: nfs
  8. when: ansible_hostname is match "NFS"
  9. - role: web_group
  10. when: ansible_hostname is match "Web*"
  11. - role: backup
  12. when: ansible_hostname is match "Backup"
  13. - role: mysql
  14. when: ansible_hostname is match "MySQL"

3、创建各个角色的目录

  1. [root@Ansible roles]# ansible-galaxy init basic
  2. - Role basic was created successfully
  3. [root@Ansible roles]# ansible-galaxy init lb_group
  4. - Role lb_group was created successfully
  5. [root@Ansible roles]# ansible-galaxy init web_group
  6. - Role web_group was created successfully
  7. [root@Ansible roles]# ansible-galaxy init nfs
  8. - Role backup was created successfully
  9. [root@Ansible roles]# ansible-galaxy init backup
  10. - Role backup was created successfully
  11. [root@Ansible roles]# ansible-galaxy init mysql
  12. - Role mysql was created successfully
  13. [root@Ansible roles]# ls
  14. backup hosts mysql site.yml
  15. basic lb_group nfs web_group

4、basic角色相关操作

任务

  1. [root@Ansible roles]# cat basic/tasks/main.yml
  2. #1.关闭防火墙
  3. #2.关闭selinux
  4. #3.关闭NetworkManager
  5. #4.修改默认的YUM仓库
  6. #5.安装扩展epel源
  7. #6.配置nginxYUM源
  8. #7.安装常用软件命令
  9. #8.时间同步
  10. #9.创建虚拟用户www
  11. #10.加大文件描述符
  12. - name: Disabled Firewalld Server
  13. systemd:
  14. name: firewalld
  15. state: stopped
  16. enabled: no
  17. - name: Disable Selinux
  18. selinux:
  19. state: disabled
  20. - name: Disabled NetworkManager Server
  21. systemd:
  22. name: NetworkManager
  23. state: stopped
  24. enabled: no
  25. - name: Configure YUM Repo
  26. yum_repository:
  27. name: CentOS-Base
  28. description: ALIYUN YUM repo
  29. baseurl: http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
  30. gpgcheck: no
  31. gpgkey: http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
  32. - name: Add repository
  33. yum_repository:
  34. name: epel
  35. description: EPEL YUM repo
  36. baseurl: http://mirrors.aliyun.com/epel/7/$basearch
  37. gpgcheck: no
  38. gpgkey: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
  39. - name: Add repository
  40. yum_repository:
  41. name: nginx
  42. description: Nginx YUM repo
  43. baseurl: http://nginx.org/packages/centos/$releasever/$basearch/
  44. gpgcheck: no
  45. gpgkey: https://nginx.org/keys/nginx_signing.key
  46. - name: Install Packages
  47. yum:
  48. name: "{{ item }}"
  49. state: present
  50. loop:
  51. - vim
  52. - tree
  53. - lrzsz
  54. - wget
  55. - unzip
  56. - net-tools
  57. - ntpdate
  58. - bash-completion.noarch
  59. - bash-completion-extras.noarch
  60. - name: ntpdate
  61. cron:
  62. name: "ntpdate"
  63. minute: '*/5'
  64. job: '/usr/sbin/ntpdate ntp1.aliyun.com &>/dev/null'
  65. - name: Create Group www
  66. group:
  67. name: www
  68. gid: 666
  69. - name: Create www User
  70. user:
  71. name: www
  72. group: www
  73. uid: 666
  74. shell: /sbin/nologin
  75. create_home: false
  76. - name: Set sysctl file limiits
  77. pam_limits:
  78. dest: "{{ item.dest }}"
  79. domain: '*'
  80. limit_type: "{{ item.limit_type }}"
  81. limit_item: "{{ item.limit_item }}"
  82. value: "{{ item.value }}"
  83. loop:
  84. - { dest: '/etc/security/limits.conf',limit_type: 'soft',limit_item: 'nofile', value: '65535' }
  85. - { dest: '/etc/security/limits.conf',limit_type: 'hard',limit_item: 'nofile', value: '65535'}

5、lb_group角色相关操作

任务

  1. [root@Ansible roles]# cat /ansible/roles/lb_group/tasks/main.yml
  2. #1.组内安装nginx,删除default.conf
  3. #2.lb01、lb02配置nginx.conf
  4. #3.lb01、lb02配置proxy_params
  5. #4.lb01和lb02配置七层负载均衡
  6. #5.lb01、lb02开启nginx
  7. #6.lb01、lb02安装keepalived
  8. #7.lb01、lb02分别配置keepalived文件
  9. #8.传送lb01防止脑裂的脚本文件,并在lb01上做与lb02的免密钥
  10. #9.lb01、lb02开启keepalived
  11. - name: install nginx
  12. yum:
  13. name: nginx
  14. state: present
  15. - name: delete default.conf
  16. file:
  17. name: /etc/nginx/conf.d/default.conf
  18. state: absent
  19. - name: configure nginx.conf
  20. template:
  21. src: nginx.conf.j2 #提前准备
  22. dest: /etc/nginx/nginx.conf
  23. - name: copy proxy_params
  24. copy:
  25. src: proxy_params #提前准备
  26. dest: /etc/nginx
  27. - name: copy ssl_key
  28. copy:
  29. src: ssl_key
  30. dest: /etc/nginx
  31. - name: configure proxy_7 to lb01 and lb02
  32. template:
  33. src: proxy_7.conf.j2 #提前准备
  34. dest: /etc/nginx/conf.d/proxy_7.conf
  35. notify: restart nginx
  36. - name: start nginx
  37. systemd:
  38. name: nginx
  39. state: started
  40. enabled: yes
  41. - name: install keepalive
  42. yum:
  43. name: keepalived
  44. state: present
  45. - name: configure keepalived
  46. template:
  47. src: keepalived.conf.j2 #提前准备
  48. dest: /etc/keepalived/keepalived.conf
  49. notify: restart keepalived
  50. - name: copy check_split_brain.sh to lb01
  51. copy:
  52. src: check_split_brain.sh #提前准备
  53. dest: /etc/keepalived/check_split_brain.sh
  54. when: ansible_hostname is match "LB01"
  55. - name: start keepalive
  56. systemd:
  57. name: keepalived
  58. state: started

提前准备的文件、变量、handlers

  1. [root@Ansible lb_group]# ls files/
  2. check_split_brain.sh proxy_params ssl_key
  3. [root@Ansible lb_group]# ls templates/
  4. keepalived.conf.j2 nginx.conf.j2 proxy_7.conf.j2
  5. [root@Ansible lb_group]# cat vars/main.yml
  6. user: www
  7. [root@Ansible lb_group]# cat handlers/main.yml
  8. - name: restart nginx
  9. systemd:
  10. name: nginx
  11. state: restarted
  12. - name: restart keepalived
  13. systemd:
  14. name: keepalived
  15. state: restarted

6、nfs角色相关操作

任务

  1. [root@Ansible roles]# cat nfs/tasks/main.yml
  2. - name: install nfs server
  3. yum:
  4. name: nfs-utils
  5. state: present
  6. - name: configure nfs server
  7. template:
  8. src: exports
  9. dest: /etc/exports
  10. notify: restart nfs server
  11. - name: create directory data/...
  12. file:
  13. path: "{{ item }}"
  14. state: directory
  15. owner: www
  16. group: www
  17. mode: 0755
  18. loop: "{{ directory_list }}"
  19. - name: start nfs server
  20. systemd:
  21. name: nfs
  22. state: started
  23. enabled: yes
  24. #实时同步
  25. - name: install rsync inotify-tools
  26. yum:
  27. name:
  28. - rsync
  29. - inotify-tools
  30. state: present
  31. - name: mkdir server
  32. file:
  33. path: /server
  34. state: directory
  35. - name: tar xf sersync.tar.gz
  36. unarchive:
  37. src: sersync2.5.4_64bit_binary_stable_final.tar.gz
  38. dest: /server
  39. - name: mv GNU-Linux-x86/ sersyncd
  40. command:
  41. cmd: mv /server/GNU-Linux-x86 /server/sersyncd
  42. become: true
  43. - name: copy confxml.xml to nfs
  44. copy:
  45. src: confxml.xml
  46. dest: /server/sersyncd/confxml.xml
  47. - name: copy rsync.pass
  48. copy:
  49. src: rsync.pass
  50. dest: /etc/rsync.pass
  51. mode: "0600"
  52. - name: ./sersync2 -dr
  53. command: cd /server/sersyncd/ && ./sersync2 -dr

提前准备的文件、变量、handlers

  1. [root@Ansible roles]# ls nfs/templates/
  2. exports
  3. [root@Ansible roles]# cat nfs/vars/main.yml
  4. directory_list:
  5. - /data/wordpress
  6. - /data/wecenter
  7. - /data/phpshe
  8. - /data/kod
  9. share_ip : 172.16.1.0/24
  10. [root@Ansible roles]# cat nfs/handlers/main.yml
  11. - name: restart nfs server
  12. systemd:
  13. name: nfs
  14. state: restarted

7、web_group角色相关操作

任务

  1. [root@Ansible roles]# cat web_group/tasks/main.yml
  2. #1.安装nginx,php,nfs
  3. #2.配置nginx.conf conf.d文件,并监控
  4. #3.配置php.ini www.conf,并监控
  5. #4.开启nginx和php
  6. #5.创建代码目录,导入代码文件,更改代码文件的权限
  7. #6.挂载存放静态文件的目录到nfs
  8. - name: install nginx
  9. yum:
  10. name: nginx
  11. state: present
  12. - name: tar php.tar.gz
  13. unarchive:
  14. src: php71.tar.gz #准备
  15. dest: /root
  16. - name: localinstall rpm
  17. yum:
  18. name:
  19. - /root/autoconf-2.69-11.el7.noarch.rpm
  20. - /root/automake-1.13.4-3.el7.noarch.rpm
  21. - /root/libevent-2.0.21-4.el7.x86_64.rpm
  22. - /root/libjpeg-turbo-1.2.90-8.el7.x86_64.rpm
  23. - /root/libmcrypt-2.5.8-13.el7.x86_64.rpm
  24. - /root/libmemcached-1.0.16-5.el7.x86_64.rpm
  25. - /root/libtool-ltdl-2.4.2-22.el7_3.x86_64.rpm
  26. - /root/libX11-1.6.7-3.el7_9.x86_64.rpm
  27. - /root/libX11-common-1.6.7-3.el7_9.noarch.rpm
  28. - /root/libXau-1.0.8-2.1.el7.x86_64.rpm
  29. - /root/libxcb-1.13-1.el7.x86_64.rpm
  30. - /root/libXpm-3.5.12-1.el7.x86_64.rpm
  31. - /root/libxslt-1.1.28-6.el7.x86_64.rpm
  32. - /root/mod_php71w-7.1.33-1.w7.x86_64.rpm
  33. - /root/pcre-devel-8.32-17.el7.x86_64.rpm
  34. - /root/perl-Data-Dumper-2.145-3.el7.x86_64.rpm
  35. - /root/perl-Test-Harness-3.28-3.el7.noarch.rpm
  36. - /root/perl-Thread-Queue-3.02-2.el7.noarch.rpm
  37. - /root/php71w-cli-7.1.33-1.w7.x86_64.rpm
  38. - /root/php71w-common-7.1.33-1.w7.x86_64.rpm
  39. - /root/php71w-devel-7.1.33-1.w7.x86_64.rpm
  40. - /root/php71w-embedded-7.1.33-1.w7.x86_64.rpm
  41. - /root/php71w-fpm-7.1.33-1.w7.x86_64.rpm
  42. - /root/php71w-gd-7.1.33-1.w7.x86_64.rpm
  43. - /root/php71w-mbstring-7.1.33-1.w7.x86_64.rpm
  44. - /root/php71w-mcrypt-7.1.33-1.w7.x86_64.rpm
  45. - /root/php71w-mysqlnd-7.1.33-1.w7.x86_64.rpm
  46. - /root/php71w-opcache-7.1.33-1.w7.x86_64.rpm
  47. - /root/php71w-pdo-7.1.33-1.w7.x86_64.rpm
  48. - /root/php71w-pear-1.10.4-1.w7.noarch.rpm
  49. - /root/php71w-pecl-igbinary-2.0.5-1.w7.x86_64.rpm
  50. - /root/php71w-pecl-memcached-3.0.4-1.w7.x86_64.rpm
  51. - /root/php71w-pecl-mongodb-1.5.3-1.w7.x86_64.rpm
  52. - /root/php71w-pecl-redis-3.1.6-1.w7.x86_64.rpm
  53. - /root/php71w-process-7.1.33-1.w7.x86_64.rpm
  54. - /root/php71w-xml-7.1.33-1.w7.x86_64.rpm
  55. state: present
  56. - name: install nfs-utils
  57. yum:
  58. name: nfs-utils
  59. state: present
  60. - name: configure nginx.conf
  61. template:
  62. src: nginx.conf.j2
  63. dest: /etc/nginx/nginx.conf
  64. notify: restart nginx
  65. - name: configure conf.d
  66. copy:
  67. src: conf.d/
  68. dest: /etc/nginx/conf.d
  69. notify: restart nginx
  70. - name: configure php.ini
  71. copy:
  72. src: php.ini
  73. dest: /etc/php.ini
  74. notify: restart php-fpm
  75. - name: configure www.conf
  76. copy:
  77. src: www.conf
  78. dest: /etc/php-fpm.d/www.conf
  79. notify: restart php-fpm
  80. - name: start nginx
  81. systemd:
  82. name: nginx
  83. state: started
  84. enabled: yes
  85. - name: start php-fpm
  86. systemd:
  87. name: php-fpm
  88. state: started
  89. enabled: yes
  90. - name: tar code.tar.gz
  91. unarchive:
  92. src: code.tar.gz
  93. dest: /
  94. creates: /code
  95. - name: chown -R www.www code
  96. file:
  97. path: /code
  98. owner: www
  99. group: www
  100. - name: Mount wordpress_NFS Server
  101. mount:
  102. src: 172.16.1.31:/data/wordpress
  103. path: /code/wordpress/wp-content/uploads
  104. fstype: nfs
  105. opts: defaults
  106. state: mounted
  107. - name: Mount wecenter_NFS Server
  108. mount:
  109. src: 172.16.1.31:/data/wecenter
  110. path: /code/wecenter/uploads
  111. fstype: nfs
  112. opts: defaults
  113. state: mounted
  114. - name: Mount phpshe_NFS Server
  115. mount:
  116. src: 172.16.1.31:/data/phpshe
  117. path: /code/phpshe/data
  118. fstype: nfs
  119. opts: defaults
  120. state: mounted
  121. - name: mount kod server
  122. mount:
  123. src: 172.16.1.31:/data/kod
  124. path: /code/kod/data
  125. fstype: nfs
  126. opts: defaults
  127. state: mounted

提前准备的文件、变量、handlers

  1. [root@Ansible roles]# ls web_group/files/
  2. code.tar.gz conf.d php71.tar.gz php.ini www.conf
  3. [root@Ansible web_group]# ls templates/
  4. nginx_.conf.j2
  5. [root@Ansible web_group]# cat vars/main.yml
  6. user: www
  7. [root@Ansible web_group]# cat handlers/main.yml
  8. - name: restart nginx
  9. systemd:
  10. name: nginx
  11. state: restarted
  12. - name: restart php-fpm
  13. systemd:
  14. name: php-fpm
  15. state: restarted

8、backup角色相关操作

任务

  1. [root@Ansible roles]# cat backup/tasks/main.yml
  2. - name: Install Rsync Server
  3. yum:
  4. name: rsync
  5. state: present
  6. - name: Copy Srsync Configure File
  7. template:
  8. src: "{{ item.src }}"
  9. dest: "{{ item.dest }}"
  10. mode: "{{ item.mode }}"
  11. loop:
  12. - { src: rsyncd.conf.j2, dest: /etc/rsyncd.conf,mode: '0644' }
  13. - { src: rsync.passwd.j2, dest: /etc/rsync.passwd,mode: '0600' }
  14. notify: restart rsyncd
  15. - name: Create Dir "{{ rsync_dir }}"
  16. file:
  17. path: /{{ rsync_dir }}
  18. state: directory
  19. owner: "{{ rs_user }}"
  20. group: "{{ rsg_user }}"
  21. - name: Start Rsync Server
  22. systemd:
  23. name: rsyncd
  24. state: started
  25. enabled: yes
  26. - name: mkdir /data
  27. file:
  28. name: "{{ item }}"
  29. state: directory
  30. owner: www
  31. group: www
  32. loop:
  33. - /data
  34. - /bash
  35. - name: copy rsync_all.sh
  36. copy:
  37. src: rsync_all.sh
  38. dest: /bash/rsync_all.sh

提前准备的文件、变量、handlers

  1. [root@Ansible roles]# ls backup/files/
  2. rsync_all.sh
  3. [root@Ansible backup]# ls templates/
  4. rsyncd.conf.j2 rsync.passwd.j2
  5. [root@Ansible backup]# cat vars/main.yml
  6. rs_user: www
  7. rsg_user: www
  8. pass: 123456
  9. rsync_dir: /backup
  10. [root@Ansible backup]# cat handlers/main.yml
  11. - name: restart rsyncd
  12. systemd:
  13. name: rsyncd
  14. state: restarted

9、mysql角色相关操作

任务

  1. [root@Ansible roles]# cat mysql/tasks/main.yml
  2. - name: Install mariadb mysql-python redis
  3. yum:
  4. name:
  5. - mariadb-server
  6. - MySQL-python
  7. - redis
  8. state: present
  9. - name: Start httpd Server
  10. systemd:
  11. name: mariadb
  12. state: started
  13. enabled: yes
  14. - name: Copy all.sql to Mysql
  15. copy:
  16. src: all.sql
  17. dest: /root/all.sql
  18. - name: import all.sql
  19. mysql_db:
  20. login_host: localhost
  21. login_port: 3306
  22. login_user: root
  23. name: all
  24. state: import
  25. target: /root/all.sql
  26. - name: Restart MariaDB Server
  27. systemd:
  28. name: mariadb
  29. state: restarted
  30. - name: copy redis.conf to mysql
  31. copy:
  32. src: redis.conf
  33. dest: /etc/redis.conf
  34. - name: start and redis
  35. systemd:
  36. name: redis
  37. state: started
  38. enabled: yes

提前准备的文件、变量、handlers

  1. [root@Ansible roles]# ls mysql/files/
  2. all.sql redis.conf

10、执行测试(密钥分发+检查playbook语法+执行playbook)

  1. [root@Ansible ~]# cd /bash/
  2. [root@Ansible bash]# sh batchSendKey.sh #执行密钥分发,要在bash目录执行
  3. [root@Ansible bash]# ansible-playbook --syntax-check /ansible/roles/site.yml
  4. playbook: /ansible/roles/site.yml
  5. [root@Ansible roles]# ansible-playbook -i hosts /ansible/roles/site.yml
  6. -----密钥分发与主机列表-----
  7. [root@Ansible bash]# cat batchSendKey.sh
  8. #!/bin/bash
  9. if [ ! -f ~/.ssh/id_rsa ];then
  10. ssh-keygen -t rsa
  11. else
  12. echo "id_rsa has created ..."
  13. fi
  14. while read line
  15. do
  16. user="root"
  17. ip=`echo $line | cut -d " " -f 1`
  18. passwd="1"
  19. expect <<EOF
  20. set timeout 10
  21. spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $user@$ip
  22. expect {
  23. "yes/no" { send "yes\n";exp_continue }
  24. "password" { send "$passwd\n" }
  25. }
  26. expect "password" { send "$passwd\n" }
  27. EOF
  28. done < hostlist.txt
  29. [root@Ansible bash]# cat hostlist.txt
  30. 172.16.1.5
  31. 172.16.1.6
  32. 172.16.1.7
  33. 172.16.1.8
  34. 172.16.1.31
  35. 172.16.1.41
  36. 172.16.1.51
  37. 172.16.1.52

运行完后浏览器访问网页查看项目是否正常访问,模拟脑裂等等操作,检查剧本执行结果。


我是koten,10年运维经验,持续分享运维干货,感谢大家的阅读和关注!

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/1011163
推荐阅读
相关标签
  

闽ICP备14008679号