在 Azure Kubernetes 服务 (AKS) 中安装并使用 Consul

Consul 是跨 Kubernetes 群集中的微服务提供关键功能集的开源服务网格。 这些功能包括服务发现、运行状况检查、服务分段和可观察性。 有关 Consul 的详细信息,请参阅官方文档什么是 Consul?

本文介绍如何安装 Consul。 Consul 组件安装在 AKS 上的 Kubernetes 群集中。

注意

这些说明引用 Consul 版本 1.6.0,并至少使用 Helm 版本 2.14.2

可针对 Kubernetes 版本 1.13+ 运行 Consul 1.6.x 版本。 可以在 GitHub - Consul 版本中找到其他 Consul 版本,并可以在 Consul - 发行说明中找到有关每个版本的信息。

在本文中,学习如何:

  • 在 AKS 上安装 Consul 组件
  • 验证 Consul 安装
  • 从 AKS 中卸载 Consul

准备阶段

本文中详述的步骤假设你已创建 AKS 群集(已启用 Kubernetes RBAC 的 Kubernetes 1.13 及更高版本)并已与该群集建立 kubectl 连接。 如果需要帮助完成这些项目,请参阅 AKS 快速入门。 确保群集在 Linux 节点池中至少有 3 个节点。

需要使用 Helm 按照这些说明安装 Consul。 建议在群集中正确安装和配置最新的稳定版本。 安装 Helm 时如需帮助,请参阅 AKS Helm 安装指南。 所有 Consul Pod 也必须按计划在 Linux 节点上运行。

本文将 Consul 安装指南分为多个独立步骤。 最终结果的结构与官方 Consul 安装指南相同。

在 AKS 上安装 Consul 组件

首先,下载 Consul Helm 图表的 v0.10.0 版本。 此版本的图表包括 Consul 版本 1.6.0

在 Linux 或适用于 Linux 的 Windows 子系统或 MacOS 上的基于 bash 的 shell 中,使用 curl 下载 Consul Helm 图表版本,如下所示:

# Specify the Consul Helm chart version that will be leveraged throughout these instructions
CONSUL_HELM_VERSION=0.10.0

curl -sL "https://github.com/hashicorp/consul-helm/archive/v$CONSUL_HELM_VERSION.tar.gz" | tar xz
mv consul-helm-$CONSUL_HELM_VERSION consul-helm

在 Linux 或适用于 Linux 的 Windows 子系统或 MacOS 上的基于 bash 的 shell 中,使用 curl 下载 Consul Helm 图表版本,如下所示:

# Specify the Consul Helm chart version that will be leveraged throughout these instructions
CONSUL_HELM_VERSION=0.10.0

curl -sL "https://github.com/hashicorp/consul-helm/archive/v$CONSUL_HELM_VERSION.tar.gz" | tar xz
mv consul-helm-$CONSUL_HELM_VERSION consul-helm

在 Windows 上基于 PowerShell 的 shell 中,使用 Invoke-WebRequest 下载 Consul Helm 图表版本,然后使用 Expand-Archive 进行解压缩,如下所示:

# Specify the Consul Helm chart version that will be leveraged throughout these instructions
$CONSUL_HELM_VERSION="0.10.0"

# Enforce TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = "tls12"
$ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest -URI "https://github.com/hashicorp/consul-helm/archive/v$CONSUL_HELM_VERSION.zip" -OutFile "consul-helm-$CONSUL_HELM_VERSION.zip"
Expand-Archive -Path "consul-helm-$CONSUL_HELM_VERSION.zip" -DestinationPath .
Move-Item -Path consul-helm-$CONSUL_HELM_VERSION -Destination consul-helm

使用 Helm 和下载的 consul-helm 图表将 Consul 组件安装到 AKS 群集的 consul 命名空间中。

注意

安装选项

我们将在安装过程中使用以下选项:

  • connectInject.enabled=true - 启用要注入到 pod 中的代理
  • client.enabled=true - 允许 Consul 客户端在每个节点上运行
  • client.grpc=true - 为 connectInject 启用 gRPC 侦听器
  • syncCatalog.enabled=true - 同步 Kubernetes 和 Consul 服务

节点选择器

Consul 目前必须安排在 Linux 节点上运行。 如果群集中有 Windows Server 节点,则必须确保 Consul Pod 仅安排在 Linux 节点上运行。 我们将使用节点选择器来确保将 Pod 安排到正确的节点。

helm install -f consul-helm/values.yaml --name consul --namespace consul ./consul-helm \
  --set connectInject.enabled=true --set connectInject.nodeSelector="beta.kubernetes.io/os: linux" \
  --set client.enabled=true --set client.grpc=true --set client.nodeSelector="beta.kubernetes.io/os: linux" \
  --set server.nodeSelector="beta.kubernetes.io/os: linux" \
  --set syncCatalog.enabled=true --set syncCatalog.nodeSelector="beta.kubernetes.io/os: linux"
helm install -f consul-helm/values.yaml --name consul --namespace consul ./consul-helm \
  --set connectInject.enabled=true --set connectInject.nodeSelector="beta.kubernetes.io/os: linux" \
  --set client.enabled=true --set client.grpc=true --set client.nodeSelector="beta.kubernetes.io/os: linux" \
  --set server.nodeSelector="beta.kubernetes.io/os: linux" \
  --set syncCatalog.enabled=true --set syncCatalog.nodeSelector="beta.kubernetes.io/os: linux"
