当前位置:网站首页>在 Istio 服务网格内连接外部 MySQL 数据库
在 Istio 服务网格内连接外部 MySQL 数据库
2022-07-26 03:46:00 【万猫学社】
为了方便理解,以 Istio 官方提供的 Bookinfo 应用示例为例,利用 ratings 服务外部 MySQL 数据库。
Bookinfo应用的架构图如下:

其中,包含四个单独的微服务:
productpage:调用details和reviews两个服务,用来生成页面。details:包含了书籍的信息。reviews:包含了书籍相关的评论。它还会调用 ratings 微服务。rating:包含了由书籍评价组成的评级信息。
其中,reviews 服务有 3 个版本:
- v1 版本不会调用
ratings服务。 - v2 版本会调用
ratings服务,并使用 1 到 5 个黑色星形图标来显示评分信息。 - v3 版本会调用
ratings服务,并使用 1 到 5 个红色星形图标来显示评分信息。
准备 MySQL 数据库
创建一个名为 test 数据库,执行以下SQL创建表和数据:
DROP TABLE IF EXISTS `ratings`;
CREATE TABLE `ratings` (
`ReviewID` int(11) NOT NULL,
`Rating` int(11) NULL DEFAULT 0,
PRIMARY KEY (`ReviewID`) USING BTREE
) ENGINE = InnoDB;
INSERT INTO ratings (ReviewID, Rating) VALUES (1, 2);
INSERT INTO ratings (ReviewID, Rating) VALUES (2, 4);
创建ServiceEntry
执行以下命令创建ServiceEntry:
kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
name: mysqldb
spec:
hosts:
- mysqldb.svc.remote
ports:
- number: 3306
name: mysql
protocol: MySQL
location: MESH_EXTERNAL
resolution: STATIC
endpoints:
- address: 192.168.1.116
ports:
mysql: 3306
EOF
其中,192.168.1.116是 MySQL 数据库的IP,3306是 MySQL 数据库的端口。
创建ratings服务
首先,执行以下命令,获取密码的Base64编码:
echo -n 'OneMoreSociety' | base64
其中,OneMoreSociety是连接 MySQL 数据库的密码。
然后,执行以下命令,创建 ratings 服务:
kubectl apply -f - <<EOF
apiVersion: v1
kind: Secret
metadata:
name: mysql-credentials
type: Opaque
data:
dbpasswd: T25lTW9yZVNvY2lldHk=
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: ratings-v2-mysql
labels:
app: ratings
version: v2-mysql
spec:
replicas: 1
selector:
matchLabels:
app: ratings
version: v2-mysql
template:
metadata:
labels:
app: ratings
version: v2-mysql
spec:
containers:
- name: ratings
image: docker.io/istio/examples-bookinfo-ratings-v2:1.16.2
imagePullPolicy: IfNotPresent
env:
- name: DB_TYPE
value: "mysql"
- name: MYSQL_DB_HOST
value: mysqldb.svc.remote
- name: MYSQL_DB_PORT
value: "3306"
- name: MYSQL_DB_USER
value: root
- name: MYSQL_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-credentials
key: dbpasswd
ports:
- containerPort: 9080
securityContext:
runAsUser: 1000
EOF
其中,T25lTW9yZVNvY2lldHk=是连接 MySQL 数据库的密码的Base64编码。
修改路由规则
执行以下命令,把对 reviews 服务的调用全部路由到 v2 版本上:
kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v2
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: reviews
spec:
host: reviews
subsets:
- labels:
version: v1
name: v1
- labels:
version: v2
name: v2
- labels:
version: v3
name: v3
EOF
执行以下命令,把对 ratings 服务的调用全部路由到 v2-mysql 版本上:
kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: ratings
spec:
hosts:
- ratings
http:
- route:
- destination:
host: ratings
subset: v2-mysql
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: ratings
spec:
host: ratings
subsets:
- labels:
version: v1
name: v1
- labels:
version: v2-mysql
name: v2-mysql
EOF
效果
访问 productpage 页面,可以看到 Reviewer1 显示2星, Reviewer2 显示4星,和数据库中的数据一致,如下图:

