一、回顾
1、mysql和python
(1)mysql5.7
1.1不需要执行mysql_ssl_rsa_setup
1.2change_master_to 不需要get public key
(2)可以使用pymysql非交互的管理mysql
2.1pymysql.connect(host,user,password,database,port)
2.2 cursor=conn.cursor()
2.3 cursor.execute("create user....")
2.4 cursor.execute("grant 权限 on 库名.表名 to 用户")
2.5 conn.commit()
2.6 cursor.fetchall()
2、mycat中间件
(1)独属于mysql主从负载均衡策略
(2)配置写主读从
(3)步骤
3.1 安装jdk mycat
bash
tar -xf jdk_8u192.tar.gz
cp jdk/ /usr/local/jdk
sed -i '$aexport $JAVA_HOME=/usr/local/jdk' /etc/profile
sed -i '$aexport $PATH=$PATH:$JAVA_HOME/bin' /etc/profile
java -version
javac -version
tar -xf Mycat.tar.gz
cp mycat/ /usr/local/
#测试启动
/usr/local/mycat/bin/mycat console
3.2 配置
1. server.xml
python
[root@localhost ~]# vim /usr/local/mycat/conf/server.xml
93 <user name="lxe" defaultAccount="true">
94 <property name="password">lxe</property>
95 <property name="schemas">test</property>
2. schema.xml
python
[root@localhost ~]# vim /usr/local/mycat/conf/schema.xml
5 <schema name="test" dataNode="dn1" checkSQLschema="false" sqlMaxLimit="100">
6 </schema>
7 <dataNode name="dn1" dataHost="localhost1" database="test" />
18 <writeHost host="hostM1" url="192.168.8.176:3306" user="lxe"
19 password="lxe">
20 <!-- can have multi read hosts -->
21 <readHost host="hostS2" url="192.168.8.177:3306" user="lxe" password="lxe" /> 22 </writeHost>
(4)启动和调试
4.1. /usr/local/mycat/bin/mycat start
4.2. Netstat -Input|grep 8066
4.3. mysql-hmycat的ip或者域名-P8066-userver.xml中填入账号-p在service.xml中填入的密码
4.4. cat /usr/local/mysql/logs/wrapper.log
- cause by.....
二、运维自动化(ansible)
1、任务背景
公司的服务器越来越多,维护一些简单的事情都会变得很繁琐。用shell
脚本来管理少量服务器效率还行,服务器多了之后,shell脚本无法实现
高效率运维。这种情况下,我们需要引入自动化运维工具,对多台服务
器实现高效运维。
2、任务要求
通过管理服务器能够按照需求灵活高效地管理所有应用服务器的运维操作
3、任务拆解
需要r-台服务器做管理端,来连接管理所有的应用服务器
考虑如果只针对一部分应用服务器进行运维操作如何实现(服务器分组)
学会将平台烂熟于心的linux操作命令转化为自动化运维的方式(常见模块的学习)
如果操作非常的冗长,学会使用playbook和role的方式来管理
4、学习目标
能够安装ansible服务器和客户端
能够定义ansible主机清单进行服务器分组
能够使用hostname模块修改主机名
能够使用file模块做基本的文件操作
能够使用copy模块把文件拷贝到远程机器
能够使用fetch模块把文件从远程拷贝到本地
能够使用user模块管理用户
能够使用group模块管理用户组
能够使用cron模块管理时间任务
能够使用yum_repository模块配置yum
能够使用yum模块安装软件包
能够使用service模块控制服务的启动,关闭,开机自启动
能够使用script模块在远程机器上执行本地脚本
能够使用command与shell模块远程执行命令
能够编写playbook实现httpd
能够使用roles实现lamp
三、认识自动化运维
假设我要去1000台服务上做一个操作(如nginx服务器修改配置文件里的某一个参数),下面两种方法缺点明显:
1. 按传统的方法,一台连着一台服务器的ssh上去手动操作
缺点:效率太低。
2. 写个shell脚本来做
缺点:
管理的机器平台不一致,脚本可能不具备通用性。
传密码麻烦(在非免密登录的环境下,需要expect来传密码)
效率较低,循环1000次也需要一个一个的完成,如果用&符放到后台执行,则会产生1000个进程。
自动化运维:将日常IT运维中大量的重复性工作,小到简单的日常检查、配置变更和软件安装,大到整个变更流程的组织调度,由过去的手工执行转为自动化操作,从而减少乃至消除运维中的延迟,实"零延时"的IT运维。
3、自动化运维主要关注的方面
假如管理很多台服务器,主要关注以下几个方面:
管理机与被管理机的连接(管理机如何将管理指令发送给被管理机)
服务器信息收集(如果被管理的服务器有centos7.5外还有其它linux发行版,如suse,ubuntu等。当你要做的事情在不同OS上有所不同,你需要收集信息,并将其分开处理)
服务器分组(因为有些时候我要做的事情不是针对所有服务器,可能只针对某一个分组)|I
管理内容的主要分类
■ 文件目录管理(包括文件的创建,删除,修改,查看状态,远程拷贝等)
■ 用户和组管理
■ cron时间任务管理
■ yum源配置与通过yum管理软件包
■ 服务管理
■ 远程执行脚本
■ 远程执行命令
4、常见的开源自动化运维工具比较
1. puppet(拓展)
基于ruby语言,成熟稳定。适合于大型架构,相对于ansible和saltstack会复杂些。
2. saltstack(拓展)
基于python语言。相对简单,大并发能力比ansible要好,需要维护被管理端的服务。如果服务断开,连接就会出问题。
3. ansible
基于python语言。简单快捷,被管理端不需要启服务。直接走ssh协议,需要验证所以机器多的话速度会较慢。
四、ansible
ansible是一种由Python开发的自动化运维工具,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
1、特点:
■ 部署简单
■ 默认使用ssh进行管理,基于python里的paramiko模块开发
■ 管理端和被管理端不需要启动服务
■ 配置简单,功能强大,扩展性强
■能过playbook(剧本)进行多个任务的编排
2、安装ansible(配置四台主机 m0 s0 s1 s2)
(1)在m0主机安装ansible
root@m0 \~\]# yum -y install epel-release \[root@m0 \~\]# yum -y install ansible \[root@m0 \~\]# ansible --version \[root@m0 \~\]# rpm -ql ansible \[root@m0 \~\]# find /etc/ -name "\*ansible\*" /etc/ansible /etc/ansible/ansible.cfg
(2)实现master对agent的免密登录,只在master上做。(如果这一步不做,则在后面操作agent时都要加-k参数传密码;或者在主机清单里传密码)
root@m0 \~\]# ssh-keygen \[root@m0 \~\]# ls ./.ssh/ id_rsa id_rsa.pub \[root@m0 \~\]# cat ./.ssh/id_rsa.pub \[root@m0 \~\]# ssh-copy-id -i 192.168.8.181 \[root@m0 \~\]# ssh-copy-id -i 192.168.8.182
(3)在master上定义主机组,并测试连接
root@m0 \~\]# vim /etc/ansible/hosts ```python [group01] 192.168.8.181 192.168.8.182 [group02] 192.168.8.181 192.168.8.182 192.168.8.183 ``` \[root@m0 \~\]# ansible 192.168.8.181 -m ping ```python 192.168.8.181 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } ``` \[root@m0 \~\]# ansible group01 -m ping ```python 192.168.8.181 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } 192.168.8.182 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } ``` \[root@m0 \~\]# ansible group02 -m ping #192.168.8.183没有免密,报错 \[root@m0 \~\]# vim /etc/ansible/hosts #重新修改文件 ```python [group01] 192.168.8.181 192.168.8.182 other ansible_ssh_host=192.168.8.183 ansible_ssh_port=22 ans ible_ssh_user=root ansible_ssh_pass=1 [group02] 192.168.8.181 192.168.8.182 other ``` \[root@m0 \~\]# ansible group02 -m ping #不报错了 \[root@m0 \~\]# ansible other -m ping #单独也是成功的 ```python other | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } ```
主机清单的作用:服务器分组。
主机清单的常见功能:
-
可以通过IP范围来分,主机名名字的范围来分
-
如果ssh端口不是22的,可以传入新的端口。
-
没有做免密登录,可以传密码。
练习:不论你用哪种环境(免密或不免密,端口是否22),请最终将两台被管理机器加入到group1组即可
pythonvim /etc/ansible/hosts web01 ansible_ssh_host=192.168.8.X ansible_ssh_user=root ansible_ssh_pass=1 ansible_ssh_port=22 web01 ansible_ssh_host=192.168.8.X ansible_ssh_user=root ansible_ssh_pass=1 ansible_ssh_port=22 [group01] web01 web02
pythonansible 主机IP|域名|组名|别名 -m ping|copy|... '参数'
3、ansible模块
ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。
ansible支持的模块非常的多,我们并不需要把每个模块都记住,而只需要熟悉一些常见的模块,其它的模块在需要用到时再查询即可。
(1)查看所有支持的模块
root@m0 \~\]# ansible-doc -l
(2)hostname 模块
用于修改主机名称
root@m0 \~\]# ansible group02 -m hostname -a 'name=ab.lxe.er' #将group02组中的主机名称分别改为ab lxe er
(3)file模块(重点)
file模块用于对文件相关的操作(创建,删除,软硬钟接等)
3.1 创建文件
root@m0 \~\]# ansible group01 -m file -a 'path=/tmp/abc state=directory' #在group01组中的主机中创建目录abc \[root@s0 \~\]# ls /tmp/ abc \[root@s1 \~\]# ls /tmp/ abc \[root@s2 \~\]# ls /tmp abc \[root@m0 \~\]# ansible group02 -m file -a 'path=/tmp/abc/def state=touch' \[root@s0 \~\]# ls /tmp/abc/ def ###### 3.2 递归修改 \[root@m0 \~\]# ansible group02 -m file -a'path=/tmp/abc recurse=yes owner=bin group=daemon mode=1777' #777是权限 \[root@s0 \~\]# ll /tmp/abc/def -rwxrwxrwt 1 bin daemon 0 8月 16 14:08 /tmp/abc/def ###### 3.3 删除目录(连同目录里的所有文件) \[root@m0 \~\]# ansible group02 -m file -a 'path=/tmp/abc state=absent' #ansent 删除 ###### 3.4 创建文件 (指定group mode owner...) \[root@m0 \~\]# ansible group02 -m file -a 'path=/tmp/aaaa state=touch owner=bin group=daemon mode=1777' \[root@s0 \~\]# ll /tmp/ -rwxrwxrwt 1 bin daemon 0 8月 16 14:21 aaaa ###### 3.5 删除 \[root@m0 \~\]# ansible group02 -m file -a 'path=/tmp/aaaa state=absent' ###### 3.6 创建软链接文件 \[root@m0 \~\]# ansible group02 -m file -a 'src=/etc/fstab path=/tmp/xxx state=link' \[root@s0 \~\]# ll /tmp/ lrwxrwxrwx 1 root root 10 8月 16 14:28 xxx -\> /etc/fstab ###### 3.7 创建硬链接文件 #硬链接指向文件,软链接指向硬链接 \[root@m0 \~\]# ansible group02 -m file -a 'src=/etc/fstab path=/tmp/xxx2 state=hard' \[root@s0 \~\]# ll /tmp/ -rw-r--r--. 2 root root 502 7月 23 09:12 xxx2 #path 文件的地址 #state 方法 #directory 创建目录 #touch 创建文件 #absent 删除文件 #link 创建软链接 #hard 创建硬链接 #recurse 是否允许递归操作 #src 文件源
(4)stat模块
root@m0 \~\]# ansible group02 -m stat -a 'path=/etc/fstab'
(5)copy模块(重点)
copy模块用于对文件的远程拷贝操作(如把本地的文件拷贝到远程的机器上)
root@m0 \~\]# mv mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz my57.tar.gz \[root@m0 \~\]# ansible group02 -m copy -a 'src=./my57.tar.gz dest=\~' \[root@s0 \~\]# ls my57.tar.gz \[root@m0 \~\]# dd if=/dev/zero of=./test bs=100M count=1 ###### 5.1 使用content直接向远程主机文件写入内容(会覆盖原内容) \[root@m0 \~\]# ansible group02 -m copy -a 'content="wo ha ha" dest=\~/test' \[root@s0 \~\]# cat test wo ha ha ###### 5.2 使用backup 备份 copy 拷贝加/是整个目录,不加只同步目录中内容 \[root@m0 \~\]# ansible group02 -m copy -a 'src=./test dest=\~ backup=yes owner=bin group=daemon mode=1777' \[root@s0 \~\]# ls -lh -rwxrwxrwt 1 bin daemon 100M 8月 16 15:26 test -rw-r--r-- 1 root root 8 8月 16 15:20 test.12559.2024-08-16@15:26:07\~ \[root@m0 \~\]# ansible group02 -m file -a 'path=/tmp/a.txt state=touch' \[root@m0 \~\]# ansible group02 -m copy -a 'src=/etc/fstab desc=/tmp/a.txt backup=yes owner=bin group=daemon mode=1777' \[root@s0 \~\]# ls /tmp/ a.txt a.txt.12737.2024-08-16@15:30:53\~ ###### 5.3 连同目录一起复制 \[root@m0 \~\]# ansible group02 -m copy -a 'src=/etc/yum.repos.d dest=/etc/yum.repos.d/ backup=yes' \[root@s0 \~\]# ls /etc/yum.repos.d CentOS-Base.repo dd.repo yum.repos.d ###### 5.4 复制目录下文件 \[root@m0 \~\]# ansible group02 -m copy -a 'src=/etc/yum.repos.d/ dest=/etc/yum.repos.d/ backup=yes' \[root@s0 \~\]# ls /etc/yum.repos.d/ CentOS-Base.repo epel.repo yum.repos.d dd.repo epel-testing.repo
(6)fetch模块
fetch模块与copy模块类似,但作⽤相反。⽤于把远程机器的⽂件拷 ⻉到本地
root@m0 \~\]# ansible group02 -m fetch -a 'src=/etc/sysconfig/network-scripts/ifcfg-ens33 dest=/tmp' \[root@m0 \~\]# ls /tmp/ 192.168.8.181 192.168.8.182 other
(7)user模块
user模块⽤于管理⽤户账号和⽤户属性。
7.1 创建aaa⽤户,默认为普通⽤户,创建家⽬录
root@m0 \~\]# ansible group02 -m user -a 'name=aaa state=present' \[root@s0 \~\]# grep aaa /etc/passwd aaa:x:1000:1000::/home/aaa:/bin/bash ###### 7.2 创建mysql系统⽤户,并且登录shell环境为/sbin/nologin \[root@m0 \~\]# ansible group02 -m user -a 'name=mysql state=present system=yes shell="/sbin/nologin"' \[root@s0 \~\]# grep mysql /etc/passwd mysql:x:997:995::/home/mysql:/sbin/nologin ###### 7.3 创建文件 指定属主 组 权限 \[root@m0 \~\]# ansible group02 -m file -a 'path=/usr/local/mysql/mysql-files state=directory owner=mysql group=mysql mode=750' \[root@s0 \~\]# ll /usr/local/mysql/ drwxr-x--- 2 mysql mysql 6 8月 16 16:10 mysql-files ###### 7.4 创建abc⽤户, 使⽤uid参数指定uid, 使⽤password参数传密码 \[root@m0 \~\]# ansible group02 -m user -a 'name=abc state=present uid=1999 password=abc' \[root@s0 \~\]# grep abc /etc/passwd abc:x:1999:1999::/home/abc:/bin/bash ###### 7.5 创建⼀个普通⽤户叫hadoop,并产⽣空密码密钥对 \[root@m0 \~\]# ansible group02 -m user -a 'name=hadoop generate_ssh_key=yes' \[root@s0 \~\]# ls ./.ssh/ authorized_keys \[root@s0 \~\]# cat ./.ssh/authorized_keys ###### 7.6 删除⽤户,但家⽬录默认没有删除 \[root@m0 \~\]# ansible group02 -m user -a 'name=hadoop state=absent' \[root@m0 \~\]# ansible group02 -m user -a 'name=abc state=absent' \[root@s0 \~\]# ll /home/ drwx------ 2 aaa aaa 62 8月 16 16:03 aaa drwx------ 2 1999 1999 62 8月 16 16:16 abc drwx------ 3 2000 2000 74 8月 16 16:20 hadoop drwx------ 2 mysql mysql 62 8月 16 16:06 mysql ###### 7.7 删除bbb⽤户,使⽤remove=yes参数让其删除⽤户的同时也删除家⽬ 录 \[root@m0 \~\]# ansible group02 -m user -a 'name=mysql state=absent remove=yes' \[root@s0 \~\]# ll /home/ drwx------ 2 aaa aaa 62 8月 16 16:03 aaa drwx------ 2 1999 1999 62 8月 16 16:16 abc drwx------ 3 2000 2000 74 8月 16 16:20 hadoop
(8)cron模块
cron模块⽤于管理周期性时间任务
root@m0 \~\]# crontab -e ```python */1 * * * * echo $( date ) >>~/data.txt ``` \[root@m0 \~\]# ansible group02 -m cron -a 'name="abc" user=root job="/usr/sbin/ntpdate cn.ntp.org.cn" hour=2' \[root@s0 \~\]# crontab -l #Ansible: abc \* 2 \* \* \* /usr/sbin/ntpdate cn.ntp.org.cn
(9)yum模块(重点)
yum模块⽤于使⽤yum命令来实现软件包的安装与卸载。
root@m0 \~\]# ansible group02 -m yum -a 'name=ntpdate state=present' \[root@m0 \~\]# ansible group02 -m yum -a 'name=tree state=present' \[root@s0 \~\]# ntpdate cn.ntp.org.cn 16 Aug 16:36:33 ntpdate\[14239\]: adjust time server 120.197.116.202 offset 0.019423 sec
service模块⽤于控制服务的启动,关闭,开机⾃启动等。
root@m0 \~\]# ansible group02 -m service -a 'name=firewalld state=stopped enabled=false' ###### 关机 \[root@m0 \~\]# ansible group02 -m command -a 'shutdown -h 0'