一. 课题:2022年大厂必备docker与微服务实战

目录:

  • docker的理念与最新版的安装及维护
  • docker与centos9-stream结合
  • 实战: MySQL主从复制docker实现版
  • 实战:docker轻量级可视化工具Portainer
  • 实战: docker容器监控:CAdvisor+InfluxDB+Granfana

正文:

docker的理念与安装

1. 安装docker

  1. # wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo

2. 查看docker的 版本

yum list docker-ce —showduplicates | sort -r

3. 开始安装

二. 实战: docker实现 MySQL主从复制

下载MySQL镜像

  1. [root@k8s-master ~]# docker pull mysql:5.7
  2. 5.7: Pulling from library/mysql
  3. 72a69066d2fe: Pull complete
  4. 93619dbc5b36: Pull complete
  5. 99da31dd6142: Pull complete
  6. 626033c43d70: Pull complete
  7. 37d5d7efb64e: Pull complete
  8. ac563158d721: Pull complete
  9. d2ba16033dad: Pull complete
  10. 0ceb82207cd7: Pull complete
  11. 37f2405cae96: Pull complete
  12. e2482e017e53: Pull complete
  13. 70deed891d42: Pull complete
  14. Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
  15. Status: Downloaded newer image for mysql:5.7
  16. docker.io/library/mysql:5.7

创建MySQL-master并创建配置文件

  1. [root@k8s-master ~]# docker run -p 3307:3306 --name mysql-master \
  2. -v /mydata/mysql-master/log:/var/log/mysql \
  3. -v /mydata/mysql-master/data:/var/lib/mysql \
  4. -v /mydata/mysql-master/conf:/etc/mysql \
  5. -e MYSQL_ROOT_PASSWORD=root \
  6. -d mysql:5.7
  7. cc88521bb0baea9f75c86fefb41dfa44cb3c4398f7e11885c9b9d873be6b93f9
  8. [root@k8s-master ~]# cd /mydata/mysql-master/conf/
  9. [root@k8s-master conf]# vim my.cnf
  10. [root@k8s-master conf]# grep -v ^# my.cnf
  11. [mysqld]
  12. server_id=101
  13. binlog-ignore-db=mysql
  14. log-bin=mall-mysql-bin
  15. binlog_cache_size=1M
  16. binlog_format=mixed
  17. expire_logs_days=7
  18. slave_skip_errors=1062

关于配置文件的注释:

  1. [root@k8s-master conf]# cat my.cnf
  2. [mysqld]
  3. ## 设置server_id,同一局域网中需要唯一
  4. server_id=101
  5. ## 指定不需要同步的数据库名称
  6. binlog-ignore-db=mysql
  7. ## 开启二进制日志功能
  8. log-bin=mall-mysql-bin
  9. ## 设置二进制日志使用内存大小(事务)
  10. binlog_cache_size=1M
  11. ## 设置使用的二进制日志格式(mixed,statement,row)
  12. binlog_format=mixed
  13. ## 二进制日志过期清理时间。默认值为0,表示不自动清理。
  14. expire_logs_days=7
  15. ## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
  16. ## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
  17. slave_skip_errors=1062

重启master

  1. [root@k8s-master conf]# docker restart mysql-master
  2. mysql-master
  3. [root@k8s-master conf]# docker ps
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. cc88521bb0ba mysql:5.7 "docker-entrypoint.s…" 4 minutes ago Up 3 seconds 33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp mysql-master
  6. [root@k8s-master conf]#

进入master创建数据和数据同步用户

  1. [root@k8s-master conf]# docker exec -it mysql-master /bin/bash
  2. root@cc88521bb0ba:/# mysql -proot
  3. mysql: [Warning] Using a password on the command line interface can be insecure.
  4. Welcome to the MySQL monitor. Commands end with ; or \g.
  5. Your MySQL connection id is 2
  6. Server version: 5.7.36-log MySQL Community Server (GPL)
  7. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  8. Oracle is a registered trademark of Oracle Corporation and/or its
  9. affiliates. Other names may be trademarks of their respective
  10. owners.
  11. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  12. mysql> CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
  13. Query OK, 0 rows affected (0.01 sec)
  14. mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
  15. Query OK, 0 rows affected (0.00 sec)
  16. mysql> exit
  17. Bye

