OpenShift / RHEL / DevSecOps 汇总目录
说明:本文已经在OpenShift 4.10 环境中验证
文章目录
说明
本文将部署一个基于 Serverless 的 Web 应用。这个应用程序使用了5个不同的微服务和前端应用UI,应用UI可以选择通过API Manager或直接连接到这些服务。其中三个微服务使用MySQL来保存和查询数据,一个服务(BankingService)连接到ActiveMQ来发送消息到一个队列,另一个服务(ExchangeRateService)使用Redis缓存结果。
部署应用
部署应用前需要现在环境中安装 OpenShift Serverless Operator,并配置了 knative-serving 环境。
执行命令,创建项目:
$ oc new-project dev
部署 Loan 服务
执行命令,基于 Git 资源部署名为 loan-val-service 的 Knative 服务。
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/LoanValService/master/configs/image.yaml
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/LoanValService/master/configs/bc.yaml
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/LoanValService/master/configs/serverless.yaml
部署 Banking 服务
执行命令,部署运行 MySQL 的 banking-db 数据库。
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/BankingService/master/configs/db-secret.yaml
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/BankingService/master/configs/banking-db-deployment.yaml
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/BankingService/master/configs/banking-db-srv.yaml
执行命令,进入名为 banking-db 的 DeploymentConfig 对应的 pod。
$ BANKING_POD=$(oc get pod -o custom-columns=POD:metadata.name --field-selector status.phase=Running --no-headers | grep banking-db)
$ oc rsh $BANKING_POD
sh-4.4$ mysql -u root
然后在其中运行 SQL 创建表和添加数据。
CREATE SCHEMA `bankaccounts` ;
CREATE TABLE `bankaccounts`.`accounts` (
`id` INT NOT NULL AUTO_INCREMENT,
`account_no` VARCHAR(45) NULL,
`balance` DOUBLE NULL,
`currency` VARCHAR(45) NULL,
PRIMARY KEY (`id`));
INSERT INTO `bankaccounts`.`accounts` (`id`, `account_no`, `balance`, `currency`) VALUES ('1', '123456-1', '1300', 'EGP');
INSERT INTO `bankaccounts`.`accounts` (`id`, `account_no`, `balance`, `currency`) VALUES ('2', '123456-2', '888', 'USD');
INSERT INTO `bankaccounts`.`accounts` (`id`, `account_no`, `balance`, `currency`) VALUES ('3', '2323445-1', '12300', 'EGP');
commit;
CREATE TABLE `bankaccounts`.`transactions` (
`transaction_id` INT NOT NULL AUTO_INCREMENT,
`account_no` VARCHAR(45) NULL,
`transaction` DOUBLE NULL,
`date` DATETIME NULL,
`transaction_details` VARCHAR(45) NULL,
PRIMARY KEY (`transaction_id`));
INSERT INTO `bankaccounts`.`transactions` (`transaction_id`, `account_no`, `transaction`, `date`, `transaction_details`) VALUES ('1', '123456-1', '-100', '2019-01-19 14:55:02', 'ATM withdraw');
INSERT INTO `bankaccounts`.`transactions` (`transaction_id`, `account_no`, `transaction`, `date`, `transaction_details`) VALUES ('2', '123456-1', '300', '2019-04-19 10:55:02', 'Cash deposit');
INSERT INTO `bankaccounts`.`transactions` (`transaction_id`, `account_no`, `transaction`, `date`, `transaction_details`) VALUES ('3', '123456-2', '888', '2019-05-10 15:55:02', 'Account Opening');
INSERT INTO `bankaccounts`.`transactions` (`transaction_id`, `account_no`, `transaction`, `date`, `transaction_details`) VALUES ('4', '2323445-1', '12500', '2019-08-23 19:55:02', 'Account Opening');
INSERT INTO `bankaccounts`.`transactions` (`transaction_id`, `account_no`, `transaction`, `date`, `transaction_details`) VALUES ('5', '2323445-1', '-200', '2019-12-19 22:55:02', 'Cash wihdraw');
commit;
执行命令,基于 Git 资源部署名为 banking-service 的 Knative 服务。
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/BankingService/master/configs/image.yaml
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/BankingService/master/configs/build-config.yaml
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/BankingService/master/configs/banking-serverless.yaml
部署 CustomerData 服务
执行命令,部署运行 MySQL 的 customerdata-db 数据库。
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/CustomerDataService/master/configs/db-secret.yaml
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/CustomerDataService/master/configs/customerdata-db-deployment.yaml
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/CustomerDataService/master/configs/customerdata-db-srv.yaml
执行命令,进入名为 customerdata-db 的 DeploymentConfig 对应的 pod。
$ CUSTOMER_POD=$(oc get pod -o custom-columns=POD:metadata.name --field-selector status.phase=Running --no-headers | grep customer-data-db)
$ oc rsh $CUSTOMER_POD
sh-4.4$ mysql -u root
然后在其中运行 SQL 创建表和添加数据。
CREATE SCHEMA `customers` ;
CREATE TABLE `customers`.`customer` (
`id` INT NOT NULL AUTO_INCREMENT,
`firstName` VARCHAR(45) NULL,
`lastName` VARCHAR(45) NULL,
`email` VARCHAR(45) NULL,
`city` VARCHAR(45) NULL,
`state` VARCHAR(45) NULL,
`birthday` VARCHAR(45) NULL,
PRIMARY KEY (`id`));
INSERT INTO `customers`.`customer` (`id`, `firstName`, `lastName`, `email`, `city`,`state`, `birthday`) VALUES ('1', 'Osama', 'Oransa', 'osa.ora@acme.com', 'Cairo','No', '2000-01-09');
INSERT INTO `customers`.`customer` (`id`, `firstName`, `lastName`, `email`, `city`,`state`, `birthday`) VALUES ('2', 'Sameh', 'Ahmed', 'sa.or@acme.com', 'Dubai','Yes', '2001-02-20');
commit;
CREATE TABLE `customers`.`customer_accounts` (
`customer_id` INT NOT NULL,
`account_no` VARCHAR(45) NOT NULL,
`type` VARCHAR(45) NULL,
PRIMARY KEY (`customer_id`, `account_no`));
INSERT INTO `customers`.`customer_accounts` (`customer_id`, `account_no`, `type`) VALUES ('1', '123456-1', '100');
INSERT INTO `customers`.`customer_accounts` (`customer_id`, `account_no`, `type`) VALUES ('1', '123456-2', '101');
INSERT INTO `customers`.`customer_accounts` (`customer_id`, `account_no`, `type`) VALUES ('2', '2323445-1', '100');
commit;
执行命令,基于 Git 资源部署名为 customerdata-service 的 Knative 服务。
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/CustomerDataService/master/configs/image.yaml
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/CustomerDataService/master/configs/build-config.yaml
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/CustomerDataService/master/configs/customerdata-serverless.yaml
部署 UserAccount 服务
执行命令,部署运行 MySQL 的 customer-db 数据库。
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/UserAccountService/master/configs/db-secret.yaml
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/UserAccountService/master/configs/customer-db-deployment.yaml
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/UserAccountService/master/configs/customer-db-service.yaml
执行命令,进入名为 user-accounts-db 的 DeploymentConfig 对应的 pod。
$ USER_ACCOUNT_POD=$(oc get pod -o custom-columns=POD:metadata.name --field-selector status.phase=Running --no-headers | grep user-accounts-db)
$ oc rsh $USER_ACCOUNT_POD
sh-4.4$ mysql -u root
然后在其中运行 SQL 创建表和添加数据。
CREATE SCHEMA `accounts` ;
CREATE TABLE `accounts`.`account` (
`id` INT NOT NULL AUTO_INCREMENT,
`login` VARCHAR(45) NULL,
`password` VARCHAR(45) NULL,
`email` VARCHAR(45) NULL,
PRIMARY KEY (`id`));
INSERT INTO `accounts`.`account` (`id`, `login`, `password`, `email`) VALUES ('1', 'Osama', '123', 'osa.ora@acme.com');
INSERT INTO `accounts`.`account` (`id`, `login`, `password`, `email`) VALUES ('2', 'Sameh', '123', 'sa.or@acme.com');
commit;
执行命令,基于 Git 资源部署名为 useraccount-service 的 Knative 服务。
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/UserAccountService/master/configs/image.yaml
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/UserAccountService/master/configs/build-config.yaml
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/UserAccountService/master/configs/useraccount-serverless.yaml
部署 ExchangeRate 服务
执行命令,部署 Redis 环境。
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/ExchangeRateService/master/configs/secret.yaml
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/ExchangeRateService/master/configs/redis-exchange.yaml
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/ExchangeRateService/master/configs/redis-exchange-srv.yaml
执行命令,基于 Git 资源部署名为 exchange-service 的 Knative 服务。
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/ExchangeRateService/master/configs/configmap.yaml
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/ExchangeRateService/master/configs/image.yaml
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/ExchangeRateService/master/configs/build-config.yaml
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/ExchangeRateService/master/configs/exchange-serverless.yaml
部署 AcmeInternetBanking 应用
执行命令,基于 Git 资源部署名为 my-gui-app 的应用。
$ oc new-app --name=my-gui-app jboss-webserver31-tomcat8-openshift:1.4~https://github.com/liuxiaoyu-git/AcmeInternetBankingApp
$ oc expose service my-gui-app
$ oc apply -f https://raw.githubusercontent.com/liuxiaoyu-git/AcmeInternetBankingApp/master/configs/configmap.yaml
$ oc set env deployment/my-gui-app --from configmap/env-settings
访问应用
当完成上述部署后,在 OpenShift 中可以看到以下部署资源。
获得应用访问地址:
$ oc get route my-gui-app --template='http://{{ .spec.host }}/MyInternetBankingApp-1.0-SNAPSHOT/'
http://my-gui-app-dev.apps.cluster-gwdv8.gwdv8.sandbox518.opentlc.com/MyInternetBankingApp-1.0-SNAPSHOT/
用浏览器访问上述地址,然后用 Osama/123 登录应用。
最后可以看到应用的页面功能,包括 Customer,Account、Loan,Exchange 等功能。
参考
https://github.com/liuxiaoyu-git/serverless-ocp-demo