0%

网络模式

Docker网络模式 配置 说明
host –net=host 容器和宿主机共享Network namespace。
container –net=container:NAME 容器和另外一个容器共享Network namespace。
none –net=none 容器有独立的Network namespace,但并没有对其进行任何网络设置,如分配veth pair 和网桥连接,配置IP等。
bridge –net=bridge 默认为该模式

默认创建3种模式

[root@node1 ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
fa18c3cc657d        bridge              bridge              local
c1196f6c46ca        host                host                local
c20416a61dd3        none                null                local

# 手动创建一个bridge网络
[root@node1 ~]# docker network create test
1ed592b9d91ae8c49f5de4b975e5adb104264bf3b03973fcf3040683a4dfcf0e
[root@node1 ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
fa18c3cc657d        bridge              bridge              local
c1196f6c46ca        host                host                local
c20416a61dd3        none                null                local
1ed592b9d91a        test                bridge              local

测试

# 启动两个容器
docker run -itd --name=c1 busybox
docker run -itd --name=c2 --net=test busybox

# 查看他们ip
docker exec -it c1 ip ad
docker exec -it c2 ip ad

# 因为使用的不同的bridge,他们之间ping不通
docker exec -it c2 ping 172.19.0.2
# 手动将c1加入到test网络
docker network connect test c1
# 查看c1网卡,会增加一个
docker exec -it c1 ip ad

与另一个容器共享一个网络IP

# 先启动一个
docker run -itd --name c1 busybox
# 共享另一个容器网络
docker run -itd --net=container:c1 --name c2 busybox

# 网卡完全一致
docker exec c1 ip ad
docker exec c2 ip ad

ThreadLocal

  • ThreadLocal类允许我们创建只能被同一个线程读写的变量,本质是一个Hash表,keyThread自身.
  • 主要方法: set() / get() / remove()

创建

// 不带初始值
ThreadLocal<String> str = new ThreadLocal<>();

// 带初始值
static ThreadLocal<String> str = new ThreadLocal<String>() {
    @Override
    protected String initialValue() {
        return "a";
    }
};

// java8便捷写法
ThreadLocal<String> str = ThreadLocal.withInitial(() -> "a");

使用

public static void main(String[] args) {
    new Thread(() -> {
//            str.set("aa");
        System.out.println(str.get()); // 初始化a
        str.remove(); // 用完主动释放,防止内存泄漏
    }).start();

    new Thread(() -> {
        str.set("bb");  //赋值
        System.out.println(str.get()); // 输出bb
        str.remove();   // 用完主动释放,防止内存泄漏
    }).start();
}

部署

# ccentos 7
yum install nginx

启动

systemctl start/stop/restart nginx

# 通过status查看nginx.conf配置文件位置
systemctl status nginx

修改配置

vim /etc/nginx/nginx.conf
# 修改默认端口80 -> 8080
listen       8080 default_server;
listen       [::]:8080 default_server;

# 检查配置是否正确,有successful表示可用
nginx -t 

# 重新加载配置文件
nginx -s reload

配置详解

...              // 全局块

events {         // events块
   ...
}

http      // http块
{
    ...   // http全局块
    server        // server块
    { 
        ...       // server全局块
        location [PATTERN]   // location块
        {
            ...
        }
        location [PATTERN] 
        {
            ...
        }
    }
    server
    {
      ...
    }
    ...     // http全局块
}
  1. 全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
  2. events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
  3. http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
  4. server块:配置虚拟主机的相关参数,一个http中可以有多个server
  5. location块:配置请求的路由,以及各种页面的处理情况。

nginx变量使用

# 变量申明
set $变量名 变量值

# 变量的引用
$变量名

预定义变量

常用变量

变量 作用
arg_参数名 URL中某个具体参数的值
query_string 与args变量完全相同
args 全部URL参数
is_args 如果请求的URL中有参数则返回?否则返回空
content_length HTTP 请求中标识包体长度的Content-Length头部的值,头部没有这个则为空
content_type 标识请求包体类型的Content-Type 头部的值
uri 请求的URI(不包含,不包含?后的参数)
document_uri 与uri完全相同
request_uri 请求的URL(包含?后的参数)
scheme 协议名 HTTP 或者 HTTPS
request_method 请求的方法GET 或者 POST
request_length 所有请求内容的大小,包括请求行,头部,包体等
remote_user 有HTTP Basic Authentication 协议传入的用户名
request 原始的url请求,含有方法和协议版本
cookie_COOKIE cookie COOKIE的值
http_HEADER HTTP请求头中的内容,HEADER为HTTP请求中的内容转为小写,-变为_(破折号变为下划线),例如:$http_user_agent(Uaer-Agent的值)
sent_http_HEADER HTTP响应头中的内容,HEADER为HTTP响应中的内容转为小写,-变为_(破折号变为下划线),例如: $sent_http_cache_control, $sent_http_content_type…;

TCP相关的变量

变量 作用
binary_remote_addr 客户端地质的整型格式,对于IPv4是4字节
remote_addr 客户端地址
remote_port 客户端端口
connection 递增的连接序号
connection_requests 当前连接上执行过的请求数,对keepalive 有意义
proxy_protocol_addr 若使用了proxy_protocol 协议则返回协议中的地址
proxy_protocol_port 若使用了proxy_protocol 协议则返回协议中的端口
server_addr 服务器端地址(本端地址)
server_port 服务器端端口
TCP_INFO tcp内核层参数($tcpinfo_rtt,$tcpinfo_rttvar,$tcpinfo_snd_cwnd,$tcpinfo_rcv_space)
server_protocol 服务端协议,例如 HTTP

Nginx处理请求过程中产生的变量

变量 作用
request_time 请求处理到现在的耗时
server_name 匹配上的请求server_name
request_completion 若请求处理完则返回OK,否则为空
request_id 以16禁止输出的请求标识id,随即生成

Nginx系统变量

变量 作用
time_local 以本地时间的标准输出
pid 所属worker进程的id
hostname 与系统上输出hostname 一致

PushGateway使用说明

  • Prometheus采用定时Pull模式,可能由于子网络或者防火墙的原因,不能直接拉取各个Target的指标数据,此时可以采用各个TargetPushGatewayPush数据,然后PrometheusPushGateway上定时pull
  • 其次在监控各个业务数据时,需要将各个不同的业务数据进行统一汇总,此时也可以采用PushGateway来统一收集,然后Prometheus来统一拉取
  • Prometheus每次从PushGateway拉取的数据,并不是拉取周期内用户推送上来的所有数据,而是最后一次PushPushGateway上的数据
  • 指标值只能是数字类型,非数字类型报错
  • 指标值支持最大长度为16位,超过16位后默认置为0

安装

# 默认不持久化数据
docker run -d -p 9091:9091 prom/PushGateway 

# 指定持久化文件和保留时间
docker run -d -p 9091:9091 prom/pushgateway "-persistence.file=pg_file –persistence.interval=5m"

测试

# 单条推送
echo "test_metric 123456" | curl --data-binary @- http://192.168.41.128:9091/metrics/job/test_job

# 多条推送
cat <<EOF | curl --data-binary @- http://192.168.41.128:9091/metrics/job/test_job/instance/test_instance
# TYPE test_metrics counter
test_metrics{label="app1",name="demo"} 100.00
# TYPE another_test_metrics gauge
# HELP another_test_metrics Just an example.
another_test_metrics 123.45
EOF

# 通过文件
vim pg.txt
# TYPE http_request_total counter
# HELP http_request_total get interface request count with different code.
http_request_total{code="200",interface="/v1/save"} 276
http_request_total{code="404",interface="/v1/delete"} 0
http_request_total{code="500",interface="/v1/save"} 1
# TYPE http_request_time gauge
# HELP http_request_time get core interface http request time.
http_request_time{code="200",interface="/v1/core"} 0.122

curl -XPOST --data-binary @pg.txt http://192.168.41.128:9091/metrics/job/app/instance/app-192.168.41.128

删除指标,参考官方

# {job="some_job",instance="some_instance"}
curl -X DELETE http://192.168.41.128:9091/metrics/job/some_job/instance/some_instance

# {job="some_job"}
curl -X DELETE http://192.168.41.128:9091/metrics/job/some_job

# 删除所有, 需要开启--web.enable-admin-api
curl -X PUT http://192.168.41.128:9091/api/v1/admin/wipe

通过SDK上传:

<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_pushgateway</artifactId>
    <version>0.7.0</version>
</dependency>
public static void main(String[] args) {
    try{
        String url = "192.168.41.128:9091";
        CollectorRegistry registry = new CollectorRegistry();
        Gauge guage = Gauge.build("my_custom_metric", "This is my custom metric.").create();
        guage.set(23.12);
        guage.register(registry);
        PushGateway pg = new PushGateway(url);
        Map<String, String> groupingKey = new HashMap<String, String>();
        groupingKey.put("instance", "my_instance");
        pg.pushAdd(registry, "my_job", groupingKey);
    } catch (Exception e){
        e.printStackTrace();
    }
}
public static void main(String[] args) {
    try{
        String url = "172.30.12.167:9091";
        CollectorRegistry registry = new CollectorRegistry();
        Gauge guage = Gauge.build("my_custom_metric", "This is my custom metric.").labelNames("app", "date").create();
        String date = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss").format(new Date());
        guage.labels("my-pushgateway-test-0", date).set(25);
        guage.labels("my-pushgateway-test-1", date).dec();
        guage.labels("my-pushgateway-test-2", date).dec(2);
        guage.labels("my-pushgateway-test-3", date).inc();
        guage.labels("my-pushgateway-test-4", date).inc(5);
        guage.register(registry);
        PushGateway pg = new PushGateway(url);
        Map<String, String> groupingKey = new HashMap<String, String>();
        groupingKey.put("instance", "my_instance");
        pg.pushAdd(registry, "my_job", groupingKey);
    } catch (Exception e){
        e.printStackTrace();
    }
}

查看

参考链接

mysql日志类型

redo log 重做日志 确保事务的持久性
undo log 回滚日志 用于回滚
bin log 二进制日志 主从复制
relay log 中继日志 主从复制
error log 错误日志
slow query log 慢查询日志
general log 一般查询日志

percona-toolkit介绍

percona-toolkit是一组高级命令行工具的集合,用来执行各种通过手工执行非常复杂和麻烦的mysql和系统任务,这些任务包括:

  1. 检查master和slave数据的一致性
  2. 有效地对记录进行归档
  3. 查找重复的索引
  4. 对服务器信息进行汇总
  5. 分析来自日志和tcpdump的查询
  6. 当系统出问题的时候收集重要的系统信息

percona-toolkit工具中最主要的三个组件分别是:

  1. pt-table-checksum 负责监测mysql主从数据一致性
  2. pt-table-sync 负责当主从数据不一致时修复数据,让它们保存数据的一致性
  3. pt-heartbeat 负责监控mysql主从同步延迟

percona-toolkit安装

  1. 安装依赖
# 安装依赖
yum install perl-IO-Socket-SSL perl-DBD-MySQL perl-Time-HiRes perl perl-DBI -y
  1. 下载安装包
  • 百度云盘下载地址:https://pan.baidu.com/s/1bp1OOgf (提取密码:y462)
  • wget https://www.percona.com/downloads/percona-toolkit/2.2.7/RPM/percona-toolkit-2.2.7-1.noarch.rpm
  1. 安装
rpm -ivh percona-toolkit-2.2.7-1.noarch.rpm

percona-toolkit使用

pt-table-checksumPercona-Toolkit的组件之一,用于检测MySQL主、从库的数据是否一致。其原理是在主库执行基于statement的sql语句来生成主库数据块的checksum,把相同的sql语句传递到从库执行,并在从库上计算相同数据块的checksum,最后,比较主从库上相同数据块的checksum值,由此判断主从数据是否一致。

为了减少对数据库的干预,pt-table-checksum还会自动侦测并连接到从库,当然如果失败,可以指定–recursion-method选项来告诉从库在哪里。它的易用性还体现在,复制若有延迟,在从库 checksum 会暂停直到赶上主库的计算时间点。

为了保证主数据库服务的安全,该工具实现了许多保护措施:

  1. 自动设置 innodb_lock_wait_timeout1s,避免引起
  2. 默认当数据库有25个以上的并发查询时,pt-table-checksum会暂停。可以设置 --max-load 选项来设置这个阀值
  3. 当用Ctrl+C停止任务后,工具会正常的完成当前chunk检测,下次使用--resume选项启动可以恢复继续下一个chunk

创建授权帐号

-- 工具在主库上执行,IP写主库IP
mysql> GRANT SELECT, PROCESS, SUPER, REPLICATION SLAVE,CREATE,DELETE,INSERT,UPDATE ON *.* TO 'pt'@'192.168.41.141' identified by '123456';
mysql> flush privileges;

pt-table-checksum

在主(master)上通过执行校验的查询对复制的一致性进行检查,对比主从的校验值,从而产生结果。

注意:第一次运行的时候需要加上--create-replicate-table参数,生成checksums表!

常用参数解释:

--nocheck-replication-filters 不检查复制过滤器,建议启用。
--no-check-binlog-format 不检查复制的binlog模式,要是binlog模式是ROW,则会报错。
--replicate-check-only 只显示不同步的信息。
--replicate= 把checksum的信息写入到指定表中,建议直接写到被检查的数据库当中。
--databases= 指定需要被检查的数据库,多个则用逗号隔开。
--tables= 指定需要被检查的表,多个用逗号隔开
--recursion-method 指定复制检查的方式,默认为processlist,hosts
h= Master的地址
u= 用户名
p= 密码
P= 端口
-- 从库需要开启slave;
start slave;
show slave status\G

-- 主库processlist可以查到slave
mysql> show processlist;
+--------+-------------+---------------+------+------------------+--------+---------------------------------------------------------------+------------------+
| Id     | User        | Host          | db   | Command          | Time   | State                                                         | Info             |
+--------+-------------+---------------+------+------------------+--------+---------------------------------------------------------------+------------------+
|      2 | system user |               | NULL | Connect          | 589655 | Slave has read all relay log; waiting for more updates        | NULL             |
|   2049 | slave       | mysql02:37382 | NULL | Binlog Dump GTID | 587611 | Master has sent all binlog to slave; waiting for more updates | NULL             |
| 588590 | root        | localhost     | NULL | Query            |      0 | starting                                                      | show processlist |
+--------+-------------+---------------+------+------------------+--------+---------------------------------------------------------------+------------------+
3 rows in set (0.03 sec)
# 执行检查命令,注意第一次添加--create-replicate-table参数创建表
[root@mysql01 ~]# pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --replicate=test2.checksums --create-replicate-table --databases=test h=192.168.41.141,u=pt,p=123456,P=3306
            TS ERRORS  DIFFS     ROWS  CHUNKS SKIPPED    TIME TABLE
08-05T17:35:54      0      1       13       1       0   0.091 test.t_department
08-05T17:35:54      0      0        8       1       0   0.202 test.testttt

# 结果字段说明
# TS :完成检查的时间。
# ERRORS :检查时候发生错误和警告的数量。
# DIFFS :0表示一致,1表示不一致。当指定--no-replicate-check时,会一直为0,当指定--replicate-check-only会显示不同的信息。
# ROWS :表的行数。
# CHUNKS :被划分到表中的块的数目。
# SKIPPED :由于错误或警告或过大,则跳过块的数目。
# TIME :执行的时间。
# TABLE :被检查的表名。

[root@mysql01 ~]# pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --replicate=test2.checksums --no-replicate-check  --databases=test h=192.168.41.141,u=pt,p=123456,P=3306
            TS ERRORS  DIFFS     ROWS  CHUNKS SKIPPED    TIME TABLE
08-06T09:30:43      0      0       13       1       0   0.023 test.t_department
08-06T09:30:43      0      0        8       1       0   0.033 test.testttt
[root@mysql01 ~]# pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --replicate=test2.checksums --replicate-check-only --databases=test h=192.168.41.141,u=pt,p=123456,P=3306
Differences on mysql02
TABLE CHUNK CNT_DIFF CRC_DIFF CHUNK_INDEX LOWER_BOUNDARY UPPER_BOUNDARY
test.t_department 1 0 1 

pt-table-sync

pt-table-sync: 高效的同步MySQL表之间的数据,他可以做单向和双向同步的表数据。他可以同步单个表,也可以同步整个库。它不同步表结构、索引、或任何其他模式对象。所以在修复一致性之前需要保证他们表存在。

# 参数解释:
# --replicate= :   指定通过pt-table-checksum得到的表,这2个工具差不多都会一直用。
# --databases= :    指定执行同步的数据库。
# --tables= :      指定执行同步的表,多个用逗号隔开。
# --sync-to-master :指定一个DSN,即从的IP,他会通过show processlist或show slave status 去自动的找主。
# h= :             服务器地址,命令里有2个ip,第一次出现的是Master的地址,第2次是Slave的地址。
# u= :             帐号。
# p= :             密码。
# --print :        打印,但不执行命令。打印出来修复数据的sql语句,可以手动的在slave从库上执行,让他们数据保持一致性
# --execute :      执行命令。

[root@mysql01 ~]# pt-table-sync --replicate=test2.checksums h=192.168.41.141,u=pt,p=123456 h=192.168.41.142,u=pt,p=123456 --print
REPLACE INTO `test`.`t_department`(`department_id`, `department_name`, `parent_id`, `level`, `create_time`, `update_time`) VALUES ('7', '8', '0', '1', '2020-07-28 16:06:40', '2020-07-28 16:06:40') /*percona-toolkit src_db:test src_tbl:t_department src_dsn:h=192.168.41.141,p=...,u=pt dst_db:test dst_tbl:t_department dst_dsn:h=mysql02,p=...,u=pt lock:1 transaction:1 changing_src:test2.checksums replicate:test2.checksums bidirectional:0 pid:114329 user:root host:mysql01*/;


[root@mysql01 ~]# pt-table-sync --replicate=test2.checksums h=192.168.41.141,u=pt,p=123456 h=192.168.41.142,u=pt,p=123456 --print --execute
REPLACE INTO `test`.`t_department`(`department_id`, `department_name`, `parent_id`, `level`, `create_time`, `update_time`) VALUES ('7', '8', '0', '1', '2020-07-28 16:06:40', '2020-07-28 16:06:40') /*percona-toolkit src_db:test src_tbl:t_department src_dsn:h=192.168.41.141,p=...,u=pt dst_db:test dst_tbl:t_department dst_dsn:h=mysql02,p=...,u=pt lock:1 transaction:1 changing_src:test2.checksums replicate:test2.checksums bidirectional:0 pid:115260 user:root host:mysql01*/;

# 同步之后再次查看,主从数据已经同步
[root@mysql01 ~]# pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --replicate=test2.checksums --databases=test h=192.168.41.141,u=pt,p=123456,P=3306
            TS ERRORS  DIFFS     ROWS  CHUNKS SKIPPED    TIME TABLE
08-06T09:36:23      0      0       13       1       0   0.293 test.t_department
08-06T09:36:23      0      0        8       1       0   0.018 test.testttt

可以编写脚本定时同步主备库数据

#!/bin/bash
NUM=$(/usr/bin/pt-table-checksum --nocheck-replication-filters --no-check-binlog-format --replicate=test2.checksums --databases=test  h=192.168.41.141,u=pt,p=123456,P=3306|awk -F" " '{print $3}'|sed -n '2p')
if [ $NUM -eq 1 ];then
  /usr/bin/pt-table-sync --replicate=test2.checksums h=192.168.41.141,u=pt,p=123456 h=192.168.41.142,u=pt,p=123456 --print --execute
else
  echo "data is ok"
fi

pt-heartbeat

对于MySQL数据库主从复制延迟的监控,可以借助pt-heartbeat来实现。

pt-heartbeat的工作原理通过使用时间戳方式在主库上更新特定表,然后在从库上读取被更新的时间戳然后与本地系统时间对比来得出其延迟。具体流程:

  1. 在主上创建一张heartbeat表,按照一定的时间频率更新该表的字段(把时间更新进去)。监控操作运行后,heartbeat表能促使主从同步!
  2. 连接到从库上检查复制的时间记录,和从库的当前系统时间进行比较,得出时间的差异。

参考链接