第13周-2022-03-01

1、ansible-playbook实现MySQL的二进制部署

环境:
centos01:192.168.184.147 客户端
centos03:192.168.184.134 ansible服务端

1.安装ansible

[root@centos03 ~]# yum install ansible

2.ssh免密认证

[root@centos03 ~]# ssh-keygen 
[root@centos03 ~]# ssh-copy-id 192.168.184.147

3.配置主机清单

[root@centos03 ~]# mkdir ansible;cd ansible
[root@centos03 ansible]# vi host
[mysql]
192.168.184.147

[root@centos03 ansible]# cat ansible.cfg
[defaults]
inventory = host
remote_user = root

4.mysql配置文件

[root@centos03 ansible]# cat my.cnf.j2
[mysqld]
datadir={{ dataDIR }}
skip_name_resolve=1
socket={{ dataDIR }}/mysql.sock
log-error={{ dataDIR }}/mysql.log
pid-file={{ dataDIR }}/mysql.pid
[client] 
socket={{ dataDIR }}/mysql.sock

5.mysql安装脚本:

[root@centos03 ansible]# cat install-mysql8.0-bin.yml 
---
- name: install mysql8.0
  hosts: mysql
  gather_facts: no

  vars:
    downloadURL: https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
    mysqlDIR: mysql-8.0.27-linux-glibc2.12-x86_64
    installDIR: /usr/local
    dataDIR: /data/mysql

  tasks:
  - name: install package
    yum:
      name:
        - libaio
        - numactl-libs
      state: present
    
  - name: add mysql group
    group:
      name: mysql
      system: yes
  - name: add mysql user
    user:
      name: mysql
      groups: mysql
      shell: /sbin/nologin
      system: yes
      home: "{{ dataDIR }}"
      create_home: no
  - name: create data directory
    file:
      path: "{{ dataDIR }}"
      state: directory
      owner: mysql
      group: mysql
      recurse: yes
  - name: download mysql package
    unarchive:
      src: "{{ downloadURL }}"
      dest: "{{ installDIR }}"
      copy: no
      owner: mysql
      group: mysql
  - name: create sort link
    file:
      src: "{{ installDIR }}/{{ mysqlDIR }}"
      dest: "{{ installDIR }}/mysql"
      state: link
      owner: mysql
      group: mysql
  - name: configure env
    copy:
      content: "PATH={{ installDIR }}/mysql/bin:$PATH"
      dest: /etc/profile.d/mysql.sh
  - name: update my.cnf
    template:
      src: my.cnf.j2
      dest: /etc/my.cnf
  - name: copy service file
    copy:
      src: "{{ installDIR }}/mysql/support-files/mysql.server"
      dest: /etc/init.d/mysqld
      remote_src: yes
      mode: 755
  - name: init mysql
    shell: 
      source /etc/profile.d/mysql.sh;mysqld --initialize-insecure --user=mysql --datadir={{ dataDIR }}
  - name: start mysqld
    service:
      name: mysqld
      state: started
      enabled: true

6.运行playbook

[root@centos03 ansible]# ansible-playbook install-mysql8.0-bin.yml

PLAY [install mysql8.0] *******************************************************************************************************************************************************

TASK [add mysql group] ********************************************************************************************************************************************************
changed: [192.168.184.147]

TASK [add mysql user] *********************************************************************************************************************************************************
changed: [192.168.184.147]

TASK [create data directory] **************************************************************************************************************************************************
changed: [192.168.184.147]

TASK [download mysql package] *************************************************************************************************************************************************
changed: [192.168.184.147]

TASK [create sort link] *******************************************************************************************************************************************************
changed: [192.168.184.147]

TASK [configure env] **********************************************************************************************************************************************************
changed: [192.168.184.147]

TASK [update my.cnf] **********************************************************************************************************************************************************
changed: [192.168.184.147]

TASK [copy service file] ******************************************************************************************************************************************************
changed: [192.168.184.147]

