ansible 搭建游戏、剧本、动静分离


1、setup模块
获取主机信息

ansible web01 -m setup -a "filter=ansible_all_ipv4_addresses"

常用参数
      ansible_all_ipv4_addresses:仅显示ipv4的信息。
      ansible_devices:仅显示磁盘设备信息。
      ansible_distribution:显示是什么系统,例:centos,suse等。
      ansible_distribution_major_version:显示是系统主版本。
      ansible_distribution_version:仅显示系统版本。
      ansible_machine:显示系统类型,例:32位,还是64位。
      ansible_eth0:仅显示eth0的信息。
      ansible_hostname:仅显示主机名(不准确)
      ansible_fqdn:仅显示主机名。
      ansible_kernel:仅显示内核版本。
      ansible_lvm:显示lvm相关信息。
      ansible_memtotal_mb:显示系统总内存。
      ansible_memfree_mb:显示可用系统内存。
      ansible_memory_mb:详细显示内存情况。
      ansible_swaptotal_mb:显示总的swap内存。
      ansible_swapfree_mb:显示swap内存的可用内存。
      ansible_mounts:显示系统磁盘挂载情况。
      ansible_processor:显示cpu个数(具体显示每个cpu的型号)。
      ansible_processor_vcpus:显示cpu个数(只显示总的个数)。

2、利用ansible搭建一个小游戏案例

1、准备好三台web服务器

2、安装Nginx

ansible web -m yum -a "name=nginx state=installed"

3、建立站点

1、创建目录

ansible web -m shell -a "chdir=/opt creates=/opt/game mkdir /opt/game"

2、编写配置文件
[root@master game]# cat game.conf
  server {
          listen 80;
          server_name game.test.com;
          location / {
                  root /opt/game;
                  index index.html;
          }
  }

3、上传配置文件
ansible web -m copy -a "src=game.conf dest=/etc/nginx/conf.d/"

4、上传代码包

1、上传代码

ansible web -m unarchive -a "src=mario.zip dest=/opt/ "
ansible web -m shell -a "chdir=/opt/html5-mario mv ./* /opt/game/"

2、修改权限

ansible web -m shell -a "chown -R www.www /opt/game"

3、重启Nginx
ansible web -m systemd -a "name=nginx state=restarted"

5、配置负载均衡,高可用

1、编写配置文件并安装Nginx
ansible lb -m yum -a "name=nginx state=installed"

2、上传配置文件
[root@master game]# cat lb-game.conf
  upstream game {
      server 172.16.1.7;
      server 172.16.1.8;
      server 172.16.1.9;
  }

  server {
      listen 80;
      server_name game.test.com;
      location / {
          proxy_pass http://game;
          include proxy_params;
      }
  }
   
  ansible lb -m copy -a "src=lb-game.conf dest=/etc/nginx/conf.d/"
   
   
3、重启Nginx

ansible lb -m systemd -a "name=nginx state=restarted"

4、安装keepalived
ansible lb -m yum -a "name=keepalived state=installed"

5、准备keepalived的配置文件
[root@lb01 conf.d]# cat /etc/keepalived/keepalived.conf
  global_defs {
      router_id hostname
  }

  vrrp_script check {
      script "/etc/keepalived/check_web.sh"
      interval 5
  }

  vrrp_instance VI_1 {
      state BACKUP
      nopreempt
      interface eth0
      virtual_router_id 50
      priority 100
      advert_int 1
      authentication {
          auth_type PASS
          auth_pass 1111
      }
      virtual_ipaddress {
           192.168.230.100
      }
      track_script {
      check
      }  
  }
   
  ansible lb -m copy -a "src=keepalived.conf dest=/etc/keepalived/"
  ansible lb -m copy -a "src=check_web.sh dest=/etc/keepalived/ mode=0755"
   
  ansible lb01 -m shell -a "chdir=/etc/keepalived sed -i 's#hostname#lb01#g' /etc/keepalived/keepalived.conf"
  ansible lb02 -m shell -a "chdir=/etc/keepalived sed -i 's#hostname#lb02#g' /etc/keepalived/keepalived.conf"
  ansible lb02 -m shell -a "chdir=/etc/keepalived sed -i 's#priority 100#priority 90#g' /etc/keepalived/keepalived.conf"
   
6、重启keepalived
ansible lb -m systemd -a "name=keepalived state=restarted"

6、开始测试

3、剧本

1、剧本的组成

1、主机(hosts)
2、任务(tasks)

