赞
踩
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
├── tasks
│ └── main.yml
├── templates
└── vars
└── 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
id int 主键约束,自增 编号
username varchar 非空约束 账号
password varchar 非空约束 密码
remark varchar 没有约束 账号权限说明
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
[root@mycat ~]# /usr/local/mycat/bin/mycat start
Starting Mycat-server...
[root@mycat ~]# netstat -lnput | grep 8066
tcp6 0 0 :::8066 :::* LISTEN 1336/java
6、配置静态资源服务器,为前段提供图⽚和视频
7、部署2台有java17.0.3环境的后端服务器,端⼝8080,不关防⽕墙
8、部署nginx代理java服务器,负载均衡策略为轮询问,端⼝为80
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。