关闭
当前位置:首页 - 国际国内新闻 - 正文

南瓜子,SpringBoot、Kubernetes和Istio微服务网格演示源码,电子邮箱是什么

admin 2019-05-05 229°c

假如单纯运用kubernetes的pod布置Spring微服务,K8s的负载平衡以及署理设置和你微服务运用之间不是十分的智能联接,。无论如何,布置新的运用程序版别pod需求愈加软化的办法。以下是典型的需求:

  • 智能调拨流量,在布置新的运用程序版别容器时,您常常需求以某种份额(即金丝雀Canary测验)切割新容器和当时出产之间的流量
  • 蓝绿布置,在布置新的运用程序版别pod时,假定您需求蓝色/绿色布置
  • 在布置新的运用程序版别pod时,让咱们说只要HTTP cookie辨认的一些用户能够测验新版别

所有这些问题都能够经过名为istio的奇特东西来处理。

Istio装置

首要是在南瓜子,SpringBoot、Kubernetes和Istio微服务网格演示源码,电子邮箱是什么Istio装置之前,需求以至少4GB的内存发动 Minikube ,否则将无法发动pilot ,阅览stackoverflow评论;第二个重要的是一直运用Istio Custom Resources Definitions开端装置:

$ kubectl apply -f  /install/kubernetes/helm/istio/templates/crds.yaml

这是装置后所需的输出:

kubernetes tomask79$ kubectl get pods -n istio-system
NAME READY STATUS RESTARTS AGE
grafana-59b8896965-6jcb8 1/1 Running 4 着床出血7d
istio-citadel-856f994c58-jwdp2 1/1 Running 4 7d
istio-cleanup-secrets-glmrz 0/1 Completed 0 7d
istio-egressgateway-5649fcf57-5b885 1/1 Running 4 7d
istio-galley-7665f65c9c-89wsz 1/1 Running 13 7陪产假d
istio-grafana-post-install-z9v7z 0/1 Completed 0 7d
istio-ingressgateway-6755b9bbf6-qrvq8 1/1 Running 4 7d
istio-pilot-698959c67b-xpqnj 2/2 Running 11 7d
istio-policy-6fcb6d655f-8mkf4 2/2 Running 18 7d
istio-security-post-install-jn4sr 0/1 Completed 0 7d
istio-sidecar-injector-768c79f7bf-p8xqr 1/1 Running 4 7d
istio-telemetry-664d896cf5-zc8sr 2/2 Running 17 7d
istio-tracing-6b994895fd-wpc2c 1/1 Running 7 7d
prometheus-76b7745b64-hqnrq 1/1 Running 4 7d
servicegraph-5c4485945b-jql54 1/1 Running 12 7d

演示示例

在向您展现Istio流量办理戏法之前,咱们将介绍第一个十分简略的Spring Boot MVC运用程序,咱们将在两个版别中布置到kubernetes。

版别1:

@RestController
public class ControllerV1 {
@GetMapping(path = "/service")
public String getResult() {
return "Hello I'm V1!";
}
}

下面是这个版别的k8s的布置,符号mvc-service开端部分:

(mvc-1/istio/kubernetes-deploy/v1-deploy.yaml)

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: mvc-service
spec:
replicas: 1
template:
metadata:
labels:
app: mvc-service
version: v1
spec:
containers:
- name: mvc-service
image: service-v1:0.0.1-SNAPSHOT
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080

版别2:

@RestController
public class CfishontrollerV2 {
@GetMapping(path = "/service")
public String getResult() {
return "Hello i'm V2!";
}
}

下面是这个版别的k8s的布置,符号m果盘游戏vc-service开端部分:

(mvc-2/istio/kubernetes-deploylwscam/v2-deploy.yaml)

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: mvc-service-v2
spec:
replicas: 1
template:
metadata:
labels:
app: mvc-service
version: v2
spec:
containers:
- name: mvc-service
image: service-v2:0.0.1-SNAPSHOT
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080

咱们在标有“mvc-service”字符串的pod中有两个版别的运用程序。因而kubernetes服务还应该运用标签'mvc-service'来定位pod后端:(mvc-1/istio/kubernetes-deploy/v1-deploy.yaml)

apiVersion: 南瓜子,SpringBoot、Kubernetes和Istio微服务网格演示源码,电子邮箱是什么v1
kind: Service
metadata:
南瓜子,SpringBoot、Kubernetes和Istio微服务网格演示源码,电子邮箱是什么name: mvc-service
labels:
app: mvc-service
spec:
type: NodePort
ports:被挽救的姜戈
- port: 8080
name: http
selector:
app: mvc-service

运用Istio支撑布置到Kubernetes

好的,咱们现已预备好了kubernetes清单,但还没有布置任何东西!为了能够运用Istio流量办理,您需求向您的pod 注入sidec大明赋ar署理。假如没有istio边车,你就不会构成服务网格。你有两个挑选:

  • 经过istioctl 手动注入边车
  • 主动边车打针

我挑选了第一个选项并将边车sidecar设置注入到这样的清单中:

istioctl kube-inject 家宴-f v1-deploy.yaml >> v1-deploy-istio.yaml

