hostport.md 5.1 KB
Newer Older
Arttii's avatar
Arttii committed
1
2
3
4
# Setting up ExternalDNS for Headless Services 

This tutorial describes how to setup ExternalDNS for usage in conjunction with a Headless service.

David Dymko's avatar
David Dymko committed
5
## Use cases
Arttii's avatar
Arttii committed
6
7
8
9
10
11
The main use cases that inspired this feature is the necessity for fixed addressable hostnames with services, such as Kafka when trying to access them from outside the cluster. In this scenario, quite often, only the Node IP addresses are actually routable and as in systems like Kafka more direct connections are preferable.

## Setup

We will go through a small example of deploying a simple Kafka with use of a headless service.

12
### External DNS
Arttii's avatar
Arttii committed
13
14

A simple deploy could look like this:
15
### Manifest (for clusters without RBAC enabled)
Arttii's avatar
Arttii committed
16
```yaml
17
apiVersion: apps/v1
Arttii's avatar
Arttii committed
18
19
kind: Deployment
metadata:
20
  name: external-dns
Arttii's avatar
Arttii committed
21
22
23
spec:
  strategy:
    type: Recreate
24
25
26
  selector:
    matchLabels:
      app: external-dns
Arttii's avatar
Arttii committed
27
  template:
28
29
30
    metadata:
      labels:
        app: external-dns
Arttii's avatar
Arttii committed
31
32
33
    spec:
      containers:
      - name: external-dns
34
        image: k8s.gcr.io/external-dns/external-dns:v0.7.3
Arttii's avatar
Arttii committed
35
        args:
36
        - --log-level=debug
Arttii's avatar
Arttii committed
37
38
39
40
41
42
43
44
45
        - --source=service
        - --source=ingress
        - --namespace=dev
        - --domain-filter=example.org. 
        - --provider=aws
        - --registry=txt
        - --txt-owner-id=dev.example.org
```

46
47
48
49
50
51
52
53
54
55
56
57
58
### 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
59
  resources: ["services","endpoints","pods"]
60
  verbs: ["get","watch","list"]
61
- apiGroups: ["extensions","networking.k8s.io"]
62
63
  resources: ["ingresses"] 
  verbs: ["get","watch","list"]
64
65
66
- apiGroups: [""]
  resources: ["nodes"]
  verbs: ["list"]
67
68
69
70
71
72
73
74
75
76
77
78
79
80
---
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
---
81
apiVersion: apps/v1
82
83
kind: Deployment
metadata:
84
  name: external-dns
85
86
87
spec:
  strategy:
    type: Recreate
88
89
90
  selector:
    matchLabels:
      app: external-dns
91
  template:
92
93
94
    metadata:
      labels:
        app: external-dns
95
96
97
98
    spec:
      serviceAccountName: external-dns
      containers:
      - name: external-dns
99
        image: k8s.gcr.io/external-dns/external-dns:v0.7.3
100
        args:
101
        - --log-level=debug
102
103
104
105
106
107
108
109
110
        - --source=service
        - --source=ingress
        - --namespace=dev
        - --domain-filter=example.org. 
        - --provider=aws
        - --registry=txt
        - --txt-owner-id=dev.example.org
```

Arttii's avatar
Arttii committed
111
112
113

### Kafka Stateful Set

114
First lets deploy a Kafka Stateful set, a simple example(a lot of stuff is missing) with a headless service called `ksvc`
Arttii's avatar
Arttii committed
115
116
117
118
119
120
121
122
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
152
153
154
155
156

```yaml
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: kafka
spec:
  serviceName: ksvc
  replicas: 3
  template:
    metadata:
      labels:
        component: kafka
    spec:
      containers:
      - name:  kafka        
        image: confluent/kafka
        ports:
        - containerPort: 9092
          hostPort: 9092
          name: external
        command:
        - bash
        - -c
        - " export DOMAIN=$(hostname -d) && \
            export KAFKA_BROKER_ID=$(echo $HOSTNAME|rev|cut -d '-' -f 1|rev) && \
            export KAFKA_ZOOKEEPER_CONNECT=$ZK_CSVC_SERVICE_HOST:$ZK_CSVC_SERVICE_PORT && \
            export KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://$HOSTNAME.example.org:9092 && \
            /etc/confluent/docker/run"
        volumeMounts:
        - name: datadir
          mountPath: /var/lib/kafka
  volumeClaimTemplates:
  - metadata:
      name: datadir
      annotations:
          volume.beta.kubernetes.io/storage-class: st1
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage:  500Gi
157
```
158
Very important here, is to set the `hostPort`(only works if the PodSecurityPolicy allows it)! and in case your app requires an actual hostname inside the container, unlike Kafka, which can advertise on another address, you have to set the hostname yourself.
Arttii's avatar
Arttii committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190

### Headless Service

Now we need to define a headless service to use to expose the Kafka pods. There are generally two approaches to use expose the nodeport of a Headless service:

1. Add `--fqdn-template={{name}}.example.org`
2. Use a full annotation 

If you go with #1, you just need to define the headless service, here is an example of the case #2:

```yaml
apiVersion: v1
kind: Service
metadata:
  name: ksvc
  annotations:
    external-dns.alpha.kubernetes.io/hostname:  example.org
spec:
  ports:
  - port: 9092
    name: external
  clusterIP: None
  selector:
    component: kafka
```
This will create 3 dns records:
```
kafka-0.example.org
kafka-1.example.org
kafka-2.example.org
```

windayski's avatar
windayski committed
191
If you set `--fqdn-template={{name}}.example.org` you can omit the annotation.
Arttii's avatar
Arttii committed
192
193
194
195
196
197
198
199
200
Generally it is a better approach to use  `--fqdn-template={{name}}.example.org`, because then
you would get the service name inside the generated A records:

```
kafka-0.ksvc.example.org
kafka-1.ksvc.example.org
kafka-2.ksvc.example.org
```