ExternalDNS为Kubernetes的入口和服务配置外部DNS服务器,帮您使用公共DNS服务器来发现Kubernetes资源。其原理与KubeDNS类似,从Kubernetes
API中检索资源列表(如Service、Ingress等资源),以确定所需的DNS记录信息。本文介绍如何在ACK集群中部署和使用ExternalDNS服务,并通过示例验证其功能特性。
步骤一:配置RAM权限
对ACK集群的Worker RAM角色授予对应的RAM权限,操作步骤如下:
- 登录容器服务管理控制台。
- 在控制台左侧导航栏中,单击集群。
- 在集群列表页面中,单击目标集群名称或者目标集群右侧操作列下的详情。
- 在集群信息页面单击集群资源,在列表中单击Worker RAM角色后面对应的链接。
- 单击添加权限,然后单击新建权限策略。
- 在创建权限策略页面,单击脚本编辑页签,将如下自定义授权策略的内容拷贝到编辑框中。
{
"Version": "1",
"Statement": [
{
"Action": "alidns:AddDomainRecord",
"Resource": "*",
"Effect": "Allow"
},
{
"Action": "alidns:DeleteDomainRecord",
"Resource": "*",
"Effect": "Allow"
},
{
"Action": "alidns:UpdateDomainRecord",
"Resource": "*",
"Effect": "Allow"
},
{
"Action": "alidns:DescribeDomainRecords",
"Resource": "*",
"Effect": "Allow"
},
{
"Action": "alidns:DescribeDomains",
"Resource": "*",
"Effect": "Allow"
},
{
"Action": "pvtz:AddZoneRecord",
"Resource": "*",
"Effect": "Allow"
},
{
"Action": "pvtz:DeleteZoneRecord",
"Resource": "*",
"Effect": "Allow"
},
{
"Action": "pvtz:UpdateZoneRecord",
"Resource": "*",
"Effect": "Allow"
},
{
"Action": "pvtz:DescribeZoneRecords",
"Resource": "*",
"Effect": "Allow"
},
{
"Action": "pvtz:DescribeZones",
"Resource": "*",
"Effect": "Allow"
},
{
"Action": "pvtz:DescribeZoneInfo",
"Resource": "*",
"Effect": "Allow"
}
]
}
- 单击下一步,在名称后面输入策略名称,然后单击确定。
- 返回5的添加权限页面,单击自定义策略,选中刚创建好的自定义策略,单击确定。
- 单击完成。
步骤二:部署ExternalDNS服务
- 登录容器服务管理控制台。
- 在控制台左侧导航栏中,选择。
- 在应用市场页面,单击应用目录页签,搜索并选中external-dns。
- 在external-dns页面,单击一键部署。
- 在创建面板中,选择集群和命名空间,然后单击下一步。
- 在参数配置部分,根据实际需求选择合适的
alibabaCloudZoneType
,默认为public
。public
表示使用阿里云公共DNS服务。
private
表示使用阿里云PrivateZone服务。

步骤三:使用ExternalDNS服务
说明 ExternalDNS只支持LoadBalancer类型的Service和Ingress配置使用。
为Service配置阿里云公共DNS服务
- 登录云解析DNS控制台,查找对应域名信息。
- 根据已有经过实名认证的域名,使用以下命令,创建测试应用。
kubectl apply -f - << EOF
apiVersion: v1
kind: Service
metadata:
name: nginx
annotations:
external-dns.alpha.kubernetes.io/hostname: nginx.**** # ****是您经过实名认证的公共域名。
spec:
type: LoadBalancer
ports:
- port: 80
name: http
targetPort: 80
selector:
app: nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
EOF
说明 其中external-dns.alpha.kubernetes.io/hostname
用来配置需挂载的DNS名称,ExternalDNS会自动创建IP对应的DNS记录。
- 使用以下命令,测试DNS解析结果。
[root@iZbp1hy7cb2g933cmy7w3aZ ~]# curl nginx.****.com
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
为Ingress配置阿里云公共DNS服务
- 登录云解析DNS控制台,查找对应域名信息。
- 根据已有经过实名认证的域名,使用以下命令,创建测试应用。
kubectl apply -f - << EOF
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
type: LoadBalancer
ports:
- port: 80
name: http
targetPort: 80
selector:
app: nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx
spec:
ingressClassName: nginx
rules:
- host: nginx-ing.**** # ****是您经过实名认证的公共域名。
http:
paths:
- backend:
service:
name: nginx
port:
number: 80
path: /
pathType: ImplementationSpecific
EOF
说明 其中host
用来配置需挂载的DNS名称,ExternalDNS会自动创建IP对应的DNS记录。
- 使用以下命令,测试DNS解析结果。
[root@iZbp1hy7cb2g933cmy7w3aZ ~]# curl nginx-ing.****.com
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
为Service配置阿里云PrivateZone服务
如果使用阿里云DNS提供PrivateZone功能,需确保6中alibabaCloudZoneType
值为private
,且PrivateZone对应的私有域名已关联VPC,操作步骤如下:
- 登录云解析DNS控制台。
- 在PrivateZone页面单击添加Zone,输入Zone名称,并单击确定。
- 找到目标Zone,单击其操作列下的解析设置,手动添加一条记录,有解析记录后才能关联VPC。
- 找到需要关联VPC的Zone,单击其操作列下的关联VPC,选择集群对应的VPC信息后,再单击确认进行关联。
- 使用以下命令,创建测试应用。
kubectl apply -f - << EOF
apiVersion: v1
kind: Service
metadata:
name: nginx
annotations:
external-dns.alpha.kubernetes.io/hostname: nginx.**** # ****是您在PrivateZone页面添加的Zone名称。
service.beta.kubernetes.io/alibaba-cloud-loadbalancer-address-type: "intranet" # 私网类型的负载均衡。
spec:
type: LoadBalancer
ports:
- port: 80
name: http
targetPort: 80
selector:
app: nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
EOF
说明 其中external-dns.alpha.kubernetes.io/hostname
为PrivateZone的域名,ExternalDNS会自动创建IP对应的DNS记录。
- 使用以下命令,测试DNS解析结果。
[root@iZbp1hy7cb2g933cmy7w3aZ ~]# curl nginx.****
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
为Ingress配置阿里云PrivateZone服务
- 登录云解析DNS控制台。
- 在PrivateZone页面单击添加Zone,输入Zone名称,并单击确定。
- 找到目标Zone,单击其操作列下的解析设置,手动添加一条记录,有解析记录后才能关联VPC。
- 找到需要关联VPC的Zone,单击其操作列下的关联VPC,选择集群对应的VPC信息后,再单击确认进行关联。
- 使用以下命令,创建测试应用。
kubectl apply -f - << EOF
apiVersion: v1
kind: Service
metadata:
name: nginx
annotations:
service.beta.kubernetes.io/alibaba-cloud-loadbalancer-address-type: "intranet" #私网类型的负载均衡。
spec:
type: LoadBalancer
ports:
- port: 80
name: http
targetPort: 80
selector:
app: nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx
spec:
ingressClassName: nginx
rules:
- host: nginx-ing.**** # ****是您在PrivateZone页面添加的Zone名称。
http:
paths:
- backend:
service:
name: nginx
port:
number: 80
path: /
pathType: ImplementationSpecific
EOF
- 使用以下命令,测试DNS解析结果。
[root@iZbp1hy7cb2g933cmy7w3aZ ~]# curl nginx-ing.****
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>