本文可帮助你开始在 Azure Stack Hub 上使用 Azure Kubernetes 服务 (AKS)。 它介绍如何将测试应用部署到群集,以便熟悉 Azure Stack Hub 上的 AKS。 Azure Stack Hub 中提供的功能是公共 Azure 中可用的功能的 子集 。
在开始之前,请确保可以在Azure Stack Hub实例上创建 AKS 群集。 有关设置和创建第一个群集的说明,请参阅通过 CLI 在Azure Stack Hub上使用Azure Kubernetes 服务。
部署测试应用
如果您的 stamp 已连接,请按照以下说明将 Prometheus 和 Grafana 部署到集群中。
下载并安装 Helm 3:
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 chmod 700 get_helm.sh ./get_helm.sh确保拥有最新的稳定 Helm 存储库。
helm repo add stable https://charts.helm.sh/stable helm repo update安装 Prometheus。
helm install my-prometheus stable/prometheus --set server.service.type=LoadBalancer --set rbac.create=false为群集授予对 Prometheus 帐户的管理访问权限。 由于安全原因,权限越低越好。
kubectl create clusterrolebinding my-prometheus-server --clusterrole=cluster-admin --serviceaccount=default:my-prometheus-server安装 Grafana。
helm install my-grafana stable/grafana --set service.type=LoadBalancer --set rbac.create=false获取 Grafana 门户的密钥。
kubectl get secret --namespace default my-grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echo
注释
在Windows,使用以下 PowerShell cmdlet 获取机密:
\$env:Path = \$env:Path + ";\$env:USERPROFILE\\.azure-kubectl"
[System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String(\$(kubectl get secret --namespace default my-grafana -o jsonpath="{.data.admin-password}")))
使用 ACR 将应用部署到 AKS
此时,客户端计算机已连接到群集,可以使用 kubectl 配置群集并部署应用程序。 如果还要测试 Azure 容器注册表 (ACR) 服务,请按照下一部分中的说明进行操作。
用于访问本地 ACR 的 Docker 注册表机密
如果要从本地 ACR 部署应用程序映像,则需要存储机密,以便 Kubernetes 群集可以从注册表访问和拉取映像。 若要创建此机密,请提供服务主体 ID(SPN)和机密,将 SPN 添加为源注册表的参与者,并创建 Kubernetes 机密。 你还需要更新 YAML 文件以引用该密钥。
将 SPN 添加到 ACR 中
将 SPN 添加为 ACR 的参与者。
注释
此脚本基于 Azure 容器注册表 网站 的(bash sample)修改而来,因为 Azure Stack Hub 尚不具备 ACRPULL 角色。 此示例是一个 PowerShell 脚本。 可以在 bash 中编写等效的脚本。 请务必为系统添加值。
# Modify for your environment. The ACR_NAME is the name of your Azure Container
# Registry, and the SERVICE_PRINCIPAL_ID is the SPN's 'appId' or
# one of its 'servicePrincipalNames' values.
ACR_NAME=mycontainerregistry
SERVICE_PRINCIPAL_ID=<service-principal-ID>
# Populate value required for subsequent command args
ACR_REGISTRY_ID=$(az acr show --name $ACR_NAME --query id --output tsv)
# Assign the desired role to the SPN.
az role assignment create --assignee $SERVICE_PRINCIPAL_ID --scope $ACR_REGISTRY_ID --role contributor
在 Kubernetes 中创建机密
使用以下命令将机密添加到 Kubernetes 群集。 请务必在代码片段中添加系统的值。
kubectl create secret docker-registry <secret name> \
kubectl create secret docker-registry <secret name> \
--docker-server=<ACR container registry URL> \
--docker-username=<service principal ID> \
--docker-password=<service principal secret>
在应用 YAML 中引用机密的示例
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: democr2.azsacr.redmond.ext-n31r1208.masd.stbtest.microsoft.com/library/nginx:1.17.3
imagePullPolicy: Always
ports:
- containerPort: 80
imagePullSecrets:
- name: democr2
---
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
selector:
app: nginx
ports:
- protocol: "TCP"
port: 80
targetPort: 80
type: LoadBalancer