适用于 Azure Kubernetes 服务 (AKS)(已停用)的 HTTP 应用程序路由加载项
可以通过 HTTP 应用程序路由加载项轻松地访问部署到 Azure Kubernetes 服务 (AKS) 群集的应用程序,方法是:
准备阶段
- HTTP 应用程序路由加载项不适用于 AKS 版本 1.22.6+。
- 如果在本地运行命令,请使用
az aks install-cli
命令安装kubectl
。
HTTP 应用程序路由加载项概述
此加载项可部署两个组件:Kubernetes 入口控制器和 External-DNS 控制器。
- 入口控制器:入口控制器使用 Kubernetes
LoadBalancer
服务公开给 Internet。 入口控制器监视并实现 Kubernetes 入口资源,并创建到应用程序终结点的路由。 - External-DNS 控制器:External-DNS 控制器监视 Kubernetes 入口资源并在特定于群集的 DNS 区域中创建 DNS
A
记录。
启用 Http 应用程序路由
使用带
--enable-addons
参数的az aks create
命令创建新的 AKS 群集并启用 HTTP 应用程序路由加载项。az aks create \ --resource-group myResourceGroup \ --name myAKSCluster \ --enable-addons http_application_routing \ --generate-ssh-keys
还可以使用带
--addons
参数的az aks enable-addons
命令在现有 AKS 群集上启用 HTTP 路由。az aks enable-addons --resource-group myResourceGroup --name myAKSCluster --addons http_application_routing
使用
az aks show
命令检索 DNS 区域名称。 需要 DNS 区域名称才能将应用程序部署到群集。az aks show --resource-group myResourceGroup --name myAKSCluster --query addonProfiles.httpApplicationRouting.config.HTTPApplicationRoutingZoneName -o table
输出应类似于以下示例输出:
9f9c1fe7-21a1-416d-99cd-3543bb92e4c3.eastus.aksapp.io
连接到 AKS 群集
使用
az aks get-credentials
命令将kubectl
配置为连接到你的 Kubernetes 群集。 以下示例获取 myResourceGroup 中名为“myAKSCluster” 的 AKS 群集的凭据:az aks get-credentials --resource-group myResourceGroup --name myAKSCluster
使用 HTTP 应用程序路由
重要
只能在具有以下注释的入口资源上触发 HTTP 应用程序路由加载项:
annotations:
kubernetes.io/ingress.class: addon-http-application-routing
创建名为 samples-http-application-routing.yaml 的文件,并将其复制到以下 YAML 中。 在第 43 行,将
<CLUSTER_SPECIFIC_DNS_ZONE>
更新为在上一步中收集的 DNS 区域名称。apiVersion: apps/v1 kind: Deployment metadata: name: aks-helloworld spec: replicas: 1 selector: matchLabels: app: aks-helloworld template: metadata: labels: app: aks-helloworld spec: containers: - name: aks-helloworld image: mcr.microsoft.com/azuredocs/aks-helloworld:v1 ports: - containerPort: 80 env: - name: TITLE value: "Welcome to Azure Kubernetes Service (AKS)" --- apiVersion: v1 kind: Service metadata: name: aks-helloworld spec: type: ClusterIP ports: - port: 80 selector: app: aks-helloworld --- apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: aks-helloworld annotations: kubernetes.io/ingress.class: addon-http-application-routing spec: rules: - host: aks-helloworld.<CLUSTER_SPECIFIC_DNS_ZONE> http: paths: - path: / pathType: Prefix backend: service: name: aks-helloworld port: number: 80
使用
kubectl apply
命令创建资源。kubectl apply -f samples-http-application-routing.yaml
以下示例显示了创建的资源:
deployment.apps/aks-helloworld created service/aks-helloworld created ingress.networking.k8s.io/aks-helloworld created
打开 web 浏览器输入 aks-helloworld.<CLUSTER_SPECIFIC_DNS_ZONE>,例如 aks-helloworld.9f9c1fe7-21a1-416d-99cd-3543bb92e4c3.eastus.aksapp.io,并验证你是否看到了演示应用程序。 应用程序可能需要几分钟才会显示。
移除 HTTP 应用程序路由
使用 [
az aks disable-addons][az-aks-disable-addons] command with the
addons` 参数移除 HTTP 应用程序路由加载项。az aks disable-addons --addons http_application_routing --name myAKSCluster --resource-group myResourceGroup --no-wait
当禁用了 HTTP 应用程序路由加载项时,某些 Kubernetes 资源可能会保留在群集中。 这些资源包括 configmap 和 secret,并且它们是在 kube-system 命名空间中创建的。 为了维护一个干净的群集,你可能想要删除这些资源。 使用以下
kubectl get
命令查找 addon-http-application-routing 资源:kubectl get deployments --namespace kube-system kubectl get services --namespace kube-system kubectl get configmaps --namespace kube-system kubectl get secrets --namespace kube-system
以下示例输出显示了应当删除的 configmap:
NAMESPACE NAME DATA AGE kube-system addon-http-application-routing-nginx-configuration 0 9m7s kube-system addon-http-application-routing-tcp-services 0 9m7s kube-system addon-http-application-routing-udp-services 0 9m7s
使用
kubectl delete
命令删除剩余的资源。 确保指定资源类型、资源名称和命名空间。 以下示例删除上面的 configmap 之一:kubectl delete configmaps addon-http-application-routing-nginx-configuration --namespace kube-system
针对群集中剩余的所有 addon-http-application-routing 资源重复前面的
kubectl delete
步骤。
疑难解答
使用
kubectl logs
命令查看 External-DNS 应用程序的应用程序日志。kubectl logs -f deploy/addon-http-application-routing-external-dns -n kube-system
这些日志应确认
A
和TXT
DNS 记录已成功创建,如以下示例输出所示:time="2018-04-26T20:36:19Z" level=info msg="Updating A record named 'aks-helloworld' to '52.242.28.189' for Azure DNS zone '471756a6-e744-4aa0-aa01-89c4d162a7a7.canadaeast.aksapp.io'." time="2018-04-26T20:36:21Z" level=info msg="Updating TXT record named 'aks-helloworld' to '"heritage=external-dns,external-dns/owner=default"' for Azure DNS zone '471756a6-e744-4aa0-aa01-89c4d162a7a7.canadaeast.aksapp.io'."
使用
kubectl logs
命令查看 NGINX 入口控制器的应用程序日志。kubectl logs -f deploy/addon-http-application-routing-nginx-ingress-controller -n kube-system
这些日志应确认入口资源的
CREATE
操作和控制器的重载操作,如以下示例输出所示:------------------------------------------------------------------------------- NGINX Ingress controller Release: 0.13.0 Build: git-4bc943a Repository: https://github.com/kubernetes/ingress-nginx ------------------------------------------------------------------------------- I0426 20:30:12.212936 9 flags.go:162] Watching for ingress class: addon-http-application-routing W0426 20:30:12.213041 9 flags.go:165] only Ingress with class "addon-http-application-routing" will be processed by this ingress controller W0426 20:30:12.213505 9 client_config.go:533] Neither --kubeconfig nor --master was specified. Using the inClusterConfig. This might not work. I0426 20:30:12.213752 9 main.go:181] Creating API client for https://10.0.0.1:443 I0426 20:30:12.287928 9 main.go:225] Running in Kubernetes Cluster version v1.8 (v1.8.11) - git (clean) commit 1df6a8381669a6c753f79cb31ca2e3d57ee7c8a3 - platform linux/amd64 I0426 20:30:12.290988 9 main.go:84] validated kube-system/addon-http-application-routing-default-http-backend as the default backend I0426 20:30:12.294314 9 main.go:105] service kube-system/addon-http-application-routing-nginx-ingress validated as source of Ingress status I0426 20:30:12.426443 9 stat_collector.go:77] starting new nginx stats collector for Ingress controller running in namespace (class addon-http-application-routing) I0426 20:30:12.426509 9 stat_collector.go:78] collector extracting information from port 18080 I0426 20:30:12.448779 9 nginx.go:281] starting Ingress controller I0426 20:30:12.463585 9 event.go:218] Event(v1.ObjectReference{Kind:"ConfigMap", Namespace:"kube-system", Name:"addon-http-application-routing-nginx-configuration", UID:"2588536c-4990-11e8-a5e1-0a58ac1f0ef2", APIVersion:"v1", ResourceVersion:"559", FieldPath:""}): type: 'Normal' reason: 'CREATE' ConfigMap kube-system/addon-http-application-routing-nginx-configuration I0426 20:30:12.466945 9 event.go:218] Event(v1.ObjectReference{Kind:"ConfigMap", Namespace:"kube-system", Name:"addon-http-application-routing-tcp-services", UID:"258ca065-4990-11e8-a5e1-0a58ac1f0ef2", APIVersion:"v1", ResourceVersion:"561", FieldPath:""}): type: 'Normal' reason: 'CREATE' ConfigMap kube-system/addon-http-application-routing-tcp-services I0426 20:30:12.467053 9 event.go:218] Event(v1.ObjectReference{Kind:"ConfigMap", Namespace:"kube-system", Name:"addon-http-application-routing-udp-services", UID:"259023bc-4990-11e8-a5e1-0a58ac1f0ef2", APIVersion:"v1", ResourceVersion:"562", FieldPath:""}): type: 'Normal' reason: 'CREATE' ConfigMap kube-system/addon-http-application-routing-udp-services I0426 20:30:13.649195 9 nginx.go:302] starting NGINX process... I0426 20:30:13.649347 9 leaderelection.go:175] attempting to acquire leader lease kube-system/ingress-controller-leader-addon-http-application-routing... I0426 20:30:13.649776 9 controller.go:170] backend reload required I0426 20:30:13.649800 9 stat_collector.go:34] changing prometheus collector from to default I0426 20:30:13.662191 9 leaderelection.go:184] successfully acquired lease kube-system/ingress-controller-leader-addon-http-application-routing I0426 20:30:13.662292 9 status.go:196] new leader elected: addon-http-application-routing-nginx-ingress-controller-5cxntd6 I0426 20:30:13.763362 9 controller.go:179] ingress backend successfully reloaded... I0426 21:51:55.249327 9 event.go:218] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"default", Name:"aks-helloworld", UID:"092c9599-499c-11e8-a5e1-0a58ac1f0ef2", APIVersion:"extensions", ResourceVersion:"7346", FieldPath:""}): type: 'Normal' reason: 'CREATE' Ingress default/aks-helloworld W0426 21:51:57.908771 9 controller.go:775] service default/aks-helloworld does not have any active endpoints I0426 21:51:57.908951 9 controller.go:170] backend reload required I0426 21:51:58.042932 9 controller.go:179] ingress backend successfully reloaded... 167.220.24.46 - [167.220.24.46] - - [26/Apr/2018:21:53:20 +0000] "GET / HTTP/1.1" 200 234 "" "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)" 197 0.001 [default-aks-helloworld-80] 10.244.0.13:8080 234 0.004 200
清理资源
移除在本文中使用
kubectl delete
命令创建的相关 Kubernetes 对象。kubectl delete -f samples-http-application-routing.yaml
以下输出示例显示已移除 Kubernetes 对象:
deployment "aks-helloworld" deleted service "aks-helloworld" deleted ingress "aks-helloworld" deleted
后续步骤
若要了解如何在 AKS 中安装受 HTTPS 保护的入口控制器,请参阅 Azure Kubernetes 服务 (AKS) 中的 HTTPS 入口。