- Istio是现目前流行的服务网格(service mess)方案。对比spring cloud微服务
- 减少开发团队的压力,运维来做
- 解藕,代码中不在依赖各类组件
- spring cloud 依赖包太大,啥事不干,几十MB
- Istio缺点:需要对k8s熟悉,配置起来较麻烦
- Istio有些功能无法实现的,如:分布式事务、配置中心
-
本例主要实现流量控制
-
安装
curl -L https://istio.io/downloadIstio | sh -
cd istio-1.8.0
- 加入到环境变量
vi ~/.profile
export PATH="$PATH:/home/your_home/istio-1.8.0/bin"
- Install Istio
istioctl install --set profile=demo -y
- 创建tomcat/jetty二个空服务
kubectl apply -f app.yaml
app.yaml
apiVersion: v1
kind: Namespace
metadata:
name: istio-example
---
# 暴露外网使用
apiVersion: v1
kind: Service
metadata:
name: app-svc
namespace: istio-example
spec:
selector:
app: web
type: NodePort
ports:
- port: 80
targetPort: 8080
nodePort: 30002
---
apiVersion: v1
kind: Service
metadata:
name: jetty-svc
namespace: istio-example
spec:
selector:
server: jetty
ports:
- port: 80
targetPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: tomcat-svc
namespace: istio-example
spec:
selector:
server: tomcat
ports:
- port: 80
targetPort: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: jetty-deployment
namespace: istio-example
labels:
app: web
server: jetty
spec:
replicas: 1
selector:
matchLabels:
app: web
server: jetty
template:
metadata:
labels:
app: web
server: jetty
spec:
containers:
- name: jetty
image: jetty:latest
ports:
- containerPort: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: tomcat-deployment
namespace: istio-example
labels:
app: web
server: tomcat
spec:
replicas: 1
selector:
matchLabels:
app: web
server: tomcat
template:
metadata:
labels:
app: web
server: tomcat
spec:
containers:
- name: tomcat
image: tomcat:latest
ports:
- containerPort: 8080
- 手工注入
kubectl apply -f <(istioctl kube-inject -f app.yaml)
- 查看注入结果, READY需2/2,实际上多注入了一个sidecar
$ kubectl get pods -n istio-example
NAME READY STATUS RESTARTS AGE
jetty-deployment-7444466f5d-jb2gn 2/2 Running 0 107s
tomcat-deployment-8d576b9fb-thzvh 2/2 Running 0 107s
- 创建网关
kubectl apply -f gateway.yaml
gateway.yaml
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
namespace: istio-example
name: istio-gateway
spec:
selector:
istio: ingressgateway # use istio default controller
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "*"
- 创建VirtualSerice。分配80%的流量到jetty,20%到tomcat
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
namespace: istio-example
name: app-vs
spec:
hosts:
- "*"
gateways:
- istio-gateway
http:
- route:
- destination:
host: jetty-svc
weight: 80
- destination:
host: tomcat-svc
weight: 20
- 导出Istio的入口网关(可理解为spring cloud gateway),NodePort端口
kubectl patch svc -n istio-system istio-ingressgateway -p '{"spec": {"type": "NodePort"}}'
查询nodePort
kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].nodePort}'
导出后,通过http://192.168.1.30:nodePort
访问网站
- Kiali dashboard。kiali地址http://192.168.1.30:20001/kiali
istioctl dashboard kiali --address 192.168.1.30
效果图
来源:oschina
链接:https://my.oschina.net/u/160697/blog/4770858