第十三周
1、ansible-playbook实现MySQL的二进制部署
1.1 安装ansible
# yum -y install ansible
1.2 配置主机清单文件
# vi /etc/ansible/hosts
[local]
10.0.0.7 ansible_connection=local #指定连接类型为本地,无需通过ssh连接
[mysql]
10.0.0.17
10.0.0.27
10.0.0.37
1.3 mysql配置文件
# cat /apps/mysql/my.cnf
[mysqld]
user=mysql
datadir=/data/mysql
socket=/data/mysql/mysql.sock
innodb_file_per_table=on
skip_name_resolve = on #禁止主机名解析,建议使用
[client]
port=3306
socket=/data/mysql/mysql.sock
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/data/mysql/mysql.pid
2.1 使用脚本实现
# bash ssh_key.sh
Generating public/private rsa key pair.
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:laHw87m60FI35AeBGdv5NhU8PW4Ol77WFPssLZK+LEY root@7-1
The key's randomart image is:
+---[RSA 2048]----+
| . .+o ... |
| oo+ = oo.|
| = B o.o|
| * + o * |
| S * = * o|
| o .E= . +.|
| o ... . ++|
| o .o.o oo=|
| oo o+o.o |
+----[SHA256]-----+
sshpass-1.06-2.el7.x86_64
/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
Number of key(s) added: 1
Now try logging into the machine, with: "ssh -o 'StrictHostKeyChecking=no' '10.0.0.7'"
and check to make sure that only the key(s) you wanted were added.
ssh: connect to host 10.0.0.3 port 22: Connection refused
lost connection
ssh: connect to host 10.0.0.3 port 22: Connection refused
lost connection
known_hosts 100% 1195 619.5KB/s 00:00
known_hosts 100% 1195 1.1MB/s 00:00
known_hosts 100% 1195 604.3KB/s 00:00
known_hosts 100% 1195 1.8MB/s 00:00
known_hosts 100% 1195 1.6MB/s 00:00
known_hosts 100% 1195 1.5MB/s 00:00
known_hosts
2.2 ssh健康性检查
# ansible mysql -m ping
10.0.0.7 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
10.0.0.17 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
10.0.0.37 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
10.0.0.27 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
3、批量安装mysql
# cat install-bin-mysql5.6.yml
---
# 批量安装二进制mysql5.6
# 将配置文件my.cnf放到目录/apps/mysql下
- hosts: mysql
remote_user: root
gather_facts: no
tasks:
- name: install packages
yum : name=libaio,perl-Data-Dumper,autoconf state=installed
- name: create group mysql
group:
name: mysql
gid: 306
system: yes
- name: create user mysql
user:
name: mysql
uid: 306
group: mysql
shell: /sbin/nologin
system: yes
home: /data/mysql
- name: download mysql_file
unarchive :
src: "http://mirrors.163.com/mysql/Downloads/MySQL-5.6/mysql-5.6.51-linux-glibc2.12-x86_64.tar.gz"
dest: "/usr/local"
owner: root
remote_src: yes
- name: prepare Soft links
shell: ln -s mysql-5.6.51-linux-glibc2.12-x86_64/ mysql
args:
chdir: "/usr/local"
- name: bash mysql_instll_db
shell: ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql/
args:
chdir: "/usr/local/mysql"
- name: prepare my.cnf
copy:
src: "/apps/mysql/my.cnf"
dest: "/etc/my.cnf"
- name: prepare service file
shell: cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld && chkconfig --add mysqld && chkconfig mysqld on
- name: add path
shell: echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh && . /etc/profile.d/mysql.sh
- name: start mysql
shell: service mysqld start
# ansible-playbook --syntax-check install-bin-mysql5.6.yml #检查语法
# ansible-playbook install-bin-mysql5.6.yml #运行
PLAY [mysql] ******************************************************************************
TASK [install packages] *******************************************************************
changed: [10.0.0.17]
changed: [10.0.0.37]
changed: [10.0.0.27]
TASK [create group mysql] *****************************************************************
changed: [10.0.0.17]
changed: [10.0.0.27]
changed: [10.0.0.37]
TASK [create user mysql] ******************************************************************
changed: [10.0.0.37]
changed: [10.0.0.17]
changed: [10.0.0.27]
TASK [download mysql_file] ****************************************************************
changed: [10.0.0.17]
changed: [10.0.0.27]
changed: [10.0.0.37]
TASK [prepare Soft links] *****************************************************************
[WARNING]: Consider using the file module with state=link rather than running 'ln'. If
you need to use command because file is insufficient you can add 'warn: false' to this
command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [10.0.0.27]
changed: [10.0.0.37]
changed: [10.0.0.17]
TASK [bash mysql_instll_db] ***************************************************************
changed: [10.0.0.37]
changed: [10.0.0.27]
changed: [10.0.0.17]
TASK [prepare my.cnf] *********************************************************************
changed: [10.0.0.37]
changed: [10.0.0.17]
changed: [10.0.0.27]
TASK [prepare service file] ***************************************************************
changed: [10.0.0.17]
changed: [10.0.0.27]
changed: [10.0.0.37]
TASK [add path] ***************************************************************************
changed: [10.0.0.27]
changed: [10.0.0.17]
changed: [10.0.0.37]
TASK [start mysql] ************************************************************************
[WARNING]: Consider using the service module rather than running 'service'. If you need
to use command because service is insufficient you can add 'warn: false' to this command
task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [10.0.0.37]
changed: [10.0.0.17]
changed: [10.0.0.27]
PLAY RECAP ********************************************************************************
10.0.0.17 : ok=10 changed=10 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.0.0.27 : ok=10 changed=10 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.0.0.37 : ok=10 changed=10 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2、Ansible playbook实现apache批量部署,并对不同主机提供以各自IP地址为内容的index.html
[root@ansible-server ansible]# tree roles/
roles/
└── httpd
├── files
│ └── httpd.conf
├── handlers
│ └── main.yml
├── tasks
│ ├── config.yml
│ ├── group.yml
│ ├── index.yml
│ ├── install.yml
│ ├── main.yml
│ ├── service.yml
│ └── user.yml
└── templates
└── index.html
[root@ansible-server httpd]# cat tasks/main.yml
- include: group.yml
- include: user.yml
- include: install.yml
- include: config.yml
- include: index.yml
- include: service.yml
[root@ansible-server httpd]# cat tasks/group.yml
- name: create group apache
group: name=apache system=yes gid=80
[root@ansible-server httpd]# cat tasks/user.yml
- name: create user apache
user: name=apache system=yes shell=/sbin/nologin home=/var/www uid=80 group=apache
[root@ansible-server httpd]# cat tasks/install.yml
- name: install httpd
yum: name=httpd state=present
[root@ansible-server httpd]# cat tasks/config.yml
- name: config file
copy: src=httpd.conf dest=/etc/httpd/conf/ backup=yes
notify: restart
[root@ansible-server httpd]# cat tasks/index.yml
- name: index.html
template: src=index.html dest=/var/www/html/
[root@ansible-server httpd]# cat tasks/service.yml
- name: start service
service: name=httpd state=started enabled=yes
[root@ansible-server httpd]# cat templates/index.html
MY ADDRESS IS {{ ansible_eth0.ipv4.address }}
[root@ansible-server httpd]# cat handlers//main.yml
- name: restart
service: name=httpd state=restarted
[root@ansible-server ansible]# ansible-playbook role_httpd.yml

