1、ansible-playbook实现MySQL的二进制部署
安装ansible并配置
[root@ansible ~]#yum -y install ansible
[root@ansible ~]#vim /etc/ansible/hosts
[dbservers]
10.0.0.18
[root@ansible ~]#ansible dbservers --list-hosts
hosts (1):
10.0.0.18
[root@ansible ~]#ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:LqM4c9HgY483iuh1Qrvit92VT9c17QuRh96phKjLKmk root@ansible
The key's randomart image is:
+---[RSA 3072]----+
| |
| |
| |
| . o .|
| o o S + oo|
| . * .. o o =.+|
| =.o .+ o = +.|
| o+EB.oo o o o .|
|+o**==.*. . . . |
+----[SHA256]-----+
[root@ansible ~]#
[root@ansible ~]#ssh-copy-id 10.0.0.18:
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@10.0.0.18's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '10.0.0.18'"
and check to make sure that only the key(s) you wanted were added.
创建二进制安装所需的文件
[root@ansible ~]#mkdir -p /data/ansible/files
[root@ansible ~]#ll /data/ansible/files/
total 473708
-rw-r--r-- 1 root root 485074552 Jul 30 16:48 mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz
[root@ansible ~]#vim /data/ansible/files/my.cnf
[mysqld]
datadir=/data/mysql
skip_name_resolve=1
socket=/tmp/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
port=3306
socket=/tmp/mysql.sock
[mysqld_safe]
vim /data/ansible/files/secure_mysql.sh
!/bin/bash
!/bin/bash
passwd=grep "temporary password" /data/mysql/mysql.log|sed -nr 's/^.*\: (.*)$/\1/p'
mysqladmin -uroot -pecho $passwd password magedu
expect <<EOF
spawn /usr/local/mysql/bin/mysql_secure_installation
expect {
"Enter password for user root:" {send magedu\n;exp_continue}
"Press y|Y for Yes, any other key for No:" {send n\n;exp_continue}
"Press y|Y for Yes, any other key for No" {send y\n;exp_continue}
"Press y|Y for Yes, any other key for No" {send y\n;exp_continue}
"Press y|Y for Yes, any other key for No" {send y\n;exp_continue}
"Press y|Y for Yes, any other key for No" {send y\n;exp_continue}
}
expect eof
EOF
[root@ansible ~]#tree /data/ansible/files/
/data/ansible/files/
├── my.cnf
├── mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz
└── secure_mysql.sh
0 directories, 3 files
[root@ansible ~]#vim /data/ansible/install_mysql.yml
insatll mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz
-
hosts: dbservers
remote_user: root
gather_facts: notasks:
- name: istall packages
yum: name=mysql,libaio,perl-Data-Dumper,perl-Getopt-Long,expect,ncurses-compat-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 home=/data/mysql - name: copy tar to remote host and file mode
unarchive: src=/data/ansible/files/mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz dest=/usr/local/ owner=root group=root - name: create linkfile /usr/local/mysql
file: src=/usr/local/mysql-8.0.19-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link - name: PATH variable
shell: echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh;source /etc/profile.d/mysql.sh - name: config my.cnf
copy: src=/data/ansible/files/my.cnf dest=/etc/my.cnf - name: data dir
shell: mysqld --initialize --user=mysql --datadir=/data/mysql
tags: data - name: service script
shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld - name: enable service
shell: /etc/init.d/mysqld start;chkconfig --add mysqld;chkconfig mysqld on
tags: service - name: secure script
script: /data/ansible/files/secure_mysql.sh
tags: script
- name: istall packages
执行mysql.yml
[root@ansible ~]#ansible-playbook /data/ansible/install_mysql.yml
PLAY [dbservers] ***************************************************************************
TASK [istall packages] *********************************************************************
changed: [10.0.0.18]
TASK [create mysql group] ******************************************************************
changed: [10.0.0.18]
TASK [create mysql user] *******************************************************************
changed: [10.0.0.18]
TASK [copy tar to remote host and file mode] ***********************************************
changed: [10.0.0.18]
TASK [create linkfile /usr/local/mysql] ****************************************************
changed: [10.0.0.18]
TASK [PATH variable] ***********************************************************************
changed: [10.0.0.18]
TASK [config my.cnf] ***********************************************************************
changed: [10.0.0.18]
TASK [data dir] ****************************************************************************
changed: [10.0.0.18]
TASK [service script] **********************************************************************
changed: [10.0.0.18]
TASK [enable service] **********************************************************************
changed: [10.0.0.18]
TASK [secure script] ***********************************************************************
changed: [10.0.0.18]
PLAY RECAP *********************************************************************************
10.0.0.18 : ok=11 changed=11 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
验证被管理端结果
[root@dbserver ~]#mysql -uroot -pmagedu
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.19 MySQL Community Server - GPL
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
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、安装ansible
root@centos7 ~]# yum install -y ansible
2、配置/etc/ansible/hosts文件
[root@centos7 ~]# vi /etc/ansible/hosts
[web_server]
10.0.0.8
10.0.0.18
3、配置基于key验证
[root@Centos8 script]# cat host.list
10.0.0.8
10.0.0.18
[root@Centos7 script]#
[root@Centos7 script]# cat push_key.sh
rpm -q sshpass &> /dev/null || yum -y install sshpass
[ -f /root/.ssh/id_rsa ] || ssh-keygen -f /root/.ssh/id_rsa -P ''
export SSHPASS=Centos
for IP in cat host.list;do
sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $IP
done;
[root@Centos7 script]# bash push_key.sh
4、编写palybook
[root@centos7 ansible]# vi install_httpd.yml
install httpd
-
hosts: web_server
remote_user: root
gather_facts: notasks:
- name: Install httpd
yum: name=httpd - name: modify index.html
shell: echohostname -I> /var/www/html/index.html - name: start service
service: name=httpd state=started enabled=yes
- name: Install httpd
5、应用palybook
[root@centos7 ansible]# ansible-playbook install_httpd.yml
PLAY [web_server] *************************************************************************************************************************************
TASK [Install httpd] **********************************************************************************************************************************
changed: [10.0.0.8]
changed: [10.0.0.18]
TASK [modify index.html] ******************************************************************************************************************************
changed: [10.0.0.18]
changed: [10.0.0.8]
TASK [start service] **********************************************************************************************************************************
changed: [10.0.0.18]
changed: [10.0.0.8]
PLAY RECAP ********************************************************************************************************************************************
10.0.0.18 : ok=3 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.0.0.8 : ok=3 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
3、http的报文结构和状态码总结
1.在TCP/IP协议簇中的位置
位于四层协议的应用层。基于运输层的TCP协议实现。
2.请求报文结构
包括报文首部、空行、报文主体3部分。
报文首部:
第一行:请求行,请求方法,请求路径,HTTP版本
后续为各个首部:包括请求首部字段、通用首部字段和实体首部字段
空行:=
报文主体:
向服务器发送的数据。如get请求中的各个参数。post请求中的参数。
3.响应报文结构
也是包括报文首部、空行、报文主体3部分。
报文首部:
第一行:状态行,包括HTTP版本 状态码 原因短语
后续为首部字段:响应首部字段、通用首部字段、实体首部字段
报文主体:服务器返回的响应体。如HTTM页面。
4.常见状态码
(1) 2xx
2开头的状态码表示成功
200 OK
正常处理并返回了
204 No Content
正常处理了,但响应中不含主体。
用于需要从客户端往服务器发送数据但不需要响应内容的情况。
206 Partial Content
客户端进行了范围请求,服务器正常返回了。请求时通过Content-Range指定范围。
(2)3xx
重定向相关
301 Moved Permanently
永久性重定向。表示请求的资源已经永久性分配了新的URI,以后应该使用该新的URI。
使用Location首部字段表示新URI地址。浏览器会重新请求一次该URI。
302 Found
临时重定向,希望用户本次使用的新分配的URI。
和301非常类似,浏览器也会根据Location字段重新进行请求。
在实际开发中常用于页面跳转。
303 See Other
和302功能相同,只是明确表明客户端应该使用get请求。
304 Not Modified
和重定向没有关系。表示资源没有改变,可直接使用客户端未过期的缓存。在请求附带条件时有可能返回这个状态码。
(4)4xx
客户端错误
400 Bad Request
请求中有语法错误。如参数拼接的的问题等。
401 Unauthorized
未认证
403
禁止访问
404 Not Found
(5)5xx
服务器错误
500
服务器内部错误
503
服务不可用
浙公网安备 33010602011771号