上午
1
、
roles
(角色)介绍
roles(
⻆⾊
):
就是通过分别将
variables, tasks
及
handlers
等放置于单独 的⽬录中
,
并可以便捷地调⽤
它们的⼀种机制。
假设我们要写⼀个
playbook
来安装管理
lamp
环境,那么这个
playbook
就会写很⻓。所以我们希望
把这个很⼤的⽂件分成多个功能 拆分
,
分成
apache
管理
,php
管理
,mysql
管理,然后在需要使⽤的时候 直
接调⽤就可以了,以免重复写。就类似编程⾥的模块化的概念,以 达到代码复⽤的效果。
2
、
roles
目录介绍
files
:
⽤
来存放由
copy
模块或
script
模块调
⽤
的
⽂
件。
tasks
:
⾄
少有
⼀
个
main.yml
⽂
件,定义各
tasks
。
handlers:
有
⼀
个
main.yml
⽂
件,定义各
handlers
。
templates
:
⽤
来存放
jinjia2
模板。
vars
:有
⼀
个
main.yml
⽂
件,定义变量。
meta
:有
⼀
个
main.yml
⽂
件,定义此
⻆⾊
的特殊设定及其依赖关系。
注意
:
在每个⻆⾊的⽬录中分别创建
files, tasks,handlers,templates,vars
和
meta
⽬录,⽤不到的⽬
录可以创建为空⽬录。
3
、使用
roles
安装
httpd
和
mysql
1
)创建
roles
角色基础目录与文件
[root@1 ~]
# cd /etc/ansible/roles/
[root@1 roles]
# mkdir {httpd,mysql}
[root@1 roles]
# mkdir {httpd,mysql}/{tasks,handlers,vars,meta}
[root@1 roles]
# touch {httpd,mysql}/{tasks,handlers,vars,meta}/main.yml
[root@1 roles]
# tree
.
├── httpd
│ ├── files
│ ├── handlers
│ │ └── main.yml
│ ├── meta
│ │ └── main.yml
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ └── vars
│ └── main.yml
├── mysql
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
2
)创建
playbook
调用角色
[root@1 roles]
# vim /etc/ansible/playbook/test002.yml
---
-
hosts: s
remote_user: root
roles:
-
httpd
-
mysql
[root@1 roles]
# cd
[root@1 ~]
# vim /etc/ansible/roles/mysql/tasks/main.yml
---
-
name:
卸载
mysql
yum:
name
=
mysql
state
=
absent
-
name:
安装
mysql
yum:
name
=
mysql
state
=
present
[root@1 ~]
# vim /etc/ansible/roles/httpd/tasks/main.yml
---
-
name:
卸载
httpd
yum:
name
=
httpd
state
=
absent
-
name:
安装
httpd
yum:
name
=
httpd
state
=
present
-
name:
启动
httpd
service
:
name
=
httpd
state
=
started
enabled
=
yes
[root@1 ~]
# ansible-playbook /etc/ansible/playbook/test002.yml
4
、练习:使用
role
来实现
lnmp
1
、
nginx
改配置
8080
2
、
mariadb
中创建
eleme
数据库
3
、创建表
t_user
![](https://i-blog.csdnimg.cn/direct/95904618c7d949e4a14ab6a88f6921d9.png)
1
)使用剧本修改
nginx
的端口号为
80
[root@m0 ~]
# vim /etc/ansible/playbook/nginx.yml
---
-
hosts: s
remote_user: root
tasks:
-
name:
卸载
httpd
yum:
name
=
httpd
state
=
absent
-
name:
安装
nginx
yum:
name
=
nginx
state
=
present
-
name:
修改资源文件
shell:
echo
'i am nginx,port is 80'
> /usr/share/nginx/html/index.html
-
name:
修改端口
command:
sed
-i
'/listen/ s/80/8080/g'
/etc/nginx/nginx.conf
notify:
-
restart
nginx
-
name:
启动服务
service
:
name
=
nginx
state
=
started
enabled
=
yes
handlers:
-
name:
restart
nginx
service
:
name
=
nginx
state
=
restarted
...
[root@m0 ~]
# ansible-playbook /etc/ansible/playbook/nginx.yml
2
)安装
mariadb
,命令行形式创建数据库和表
[root@1 ~]
# yum -y remove *mariadb*
[root@1 ~]
# yum -y install mariadb
[root@1 ~]
# yum -y install mariadb-server.x86_64
[root@1 ~]
# yum -y install mariadb-devel.x86_64
[root@1 ~]
# systemctl start mariadb
[root@1 ~]
# mysql
MariaDB [(none)]> quit
Bye
[root@1 ~]
# mysql << EOF
> create database
if
not exists eleme charset utf8mb4;
> use eleme;
> create table user(id int primary key auto_increment,username varchar(45)
not null,password varchar(45) not null,remark varchar(45));
> quit
> EOF
[root@1 ~]
# mysql
MariaDB [(none)]> show databases;
+
--------------------
+
| Database |
+
--------------------
+
| information_schema |
| eleme |
| mysql |
| performance_schema |
| test |
+
--------------------
+
5
rows
in set
(0.00 sec)
MariaDB [test]> use eleme;
Database changed
MariaDB [eleme]> show tables;
+
-----------------
+
| Tables_in_eleme |
+
-----------------
+
| user |
+
-----------------
+
1
row
in set
(0.00 sec)
3
)使用
roles
安装
mariadb
并执行脚本创建数据库和表
[root@1 ~]
# cd /etc/ansible/roles/
[root@1 roles]
# cp -r httpd mariadb
[root@1 roles]
# cd mariadb/
[root@1 mariadb]
# ls
files handlers meta tasks templates vars
[root@1 mariadb]
# vim tasks/main.yml
---
-
name:
卸载
mariadb
yum:
name
=
mariadb,mariadb-server,mariadb-devel
state
=
absent
-
name:
安装
mariadb
yum:
name
=
mariadb,mariadb-server,mariadb-devel
state
=
present
-
name:
启动
mariadb
service
:
name
=
mariadb
state
=
started
enabled
=
yes
-
name:
执行脚本,创建数据库和数据表
script: /etc/ansible/script/dbtable.sh
[root@1 mariadb]
# cd /etc/ansible/
[root@1 ansible]
# mkdir script
[root@1 ansible]
# cd script/
[root@1 script]
# vim dbtable.sh
#!/bin/bash
mysql << EOF
create database
if
not exists eleme charset utf8mb4;
use eleme;
create table t_user(
id int primary key auto_increment,
username varchar(45) not null,
password varchar(45) not null,
remark varchar(45)
);
quit
EOF
[root@1 script]
# vim /etc/ansible/playbook/test003.yml
---
-
hosts: s
remote_user: root
roles:
-
mariadb
[root@1 script]
# cd /etc/ansible/playbook/
[root@1 playbook]
# ansible-playbook test003.yml
下午
配置⼀主⼆从的
mysql
服务器
mysql57
1
)
mysql
服务器端⼝
3306
,不要关闭防⽕墙
#
主从配置
[root@mysql57 ~]
# firewall-cmd --permanent --add-port=3306/tcp
success
[root@mysql57 ~]
# firewall-cmd --reload
success
[root@mysql57 ~]
# firewall-cmd --list-ports
3306
/tcp
96
yum
-y
install lrzsz
66
tar
-zxf
mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz
67
cp
-r
mysql-5.7.44-linux-glibc2.12-x86_64 /usr/local/mysql
68
mkdir
/usr/local/mysql/mysql-files
69
useradd
-r -s
/sbin/nologin mysql
70
chown
mysql:mysql /usr/local/mysql/mysql-files/
71
chmod
750
/usr/local/mysql/mysql-files/
72
rm
-rf
/etc/my.cnf
#
主配置
73
/usr/local/mysql/bin/mysqld
--initialize --user
=
mysql
--
basedir
=
/usr/local/mysql
74
/usr/local/mysql/bin/mysql_ssl_rsa_setup
--datadir
=
/usr/local/mysql/data
76
cp
/usr/local/mysql/support-files/mysql.server /etc/init.d/mysql57
77
service
mysql57
start
79
sed
-i
'$aexport PATH=/usr/local/mysql/bin:$PATH'
/etc/profile
80
source
/etc/profile
vim
/usr/local/mysql/my.cnf
[mysqld]
basedir
=
/usr/local/mysql
datadir
=
/usr/local/mysql/data
socket
=
/tmp/mysql.sock
activate_all_roles_on_login
=
on
port
=
3306
log-error
=
/usr/local/mysql/data/db01-master.err
log-bin
=
/usr/local/mysql/data/binlog
server-id
=
10
character_set_server
=
utf8mb4
service
mysql57
restart
81
mysql
-p
'zc0fLfoM%SDP'
mysql> alter user
'root'
@
'localhost'
identified by
'123'
;
Query OK,
0
rows affected (0.15 sec) //
修改
root
密码
mysql> create user
'slave0'
@
'%'
identified by
'123'
;
Query OK,
0
rows affected (0.03 sec) //
创建
slave
用户
mysql> grant replication slave on *.* to
'slave0'
@
'%'
;
Query OK,
0
rows affected (0.02 sec) //
授予
replication slave
权限
mysql> flush privileges; //
重新加载权限表
Query OK,
0
rows affected (0.00 sec)
mysql> flush tables with read lock; //
刷新所有表并施加一个全局读锁
Query OK,
0
rows affected (0.01 sec)
mysql> show master status; //
查看主服务器当前二进制日志的状态信息
+
---------------
+
----------
+
--------------
+
------------------
+
--------------
-----
+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
Executed_Gtid_Set |
+
---------------
+
----------
+
--------------
+
------------------
+
--------------
-----
+
| binlog.000003 |
1178
| | |
|
+
---------------
+
----------
+
--------------
+
------------------
+
--------------
-----
+
1
row
in set
(0.00 sec)
rm
-rf
/usr/local/mysql/data/auto.cnf
rsync
-av
/usr/local/mysql/data root@10.0.0.58:/usr/local/mysql/
rsync
-av
/usr/local/mysql/data root@10.0.0.59:/usr/local/mysql/
#
从配置
cp
/usr/local/mysql/support-files/mysql.server /etc/init.d/mysql57
service
mysql57
start
sed
-i
'$aexport PATH=/usr/local/mysql/bin:$PATH'
/etc/profile
source
/etc/profile
vim
/usr/local/mysql/my.cnf
[mysqld]
basedir
=
/usr/local/mysql
datadir
=
/usr/local/mysql/data
socket
=
/tmp/mysql.sock
activate_all_roles_on_login
=
on
port
=
3310
log-error
=
/usr/local/mysql/data/db01-slave.err
relay-log
=
/usr/local/mysql/data/relaylog
server-id
=
11
character_set_server
=
utf8mb4
service
mysql57
restart
mysql
-p
'123'
mysql> change master to
-
>
master_host
=
'10.0.0.57'
,
-
>
master_user
=
'slave0'
,
-
>
master_password
=
'123'
,
-
>
master_port
=
3306
,
-
>
master_log_file
=
'binlog.000003'
,
-
>
master_log_pos
=
1178
;
Query OK,
0
rows affected,
9
warnings (0.02 sec)
mysql>
start
slave;
Query OK,
0
rows affected,
1
warning (0.03 sec)
mysql> show slave status\G
***************************
1
. row ***************************
Slave_IO_State: Waiting
for
source
to send event
Master_Host:
10
.0.0.57
Master_User: slave
Master_Port:
3306
Connect_Retry:
60
Master_Log_File: binlog.000003
Read_Master_Log_Pos:
1178
Relay_Log_File: relaylog.000002
Relay_Log_Pos:
323
Relay_Master_Log_File: binlog.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
2
)在
mysql
中添加
eleme
数据库设置为
utf8mb4
[root@mysql57 ~]
# mysql -p'123'
mysql> create database
if
not exists eleme charset utf8mb4;
Query OK,
1
row affected (0.00 sec)
mysql> show databases;
+
--------------------
+
| Database |
+
--------------------
+
| information_schema |
| eleme |
| mysql |
| performance_schema |
| sys |
+
--------------------
+
6
rows
in set
(0.00 sec)
3
)添加表
t_user
mysql> create table eleme.t_user(
-
> id int not null primary key,
-
> name varchar(32) not null,
-
> username varchar(32) not null,
-
> password varchar(32) not null,
-
> remark varchar(32) not null
-
> );
Query OK,
0
rows affected (0.01 sec)
mysql> desc eleme.t_user;
+
----------
+
--------------
+
------
+
-----
+
---------
+
-------
+
| Field | Type | Null | Key | Default | Extra |
+
----------
+
--------------
+
------
+
-----
+
---------
+
-------
+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(32) | NO | | NULL | |
| username | varchar(32) | NO | | NULL | |
| password | varchar(32) | NO | | NULL | |
| remark | varchar(32) | NO | | NULL | |
+
----------
+
--------------
+
------
+
-----
+
---------
+
-------
+
5
rows
in set
(0.02 sec)
4
)添加
2
⾏记录
mysql> insert into eleme.t_user values(1,
'
超级管理员
'
,
'admin'
,
'admin'
,
'
超级管理
员
'
),(2,
'
普通用户
'
,
'guest'
,
'guest'
,
'
普通用户
'
);
Query OK,
2
rows affected (0.00 sec)
Records:
2
Duplicates:
0
Warnings:
0
mysql> select * from eleme.t_user;
+
----
+
-----------------
+
----------
+
----------
+
-----------------
+
| id | name | username | password | remark |
+
----
+
-----------------
+
----------
+
----------
+
-----------------
+
|
1
|
超级管理员
| admin | admin |
超级管理员
|
|
2
|
普通用户
| guest | guest |
普通用户
|
+
----
+
-----------------
+
----------
+
----------
+
-----------------
+
2
rows
in set
(0.00 sec)
5
)使⽤
mycat
为
3
台数据库设置负载均衡(读写分离)
[root@mycat ~]
# systemctl stop firewalld
[root@mycat ~]
# systemctl disable firewalld
[root@mycat ~]
# ls
anaconda-ks.cfg Mycat-server-1.6.5-release-20180122220033-
linux.tar.gz
jdk-8u192-linux-x64.tar.gz v
[root@mycat ~]
# tar -xf jdk-8u192-linux-x64.tar.gz
[root@mycat ~]
# tar -xf Mycat-server-1.6.5-release-20180122220033-
linux.tar.gz
[root@mycat ~]
# cp -r jdk1.8.0_192/ /usr/local/jdk
[root@mycat ~]
# cp -r mycat/ /usr/local/
[root@mycat ~]
# ls /usr/local/jdk/
bin lib src.zip
COPYRIGHT LICENSE THIRDPARTYLICENSEREADME-JAVAFX.txt
include man THIRDPARTYLICENSEREADME.txt
javafx-src.zip README.html
jre release
[root@mycat ~]
# sed -i '$aexport JAVA_HOME=/usr/local/jdk' /etc/profile
[root@mycat ~]
# source /etc/profile
[root@mycat ~]
# $JAVA_HOME
-bash
: /usr/local/jdk:
是一个目录
[root@mycat ~]
# sed -i '$aexport PATH=$PATH:$JAVA_HOME/bin' /etc/profile
[root@mycat ~]
# source /etc/profile
[root@mycat ~]
# $PATH
-bash
: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/jdk/bin:
没有那个文件或目录
[root@mycat ~]
# javac -version
javac
1
.8.0_192
[root@mycat ~]
# ls /usr/local/mycat/
bin catlet conf lib logs version.txt
[root@mycat ~]
# ll /usr/local/mycat/
总用量
12
drwxr-xr-x.
2
root root
190 8
月
15 15
:19 bin
drwxr-xr-x.
2
root root
6 8
月
15 15
:19 catlet
drwxr-xr-x.
4
root root
4096 8
月
15 15
:19 conf
drwxr-xr-x.
2
root root
4096 8
月
15 15
:19 lib
drwxr-xr-x.
2
root root
6 8
月
15 15
:19 logs
-rwxr-xr-x
.
1
root root
219 8
月
15 15
:19 version.txt
[root@mycat ~]
# ls /usr/local/mycat/bin/mycat
[root@mycat ~]
# vim /usr/local/mycat/conf/server.xml
<user
name
=
"li"
defaultAccount
=
"true"
>
<property
name
=
"password"
>li</property>
<property
name
=
"schemas"
>eleme</property>
<!--
表级
DML
权限设置
--
>
<!--
<privileges
check
=
"false"
>
<schema
name
=
"TESTDB"
dml
=
"0110"
>
<table
name
=
"tb01"
dml
=
"0000"
></table>
<table
name
=
"tb02"
dml
=
"1111"
></table>
</schema>
</privileges>
--
>
</user>
<!--
<user
name
=
"user"
>
<property
name
=
"password"
>user</property>
<property
name
=
"schemas"
>TESTDB</property>
<property
name
=
"readOnly"
>true</property>
</user>
--
>
[root@mycat ~]
# vim /usr/local/mycat/conf/schema.xml
![](https://i-blog.csdnimg.cn/direct/eec05c33e26c43f4971673e324402f5b.png)
[root@mycat ~]# /usr/local/mycat/bin/mycat start
Starting Mycat-server...
[root@mycat ~]# netstat -lnput | grep 8066
tcp6 0 0 :::8066 :::* LISTEN
1336/java
![](https://i-blog.csdnimg.cn/direct/e60570b6cff74ef9a785a13fc534737d.png)
6
、配置静态资源服务器,为前段提供图⽚和视频
7
、部署
2
台有
java17.0.3
环境的后端服务器,端⼝
8080
,不关防⽕墙
8
、部署
nginx
代理
java
服务器,负载均衡策略为轮询问,端⼝为
80