第十三周

【第十三周】一年好景君须记,最是橙黄橘绿时。这篇文章主要讲述第十三周相关的知识,希望能为你提供帮助。
第十三周 1.ansible-playbook实现mysql的二进制部署 实验环境

需要三台主机:CentOS Linux release 7.9.2009 (Core) 1台ansible主机安装ansible ansible 192.168.80.103 (yum 安装ansible 需要epel源)2台新安装的机器,配置yum源 node00.magedu.org 192.168.80.100 node01.magedu.org 192.168.80.101ansible主机至2台新主机ssh免密

ansible主机
#ansible安装 [root@ansible ~]# yum install -yansible#创建mysql项目专用目录,将mysql项目相关的ansible文件都统一存放 [root@ansible ~]# mkdir -p ansible/mysql/{files,inventory,log} #目录结构 [root@ansible ~]# tree ansible/mysql ansible/mysql ├── ansible.cfg ├── files │├── my.cnf │├── mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz │└── mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz ├── install_mysql5.7or8.0.yml ├── inventory │└── hosts ├── key_ssh_expect.sh └── log └── ansible.log3 directories, 8 files#ansible [root@ansible mysql]# ansible --version ansible 2.9.27 config file = /root/ansible/mysql/ansible.cfg configured module search path = [/root/.ansible/plugins/modules, /usr/share/ansible/plugins/modules] ansible python module location = /usr/lib/python3.6/site-packages/ansible executable location = /usr/bin/ansible python version = 3.6.8 (default, Apr 16 2020, 01:36:27) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]#ansible.cfg [root@ansible ~]# cp /etc/ansible/ansible.cfgansible/mysql/ansible.cfg [root@ansible mysql]# vim /root/ansible/mysql/ansible.cfg #修改下面几行其他保持默认即可 [defaults] # some basic default values... #去掉# 修改hosts文件路径 inventory= ./inventory/hosts #去掉# 修改日志路径 log_path = log/ansible.log #直接去掉注释 host_key_checking = False#my.cnf [root@ansible ~]# vim ansible/mysql/files/my.cnf [client] socket=/data/datadb/mysql.sock [mysql] default-character-set=utf8mb4 prompt=(\\\\u@\\\\h) [\\\\d]> \\\\_[mysqld] character-set-server=utf8mb4 server-id=21 log-bin=/data/binlog/mysql-bin datadir=/data/datadb/ socket=/data/datadb/mysql.sock log-error=/data/datadb/mysql.log pid-file=/data/datadb/mysql.pid#hosts文件 [root@ansible ~]# vim ansible/mysql/hosts [mysqldb] 192.168.80.100 192.168.80.101

#yml文件 #ansible-playbook 文件 [root@ansible ~]# cat ansible/mysql/install_mysql5.7or8.0.yml--- #install_mysql5.7or8.0.yml #install mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz - hosts: mysqldb remote_user: root gather_facts: no vars: mysql_version: 8.0.26 mysql_file: mysql-{{mysql_version}}-linux-glibc2.12-x86_64.tar.xz mysql_root_password: mysql#123456 tasks: - name: install packages yum: name: - libaio - numactl-libs - name: create mysql group group: name=mysql gid=306 - name: create mysql user user: name=mysql uid=306 group=mysql shell=/sbin/nologin system=yes create_home=no - name: create /data/datadb file: path=/data/datadb/ state=directory owner=mysql group=mysql mode=755 - name: create /data/binlog file: path=/data/binlog/ state=directory owner=mysql group=mysql mode=755 - name: copy tar to remote host and file mode unarchive: src=https://www.songbingjia.com/root/ansible/mysql/files/{{mysql_file}} dest=/usr/local/ owner=root group=root - name: create linkfile /usr/local/mysql file: src=/usr/local/mysql-{{ mysql_version }}-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link - name: initialize database shell: /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/data/datadb/ tags: data - name: config my.cnf copy: src=/root/ansible/mysql/files/my.cnf dest=/etc/my.cnf - name: service script shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld - name: PATH variable copy: content=PATH=/usr/local/mysql/bin:$PATH dest=/etc/profile.d/mysql.sh - name: enable service shell: checkconfig --add mysqld; /etc/init.d/mysqld start; chkconfig mysqld on tags: service - name: change password shell: /usr/local/mysql/bin/mysqladmin -urootpassword {{mysql_root_password}}