2、剧本是由yaml文件编写的
ansible web -m yum -a "name=nginx state=installed"

- hosts: web
tasks:
   - name: install nginx
    yum:
      name: nginx
      state: installed

3、剧本使用方法
   #模拟执行
  [root@m01 ~]# ansible-playbook -C touch.yml
   #验证语法
  [root@m01 ~]# ansible-playbook --syntax-check touch.yml
   #注意:只能验证语法,验证不了逻辑
   #执行
  [root@master game]# ansible-playbook game.yaml

4、翻译成剧本

- hosts: web
tasks:
  - name: Install Nginx
    yum:
      state: installed
      name: nginx
  - name: Create Dir
    shell: chdir=/opt creates=/opt/game mkdir /opt/game
     
  - name: Copy Config File
    copy:
      src: game.conf
      dest: /etc/nginx/conf.d/

  - name: Upload Code File
    unarchive:
      src: mario.zip
      dest: /opt

  - name: Move File To Game
    shell: chdir=/opt/html5-mario creates=/opt/game/images mv ./* /opt/game/

  - name: UpDate Files Jurisdiction
    shell: chown -R www.www /opt/game

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

- hosts: lb
tasks:
  - name: Install Nginx
    yum:
      name: nginx
      state: installed
   
  - name: Upload Nginx Config File
    copy:
      src: lb-game.conf
      dest: /etc/nginx/conf.d/
       
  - name: Restart Nginx Service
    systemd:
      name: nginx
      state: restarted
       
  - name: Install Keepalived
    yum:
      name: keepalived
      state: installed
   
  - name: Upload Keepalived Config File
    copy:
      src: keepalived.conf
      dest: /etc/keepalived/
   
  - name: Upload Check Web Service Config File
    copy:
      src: check_web.sh
      dest: /etc/keepalived/
      mode: 0755

- hosts: lb01
tasks:
  - name: Modify lb01 Keepalived Config File
    shell: chdir=/etc/keepalived sed -i 's#hostname#lb01#g' /etc/keepalived/keepalived.conf
   
- hosts: lb02
tasks:
  - name: Modify lbo2 Keepalived Config File
    shell: chdir=/etc/keepalived sed -i 's#hostname#lb02#g' /etc/keepalived/keepalived.conf
   
  - name: Modify lbo2 Keepalived Config File
    shell: chdir=/etc/keepalived sed -i 's#priority 100#priority 90#g' /etc/keepalived/keepalived.conf
 
- hosts: lb
tasks:
  - name: Restart Keepalived Service
    systemd:
      name: keepalived
      state: restarted

5、动静分离

1、把静态文件传送至NFS

1、把源码包上传到NFS

ansible nfs -m unarchive -a "src=mario.zip dest=/opt/"

2、把images文件夹移动到挂载点

ansible nfs -m shell -a "chdir=/data creates=/data/game mkdir /data/game"
ansible nfs -m shell -a "chdir=/opt/html5-mario creates=/data/game/logo.gif mv /opt/html5-mario/images/* /data/game"

2、添加挂载点

添加/data/game
[root@master game]# cat add_nfs.sh
   #!/bin/bash

   grep "/data/game" /etc/exports

   if [ $? -ne 0 ];then
       echo '/data/game   172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)' >> /etc/exports
   fi
   
  ansible nfs -m script -a "./add_nfs.sh"
   
  ansible nfs -m systemd -a "name=nfs-server state=restarted"
  ansible nfs -m systemd -a "name=rpcbind state=restarted"

3、挂载至负载均衡

1、创建挂载点
ansible lb -m file -a "path=/opt/game_images state=directory"
2、挂载
ansible lb -m mount -a "src=172.16.1.31:/data/game path=/opt/game_images fstype=nfs opts=defaults state=mounted"
4、修改负载均衡配置文件

upstream game {
server 172.16.1.7;
server 172.16.1.8;
server 172.16.1.9;
}

server {
listen 80;
server_name game.test.com;
location / {
proxy_pass http://game;
include proxy_params;
}
location ~* \.(png|jpg|jpeg|gif)$ {
root /opt/game_images;
}
}

ansible lb -m copy -a "src=lb-game1.conf dest=/etc/nginx/conf.d/lb-game.conf"

5、重启负载均衡
ansible lb -m systemd -a "name=nginx state=restarted"
ansible lb -m systemd -a "name=keepalived state=restarted"

6、测试

 

posted @ 2022-04-04 20:37  甜甜de微笑  阅读(326)  评论(0)    收藏  举报