ansible-playbook 部署可道云(Lb+Nginx+Php+redis)

ansible-playbook部署可道云(Lb+Nginx+php+Redis)

1.节点网络规划

节点服务
10.0.0.61ansible
172.16.1.51(db)redis
172.16.1.7/8(web)nginx+php
172.16.1.5(lb)nginx(lb)

2.节点部署原理图

在这里插入图片描述

3.hosts文件规划

我们针对这个项目先去创建一个新的目录 web_cluster
我们在web_cluster下 复制两个原文件进行配置

1.ansible.cfg
inventory      = ./hosts 
定义我们运行的剧本的默认清单优先级
2.hosts
[webservers]
172.16.1.7
172.16.1.8

[lbservers]
172.16.1.5

[dbservers]
172.16.1.51

4.编写第一个部署redis(172.16.1.51)的剧本

4.1 书写剧本部署redis节点思路

> 1.安装redis服务                          #yum模块
> 2.配置redis主配置文件 (监听在51网卡上)    #copy模块 
> 3.启动redis服务                          #systemd模块
> 4.监听配置文件                           #notify与handlers

4.2 部署剧本

vim redis.server.yaml

- hosts: dbservers
  tasks:
    - name: Installed Redis Server
      yum:
        name: redis
        state: present

    - name: Configure Redis Server
      copy:
        src: ./files/redis.conf.j2
        dest: /etc/redis.conf
        owner: redis
        group: root
        mode: 0640
      notify: Restart Redis Server

    - name: Systemctl Redis Server Started
      systemd:
        name: redis
        state: started
        enabled: yes

  handlers:
    - name: Restart Redis Server
      systemd:
        name: redis
        state: restarted

5.编写第二个部署Nginx和php(172.16.1.7/8)的剧本

5.1书写剧本部署Nginx和php节点思路

> 1.首先配置repo仓库                                               
> 1.1 配置nginx仓库                                  #yum_repository模块
> 1.2 配置php仓库                                    #yum_repository模块
> 2.安装nginx与php服务                               #yum模块
> 3.配置nginx主配置文件nginx.conf(修改用户)          #copy模块
> 4. 配置php两个配置文件
> 4.1 /etc/php-fpm.d/www.conf                       #copy模块
> 4.2 /etc/php.ini                                  #copy模块                   
> 5.配置nginx的网站(kcloud)                         #copy模块
> 6,用户统一化管理 
> 6.1 创建www组                                      #group模块                  
> 6.2创建www用户                                     #user模块
> 7.创建网站对应的站点目录 /code/kcloud               #file模块
> 8.推送可道云代码到站点目录下                        #unarchive模块
> 9.启动nginx和php服务                              #systemd模块
> 10.监听配置文件                                   #notify与handlers

5.2 部署剧本

vim nginx.php.server.yaml

