alibabacloud.md 9.96 KB
Newer Older
Li Yi's avatar
Li Yi committed
1
2
# Setting up ExternalDNS for Services on Alibaba Cloud

3
This tutorial describes how to setup ExternalDNS for usage within a Kubernetes cluster on Alibaba Cloud. Make sure to use **>=0.5.6** version of ExternalDNS for this tutorial
Li Yi's avatar
Li Yi committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

## RAM Permissions

```json
{
  "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"
    },
xianlubird's avatar
xianlubird committed
31
32
33
34
35
    {
      "Action": "alidns:DescribeDomains",
      "Resource": "*",
      "Effect": "Allow"
    },
Li Yi's avatar
Li Yi committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
    {
      "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"
    }
  ]
}
```

When running on Alibaba Cloud, you need to make sure that your nodes (on which External DNS runs) have the RAM instance profile with the above RAM role assigned.

## Set up a Alibaba Cloud DNS service or Private Zone service

Alibaba Cloud DNS Service is the domain name resolution and management service for public access. It routes access from end-users to the designated web app.
Alibaba Cloud Private Zone is the domain name resolution and management service for VPC internal access. 

*If you prefer to try-out ExternalDNS in one of the existing domain or zone you can skip this step*

Create a DNS domain which will contain the managed DNS records. For public DNS service, the domain name should be valid and owned by yourself.

```console
$ aliyun alidns AddDomain --DomainName "external-dns-test.com"
```


Make a note of the ID of the hosted zone you just created.

```console
$ aliyun alidns DescribeDomains --KeyWord="external-dns-test.com" | jq -r '.Domains.Domain[0].DomainId'
```

## Deploy ExternalDNS

Connect your `kubectl` client to the cluster you want to test ExternalDNS with.
Then apply one of the following manifests file to deploy ExternalDNS.

### Manifest (for clusters without RBAC enabled)
```yaml
99
apiVersion: apps/v1
Li Yi's avatar
Li Yi committed
100
101
102
103
104
105
kind: Deployment
metadata:
  name: external-dns
spec:
  strategy:
    type: Recreate
106
107
108
  selector:
    matchLabels:
      app: external-dns
Li Yi's avatar
Li Yi committed
109
110
111
112
113
114
115
  template:
    metadata:
      labels:
        app: external-dns
    spec:
      containers:
      - name: external-dns
116
        image: k8s.gcr.io/external-dns/external-dns:v0.7.3
Li Yi's avatar
Li Yi committed
117
118
119
120
121
122
        args:
        - --source=service
        - --source=ingress
        - --domain-filter=external-dns-test.com # will make ExternalDNS see only the hosted zones matching provided domain, omit to process all available hosted zones
        - --provider=alibabacloud
        - --policy=upsert-only # would prevent ExternalDNS from deleting any records, omit to enable full synchronization
123
        - --alibaba-cloud-zone-type=public # only look at public hosted zones (valid values are public, private or no value for both)
Li Yi's avatar
Li Yi committed
124
125
        - --registry=txt
        - --txt-owner-id=my-identifier
xianlubird's avatar
xianlubird committed
126
127
128
129
130
131
132
133
        volumeMounts:
        - mountPath: /usr/share/zoneinfo
          name: hostpath
      volumes:
      - name: hostpath
        hostPath:
          path: /usr/share/zoneinfo
          type: Directory
Li Yi's avatar
Li Yi committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
```

### Manifest (for clusters with RBAC enabled)

```yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: external-dns
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
  name: external-dns
rules:
- apiGroups: [""]
Alfred Krohmer's avatar
Alfred Krohmer committed
150
  resources: ["services","endpoints","pods"]
Li Yi's avatar
Li Yi committed
151
  verbs: ["get","watch","list"]
152
- apiGroups: ["extensions","networking.k8s.io"]
Li Yi's avatar
Li Yi committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
  resources: ["ingresses"] 
  verbs: ["get","watch","list"]
- apiGroups: [""]
  resources: ["nodes"]
  verbs: ["list"]
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: external-dns-viewer
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: external-dns
subjects:
- kind: ServiceAccount
  name: external-dns
  namespace: default
---
172
apiVersion: apps/v1
Li Yi's avatar
Li Yi committed
173
174
175
176
177
178
kind: Deployment
metadata:
  name: external-dns
spec:
  strategy:
    type: Recreate
179
180
181
  selector:
    matchLabels:
      app: external-dns
Li Yi's avatar
Li Yi committed
182
183
184
185
186
187
188
189
  template:
    metadata:
      labels:
        app: external-dns
    spec:
      serviceAccountName: external-dns
      containers:
      - name: external-dns
190
        image: k8s.gcr.io/external-dns/external-dns:v0.7.3
Li Yi's avatar
Li Yi committed
191
192
193
194
195
196
197
198
199
        args:
        - --source=service
        - --source=ingress
        - --domain-filter=external-dns-test.com # will make ExternalDNS see only the hosted zones matching provided domain, omit to process all available hosted zones
        - --provider=alibabacloud
        - --policy=upsert-only # would prevent ExternalDNS from deleting any records, omit to enable full synchronization
        - --alibaba-cloud-zone-type=public # only look at public hosted zones (valid values are public, private or no value for both)
        - --registry=txt
        - --txt-owner-id=my-identifier
xianlubird's avatar
xianlubird committed
200
        - --alibaba-cloud-config-file= # enable sts token 
xianlubird's avatar
xianlubird committed
201
202
203
204
205
206
207
208
        volumeMounts:
        - mountPath: /usr/share/zoneinfo
          name: hostpath
      volumes:
      - name: hostpath
        hostPath:
          path: /usr/share/zoneinfo
          type: Directory
Li Yi's avatar
Li Yi committed
209
210
211
212
213
214
215
216
```