#ansible主机到目标主机ssh免密[root@ansible ~]# cat key_ssh_expect.sh #!/bin/bash #===================================================================================================== #File Name:key_ssh_expect.sh #Date:2021-12-02 00-20-20 #Author:Create by gonghairong #Description:This script function is #Shell Version:GNU bash version 4.1.2(2)-release x86_64-redhat-linux-gnu #Copyright (C):2021 All rights reserved #===================================================================================================== #------------------------------------------------------------------------------------------------------------------ password=123456 port=22 user=root ip_list=" 192.168.80.100 192.168.80.101 " #------------------------------------------------------------------------------------------------------------------rpm -a expect & > /dev/null|| yum install -y -qexpect& > /dev/null[ -f~/.ssh/id_ecdsa ] & & echo "id_ecdsa is ok " || ssh-keygen -q -t ecdsa -P-f~/.ssh/id_ecdsa & > /dev/nullfori in $ip_list do { /usr/bin/expect< < -eofset time 60spawnssh-copy-id -f-i /root/.ssh/id_ecdsa.pub-p $port$user@$iexpect {"yes/no" { send "yes\\n"; exp_continue } "password" { send "$password\\n"}} expect eof eofecho -e "${GREEN_COLOR}$i is ok${RES}"}& donewaitecho-e" keyis ok "#执行ssh免密脚本 [root@ansible ~]# sh key_ssh_expect.sh id_ecdsa is ok spawn ssh-copy-id -f -i /root/.ssh/id_ecdsa.pub -p 22 root@192.168.80.101 spawn ssh-copy-id -f -i /root/.ssh/id_ecdsa.pub -p 22 root@192.168.80.100 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_ecdsa.pub" /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_ecdsa.pub" root@192.168.80.100s password:Number of key(s) added: 1Now try logging into the machine, with:"ssh -p 22 root@192.168.80.101" and check to make sure that only the key(s) you wanted were added.192.168.80.101 is okNumber of key(s) added: 1Now try logging into the machine, with:"ssh -p 22 root@192.168.80.100" and check to make sure that only the key(s) you wanted were added.192.168.80.100 is ok

ansible-playbook 语法检
#ansible-playbook yml 文件语法检查 [root@ansible mysql]# cd /root/ansible/mysql [root@ansible mysql]# ansible-playbook install_mysql5.7or8.0.yml--syntax-check playbook: install_mysql5.7or8.0.yml#语法检查没有问题

ansible-playbook执行
[root@ansible mysql]# cd /root/ansible/mysql#安装mysql8.0 [root@ansible ~]# ansible-playbook install_mysql5.7or8.0_v2.ymlPLAY [mysqldb] ******************************************************************************************************************************************TASK [install packages] ********************************************************************************************************************************* ok: [192.168.80.101] ok: [192.168.80.100]TASK [create mysql group] ******************************************************************************************************************************* changed: [192.168.80.101] changed: [192.168.80.100]TASK [create mysql user] ******************************************************************************************************************************** changed: [192.168.80.100] changed: [192.168.80.101]TASK [create /data/datadb] ****************************************************************************************************************************** changed: [192.168.80.101] changed: [192.168.80.100]TASK [create /data/binlog] ****************************************************************************************************************************** changed: [192.168.80.100] changed: [192.168.80.101]TASK [copy tar to remote host and file mode] ************************************************************************************************************ changed: [192.168.80.100] changed: [192.168.80.101]TASK [create linkfile /usr/local/mysql] ***************************************************************************************************************** changed: [192.168.80.100] changed: [192.168.80.101]TASK [initialize database] ****************************************************************************************************************************** changed: [192.168.80.101] changed: [192.168.80.100]TASK [config my.cnf] ************************************************************************************************************************************ changed: [192.168.80.100] changed: [192.168.80.101]TASK [service script] *********************************************************************************************************************************** changed: [192.168.80.101] changed: [192.168.80.100]TASK [PATH variable] ************************************************************************************************************************************ changed: [192.168.80.100] changed: [192.168.80.101]TASK [enable service] *********************************************************************************************************************************** changed: [192.168.80.100] changed: [192.168.80.101]TASK [change password] ********************************************************************************************************************************** changed: [192.168.80.100] changed: [192.168.80.101]PLAY RECAP ********************************************************************************************************************************************** 192.168.80.100: ok=13changed=12unreachable=0failed=0skipped=0rescued=0ignored=0 192.168.80.101: ok=13changed=12unreachable=0failed=0skipped=0rescued=0ignored=0#查看mysql安装效果 [root@ansible mysql]# ansible mysqldb -m shell -a /etc/init.d/mysqld status 192.168.80.100 | CHANGED | rc=0 > > SUCCESS! MySQL running (74816) 192.168.80.101 | CHANGED | rc=0 > > SUCCESS! MySQL running (74611)

2.Ansible playbook实现apache/nginx 批量部署,并对不同主机提供以各自IP地址为内容的index.html 实验环境
需要三台主机:CentOS Linux release 7.9.2009 (Core) 1台ansible 主机 安装ansible ansible 192.168.80.1032台新安装的机器 node00.magedu.org 192.168.80.100 node01.magedu.org 192.168.80.101ansible主机至2台新主机ssh免密(参考1中的脚本)