3、http的报文结构和状态码总结
1.请求报文结构
包括报文首部、空行、报文主体3部分。
报文首部: 第一行:请求行,请求方法,请求路径,HTTP版本 后续为各个首部:包括请求首部字段、通用首部字段和实体首部字段
空行:
报文主体: 向服务器发送的数据。如get请求中的各个参数。post请求中的参数。
2.响应报文结构
也是包括报文首部、空行、报文主体3部分。
报文首部: 第一行:状态行,包括HTTP版本 状态码 原因短语 后续为首部字段:响应首部字段、通用首部字段、实体首部字段
报文主体:服务器返回的响应体。如HTTM页面。
3.常见状态码
4.1 2xx
2开头的状态码表示成功
200 OK
正常处理并返回了
204 No Content
正常处理了,但响应中不含主体。 用于需要从客户端往服务器发送数据但不需要响应内容的情况。
206 Partial Content
客户端进行了范围请求,服务器正常返回了。请求时通过Content-Range指定范围。
4.2 3xx
重定向相关
301 Moved Permanently
永久性重定向。表示请求的资源已经永久性分配了新的URI,以后应该使用该新的URI。 使用Location首部字段表示新URI地址。浏览器会重新请求一次该URI。
302 Found
临时重定向,希望用户本次使用的新分配的URI。 和301非常类似,浏览器也会根据Location字段重新进行请求。 在实际开发中常用于页面跳转。
303 See Other
和302功能相同,只是明确表明客户端应该使用get请求。
304 Not Modified
和重定向没有关系。表示资源没有改变,可直接使用客户端未过期的缓存。在请求附带条件时有可能返回这个状态码。
4.3 4xx
客户端错误
400 Bad Request
请求中有语法错误。如参数拼接的的问题等。
401 Unauthorized
未认证
403
禁止访问
404 Not Found
4.4 5xx
服务器错误
500
服务器内部错误
503
服务不可用
浙公网安备 33010602011771号