在Kiali中也可以看到对应的拓扑结构,如下图:

流量转移
访问 MySQL 数据库时,所有流量都路由到v1版本,具体配置如下:
kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
name: mysqldb
spec:
hosts:
- mysqldb.svc.remote
ports:
- number: 3306
name: tcp
protocol: TCP
location: MESH_EXTERNAL
resolution: STATIC
endpoints:
- address: 192.168.1.116
ports:
tcp: 3306
labels:
version: v1
- address: 192.168.1.118
ports:
tcp: 3306
labels:
version: v2
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: mysqldb
spec:
hosts:
- mysqldb.svc.remote
tcp:
- route:
- destination:
host: mysqldb.svc.remote
subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: mysqldb
spec:
host: mysqldb.svc.remote
subsets:
- labels:
version: v1
name: v1
- labels:
version: v2
name: v2
EOF
访问 MySQL 数据库时,把50%流量转移到v2版本,具体配置如下:
kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: mysqldb
spec:
hosts:
- mysqldb.svc.remote
tcp:
- route:
- destination:
host: mysqldb.svc.remote
subset: v1
weight: 50
- destination:
host: mysqldb.svc.remote
subset: v2
weight: 50
EOF
访问 MySQL 数据库时,所有流量都路由到v2版本,具体配置如下:
kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: mysqldb
spec:
hosts:
- mysqldb.svc.remote
tcp:
- route:
- destination:
host: mysqldb.svc.remote
subset: v2
EOF
最后,感谢你这么帅,还给我点赞。
边栏推荐
- php 保存数组到文件 var_export、serialize
- Tactile intelligent sharing-rk3568 application in scenic spot navigation robot
- Hurry in!!! Write a number guessing game with dozens of lines of code based on the basic knowledge of C language
- Easyexcel sets row hiding to solve the problem of sethidden (true) invalidation
- Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network
- 微信小程序实现音乐播放器(4)(使用pubsubjs实现页面间通信)
- Dat of deep learning
- Asemi rectifier bridge gbu1510 parameters, gbu1510 specifications, gbu1510 package
- zkEVM:MINA的CEO对zkEVM和L1相关内容的总结
- 【单片机仿真项目】外部中断0控制8个发光二极管闪烁
猜你喜欢

Kbpc1510-asemi large chip 15A rectifier bridge kbpc1510

ACM mm 2022 | end to end multi granularity comparative learning for video text retrieval

基于SSM选课信息管理系统

Realization of online shopping mall system based on JSP

【创建交互式 Dice Roller 应用】

用GaussDB(for Redis)存画像,推荐业务轻松降本60%

Derivation of linear regression principle

基于JSP实现网上商城系统

Dat of deep learning

Mbr3045ct Schottky diode, mbr0100, mbr2060ct diode parameters
随机推荐
redis集群的三种方式
Oracle 11g "password delayed verification" feature
基本折线图:最直观呈现数据的趋势和变化
cpu和gpu已过时,npu和apu的时代开始
微信小程序实现音乐播放器(4)(使用pubsubjs实现页面间通信)
申请SSL证书,并给域名配置SSL证书,并部署服务器;SSL证书的下载和安装
Usage of tf.variable() function in tensorflow
UDP和TCP可以使用同一个端口吗?
大厂面试都面试些啥,看了不亏(一)
Easyexcel sets row hiding to solve the problem of sethidden (true) invalidation
Sentinel vs Hystrix 到底怎么选?
The convolution kernel is expanded to 51x51, and the new CNN architecture slak counterattacks the transformer
第十八章:2位a~b进制中均位奇观探索,指定整数的 3x+1 转化过程,指定区间验证角谷猜想,探求4份黑洞数,验证3位黑洞数
Dtcloud the next day
Offline data warehouse from 0 to 1 - phase I resource purchase configuration
Failed to install the hcmon driver
Realization of online shopping mall system based on JSP
研发了 5 年的时序数据库,到底要解决什么问题?
想要做好软件测试,可以先了解AST、SCA和渗透测试
Summary of basic knowledge of C language pointer (I)