在虚拟机上部署 Bookinfo 应用程序

本示例通过在虚拟机(VM)上运行一项服务来跨 Kubernetes 部署 Bookinfo 应用程序,并说明了如何以单个网格的形式控制此基础架构。

概述

在虚机上运行 Bookinfo

在虚机上运行 Bookinfo

开始之前

在虚拟机上运行 MySQL

您将在虚拟机上安装 MySQL,并将其配置为 ratings 服务的后端。

下列的所有命令都在虚机上执行。

安装 mariadb

  1. $ sudo apt-get update && sudo apt-get install -y mariadb-server
  2. $ sudo sed -i '/bind-address/c\bind-address = 0.0.0.0' /etc/mysql/mariadb.conf.d/50-server.cnf

设置认证信息:

  1. $ cat <<EOF | sudo mysql
  2. # 授予 root 的访问权限
  3. GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
  4. # 授予 root 其他 IP 的访问权限
  5. CREATE USER 'root'@'%' IDENTIFIED BY 'password';
  6. GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
  7. FLUSH PRIVILEGES;
  8. quit;
  9. EOF
  10. $ sudo systemctl restart mysql

您可以在 Mysql 中找到配置 MySQL 的详细信息。

在虚拟机上,将 ratings 数据库添加到 mysql 中。

  1. $ curl -LO https://raw.githubusercontent.com/istio/istio/release-1.10/samples/bookinfo/src/mysql/mysqldb-init.sql
  2. $ mysql -u root -ppassword < mysqldb-init.sql

为了便于直观地检查 Bookinfo 应用程序输出中的差异,您可以使用以下命令来更改所生成的 ratings 数据库并且检查它:

  1. $ mysql -u root -ppassword test -e "select * from ratings;"
  2. +----------+--------+
  3. | ReviewID | Rating |
  4. +----------+--------+
  5. | 1 | 5 |
  6. | 2 | 4 |
  7. +----------+--------+

更改 ratings 数据库:

  1. $ mysql -u root -ppassword test -e "update ratings set rating=1 where reviewid=1;select * from ratings;"
  2. +----------+--------+
  3. | ReviewID | Rating |
  4. +----------+--------+
  5. | 1 | 1 |
  6. | 2 | 4 |
  7. +----------+--------+

向网格中注册 mysql 服务

当虚机启动时,将会自动被注册到网格中。然而,就像我们创建一个 Pod 一样,在更加便捷的访问之前需要创建一个 Service 。

  1. $ cat <<EOF | kubectl apply -f - -n vm
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. name: mysqldb
  6. labels:
  7. app: mysqldb
  8. spec:
  9. ports:
  10. - port: 3306
  11. name: tcp
  12. selector:
  13. app: mysqldb
  14. EOF

使用 mysql 服务

Bookinfo 中的 ratings 服务将使用该虚拟机上的数据库。为了验证它是否正常工作,请在虚拟机上创建使用 mysql 数据库的 ratings 服务第二个版本。然后指定路由规则,用于强制 review 服务使用 ratings 服务的第二个版本。

Zip

  1. $ kubectl apply -n bookinfo -f @samples/bookinfo/platform/kube/bookinfo-ratings-v2-mysql-vm.yaml@

创建强制 Bookinfo 使用 ratings 后端的路由规则:

Zip

  1. $ kubectl apply -n bookinfo -f @samples/bookinfo/networking/virtual-service-ratings-mysql-vm.yaml@

您可以验证 Bookinfo 应用程序的输出显示的是 Reviewer1 的 1 个星,还是 Reviewer2 的 4 个星,或者更改虚拟机的 ratings 服务并查看结果。

从虚机访问 Kubernetes 服务

在上面的示例中,我们将虚拟机视为一个服务。 您还可以在您的虚拟机中无缝调用 Kubernetes 的服务:

  1. $ curl productpage.bookinfo:9080
  2. ...
  3. <title>Simple Bookstore App</title>
  4. ...

Istio 的 DNS 代理自动为您的虚机配置 DNS , 允许我们 Kubernetes 的主机名进行访问。