aws.md 9.26 KB
Newer Older
1 2
# Setting up ExternalDNS for Services on AWS

3
This tutorial describes how to setup ExternalDNS for usage within a Kubernetes cluster on AWS. Make sure to use **>=0.4** version of ExternalDNS for this tutorial
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 31 32 33
## IAM Permissions

```json
{
 "Version": "2012-10-17",
 "Statement": [
   {
     "Effect": "Allow",
     "Action": [
       "route53:ChangeResourceRecordSets"
     ],
     "Resource": [
       "arn:aws:route53:::hostedzone/*"
     ]
   },
   {
     "Effect": "Allow",
     "Action": [
       "route53:ListHostedZones",
       "route53:ListResourceRecordSets"
     ],
     "Resource": [
       "*"
     ]
   }
 ]
}
```

34
When running on AWS, you need to make sure that your nodes (on which External DNS runs) have the IAM instance profile with the above IAM role assigned (either directly or via something like [kube2iam](https://github.com/jtblin/kube2iam)).
35

36 37 38
## Set up a hosted zone

*If you prefer to try-out ExternalDNS in one of the existing hosted-zones you can skip this step*
39 40 41 42

Create a DNS zone which will contain the managed DNS records.

```console
43
$ aws route53 create-hosted-zone --name "external-dns-test.my-org.com." --caller-reference "external-dns-test-$(date +%s)"
44 45
```

46

47
Make a note of the ID of the hosted zone you just created, which will serve as the value for my-hostedzone-identifier.
48 49

```console
50
$ aws route53 list-hosted-zones-by-name --output json --dns-name "external-dns-test.my-org.com." | jq -r '.HostedZones[0].Id'
51
/hostedzone/ZEWFWZ4R16P7IB
52 53 54 55 56
```

Make a note of the nameservers that were assigned to your new zone.

```console
57
$ aws route53 list-resource-record-sets --output json --hosted-zone-id "/hostedzone/ZEWFWZ4R16P7IB" \
58
    --query "ResourceRecordSets[?Type == 'NS']" | jq -r '.[0].ResourceRecords[].Value'
59 60
ns-5514.awsdns-53.org.
...
61 62 63 64
```

In this case it's the ones shown above but your's will differ.

65
## Deploy ExternalDNS
66 67

Connect your `kubectl` client to the cluster you want to test ExternalDNS with.
68
Then apply one of the following manifests file to deploy ExternalDNS. You can check if your cluster has RBAC by `kubectl api-versions | grep rbac.authorization.k8s.io`.
69

70
### Manifest (for clusters without RBAC enabled)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
```yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: external-dns
spec:
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: external-dns
    spec:
      containers:
      - name: external-dns
Nick Jüttner's avatar
Nick Jüttner committed
86
        image: registry.opensource.zalan.do/teapot/external-dns:latest
87 88
        args:
        - --source=service
89
        - --source=ingress
90
        - --domain-filter=external-dns-test.my-org.com # will make ExternalDNS see only the hosted zones matching provided domain, omit to process all available hosted zones
91
        - --provider=aws
92
        - --policy=upsert-only # would prevent ExternalDNS from deleting any records, omit to enable full synchronization
93
        - --aws-zone-type=public # only look at public hosted zones (valid values are public, private or no value for both)
94
        - --registry=txt
95
        - --txt-owner-id=my-hostedzone-identifier
96 97
```

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
### 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: [""]
  resources: ["services"]
  verbs: ["get","watch","list"]
114 115 116
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get","watch","list"]
117 118 119
- apiGroups: ["extensions"] 
  resources: ["ingresses"] 
  verbs: ["get","watch","list"]
120 121
- apiGroups: [""]
  resources: ["nodes"]
Paweł Prażak's avatar
Paweł Prażak committed
122
  verbs: ["list","watch"]
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
---
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
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: external-dns
spec:
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: external-dns
    spec:
      serviceAccountName: external-dns
      containers:
      - name: external-dns
Nick Jüttner's avatar
Nick Jüttner committed
152
        image: registry.opensource.zalan.do/teapot/external-dns:latest
153 154 155 156 157 158 159 160
        args:
        - --source=service
        - --source=ingress
        - --domain-filter=external-dns-test.my-org.com # will make ExternalDNS see only the hosted zones matching provided domain, omit to process all available hosted zones
        - --provider=aws
        - --policy=upsert-only # would prevent ExternalDNS from deleting any records, omit to enable full synchronization
        - --aws-zone-type=public # only look at public hosted zones (valid values are public, private or no value for both)
        - --registry=txt
161
        - --txt-owner-id=my-hostedzone-identifier
162 163 164 165
```



166 167 168 169 170 171 172 173
## Arguments

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

### aws-zone-type

`aws-zone-type` allows filtering for private and public zones

vaegt's avatar
vaegt committed
174 175 176 177 178 179
## Annotations

Annotations which are specific to AWS.

### alias

180
`external-dns.alpha.kubernetes.io/alias` if set to `true` on an ingress, it will create an ALIAS record when the target is an ALIAS as well. To make the target an alias, the ingress needs to be configured correctly as described in [the docs](./nginx-ingress.md#with-a-separate-tcp-load-balancer).
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

## 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
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: foo
  annotations:
    kubernetes.io/ingress.class: "nginx" # use the one that corresponds to your ingress controller.
spec:
  rules:
  - host: foo.bar.com
    http:
      paths:
      - backend:
          serviceName: foo
          servicePort: 80
```

## Verify ExternalDNS works (Service example)
206

207 208
Create the following sample application to test that ExternalDNS works.

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

211 212
> If you want to give multiple names to service, you can set it to external-dns.alpha.kubernetes.io/hostname with a comma separator.

213 214 215 216 217 218
```yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx
  annotations:
219
    external-dns.alpha.kubernetes.io/hostname: nginx.external-dns-test.my-org.com
220 221 222 223
spec:
  type: LoadBalancer
  ports:
  - port: 80
224
    name: http
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    targetPort: 80
  selector:
    app: nginx

---

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
spec:
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        ports:
        - containerPort: 80
246
          name: http
247 248 249 250 251
```

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

```console
252
$ aws route53 list-resource-record-sets --output json --hosted-zone-id "/hostedzone/ZEWFWZ4R16P7IB" \
253
    --query "ResourceRecordSets[?Name == 'nginx.external-dns-test.my-org.com.']|[?Type == 'A']"
254 255
[
    {
256 257 258 259 260 261 262 263 264 265 266 267 268
      "AliasTarget": {
          "HostedZoneId": "ZEWFWZ4R16P7IB",
          "DNSName": "ae11c2360188411e7951602725593fd1-1224345803.eu-central-1.elb.amazonaws.com.",
          "EvaluateTargetHealth": true
      },
      "Name": "external-dns-test.my-org.com.",
      "Type": "A"
    },
    {
      "Name": "external-dns-test.my-org.com",
      "TTL": 300,
      "ResourceRecords": [
          {
269
              "Value": "\"heritage=external-dns,external-dns/owner=my-hostedzone-identifier\""
270 271 272
          }
      ],
      "Type": "TXT"
273 274 275 276
    }
]
```

277 278
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.

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

```console
282
$ dig +short @ns-5514.awsdns-53.org. nginx.external-dns-test.my-org.com.
283 284 285 286 287 288
ae11c2360188411e7951602725593fd1-1224345803.eu-central-1.elb.amazonaws.com.
```

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

```console
289
$ curl nginx.external-dns-test.my-org.com.
290 291 292 293 294 295 296 297 298 299 300 301 302 303
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...
</head>
<body>
...
</body>
</html>
```

Ingress objects on AWS require a separately deployed Ingress controller which we'll describe in another tutorial.

304 305 306 307 308 309 310 311 312 313 314
## 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:
315
    external-dns.alpha.kubernetes.io/hostname: nginx.external-dns-test.my-org.com
316 317 318 319 320 321 322
    external-dns.alpha.kubernetes.io/ttl: 60
spec:
    ...
```

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

323 324 325 326 327 328 329 330
## 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
```

331
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.
332 333

```console
334
$ aws route53 delete-hosted-zone --id /hostedzone/ZEWFWZ4R16P7IB
335
```