关于第二个版别(文件夹mvc-2 / istio / kubernetes-deploy)相同这么做:

istioctl kube-inject -f v2-deploy.yaml >> v2-deploy-istio.yaml

现在布置生成的kubernetes清单注入istio-sidecar:

kubectl a三皇五帝pply -f v1-deploy-istio.yaml

然后布置第二个版别:

kubectl apply -f v2-deploy-istio.yaml

这是布置后所需的输出:

kubectl get pods
NAME READY STATUS RESTARTS AGE
mvc-service-76ffb4bc9f-sdrtn 2/2 Running 10 10d
mvc-service-v2-59ff7d6886-v87jt 2/2 Running 10 10d

每个POD都有两个容器,由于它运转app容器和istio署理边车容器。要查看为pod发动的边车署理,只需键入:

kubernetes - deploy tomask79 $ kubectl describe pod mvc - service - 76 ffb4bc9f - sdrtn

查看事情:

Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SandboxChanged 8m41s kubelet, minikube Pod sandbox changed, it will be killed and re-created.
Normal Pulled 8m37s kubelet, minikube Container image "docker.io/istio/proxy_init:1.0.5" already present on machine
Normal Created 8m35s kubelet, minikube Created container
Normal Started 8m34s kubelet, minikube Started container
Normal Pulled 8m34s kubelet, minikube Container image "service-v1:0.0.1-SNAPSHOT" already present on machine
Normal Created 8m33s kubelet, minikube Created container
Normal Started 8m33s kubelet, minikube Started container
Normal Pulled 8m33s kubelet, minikube Container image "docker.io/istio/proxyv2:1.0.5" already present on machine
Normal Created 8m33s kubelet, minikube Created container
Normal Started 8m33s kubelet, minikube Started container

Istio构成服务网格

好的,布置了两个版别的Spring Boot MVC运用程序。现在我打赌你对像VirtualService,DestinationRule这样的Istio方针感到困惑......什么时候运用它们,你还在Kubernetes服务吗?在stackoverflow有一个十分好的评论,这对我来说十分有用。简而言之:

在咱们的Spring MVC演示中,咱们获得了名为mvc-service的kubernete高占武导弹s 服务。这将是DestinationRule 方针中的主机参数,由于这是供给方针服务的后端。现在咱们的mvc-service供给了两个版别的Spring MVC运用程序v1和v2 ,它们将构成服务网格,因而DestinationRule看起来像:

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: mvc-service
spec:
host: mvc-service
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2

在咱们的演示中,经过kubectl布置它:

spring-kubernetes-istio tomask79$ kubectl apply -f ppssppistio-destionation-rule.yaml 

现在VirtualService进入游戏:

VirtualService界说了一组要在主机被寻址时运用的流量路由规矩。每个路由规矩界说特定协议的流量的匹配规范。假如流量匹配,则将其发送到注册表中界说的命名方针服务(或其子集 /版别)。

换句话说,您首要布置K8s布置和服务。然后,经过Istio DestionRule界说微服务的网络,然后经过VirtualService设置HTTP路由规矩。

用Istio进行金丝雀测验

因而,假定咱们的演示SpringMVC运用程序的第二个版别(在服务网格中,子集v2)不行安稳,无法处理满负载,因而咱们只将20%的流量路由到它。

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: service-gateway
spec:
hosts:
- "*"
gateways:
- service-gateway
http:
- match:
- 白灵uri:
exact: /service
route:
- destination:
host: mvc-service
subset: v1
weight: 80
- destination:
host: mvc-service
subset: v2
weight: 20

这真的很简单。作为最终一步,咱们需求揭露服务网关,即Istio-Ingress网关 ,它从服务网格外部获取流量并将其转发到该网关。

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: service-gateway
spec:
selector:
istio: ingressgateway # use istio default controller
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "*"

在这个repo中说到的网关和虚拟服务都在istio-gateway.yaml文件中,所以让咱们布置它:

spring-kubernetes-istio tomask79$ kubectl apply -f istio-gateway.yaml 

好的,这是测验之前所需的输出,服务网格在前:

istioctl get destinationrules -o yaml
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"networking.istio.io/v1alpha3","kind":"DestinationRule","metadata":{"annotations":{},"name":"mvc-service","namespace":"default"},"spec":{"host":"mvc-service","subsets":[{"labels":{"version":"v1"},"name":"v1"},{"labels":{"version":"v2"},"name":"v2"}]}}
creationTimestamp: null
name: mvc-service
namespace: default
resourceVersion: "5722"
spec:
host: mvc-service
subsets:
- 南瓜子,SpringBoot、Kubernetes和Istio微服务网格演示源码,电子邮箱是什么labels:
version: v1
name: v1
- labels:
version: v2
name: v2
---

然后是virtualservices:

istioctl get virtualservices -o short
VIRTUAL-SERVICE NAME GATEWAYS HOSTS #HTTP #TCP NAMESPACE AGE
mvc-service mvc-service 1 0 default 11d
service-gateway service-gateway * 1 0 default 11d

金丝雀测验

咱们需求获取istio-ingress网关和端口的IP地址。