TASK [init mysql] *************************************************************************************************************************************************************
changed: [192.168.184.147]

TASK [start mysqld] ***********************************************************************************************************************************************************
[WARNING]: The service (mysqld) is actually an init script but the system is managed by systemd
changed: [192.168.184.147]

PLAY RECAP ********************************************************************************************************************************************************************
192.168.184.147            : ok=10   changed=10   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

7.登录mysql:

[root@centos01 ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.27 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

2、Ansible playbook实现apache批量部署,并对不同主机提供以各自IP地址为内容的index.html

1.配置主机清单

[root@centos03 ansible]# cat host
[httpd]
192.168.184.147
192.168.184.129

2.httpd主页文件

[root@centos03 ansible]# cat index.html.j2 

{{ ansible_default_ipv4.address }}

3.httpd安装脚本

[root@centos03 ansible]# cat install-httpd.yml 
---
- name: install httpd
  hosts: httpd

  tasks:
  - name: install httpd
    yum:
      name: httpd
      state: latest
  - name: start httpd
    service:
      name: httpd
      state: started
      enabled: true
  - name: update index.html
    template:
      src: index.html.j2
      dest: /var/www/html/index.html
    notify:
    - restart httpd
  handlers:
    - name: restart httpd
      service:
        name: httpd
        state: restarted

4.运行playbook

[root@centos03 ansible]# ansible-playbook install-httpd.yml 

PLAY [install httpd] **********************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************
ok: [192.168.184.129]
ok: [192.168.184.147]

TASK [install httpd] **********************************************************************************************************************************************************
changed: [192.168.184.147]
changed: [192.168.184.129]

TASK [start httpd] ************************************************************************************************************************************************************
changed: [192.168.184.147]
changed: [192.168.184.129]

TASK [update index.html] ******************************************************************************************************************************************************
changed: [192.168.184.129]
changed: [192.168.184.147]

RUNNING HANDLER [restart httpd] ***********************************************************************************************************************************************
changed: [192.168.184.147]
changed: [192.168.184.129]

PLAY RECAP ********************************************************************************************************************************************************************
192.168.184.129            : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.184.147            : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

5.验证:

[root@centos03 ansible]# curl http://192.168.184.147

192.168.184.147

[root@centos03 ansible]# curl http://192.168.184.129

192.168.184.129

3、http的报文结构和状态码总结

  • http请求报文:
    报文由三个部分组成,即开始行、首部行和实体主体。
    在请求报文中,开始行就是请求行。


    http请求报文结构
  • http响应报文:
    响应报文的开始行是状态行。
    状态行包括三项内容,即HTTP的版本、状态码,以及解释状态码的简单短语。


    http响应报文结构
  • http状态码分类
    1xx:100-101 信息提示
    2xx:200-206 成功
    3xx:300-307 重定向
    4xx:400-415 错误类信息,客户端错误
    5xx:500-505 错误类信息,服务器端错误

  • http常见状态码:
    200: 成功,请求数据通过响应报文的entity-body部分发送;OK
    301: 请求的URL指向的资源已经被删除;但在响应报文中通过首部Location指明了资源现在所处的新位置;Moved Permanently
    302: 响应报文Location指明资源临时新位置 Moved Temporarily
    304: 客户端发出了条件式请求,但服务器上的资源未曾发生改变,则通过响应此响应状态码通知客户端;Not Modified
    307: 浏览器内部重定向
    401: 需要输入账号和密码认证方能访问资源;Unauthorized
    403: 请求被禁止;Forbidden
    404: 服务器无法找到客户端请求的资源;Not Found
    500: 服务器内部错误;Internal Server Error
    502: 代理服务器从后端服务器收到了一条伪响应,如无法连接到网关;Bad Gateway
    503: 服务不可用,临时服务器维护或过载,服务器无法处理请求
    504: 网关超时

你可能感兴趣的:(第13周-2022-03-01)