新建从服务器

  1. a/mysql-slave/log:/var/log/mysql \
  2. > -v /mydata/mysql-slave/data:/var/lib/mysql \
  3. > -v /mydata/mysql-slave/conf:/etc/mysql \
  4. > -e MYSQL_ROOT_PASSWORD=root \
  5. > -d mysql:5.7
  6. 15514af9443fce00c9c6f3cc182ad210440a0052e54123a9516503d23a6bc7bb
  7. [root@k8s-master conf]# cd /mydata/mysql-slave/conf/
  8. [root@k8s-master conf]# vim my.cnf
  9. [root@k8s-master conf]# grep -v ^# my.cnf
  10. [mysqld]
  11. server_id=102
  12. binlog-ignore-db=mysql
  13. log-bin=mall-mysql-slave1-bin
  14. binlog_cache_size=1M
  15. binlog_format=mixed
  16. expire_logs_days=7
  17. slave_skip_errors=1062
  18. relay_log=mall-mysql-relay-bin
  19. log_slave_updates=1
  20. read_only=1

配置文件解析:

  1. [mysqld]
  2. ## 设置server_id,同一局域网中需要唯一
  3. server_id=102
  4. ## 指定不需要同步的数据库名称
  5. binlog-ignore-db=mysql
  6. ## 开启二进制日志功能,以备Slave作为其它数据库实例的Master时使用
  7. log-bin=mall-mysql-slave1-bin
  8. ## 设置二进制日志使用内存大小(事务)
  9. binlog_cache_size=1M
  10. ## 设置使用的二进制日志格式(mixed,statement,row)
  11. binlog_format=mixed
  12. ## 二进制日志过期清理时间。默认值为0,表示不自动清理。
  13. expire_logs_days=7
  14. ## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
  15. ## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
  16. slave_skip_errors=1062
  17. ## relay_log配置中继日志
  18. relay_log=mall-mysql-relay-bin
  19. ## log_slave_updates表示slave将复制事件写进自己的二进制日志
  20. log_slave_updates=1
  21. ## slave设置为只读(具有super权限的用户除外)
  22. read_only=1

重启从服务器

  1. [root@k8s-master conf]# docker restart mysql-slave
  2. mysql-slave

在主服务器查看同步状态

  1. [root@k8s-master conf]# docker exec -it mysql-master bash
  2. root@cc88521bb0ba:/# mysql -proot
  3. mysql: [Warning] Using a password on the command line interface can be insecure.
  4. Welcome to the MySQL monitor. Commands end with ; or \g.
  5. Your MySQL connection id is 3
  6. Server version: 5.7.36-log MySQL Community Server (GPL)
  7. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  8. Oracle is a registered trademark of Oracle Corporation and/or its
  9. affiliates. Other names may be trademarks of their respective
  10. owners.
  11. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  12. mysql> show master status;
  13. +-----------------------+----------+--------------+------------------+-------------------+
  14. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  15. +-----------------------+----------+--------------+------------------+-------------------+
  16. | mall-mysql-bin.000001 | 617 | | mysql | |
  17. +-----------------------+----------+--------------+------------------+-------------------+
  18. 1 row in set (0.00 sec)

进入从服务器配置主从同步

  1. [root@k8s-master conf]# docker exec -it mysql-slave /bin/bash
  2. root@15514af9443f:/# mysql -proot
  3. mysql: [Warning] Using a password on the command line interface can be insecure.
  4. Welcome to the MySQL monitor. Commands end with ; or \g.
  5. Your MySQL connection id is 2
  6. Server version: 5.7.36-log MySQL Community Server (GPL)
  7. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  8. Oracle is a registered trademark of Oracle Corporation and/or its
  9. affiliates. Other names may be trademarks of their respective
  10. owners.
  11. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  12. mysql> change master to master_host='192.168.3.71', master_user='slave', master_password='123456', master_port=3307, master_log_file='mall-mysql-bin.000001', master_log_pos=617, master_connect_retry=30;
  13. Query OK, 0 rows affected, 2 warnings (0.01 sec)
  14. mysql>

查看主从状态

  1. mysql> show slave status \G;
  2. *************************** 1. row ***************************
  3. Slave_IO_State:
  4. Master_Host: 192.168.3.71
  5. Master_User: slave
  6. Master_Port: 3307
  7. Connect_Retry: 30
  8. Master_Log_File: mall-mysql-bin.000001
  9. Read_Master_Log_Pos: 617
  10. Relay_Log_File: mall-mysql-relay-bin.000001
  11. Relay_Log_Pos: 4
  12. Relay_Master_Log_File: mall-mysql-bin.000001
  13. Slave_IO_Running: No
  14. Slave_SQL_Running: No