helm install -f consul-helm/values.yaml --name consul --namespace consul ./consul-helm `
  --set connectInject.enabled=true --set connectInject.nodeSelector="beta.kubernetes.io/os: linux" `
  --set client.enabled=true --set client.grpc=true --set client.nodeSelector="beta.kubernetes.io/os: linux" `
  --set server.nodeSelector="beta.kubernetes.io/os: linux" `
  --set syncCatalog.enabled=true --set syncCatalog.nodeSelector="beta.kubernetes.io/os: linux"

Consul Helm 图表将部署许多对象。 上述 helm install 命令的输出会显示对象列表。 部署 Consul 组件可能需要大约 3 分钟才能完成,具体取决于群集环境。

此时,已将 Consul 部署到 AKS 群集。 为确保成功部署 Consul,让我们转到下一部分来验证 Consul 安装。

验证 Consul 安装

确认已成功创建资源。 使用 kubectl get svckubectl get pod 命令查询 consul 命名空间,在该命名空间中已通过 helm install 命令安装了 Consul 组件:

kubectl get svc --namespace consul --output wide
kubectl get pod --namespace consul --output wide

以下示例输出显示了现在应该正在运行的服务和 Pod(安排在 Linux 节点上):

NAME                                 TYPE           CLUSTER-IP    EXTERNAL-IP             PORT(S)                                                                   AGE     SELECTOR
consul                               ExternalName   <none>        consul.service.consul   <none>                                                                    38s     <none>
consul-consul-connect-injector-svc   ClusterIP      10.0.98.102   <none>                  443/TCP                                                                   3m26s   app=consul,component=connect-injector,release=consul
consul-consul-dns                    ClusterIP      10.0.46.194   <none>                  53/TCP,53/UDP                                                             3m26s   app=consul,hasDNS=true,release=consul
consul-consul-server                 ClusterIP      None          <none>                  8500/TCP,8301/TCP,8301/UDP,8302/TCP,8302/UDP,8300/TCP,8600/TCP,8600/UDP   3m26s   app=consul,component=server,release=consul
consul-consul-ui                     ClusterIP      10.0.50.188   <none>                  80/TCP                                                                    3m26s   app=consul,component=server,release=consul

NAME                                                              READY   STATUS    RESTARTS   AGE    IP            NODE                            NOMINATED NODE   READINESS GATES
consul-consul-connect-injector-webhook-deployment-99f74fdbcr5zj   1/1     Running   0          3m9s   10.240.0.68   aks-linux-92468653-vmss000002   <none>           <none>
consul-consul-jbksc                                               1/1     Running   0          3m9s   10.240.0.44   aks-linux-92468653-vmss000001   <none>           <none>
consul-consul-jkwtq                                               1/1     Running   0          3m9s   10.240.0.70   aks-linux-92468653-vmss000002   <none>           <none>
consul-consul-server-0                                            1/1     Running   0          3m9s   10.240.0.91   aks-linux-92468653-vmss000002   <none>           <none>
consul-consul-server-1                                            1/1     Running   0          3m9s   10.240.0.38   aks-linux-92468653-vmss000001   <none>           <none>
consul-consul-server-2                                            1/1     Running   0          3m9s   10.240.0.10   aks-linux-92468653-vmss000000   <none>           <none>
consul-consul-sync-catalog-d846b79c-8ssr8                         1/1     Running   2          3m9s   10.240.0.94   aks-linux-92468653-vmss000002   <none>           <none>
consul-consul-tz2t5                                               1/1     Running   0          3m9s   10.240.0.12   aks-linux-92468653-vmss000000   <none>           <none>

所有 pod 应显示 Running 状态。 如果 Pod 没有这些状态,请在运行之前等待一两分钟。 如果任何 Pod 报告问题,请使用 kubectl describe pod 命令查看其输出和状态。

访问 Consul UI

Consul UI 在上述安装过程中已进行了安装,它为 Consul 提供基于 UI 的配置。 Consul 的 UI 不会通过外部 IP 地址公开。 若要访问 Consul 用户界面,请使用 kubectl port-forward 命令。 此命令在客户端计算机与 AKS 群集中相关 Pod 之间建立安全连接。

kubectl port-forward -n consul svc/consul-consul-ui 8080:80

现在可以打开一个浏览器并指向 http://localhost:8080/ui,以打开 Consul UI。 打开 UI 时,应会看到以下内容:

Consul UI

从 AKS 中卸载 Consul

警告

从正在运行的系统中删除 Consul 可能会导致服务之间出现流量相关的问题。 在继续之前,请确保对系统进行预配,以便在没有 Consul 的情况下系统仍可正常运行。

删除 Consul 组件和命名空间

若要从 AKS 群集中删除 Consul,请使用以下命令。 helm delete 命令将删除 consul 图表,kubectl delete namespace 命令将删除 consul 命名空间。

helm delete --purge consul
kubectl delete namespace consul

后续步骤

若要了解 Consul 的更多安装和配置选项,请参阅以下官方 Consul 文章:

也可以使用以下示例应用程序按照其他方案操作: