|
| 1 | +[TOC] |
| 2 | + |
| 3 | +# 为MySQL客户端开启加密连接 |
| 4 | + |
| 5 | +# `TLS`(传输层加密)简介 |
| 6 | + |
| 7 | +RadonDB MySQL Operator 默认采用非加密连接,如果具备网络嗅探及监视的第三方工具可能截获服务端与客户端之间的数据,容易造成信息泄露,因此建议开启加密连接来确保数据安全。 |
| 8 | + |
| 9 | +RadonDB MySQL Operator 服务端支持`TLS`,协议为MySQL支持的加密协议,如`5.7`版本支持`TLS 1.0、TLS 1.1、TLS 1.2`、`8.0`版本支持`TLS 1.0、TLS 1.1、TLS 1.2、TLS 1.3`。 |
| 10 | + |
| 11 | +使用加密连接需要满足两个条件: |
| 12 | + |
| 13 | +* MySQL Operator 服务端开启加密连接支持 |
| 14 | +* 客户端使用加密连接 |
| 15 | + |
| 16 | +# 配置`MySQL Operator`使用加密连接 |
| 17 | + |
| 18 | +## 准备证书 |
| 19 | + |
| 20 | +* `ca.crt` - 服务端`CA`证书 |
| 21 | +* `tls.key` - 服务端证书私钥 |
| 22 | +* `tls.crt` - 服务端证书 |
| 23 | + |
| 24 | +可以用`OpenSSL`生成,也可以用`MySQL`自带的`mysql_ssl_rsa_setup`快捷生成: |
| 25 | + |
| 26 | +`mysql_ssl_rsa_setup --datadir=/tmp/certs` |
| 27 | + |
| 28 | +运行该命令后会生成如下文件: |
| 29 | + |
| 30 | +```shell |
| 31 | +certs |
| 32 | +├── ca-key.pem |
| 33 | +├── ca.pem |
| 34 | +├── client-cert.pem |
| 35 | +├── client-key.pem |
| 36 | +├── private_key.pem |
| 37 | +├── public_key.pem |
| 38 | +├── server-cert.pem |
| 39 | +└── server-key.pem |
| 40 | +``` |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +### 根据证书文件创建secret |
| 45 | + |
| 46 | +```shell |
| 47 | +kubectl create secret generic sample-ssl --from-file=tls.crt=server.pem -- |
| 48 | +from-file=tls.key=server-key.pem --from-file=ca.crt=ca.pem -- |
| 49 | +type=kubernetes.io/tls |
| 50 | +``` |
| 51 | + |
| 52 | +### 配置RadonDB MySQL 集群使用`TLS` |
| 53 | + |
| 54 | +```shell |
| 55 | +kubectl patch mysqlclusters.mysql.radondb.com sample --type=merge -p '{"spec":{"tlsSecretName":"sample-ssl"}}' |
| 56 | +``` |
| 57 | + |
| 58 | +> 配置之后会触发`rolling update`即集群会重启 |
| 59 | +
|
| 60 | +### 验证测试 |
| 61 | + |
| 62 | +* 不使用`SSL`连接 |
| 63 | + |
| 64 | + ```shell |
| 65 | + kubectl exec -it sample-mysql-0 -c mysql -- mysql -uradondb_usr -p"RadonDB@123" -e "\s" |
| 66 | + mysql Ver 14.14 Distrib 5.7.34-37, for Linux (x86_64) using 7.0 |
| 67 | + Connection id: 7940 |
| 68 | + Current database: |
| 69 | + Current user: radondb_usr@localhost |
| 70 | + SSL: Not in use |
| 71 | + Current pager: stdout |
| 72 | + Using outfile: '' |
| 73 | + Using delimiter: ; |
| 74 | + Server version: 5.7.34-37-log Percona Server (GPL), Release 37, Revision 7c516e9 |
| 75 | + Protocol version: 10 |
| 76 | + Connection: Localhost via UNIX socket |
| 77 | + Server characterset: utf8mb4 |
| 78 | + Db characterset: utf8mb4 |
| 79 | + Client characterset: latin1 |
| 80 | + Conn. characterset: latin1 |
| 81 | + UNIX socket: /var/lib/mysql/mysql.sock |
| 82 | + Uptime: 21 hours 49 min 36 sec |
| 83 | + |
| 84 | + Threads: 5 Questions: 181006 Slow queries: 0 Opens: 127 Flush tables: 1 Open tables: 120 Queries per second avg: 2.303 |
| 85 | + ``` |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +* 使用`SSL`连接 |
| 90 | + |
| 91 | +```shell |
| 92 | + kubectl exec -it sample-mysql-0 -c mysql -- mysql -uradondb_usr -p"RadonDB@123" --ssl-mode=REQUIRED -e "\s" |
| 93 | +mysql: [Warning] Using a password on the command line interface can be insecure. |
| 94 | +-------------- |
| 95 | +mysql Ver 14.14 Distrib 5.7.34-37, for Linux (x86_64) using 7.0 |
| 96 | + |
| 97 | +Connection id: 7938 |
| 98 | +Current database: |
| 99 | +Current user: radondb_usr@localhost |
| 100 | +SSL: Cipher in use is ECDHE-RSA-AES128-GCM-SHA256 |
| 101 | +Current pager: stdout |
| 102 | +Using outfile: '' |
| 103 | +Using delimiter: ; |
| 104 | +Server version: 5.7.34-37-log Percona Server (GPL), Release 37, Revision 7c516e9 |
| 105 | +Protocol version: 10 |
| 106 | +Connection: Localhost via UNIX socket |
| 107 | +Server characterset: utf8mb4 |
| 108 | +Db characterset: utf8mb4 |
| 109 | +Client characterset: latin1 |
| 110 | +Conn. characterset: latin1 |
| 111 | +UNIX socket: /var/lib/mysql/mysql.sock |
| 112 | +Uptime: 21 hours 49 min 26 sec |
| 113 | + |
| 114 | +Threads: 5 Questions: 180985 Slow queries: 0 Opens: 127 Flush tables: 1 Open tables: 120 Queries per second avg: 2.303 |
| 115 | +``` |
0 commit comments