## Arguments

This list is not the full list, but a few arguments that where chosen.

217
### alibaba-cloud-zone-type
Li Yi's avatar
Li Yi committed
218

219
`alibaba-cloud-zone-type` allows filtering for private and public zones
Li Yi's avatar
Li Yi committed
220
221
222
223
224
225
226
227
228
229
230
231

* If value is `public`, it will sync with records in Alibaba Cloud DNS Service
* If value is `private`, it will sync with records in Alibaba Cloud Private Zone Service


## Verify ExternalDNS works (Ingress example)

Create an ingress resource manifest file.

> For ingress objects ExternalDNS will create a DNS record based on the host specified for the ingress object.

```yaml
232
apiVersion: networking.k8s.io/v1beta1
Li Yi's avatar
Li Yi committed
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
kind: Ingress
metadata:
  name: foo
  annotations:
    kubernetes.io/ingress.class: "nginx" # use the one that corresponds to your ingress controller.
spec:
  rules:
  - host: foo.external-dns-test.com
    http:
      paths:
      - backend:
          serviceName: foo
          servicePort: 80
```

## Verify ExternalDNS works (Service example)

Create the following sample application to test that ExternalDNS works.

> For services ExternalDNS will look for the annotation `external-dns.alpha.kubernetes.io/hostname` on the service and use the corresponding value.

```yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx
  annotations:
    external-dns.alpha.kubernetes.io/hostname: nginx.external-dns-test.com.
spec:
  type: LoadBalancer
  ports:
  - port: 80
    name: http
    targetPort: 80
  selector:
    app: nginx

---

272
apiVersion: apps/v1
Li Yi's avatar
Li Yi committed
273
274
275
276
kind: Deployment
metadata:
  name: nginx
spec:
277
278
279
  selector:
    matchLabels:
      app: nginx
Li Yi's avatar
Li Yi committed
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        ports:
        - containerPort: 80
          name: http
```

After roughly two minutes check that a corresponding DNS record for your service was created.

```console
296
$ aliyun alidns DescribeDomainRecords --DomainName=external-dns-test.com
Li Yi's avatar
Li Yi committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
{
  "PageNumber": 1,
  "TotalCount": 1,
  "PageSize": 20,
  "RequestId": "1DBEF426-F771-46C7-9802-4989E9C94EE8",
  "DomainRecords": {
    "Record": [
      {
        "RR": "nginx",
        "Status": "ENABLE",
        "Value": "1.2.3.4",
        "Weight": 1,
        "RecordId": "3994015629411328",
        "Type": "A",
        "DomainName": "external-dns-test.com",
        "Locked": false,
        "Line": "default",
        "TTL": 600
      },
      {
        "RR": "nginx",
        "Status": "ENABLE",
        "Value": "heritage=external-dns;external-dns/owner=my-identifier",
        "Weight": 1,
        "RecordId": "3994015629411329",
        "Type": "TTL",
        "DomainName": "external-dns-test.com",
        "Locked": false,
        "Line": "default",
        "TTL": 600
      }      
    ]
  }
}
```

Note created TXT record alongside ALIAS record. TXT record signifies that the corresponding ALIAS record is managed by ExternalDNS. This makes ExternalDNS safe for running in environments where there are other records managed via other means.

Let's check that we can resolve this DNS name. We'll ask the nameservers assigned to your zone first.

```console
$ dig nginx.external-dns-test.com.
```

If you hooked up your DNS zone with its parent zone correctly you can use `curl` to access your site.

```console
$ curl nginx.external-dns-test.com.
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...
</head>
<body>
...
</body>
</html>
```

## Custom TTL

The default DNS record TTL (Time-To-Live) is 300 seconds. You can customize this value by setting the annotation `external-dns.alpha.kubernetes.io/ttl`.
e.g., modify the service manifest YAML file above:

```yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx
  annotations:
    external-dns.alpha.kubernetes.io/hostname: nginx.external-dns-test.com
    external-dns.alpha.kubernetes.io/ttl: 60
spec:
    ...
```

This will set the DNS record's TTL to 60 seconds.

## Clean up

Make sure to delete all Service objects before terminating the cluster so all load balancers get cleaned up correctly.

```console
$ kubectl delete service nginx
```

Give ExternalDNS some time to clean up the DNS records for you. Then delete the hosted zone if you created one for the testing purpose.

```console
$ aliyun alidns DeleteDomain --DomainName external-dns-test.com
```
xianlubird's avatar
xianlubird committed
389

xianlubird's avatar
xianlubird committed
390
For more info about Alibaba Cloud external dns, please refer this [docs](https://yq.aliyun.com/articles/633412)