可以看到现在还没开始,需要开启主从同步。

  1. mysql> start slave;
  2. Query OK, 0 rows affected (0.02 sec)
  3. mysql> show slave status \G;
  4. *************************** 1. row ***************************
  5. Slave_IO_State: Waiting for master to send event
  6. Master_Host: 192.168.3.71
  7. Master_User: slave
  8. Master_Port: 3307
  9. Connect_Retry: 30
  10. Master_Log_File: mall-mysql-bin.000001
  11. Read_Master_Log_Pos: 617
  12. Relay_Log_File: mall-mysql-relay-bin.000002
  13. Relay_Log_Pos: 325
  14. Relay_Master_Log_File: mall-mysql-bin.000001
  15. Slave_IO_Running: Yes
  16. Slave_SQL_Running: Yes

可以看到 主从同步已经开始,然后我们来测试下

插入数据测试

主服务上创建数据

  1. mysql> create database db02;
  2. Query OK, 1 row affected (0.00 sec)
  3. mysql> use db02
  4. Database changed
  5. mysql> create table zmedu(id int,name varchar(28));
  6. Query OK, 0 rows affected (0.05 sec)
  7. mysql> insert into zmedu values(1,'zhang');
  8. Query OK, 1 row affected (0.02 sec)
  9. mysql> select * from zmedu;
  10. +------+-------+
  11. | id | name |
  12. +------+-------+
  13. | 1 | zhang |
  14. +------+-------+
  15. 1 row in set (0.00 sec)
  16. mysql>

从服务器查询数据

  1. mysql> show databases;
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | db02 |
  7. | mysql |
  8. | performance_schema |
  9. | sys |
  10. +--------------------+
  11. 5 rows in set (0.00 sec)
  12. mysql> use db02
  13. Reading table information for completion of table and column names
  14. You can turn off this feature to get a quicker startup with -A
  15. Database changed
  16. mysql> select * from zmedu;
  17. +------+-------+
  18. | id | name |
  19. +------+-------+
  20. | 1 | zhang |
  21. +------+-------+
  22. 1 row in set (0.00 sec)
  23. mysql>

到现在为止,主从已经同步了。

三. 实战:docker轻量级可视化工具Portainer

Portainer 是一款轻量级的应用,它提供了图形化界面,用于方便地管理Docker环境,包括单机环境和集群环境。
‘pɔ:teinə

官网: www.portainer.io

docker启动portainer容器

  1. [root@k8s-master ~]# docker run -d -p 8000:8000 -p 9000:9000 --name portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer
  2. Unable to find image 'portainer/portainer:latest' locally
  3. latest: Pulling from portainer/portainer
  4. 94cfa856b2b1: Pull complete
  5. 49d59ee0881a: Pull complete
  6. a2300fd28637: Pull complete
  7. Digest: sha256:fb45b43738646048a0a0cc74fcee2865b69efde857e710126084ee5de9be0f3f
  8. Status: Downloaded newer image for portainer/portainer:latest
  9. 18032a097618298fc4f3140a72cfb282537448fdb1125aa2de52e7899eb107eb

查看已经启动的容器

  1. [root@k8s-master ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 18032a097618 portainer/portainer "/portainer" 52 seconds ago Up 49 seconds 0.0.0.0:8000->8000/tcp, :::8000->8000/tcp, 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp portainer
  4. 5a735193e74a redis:6.0.8 "docker-entrypoint.s…" 21 minutes ago Up 21 minutes

通过网页端登陆

docker实现MySQL主从架构及容器监控 - 图1

docker实现MySQL主从架构及容器监控 - 图2

docker实现MySQL主从架构及容器监控 - 图3

docker实现MySQL主从架构及容器监控 - 图4

到现在为止我们可以看到dashboard里面的监控项了

docker实现MySQL主从架构及容器监控 - 图5

这里的监控项,与系统中用命令查到的一致

  1. [root@k8s-master ~]# docker system df
  2. TYPE TOTAL ACTIVE SIZE RECLAIMABLE
  3. Images 2 2 79.1MB 0B (0%)
  4. Containers 2 1 0B 0B
  5. Local Volumes 1 1 131.9kB 0B (0%)
  6. Build Cache 0 0 0B 0B

可以在后台进行相应的管理
docker实现MySQL主从架构及容器监控 - 图6

启动容器
docker实现MySQL主从架构及容器监控 - 图7

总结

使用这个工具,再也不需要命令行了,直接在网站上点点点就可以了。