kubectl get service istio-ingressgateway -n istio-system
NAME TYPE C合生元LUSTER-IP EXTERNAL-IP PORT(S) AGE
istio-ingressgateway LoadBalancer 10.111.15.19 80:31380/TCP,443:31390/TCP,31400:31南瓜子,SpringBoot、Kubernetes和Istio微服务网格演示源码,电子邮箱是什么400/TCP,15011:32464/TCP,8060:30626/TCP,853:30365/TCP,15030:31121/TCP,15031:31359/TCP 13d

istio-ingress文档如是说:

假如设置了EXTERNAL-IP值,则您的环境具有可 用于进口网关的外部负载平衡器。假如EXTERNAL-IP值为(或永久),则您的环境 不会为进口网关供给外部负载平衡器。在这种情况下,您能够 运用service s节点端口拜访网关。

这意味着调用咱们的金丝雀版别,咱们将持续:

 minikube ip
19安徽移动2.168.99.110

现在总算调用了后台,请注意文档中指出的nodeport端口:

10个点击中:2个进入V2版别,8个点击进入V1。

tomask79:spring-kubernetes-istio tomask79$ curl http://192.168.99.110:31380/service
Hello I'm V1!tomask79:spring-kubernetes-istio tomask79$ curl http://192.168.99.110:31380/service
Hello I'm V1!tomask79:spring-kubernetes-istio tomask南瓜子,SpringBoot、Kubernetes和Istio微服务网格演示源码,电子邮箱是什么79$ curl http://192.168.99.110:31380/service
Hello I'm V1!tomask79:spring-kubernetes-istio tomask79$ curl 重视http://192.168.99.110:31380/service
Hello i'm V2!tomask79:sprin南瓜子,SpringBoot、Kubernetes和Istio微服务网格演示源码,电子邮箱是什么g-kubernetes-istio tomask79$ curl http://192.168.99.110:31380/service
Hello I'm V1!tomask79:spring-kubernetes-istio tomask79$ curl http://192.168.99.110:31380/service
Hello I'm V1!tomask79:spring-kubernetes-istio tomask79$ curl http://192.168.99.110:31380/service
Hello I'm V1!tomask79:spring-kubernetes-istio tomask79$ curl http://192.168.99.110:31380/service
Hello I'm V1!tomask79:spring-kubernetes-istio tomask79$ curl http://192.168.99.110:31380/service
Hello i'm V2!tomask79:spring-kubernetes-istio tomask79$ curl http://192.168.99.110:31380/service
Hello I'm V1!tomask79:spring-kubernetes-istio tomask79$

运用Istio进行蓝/绿布置

现在让咱们说咱们的运用程序的V2版别满足安稳,咱们能够将100%的流量路由到它。要运用Istio完成这一点,咱们将更改VirtualService中的规矩:

将istio-gateway.yaml文件中的VirtualService修改为:

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: service-gateway
spec:
hosts:
- g7506"*"
gateways:
- service-gateway
http:
- match:
- uri:
exact: /service
route:
- destination:
host: mvc-service
subset: v2

重新布置:

tomask79:spring-kubernetes-istio tomask79$ kubectl apply -f istio-gateway.yaml 
gateway.networking.istio.io/service-gateway unchanged
virtualservice.networking.istio.io/service-gateway configured

测验蓝/绿布置

现在V2版别应该成为咱们的出产版别并处理100%的流量:

tomask79:spring-kubernetes-istio tomask79$ curl http://192.168.99.110:31380/service
Hello i'm V2!tomask79:spring-kubernetes-istio tomask79$ curl http://192.168.99.110:31380/service
Hello i'm V2!tomask79:spring-kubernetes-istio tomask邱晨79$ curl上吐下泻 http://192.168.99.110:31380/service
Hello i'm V2!tomask79:spring-kubernetes-istio tomask79$ curl http://192.168.99.110:31380/service
Hello i'm V2!tomask79:spring-kubernetes-istio tomask79$ curl http://192.168.99.110:31380/service
Hello i'm V2!tomask79:spring-kubernetes-istio tomask79$ curl http://192.168.99.110:31380/service
Hello i'm V2!tomask79:spring-kubernetes-istio tomask79$ curl http://192.168.99.110:31380/service
Hello i'm V2!tomask79:spring-kubernetes-istio tomask79$ curl http://192.168.99.110:31380/service
Hello i'm V2!tomask79:spring-kubernetes-istio tomask79$ curl http://192.168.99.110:31380/service
Hello i'm V2!tomask79:spring-kubernetes-istio tomask79$ curl http://192.168.99.110:31380/service
Hello i'm V2!tomask79:spring-kubernetes-istio tomask79$

总结

Istio看起来对我来说是超级强壮的东西。但它的学习曲线有点长。此外,他们还在装备模型之间进行了严重更改。无论如何,他们支撑粘性会话乃至websockets。例如,关于我在EmbedIT中运用的体系,这是两个“有必要具有”的东西。像istio这样的另一个相似东西是Li煎牛排的家常做法nkerd。你也能够看一下:)

admin 14文章 0评论 主页

相关文章

  用户登录