Istio,希腊语,意思就跟他的图标一样,意扬帆起航。据说作者为了取名,翻阅了几个小时的希腊字段,主要是为了沿用k8s建立的航海主题,帆船与船舵相互配合才能扬帆起航。
官方对 Istio 的介绍浓缩成了一句话:An open platform to connect, secure, control and observe services. 可以说Istio是用来处理服务间通信的基础设施层,这个简介也介绍了Istio的四个重要的功能:
经过Istio社区的演进和迭代,Istio已经慢慢开始可以应用于生产,比如:
对于Java的开发者来说,dubbo也是一个非常流行的微服务框架,如何将dubbo服务通过k8s和istio部署与管理是个问题。本文介绍下一种dubbo与istio共生方法,并进行流量管理的例子。
customer首先建立一个简单的customer,main函数主要是定时去调用provider的sayHello方法
packagecom.alibaba.dubbo.demo.consumer;
importcom.alibaba.dubbo.demo.DemoService;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
importjava.util.Date;
publicclassConsumer{
publicstaticvoidmain(String[]args) {
System.setProperty("java.net.preferIPv4Stack","true");
ClassPathXmlApplicationContextcontext=newClassPathXmlApplicationContext(newString[]{"META-INF/spring/dubbo-demo-consumer.xml"});
context.start();
// get remote service proxy
DemoServicedemoService=(DemoService)context.getBean("demoService");
while(true) {
try{
Thread.sleep(5000);
// call remote method
Stringhello=demoService.sayHello("world");
// get result
System.out.println(hello "-" newDate());
}catch(Throwablethrowable) {
throwable.printStackTrace();
}
}
}
}
附上customer的xml配置
<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
don't set it same as provider -->
<dubbo:applicationname="demo-consumer"/>
<!-- use multicast registry center to discover service -->
<dubbo:registryaddress="multicast://224.5.6.7:1234"register="false"/>
<!-- url要写上provider在istio运行的服务名字 -->
<dubbo:referenceid="demoService"check="false"interface="com.alibaba.dubbo.demo.DemoService"
url="dubbo://dubbo-demo-provider:20880"/>
</beans>
使用maven打包后放进tomcat的镜像
mvn clean && mvn package-Dmaven.test.skip=true
Dockerfile示例
FROMtomcat
COPYdubbo-demo-consumer.jar dubbo-demo-consumer.jar
RUNcd /usr/local/tomcat/bin/
ENVJAVA_OPTS="-Xms2048m -Xmx2048m "
ENTRYPOINT[ "sh", "-c", "java $JAVA_OPTS -jar dubbo-demo-consumer.jar" ]
我已经打包好一个镜像,镜像地址:shuxnhs/dubbo-mesh-consumer:v1.1
provider同样创建一个简单的provider,实现下sayHello方法
v1版本返回:Hello world, response from provider: ip
v2版本返回:Hello world, I'm V2, response from provider: ip
packagecom.alibaba.dubbo.demo.provider;
importcom.alibaba.dubbo.demo.DemoService;
importcom.alibaba.dubbo.rpc.RpcContext;
importjava.text.SimpleDateFormat;
importjava.util.Date;
publicclassDemoServiceImplimplementsDemoService{
@Override
publicStringsayHello(Stringname) {
return"Hello " name ", I'm V2, response from provider: " RpcContext.getContext().getLocalAddress();
}
}
附上provider的xml配置
<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- provider's application name, used for tracing dependency relationship -->
<dubbo:applicationname="demo-provider"/>
<!-- use multicast registry center to export service -->
<dubbo:registryaddress="multicast://224.5.6.7:1234"register="false"/>
<!-- use dubbo protocol to export service on port 20880 -->
<dubbo:protocolname="dubbo"port="20880"/>
<!-- service implementation, as same as regular local bean -->
<beanid="demoService"class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>
<!-- declare the service interface to be exported -->
<dubbo:serviceinterface="com.alibaba.dubbo.demo.DemoService"ref="demoService"/>
</beans>
同样方法打包镜像
v1版本provider: shuxnhs/dubbo-mesh-provider:v1.0
v2版本provider: shuxnhs/dubbo-mesh-provider:v2.0
Istio中部署注入customer.yaml
apiVersion:apps/v1
kind:Deployment
metadata:
name:dubbo-demo-consumer-deployment
spec:
replicas:4
selector:
matchLabels:
app:dubbo-demo-consumer
template:
metadata:
name:dubbo-demo-consumer
labels:
app:dubbo-demo-consumer
spec:
containers:
-name:dubbo-demo-consumer
image:shuxnhs/dubbo-mesh-consumer:v1.1
imagePullPolicy:Always
command: ["sh","-c","java $JAVA_OPTS -jar dubbo-demo-consumer.jar "]
Provider.yaml
apiVersion:apps/v1
kind:Deployment
metadata:
name:dubbo-demo-provider-deployment-v1
spec:
replicas:1
selector:
matchLabels:
app:dubbo-demo-provider
version:v1
template:
metadata:
name:dubbo-demo-provider
labels:
app:dubbo-demo-provider
version:v1
spec:
containers:
-name:dubbo-demo-provider
image:shuxnhs/dubbo-mesh-provider:v1.0
imagePullPolicy:Always
command: ["sh","-c","java $JAVA_OPTS -jar dubbo-demo-provider.jar "]
---
apiVersion:apps/v1
kind:Deployment
metadata:
name:dubbo-demo-provider-deployment-v2
spec:
replicas:1
selector:
matchLabels:
app:dubbo-demo-provider
version:v2
template:
metadata:
name:dubbo-demo-provider
labels:
app:dubbo-demo-provider
version:v2
spec:
containers:
-name:dubbo-demo-provider
image:shuxnhs/dubbo-mesh-provider:v2.0
imagePullPolicy:Always
command: ["sh","-c","java $JAVA_OPTS -jar dubbo-demo-provider.jar "]
---
apiVersion:v1
kind:Service
metadata:
name:dubbo-demo-provider
labels:
app:dubbo-demo-provider
spec:
ports:
-name:dubbo
protocol:TCP
port:20880
targetPort:20880
selector:
app:dubbo-demo-provider
istioctl注入
istioctl kube-inject-fcustomer.yaml | kubectl apply-f-
istioctl kube-inject-fprovider.yaml | kubectl apply-f-
创建对应的VirtualService和DestinationRule对dubbo的流量进行控制,对两个provider分v1和v2版本,对将流量各自50%
apiVersion:networking.istio.io/v1beta1
kind:VirtualService
metadata:
name:dubbo-demo-provider-route
spec:
hosts:
-dubbo-demo-provider
tcp:
-route:
-destination:
host:dubbo-demo-provider
port:
number:20880
subset:v2
weight:50
-destination:
host:dubbo-demo-provider
port:
number:20880
subset:v1
weight:50
---
apiVersion:networking.istio.io/v1beta1
kind:DestinationRule
metadata:
name:dubbo-demo-provider-destination
spec:
host:dubbo-demo-provider
subsets:
-name:v1
labels:
version:v1
-name:v2
labels:
version:v2
查看kiali
调整各自的权重为90%和10%,重启constomer
总结展望Istio的功能很强大,如果将dubbo服务迁移到istio,即需要将dubbo的流量治理等能力下沉到Istio去处理,把dubbo即变成一个普通的服务,而且改造的过程也面临了几个问题:
Dubbo服务迁移上Istio的工作还是任重而道远,在真实的业务系统中的架构复杂度很大,不是上述demo这种hello-world小例子能比,实际的改造难度是很大的。或许未来的某一天Istio社区或者dubbo社区会提供一个完美的解决方案,但K8s Istio是未来微服务架构的方向这是毋庸置疑的。
Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved