如何在Kubernetes上使用Istio服务网格设置Java微服务

Øriginally published in deepu.tech.

Originally published at Medium on 17-Nov-2018. This post has been updated since to work with the latest version of JHipster(6.3.0) and Istio(1.3.0).


Istio is the coolest kid on the DevOps and Cloud block now. For those of you who aren’t following close enough — 一世stio is a service mesh for distributed application architectures, especially the ones that you run on the cloud with Kubernetes. Istio plays extremely nice with Kubernetes, so nice that you might think that it’s part of the Kubernetes platform.

如果您仍然想知道,到底什么是服务网格或Istio? 接下来让我们来概述一下Istio。

Istio

Istio在分布式应用程序体系结构中提供以下功能:

  • Service discovery — Traditionally provided by platforms like Netflix Eureka or Consul.

  • Automatic load balancing — You might have used Netflix Zuul for this.

  • Routing, circuit breaking, retries, fail-overs, fault injection — Think of Netflix Ribbon, Hytrix and so on.

  • Policy enforcement for access control, rate limiting, A/B testing, traffic splits, and quotas — Again you might have used Zuul to do some of these.

  • Metrics, logs, and traces — Think of ELK or Stack driver

  • Secure service-to-service communication

下面是Istio的体系结构。

Istio architecture一世stio architecture

它可以分为2个不同的平面。

data plane: Is made of Envoy proxies deployed as sidecars to the application containers. They control all the incoming and outgoing traffic to the container.

控制平面:使用Pilot来管理和配置代理以路由流量。 它还将Mixer配置为强制执行策略并收集遥测。 它还具有其他组件,例如用于管理安全性的Citadel和用于管理配置的Galley。

Istio can also configure an instance of Grafana, Prometheus, Jaeger, and Kiali for Monitoring and Observability. You can use this or use your existing monitoring stack as well if you wish to do so.

希望本文提供了Istio的概述,现在让我们关注本文的目标。

Preparing the Kubernetes cluster

首先,让我们准备一个Kubernetes集群以部署Istio和我们的应用程序容器。 请按照您喜欢的任何平台上的说明进行操作。

Prerequisites

We will be using Helm to install Istio on the Kubernetes cluster and kubectl for deploying the applications.

Helm: The Kubernetes package manager. Install it.

kubectl: The command-line tool to interact with Kubernetes. Install and configure it.

Create a cluster on Azure Kubernetes Service(AKS)

If you are going to use Azure, then install Azure CLI to interact with Azure. Install and login with your Azure account (you can create a free account if you don’t have one already). If not skip this section.

首先,让我们创建一个资源组。 您可以在这里使用任何您喜欢的地区,而不是美国东部。

$ az group create --name eCommerceCluster --location eastus

创建Kubernetes集群:

$ az aks create \
  --resource-group eCommerceCluster \
  --name eCommerceCluster \
  --node-count 4 \
  --kubernetes-version 1.13.7 \
  --enable-addons monitoring \
  --generate-ssh-keys

的节点数该标志很重要,因为安装程序至少需要四个带有默认CPU的节点才能运行所有内容。 您可以尝试使用更高的kubernetes版本如果受支持,则坚持1.13

创建集群可能需要一些时间,因此请放松休息。 🍹

创建集群后,通过运行以下命令从kubectl获取其凭据。 它会自动将凭据注入到您的kubectl配置下〜/ .kube /配置

$ az aks get-credentials \
  --resource-group eCommerceCluster \
  --name eCommerceCluster

您可以在Azure门户中查看创建的群集:

Kubernetes cluster in AKSķubernetes cluster in AKS

跑kubectl获取节点在命令行中查看它,并验证kubectl是否可以连接到您的集群。

Cluster NodesCluster Nodes

Proceed to the Install and setup Istio section.

Create a cluster on Google Kubernetes Engine(GKE)

If you are going to use Google Cloud Platform(GCP) then install Gcloud CLI to interact with GCP. Install and login with your GCP account (you can create a free account if you don’t have one already).

您可以使用以下命令设置区域和区域,也可以在执行每个命令时通过zone选项。

$ gcloud config set compute/region europe-west1
$ gcloud config set compute/zone europe-west1-b

首先,我们需要一个GCP项目,您可以使用现有的项目,也可以使用GCloud CLI通过以下命令创建一个新项目:

$ gcloud projects create jhipster-demo-deepu

将您要使用的项目设置为默认项目:

$ gcloud config set project jhipster-demo-deepu

现在,让我们使用以下命令为我们的应用程序创建集群:

$ gcloud container clusters create hello-hipster \
    --cluster-version 1.13 \
    --num-nodes 4 \
    --machine-type n1-standard-2

的数量节点和机器的种类由于设置需要至少四个具有更大CPU的节点才能运行所有内容,因此标记很重要。 您可以尝试使用更高的集群版本如果受支持,则坚持1.13。

创建集群可能需要一些时间,因此请放松休息。 🍹

创建集群后,通过运行以下命令从kubectl获取其凭据。 它会自动将凭据注入到您的kubectl配置下〜/ .kube /配置

$ gcloud container clusters get-credentials hello-hipster

您可以在GCP GUI中查看创建的集群。

Kubernetes cluster on GKEķubernetes cluster on GKE

跑kubectl获取节点在命令行中查看它,并验证kubectl是否可以连接到您的集群。

Cluster NodesCluster Nodes

Install and setup Istio

请按照以下步骤在本地计算机上安装Istio:

$ cd ~/

$ export ISTIO_VERSION=1.3.0

$ curl -L https://git.io/getLatestIstio | sh -

$ ln -sf istio-$ISTIO_VERSION istio

$ export PATH=~/istio/bin:$PATH

首先,在Kubernetes集群上为Istio创建角色绑定。

$ kubectl create clusterrolebinding cluster-admin-binding \
  --clusterrole=cluster-admin \
  --user="$(gcloud config get-value core/account)"

让我们为Istio创建一个名称空间。

$ kubectl create namespace istio-system

现在,让我们使用Istio提供的头盔图表将Istio安装在Kubernetes集群上。

$ cd ~/istio-$ISTIO_VERSION

# Install the Istio CRDs
$ helm template install/kubernetes/helm/istio-init --name istio-init --namespace istio-system | kubectl apply -f -

# Run this to verify all CRDs are installed. It should output 23 for this version of Istio.
$ kubectl get crds | grep 'istio.io\|certmanager.k8s.io' | wc -l

# Install the Istio demo set up so that we get Grafana, Jaeger & Kiali set up as well.
# For production, use the Istio default setup. Refer https://istio.io/docs/setup/kubernetes/install/helm/
$ helm template install/kubernetes/helm/istio --name istio --namespace istio-system \
    --values install/kubernetes/helm/istio/values-istio-demo.yaml | kubectl apply -f -

等待Pod运行,这些Pod将部署到组织系统命名空间。

$ watch kubectl get pods -n istio-system

窗格处于运行状态后,请退出监视循环并运行以下内容以获取Ingress网关服务的详细信息。 这是公开给外部IP的唯一服务。

$ kubectl get svc istio-ingressgateway -n istio-system

NAME                   TYPE           CLUSTER-IP     EXTERNAL-IP
istio-ingressgateway   LoadBalancer   10.27.249.83   35.195.81.130

如果历史门户将外部IP显示为,请等待几分钟,直到分配了IP地址。

外部IP在这里非常重要,让我们将其保存到环境变量中,以便我们可以在其他命令中使用它。

$ export \
  INGRESS_IP=$(kubectl -n istio-system get svc \
  istio-ingressgateway \
  -o jsonpath='{.status.loadBalancer.ingress[0].ip}')

现在我们的Kubernetes集群已经为Istio做好了准备。 🎉

For advanced Istio setup options refer to https://istio.io/docs/setup/kubernetes/

Creating the microservice application stack

In one of my previous posts, I showcased how to create a full-stack microservice architecture using JHipster and JDL. You can read the post here if you want to learn more details about it. For this exercise, we will use the same application but we will not use the Eureka service discovery option we used earlier. Also, note that the store application is further split into Gateway and Product applications.

Architecture

这是我们今天将要创建和部署的微服务的体系结构。

Microservice architecture with Istio中号icroservice architecture with Istio

它具有一个网关应用程序和三个微服务应用程序。 他们每个人都有自己的数据库。 您可以看到每个应用程序都有一个Envoy代理作为附加工具附加到了pod上。 Istio控制平面组件也与Prometheus,Grafana和Jaeger一起部署到同一群集中。

Istio的Ingress网关是流量的唯一入口点,它会将流量路由到所有微服务。 遥测是从群集中运行的所有容器(包括应用程序,数据库和Istio组件)收集的。

Compared to the architecture of the original application here, you can clearly see that we replaced the JHipster registry and Netflix OSS components with Istio. The ELK monitoring stack is replaced with Prometheus, Grafana and Jaeger configured by Istio. Here is the original architecture diagram without Istio for a quick visual comparison.

Microservice architecture with Netflix OSS中号icroservice architecture with Netflix OSS

Application JDL

让我们看一下修改后的JDL声明。 您可以看到我们已经声明serviceDiscoveryType否在这里,因为我们将为此使用Istio。

Deployment JDL

JHipster版本5.7.0引入了直接在JDL中对部署声明的支持

我们的JDL中包含以下内容,用于声明我们的Kubernetes部署:

deployment {
  deploymentType kubernetes
  appsFolders [store, invoice, notification, product]
  dockerRepositoryName "deepu105"
  serviceDiscoveryType no
  istio true
  kubernetesServiceType Ingress
  kubernetesNamespace jhipster
  ingressDomain "35.195.81.130.nip.io"
}

的serviceDiscoveryType已禁用,我们已启用伊斯蒂奥 support — the Envoy sidecars are injected automatically for the selected applications. 伊斯蒂奥 routes are also generated for the applications automatically.

The kubernetesServiceŤype is set as Ingress, which is very important as Istio can only work with an Ingress controller service type. For Ingress, we need to set the domain DNS and this is where the Istio ingress gateway IP is needed. Now we need a DNS for our IP. For real use-cases, you should map a DNS for the IP, but for testing and demo purposes we can use a wildcard DNS service like nip.io to resolve our IP. Just append nip.io to our IP and use that as the ingressDomain.

注意:在撰写本文时,我在多个群集之间切换,因为我不想让它们继续运行,因此示例和屏幕截图之间的istio-ingressgateway IP可能会有所不同。 如果要运行这些示例,请根据自己的设置使用IP。

Generate the applications and deployment manifests

现在我们的JDL已经准备就绪,让我们搭建应用程序和Kubernetes清单。 创建一个新目录,并将上面的JDL保存在该目录中。 让我们命名app-istio.jdl然后运行import-jdl命令。

$ mkdir istio-demo && cd istio-demo
$ jhipster import-jdl app-istio.jdl

这将生成所有应用程序,并在每个应用程序中安装所需的NPM依赖项。 生成应用程序后,将生成部署清单,并将一些有用的指令打印到控制台。

Generation outputGeneration output

在您喜欢的IDE /编辑器中打开生成的代码并浏览代码。

Interim issues with generated code

There was a bug in the latest JHipster version which creates some incorrect URLs for Istio, it has been fixed as of JHipster version 6.3.0 here is the PR for the issue.

Deploy to Kubernetes cluster using Kubectl

现在,让我们构建和部署应用程序。 跑过./gradlew bootJar -Pprod jibDockerBuild在商店,产品,发票和通知文件夹中使用命令来构建Docker映像。 生成映像后,使用以下命令将它们推送到您的docker repo。 注意从更改Docker Hub ID第105章到你的身份证。

$ docker image tag store deepu105/store
$ docker push deepu105/store

$ docker image tag invoice deepu105/invoice
$ docker push deepu105/invoice

$ docker image tag notification deepu105/notification
$ docker push deepu105/notification

$ docker image tag product deepu105/product
$ docker push deepu105/product

推送映像后,导航到生成的Kubernetes目录并运行提供的启动脚本。 (如果您使用的是Windows,则可以运行kubectl-apply.sh一一手动)。

$ cd kubernetes
$ ./kubectl-apply.sh

跑观看kubectl获得豆荚-n jhipster监视状态。

Deployed applications

一旦所有Pod都处于运行状态,我们就可以浏览已部署的应用程序

Application gateway

商店网关应用程序是我们微服务的入口点。 通过运行获取商店应用程序的URL回声store.jhipster。$ INGRESS_IP.nip.io,我们已经存储了INGRESS_IP创建Istio设置时输入环境变量。 URL也由控制台打印在控制台上kubectl-apply.sh脚本。 在您喜欢的浏览器中访问URL并浏览应用程序。 尝试为微服务创建一些实体:

Store gateway application小号tore gateway application

Monitoring

Istio设置包括Grafana和Prometheus,它们配置为从我们的容器中收集和显示指标。 让我们来看看。

让我们通过访问提供的URL来查看Grafana。 通过运行获取它回声grafana.istio-system。$ INGRESS_IP.nip.io:

Grafana dashboard for the Store applicationGrafana dashboard for the Store application

Grafana uses the metrics scraped by Prometheus. By default, only Grafana is exposed to external IP and hence we will use kubectl port forwarding to set up a secure tunnel to Prometheus available on localhost:9090:

$ kubectl -n istio-system \
    port-forward $(kubectl -n istio-system get pod -l \
    app=prometheus -o jsonpath='{.items[0].metadata.name}') 9090:9090

Prometheus dashboardPrometheus dashboard

Observability

Istio将Jaeger配置为进行分布式跟踪,将Kiali配置为进行服务可观察性。 让我们看看它们。

通过运行获取Jaeger URL回声jaeger.istio-system。$ INGRESS_IP.nip.io:

Jaeger tracing dashboardĴaeger tracing dashboard

您可以在应用程序中提出一些请求,并通过查询服务在跟踪仪表板中找到它。 单击任何请求以查看跟踪详细信息。

现在让我们看看Kiali。 通过运行获取URL回声kiali.istio-system。$ INGRESS_IP.nip.io,请使用凭据用户:管理员,密码:管理员登录:

Kiali service graphķiali service graph

Conclusion

Istio提供了构建模块,以更Kubernetes原生的方式构建分布式微服务,并承担了使这些模块远离您的复杂性和责任。 这意味着您不必担心维护代码或部署以便进行服务发现,跟踪等。

Istio文档说

在Istio服务网格中部署基于微服务的应用程序,可以使服务跨应用程序以一致的方式从外部控制服务监视和跟踪,请求(版本)路由,弹性测试,安全性和策略执行等。 整个。

Werner Vogels(AWS的首席技术官)在AWS Re:Invent上被引用

“将来,您编写的所有代码都将成为业务逻辑。”

Istio Service网格有助于使现实更加接近。 这使您仅需担心正在开发的应用程序,就可以使用JHipster来开发真正的未来,而您只需要担心编写业务逻辑即可。

虽然这很棒,但这不是万灵丹。 请记住,与其他稳定且经过考验的解决方案(如JHipster Registry(Eureka)或Consul)相比,Istio还是相当新的,总体而言,此类架构仅适用于复杂的分布式应用程序。

另外,要记住的另一件事是资源需求。 可以将具有JHipster Registry或Consul的微服务部署到GCP中每个节点具有1个vCPU和3.75 GB内存的2节点群集中,而对于启用Istio的部署,则需要具有2个vCPU和每个节点7.5 GB内存的4节点群集。 我们使用的Istio演示配置文件没有对资源施加任何请求限制,并且通过添加和调整这些请求限制,可以减少最低要求。 但是,我认为您无法将其降低到JHipster注册表选项所需的水平。

在现实的用例中,不必维护基础结构的复杂部分而不必支付更多资源的优势可能是必须根据优先级和目标做出的决定。

A huge shout out to Ray Tsang for helping me figure out an optimal cluster size for this application originally. Also a huge thank you from myself and the community to both Ray and Srinivasa Vasu for adding the Istio support to JHipster.

JHipster provides a great Kubernetes setup to start with which you can further tweak as per your needs and platform. The Istio support will improve further over time, but it's still a great starting point especially to learn.

To learn more about JHipster and Full stack development, check out my book “Full Stack Development with JHipster” on Amazon and Packt.

There is a great Istio tutorial from Ray Tsang here.


If you liked this article you might like my book as well. You can get it from Packt and Amazon.

book cover


Devoxx 2018

I did a talk at Devoxx 2018 along with Julien Dubois doing the same demo and promised that I’d write a detailed blog about it. This blog was originally based on that.

您可以观看此视频以查看JHipster + Istio的运行情况。

这是扬声器甲板上的幻灯片。


If you like JHipster don’t forget to give it a star on Github.

如果您喜欢这篇文章,请留下喜欢/评论。 我希望尽快写更多有关Istio的文章。

You can follow me on Twitter and LinkedIn.

from: https://dev.to//deepu105/how-to-set-up-java-microservices-with-istio-service-mesh-on-kubernetes-5bkn

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值