ansible主机
#ansible安装 [root@ansible ~]# yum install -yansible#创建 nginx 项目专用目录,将 nginx 项目相关的ansible文件都统一存放 [root@ansible ~]# mkdir -pansible/nginx/{templates,log,inventory,files}[root@ansible ~]# treeansible/nginx ansible/nginx ├── ansible.cfg ├── files │└── nginx-1.18.0.tar.gz ├── inventory │└── hosts ├── key_ssh_expect.sh ├── log │└── ansible.log ├── templates │├── nginx.conf.j2 │└── nginx.service.j2 └── v00.yml4 directories, 8 files#ansible.cfg [root@ansible ~]# cp /etc/ansible/ansible.cfgansible/mysql/ansible.cfg [root@ansible mysql]# vim /root/ansible/mysql/ansible.cfg #修改下面几行其他保持默认即可 [defaults] # some basic default values... #去掉# 修改路径 inventory= ./inventory/hosts #去掉# 修改日志路径 log_path = log/ansible.log #直接去掉注释 host_key_checking = False#hosts文件 [root@ansible ~]# vim ansible/nginx/inventory/hosts [mysqldb] 192.168.80.100 hostname=node1 domain=mgedu.org 192.168.80.101 hostname=node2 domain=mgedu.org[mysqldb:vars] mark="-"[all:vars] domain=mgedu.org

#模板文件 nginx.conf #为了保持配置完整性注释的信息也保留 [root@ansible ~]# vimansible/nginx/templates/nginx.conf.j2 #usernobody; #worker_processesauto; worker_processes{{ ansible_processor_vcpus+1 }}; #error_loglogs/error.log; #error_loglogs/error.lognotice; #error_loglogs/error.loginfo; #pidlogs/nginx.pid; events {worker_connections{{ 1024*ansible_processor_vcpus }}; }http {includemime.types; default_typeapplication/octet-stream; log_formatmain$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"; access_loglogs/access.logmain; sendfileon; #tcp_nopushon; #keepalive_timeout0; keepalive_timeout65; #server_tokens off ; #gzipon; server { listen80; server_namewww.magedu.org; #charset koi8-r; #access_loglogs/host.access.logmain; location / { roothtml; indexindex.html index.htm; }#error_page404/404.html; # redirect server error pages to the static page /50x.html # error_page500 502 503 504/50x.html; location = /50x.html { roothtml; }# proxy the php scripts to Apache listening on 127.0.0.1:80 # #location ~ \\.php$ { #proxy_passhttp://127.0.0.1; #}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \\.php$ { #roothtml; #fastcgi_pass127.0.0.1:9000; #fastcgi_indexindex.php; #fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name; #includefastcgi_params; #}# deny access to .htaccess files, if Apaches document root # concurs with nginxs one # #location ~ /\\.ht { #denyall; #} }# another virtual host using mix of IP-, name-, and port-based configuration # #server { #listen8000; #listensomename:8080; #server_namesomenamealiasanother.alias; #location / { #roothtml; #indexindex.html index.htm; #} #}# HTTPS server # #server { #listen443 ssl; #server_namelocalhost; #ssl_certificatecert.pem; #ssl_certificate_keycert.key; #ssl_session_cacheshared:SSL:1m; #ssl_session_timeout5m; #ssl_ciphersHIGH:!aNULL:!MD5; #ssl_prefer_server_cipherson; #location / { #roothtml; #indexindex.html index.htm; #} #}}

#service 模板文件 [root@ansible ~]#vimansible/nginx/templates/nginx.service.j2[Unit] Description=The nginx HTTP and reverse proxy server After=network.target remote-fs.target nss-lookup.target[Service] Type=forking PIDFile={{ install_dir }}/logs/nginx.pid ExecStartPre=/bin/rm -f {{ install_dir }}/logs/nginx.pid ExecStartPre={{ install_dir }}/sbin/nginx -t ExecStart={{ install_dir }}/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID KillSignal=SIGQUIT TimeoutStopSec=5 KillMode=process PrivateTmp=true LimitNOFILE=1000000 [Install] WantedBy=multi-user.target

ansible-playbook 语法检
#ansible-playbook 语法检 [root@ansible nginx]# ansible-playbookv00.yml--syntax-checkplaybook: v00.yml#检查语法正确