- hosts: webservers
  tasks:
    - name: Configure Nginx Repo
      yum_repository:
        name: ansible_nginx
        description: ansible_nginx_repo
        baseurl: http://nginx.org/packages/centos/$releasever/$basearch/
        gpgcheck: yes
        gpgkey: https://nginx.org/keys/nginx_signing.key
    - name: Configure Php Repo
      yum_repository:
        name: ansible_php
        description: ansible_php_repo
        baseurl: http://10.0.0.10/ 
        gpgcheck: no
        #(我自己搭建的本地yum仓库)
    - name: Installed Nginx PHP
      yum:
        name: "{{ packages }}"
        state: present
      vars:
        packages:
          - nginx
          - php71w
          - php71w-cli
          - php71w-common
          - php71w-devel
          - php71w-embedded
          - php71w-gd
          - php71w-mcrypt
          - php71w-mbstring
          - php71w-pdo
          - php71w-xml
          - php71w-fpm
          - php71w-mysqlnd
          - php71w-opcache
          - php71w-pecl-memcached
          - php71w-pecl-redis
          - php71w-pecl-mongodb

    - name: Configure Nginx Server nginx.conf
      copy:
        src: ./files/nginx.conf.j2
        dest: /etc/nginx/nginx.conf
        owner: root
        group: root
        mode: 0644
      notify: Restart Nginx Server

    - name: Configure PHP Server php-fpm.conf
      copy:
        src: ./files/php-www.conf.j2
        dest: /etc/php-fpm.d/www.conf
        owner: root
        group: root
        mode: 0644
      notify: Restart PHP Server

    - name: Configure PHP Server php.ini
      copy:
        src: ./files/php.ini.j2
        dest: /etc/php.ini
        owner: root
        group: root
        mode: 0644
      notify: Restart PHP Server

    - name: Add kcloud.etiantian.org Web Site
      copy:
        src: ./files/kcloud.etiantian.org.conf.j2
        dest: /etc/nginx/conf.d/kcloud.etiantian.org.conf
        owner: root
        group: root
        mode: 0644
      notify: Restart Nginx Server

    - name: Add Group www
      group:
        name: www
        gid: 666

    - name: Add User www
      user:
        name: www
        uid: 666
        group: '666'
        shell: /sbin/nologin
        create_home: no

    - name: Add Kcloud Code Directory
      file:
        path: /code/kcloud
        state: directory
        owner: www
        group: www
        mode: 0755

    - name: Unzip Kodcloud Code
      unarchive:
        src: ./files/kodbox.1.13.zip
        dest: /code/kcloud/
        owner: www
        group: www
        mode: 0755
        creates: /code/kcloud/index.php
        
    - name: Systemctl Nginx Server Started
      systemd:
        name: nginx
        state: started
        enabled: yes
 
    - name: Systemctl PHP Server Started
      systemd:
        name: php-fpm
        state: started
        enabled: yes

  handlers:
    - name: Restart Nginx Server
      systemd:
        name: nginx
        state: restarted

    - name: Restart PHP Server
      systemd:
        name: php-fpm
        state: restarted

5.3 添加本地域名劫持

在这里插入图片描述
在这里插入图片描述

6.编写第三个部署lb负载均衡(172.16.1.5)的剧本

6.1书写剧本部署Nginx和php节点思路

1.配置nginx仓库                                   #yum_repository模块
2.安装nginx服务                                   #yum模块
3.配置nginx主配置文件                             #copy模块
4.添加自定义的代理参数文件                         #copy模块
5.用户统一化创建www用户和组                        #group模块和user模块
6.开启服务                                       #systemd模块
7.监听配置文件                                   #notify与handlers

6.2部署剧本

lb.server.yaml

- hosts: lbservers
  tasks:

    - name: Configure Nginx Repo
      yum_repository:
        name: ansible_nginx
        description: ansible_nginx_repo
        baseurl: http://nginx.org/packages/centos/$releasever/$basearch/
        gpgcheck: yes
        gpgkey: https://nginx.org/keys/nginx_signing.key

    - name: Installed Nginx Server
      yum:
        name: nginx
        state: present

    - name: Configure Nginx Server nginx.conf
      copy:
        src: ./files/nginx.conf.j2
        dest: /etc/nginx/nginx.conf
        owner: root
        group: root
        mode: 0644
      notify: Restart Nginx Server

    - name: Configure Nginx Proxy Virtual Server 
      copy:
        src: ./files/proxy_kcloud.etiantian.org.conf.j2
        dest: /etc/nginx/conf.d/proxy_kcloud.etiantian.org.conf
        owner: root
        group: root
        mode: 0644
      notify: Restart Nginx Server

    - name: Configure Nginx Proxy parmas
      copy:
        src: ./files/proxy_params.j2
        dest: /etc/nginx/proxy_params
        owner: root
        group: root
        mode: 0644
      notify: Restart Nginx Server
    
    - name: Groupadd WWW
      group: 
        name: www
        gid: 666
    - name: Useradd WWW
      user:
        name: www
        uid: 666
        group: 666


    - name: Systemctl Nginx Server Started
      systemd:
        name: nginx
        state: started
        enabled: yes

  handlers:
    - name: Restart Nginx Server
      systemd:
        name: nginx
        state: restarted
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 黑客帝国 设计师:上身试试 返回首页