Bookinfo 应用分为四个单独的微服务:
- productpage :productpage 微服务会调用 details 和 reviews 两个微服务,用来生成页面。
- details :这个微服务包含了书籍的信息
- reviews :这个微服务包含了书籍相关的评论。它还会调用 ratings 微服务。
- ratings :ratings 微服务中包含了由书籍评价组成的评级信息。
reviews 微服务有 3 个版本:
- v1 版本不会调用 ratings 服务。
- v2 版本会调用 ratings 服务,并使用 5个黑色五角星来显示评 分信息。
- v3 版本会调用 ratings 服务,并使用5个红色五角星 来显示评 分信息。
我们分为三个目标;
1、部署bookinfo服务。
kubectl create ns bookinfo
#自动注入
kubectl label namespace bookinfo istio-injection=enabled
cd istio-1.4.2/samples/bookinfo
kubectl apply -f platform/kube/bookinfo.yaml -n bookinfo
查看bookinfo状态
kubectl get pod -n bookinfo
details-v1-78d78fbddf-zthb5 2/2 Running 0 3m22s
productpage-v1-596598f447-85lzq 2/2 Running 0 3m19s
ratings-v1-6c9dbf6b45-r2s4b 2/2 Running 0 3m21s
reviews-v1-7bb8ffd9b6-zn6f7 2/2 Running 0 3m21s
reviews-v2-d7d75fff8-rcj6f 2/2 Running 0 3m20s
reviews-v3-68964bc4c8-2jskz 2/2 Running 0 3m20s
2、部署gateway
kubectl apply -f networking/bookinfo-gateway.yaml -n bookinfo
确定访问端口:
[root@master networking]# kubectl get svc -n istio-system |grep istio-ingressgateway
istio-ingressgateway LoadBalancer 10.0.0.102 <pending> 15020:35582/TCP,80:31513/TCP,443:48478/TCP,15029:44880/TCP,15030:45669/TCP,15031:48045/TCP,15032:30890/TCP,15443:32749/TCP 36d
查看得知,通过80端口转发对外的nodeport端口为31513,
我们的bookinfo访问地址为:http://192.168.25.137:31513/productpage
3、通过域名访问:
此时我们通过ip访问httpbin和bookinfo有些不方便,我们需要通过域名访问来区分2个项目。
首先我们再本地hosts选择一个节点来添加域名解析:
编辑C:\Windows\System32\drivers\etc\hosts,新增如下
192.168.25.137 httpbin.liaochao.com bookinfo.liaochao.com
我们部署一个nginx,在nginx的配置中指定进行反向代理。
在虚拟机中直接yum install nginx,
修改nginx配置文件,增加反向配置
upstream ingressgateway {
#server 192.168.25.136:31513;
server 192.168.25.137:31513;
}
server {
listen 80;
server_name _;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://ingressgateway;
proxy_set_header Host $host;
proxy_http_version 1.1;
}
重载nginx
systemctl reload nginx
此时我们已经可以通过域名访问了,httpbin.liaochao.com bookinfo.liaochao.com .
但是因为域名都是指向的同一个ip,所以每个域名都能访问同一个项目,不能进行域名分流,所以我们需要将项目进行域名绑定。
修改httpbin的网关配置,httpbin-gateway.yaml
在hosts: 这里指定域名即可。
[root@master httpbin]# cat httpbin-gateway.yaml
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: httpbin-gateway
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "httpbin.liaochao.com"
....
同理,修改bookinfo的gateway配置
[root@master networking]# cat bookinfo-gateway.yaml
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: bookinfo-gateway
spec:
selector:
istio: ingressgateway # use istio default controller
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "bookinfo.liaochao.com"
....
更新配置:
kubectl apply -f httpbin-gateway.yaml
kubectl apply -f bookinfo-gateway.yaml
此时通过域名bookinfo.liaochao.com已经不能访问httpbin的项目了。
此时通过域名httpbin.liaochao.com已经不能访问bookinfo的项目了。