ansible-playbook执行
#ansible-playbook执行 [root@ansible ~]# cd /root/ansible/nginx[root@ansible nginx]# ansible-playbook v00.ymlPLAY [all] **************************************************************************************************************************************************TASK [Gathering Facts] ************************************************************************************************************************************** ok: [192.168.80.101] ok: [192.168.80.100]TASK [install packages] ************************************************************************************************************************************* ok: [192.168.80.100] ok: [192.168.80.101]TASK [create nginx group] *********************************************************************************************************************************** ok: [192.168.80.100] ok: [192.168.80.101]TASK [create nginx user] ************************************************************************************************************************************ ok: [192.168.80.101] ok: [192.168.80.100]TASK [create /app] ****************************************************************************************************************************************** ok: [192.168.80.101] ok: [192.168.80.100]TASK [copy nginx file to remote host /tmp/] ***************************************************************************************************************** ok: [192.168.80.101] ok: [192.168.80.100]TASK [configure make make install] ************************************************************************************************************************** changed: [192.168.80.101] changed: [192.168.80.100]TASK [create linkfile/app/nginx-1.18.0/app/nginxstate=link] ***************************************************************************************** ok: [192.168.80.100] ok: [192.168.80.101]TASK [set PATH] ********************************************************************************************************************************************* changed: [192.168.80.100] changed: [192.168.80.101]TASK [index.html] ******************************************************************************************************************************************* changed: [192.168.80.100] changed: [192.168.80.101]TASK [prepare service file] ********************************************************************************************************************************* ok: [192.168.80.100] ok: [192.168.80.101]TASK [nginx.conf] ******************************************************************************************************************************************* ok: [192.168.80.100] ok: [192.168.80.101]TASK [systemctl daemon-reload] ****************************************************************************************************************************** changed: [192.168.80.100] changed: [192.168.80.101]TASK [start service] **************************************************************************************************************************************** ok: [192.168.80.101] changed: [192.168.80.100]PLAY RECAP ************************************************************************************************************************************************** 192.168.80.100: ok=14changed=5unreachable=0failed=0skipped=0rescued=0ignored=0 192.168.80.101: ok=14changed=4unreachable=0failed=0skipped=0rescued=0ignored=0

#测试网页 [root@ansible ~]# curl http://192.168.80.100 nginx pages 192.168.80.100 node00.magedu.org[root@ansible ~]# curl http://192.168.80.101 nginx pages 192.168.80.101 node01.magedu.org

3.http的报文结构和状态码总结
#常见http 状态码200 服务器成功返回网页,这是成功的HTTP请求返回的标准状态码301 Moved Permanently 永久重定向,所请求的网页将永久跳转到被设定的新位置,例如从www.qq.com 跳转至www.baidu.com302 Moved Temporarily 临时重定向,响应报文Location指明资源临时新位置 304 Not Modified 客户端发出条件请求,但服务器上的资源未曾发生改变,则通过响应此响状态码通知客户端 401 Unauthorized 需要输入账号和密码认证方能访问资源403 Forbidden 请求被禁止,禁止访问,虽然这个请求是合法的,但是服务器端因为匹配了预先设置的规则二拒绝响应客户端的请求,此类问题一般为服务器或服务器权限配置不当所致404 Not Fount 服务器找不到客户端请求的指定页面,可能是客户但请求了服务器上不存在的资源所致500 Internal Server Error 服务器内部错误,服务器遇到了意料不到的情况,不能完成客户端的请求。是一个笼统的报错,一般为服务器的设置或程序问题导致。例如 SELINUX 开启 而又没有为HTTP 设置规则许可,客户端访问就是500502 Bad Gateway 代理服务器从后端服务器收到了一条伪响应,如无法连接到网关,一般是代理服务器请求后端服务器时候,后端服务器不可用或者没有完成响应网关服务器,这通常为反向代理服务器下面的节点出现问题所致503 服务不可用,临时服务器维护或过载,服务器无法处理请求,或者是反向代理服务器后面没有可以提供服务的节点504 网关超时,一般是网关代理服务器请求后端服务器时,后端服务没有在特定的时间内完成处理请求,多数是服务器过载导致没有在指定的时间内返回数据给前端代理服务器#HTTP状态码的命令行查看 #可以通过 curl命令(附带相关参数)在linux 命令行查看 http 响应的数字状态吗[root@ansible ~]# curl -I http://www.qq.com HTTP/1.1 302 Moved Temporarily Server: ias/1.4.2.4_1.17.3 Date: Wed, 01 Dec 2021 18:32:06 GMT Content-Type: text/html Content-Length: 151 Connection: keep-alive Location: https://www.qq.com/[root@ansible ~]# curl -Ihttps://www.qq.com HTTP/1.1 200 OK Date: Wed, 01 Dec 2021 18:34:38 GMT Content-Type: text/html; charset=GB2312 Connection: keep-alive Server: squid/3.5.24 Vary: Accept-Encoding Vary: Accept-Encoding Expires: Wed, 01 Dec 2021 18:35:38 GMT Cache-Control: max-age=60 Vary: Accept-Encoding Vary: Accept-Encoding X-Cache: HIT from shenzhen.qq.com X-Frame-Options: SAMEORIGIN Content-Security-Policy: frame-ancestors https://*.qq.com X-Content-Type-Options: nosniff


    推荐阅读