App Gateway(AGIC)应该指向Kubernetes Service而不是Pod。解决方法是,在Kubernetes环境中创建一个Service并将它绑定到App gateway上。具体实现可参考以下代码示例:
apiVersion: apps/v1
kind: Deployment
metadata:
name: sample-app
spec:
replicas: 3
selector:
matchLabels:
app: sample-app
template:
metadata:
labels:
app: sample-app
spec:
containers:
- name: sample-app
image: mcr.microsoft.com/oss/nginx/nginx:1.17.9-alpine
ports:
- containerPort: 80
apiVersion: v1
kind: Service
metadata:
name: sample-app-service
spec:
selector:
app: sample-app
ports:
- name: http
port: 80
targetPort: 80
创建一个App Gateway,将它配置为使用已创建的sample-app-service。以下是App Gateway配置文件的示例:
{
"type": "Microsoft.Network/applicationGateways",
"name": "myAppGateway",
"location": "[resourceGroup().location]",
"properties": {
"sku": {
"name": "WAF_Medium",
"tier": "WAF",
"capacity": 2
},
"gatewayIPConfigurations": [
{
"name": "myAppGatewayIpConfig",
"properties": {
"subnet": {
"id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('virtualNetworkName'), variables('subnetName'))]"
}
}
}
],
"frontendIPConfigurations": [
{
"name": "myFrontendIp",
"properties": {
"privateIPAllocationMethod": "Dynamic",
"subnet": {
"id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('virtualNetworkName'), variables('subnetName'))]"
}
}
}
],
"frontendPorts": [
{
"name": "myFrontendHttpPort",
"properties": {
"Port": 80
}
}
],
"backendAddressPools": [
{
"name": "myBackendAddressPool",
"properties": {
"BackendAddresses": [
{
"fqdn": "[concat('sample-app-service.default.svc.cluster.local:', '80')]"
}
]
}
}
],
"backendHttpSettingsCollection": [
{
"name": "myBackendHttpSettings",
"properties": {
"Port": 80,
"Protocol": "Http",
"CookieBasedAffinity": "Disabled"
}
}
],
"httpListeners": [
{
"name": "myHttpListener",
"properties": {
"FrontendIPConfiguration": {
"id": "[concat(resourceGroupId('Microsoft.Network/applicationGateways'), '/frontendIPConfigurations/', 'myFrontendIp')]"
},
"FrontendPort": {
"id": "[concat(resourceGroupId('Microsoft.Network/applicationGateways'), '/frontendPorts/', 'myFrontendHttpPort')]"
},
"Protocol": "Http",
"SslCertificate