Skip to content

Commit 3a0049c

Browse files
author
Matthew Fisher
committed
Merge pull request #2837 from Blystad/controller_set_hostname
feat(controller): add hostname exposing to units
2 parents 10ae1ad + 6022e6c commit 3a0049c

5 files changed

Lines changed: 63 additions & 4 deletions

File tree

controller/bin/boot

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ etcd_set_default secretKey ${DEIS_SECRET_KEY:-`openssl rand -base64 64 | tr -d '
4949
etcd_set_default builderKey ${DEIS_BUILDER_KEY:-`openssl rand -base64 64 | tr -d '\n'`}
5050
etcd_set_default registrationEnabled 1
5151
etcd_set_default webEnabled 0
52+
etcd_set_default unitHostname default
5253

5354
# safely create required keyspaces
5455
etcd_safe_mkdir /deis/services

controller/deis/settings.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,13 @@
308308
# see https://docs.djangoproject.com/en/1.6/ref/settings/#secure-proxy-ssl-header
309309
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
310310

311+
# Unit Hostname handling.
312+
# Supports:
313+
# default - Docker generated hostname
314+
# application - Hostname based on application unit name (i.e. my-application.v2.web.1)
315+
# server - Hostname based on CoreOS server hostname
316+
UNIT_HOSTNAME = 'default'
317+
311318
# Create a file named "local_settings.py" to contain sensitive settings data
312319
# such as database configuration, admin email, or passwords and keys. It
313320
# should also be used for any settings which differ between development

controller/scheduler/fleet.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import socket
88
import re
99
import time
10+
from django.conf import settings
1011

1112

1213
MATCH = re.compile(
@@ -112,6 +113,8 @@ def _create_container(self, name, image, command, unit, **kwargs):
112113
l.update({'cpu': '-c {}'.format(cpu)})
113114
else:
114115
l.update({'cpu': ''})
116+
# set unit hostname
117+
l.update({'hostname': self._get_hostname(name)})
115118
# should a special entrypoint be used
116119
entrypoint = kwargs.get('entrypoint')
117120
if entrypoint:
@@ -137,6 +140,19 @@ def _create_container(self, name, image, command, unit, **kwargs):
137140
if attempt == (RETRIES - 1): # account for 0 indexing
138141
raise
139142

143+
def _get_hostname(self, application_name):
144+
hostname = settings.UNIT_HOSTNAME
145+
if hostname == "default":
146+
return ''
147+
elif hostname == "application":
148+
# replace underscore with dots, since underscore is not valid in DNS hostnames
149+
dns_name = application_name.replace("_", ".")
150+
return '-h ' + dns_name
151+
elif hostname == "server":
152+
return '-h %H'
153+
else:
154+
raise RuntimeError('Unsupported hostname: ' + hostname)
155+
140156
def start(self, name):
141157
"""Start a container"""
142158
self._wait_for_container(name)
@@ -294,7 +310,7 @@ def attach(self, name):
294310
{"section": "Unit", "name": "Description", "value": "{name}"},
295311
{"section": "Service", "name": "ExecStartPre", "value": '''/bin/sh -c "IMAGE=$(etcdctl get /deis/registry/host 2>&1):$(etcdctl get /deis/registry/port 2>&1)/{image}; docker pull $IMAGE"'''}, # noqa
296312
{"section": "Service", "name": "ExecStartPre", "value": '''/bin/sh -c "docker inspect {name} >/dev/null 2>&1 && docker rm -f {name} || true"'''}, # noqa
297-
{"section": "Service", "name": "ExecStart", "value": '''/bin/sh -c "IMAGE=$(etcdctl get /deis/registry/host 2>&1):$(etcdctl get /deis/registry/port 2>&1)/{image}; port=$(docker inspect -f '{{{{range $k, $v := .ContainerConfig.ExposedPorts }}}}{{{{$k}}}}{{{{end}}}}' $IMAGE | cut -d/ -f1) ; docker run --name {name} {memory} {cpu} -P -e PORT=$port $IMAGE {command}"'''}, # noqa
313+
{"section": "Service", "name": "ExecStart", "value": '''/bin/sh -c "IMAGE=$(etcdctl get /deis/registry/host 2>&1):$(etcdctl get /deis/registry/port 2>&1)/{image}; port=$(docker inspect -f '{{{{range $k, $v := .ContainerConfig.ExposedPorts }}}}{{{{$k}}}}{{{{end}}}}' $IMAGE | cut -d/ -f1) ; docker run --name {name} {memory} {cpu} {hostname} -P -e PORT=$port $IMAGE {command}"'''}, # noqa
298314
{"section": "Service", "name": "ExecStop", "value": '''/usr/bin/docker rm -f {name}'''},
299315
{"section": "Service", "name": "TimeoutStartSec", "value": "20m"},
300316
{"section": "Service", "name": "RestartSec", "value": "5"},

controller/templates/confd_settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,5 @@
4444
{{ if .deis_controller_webEnabled }}
4545
WEB_ENABLED = bool({{ .deis_controller_webEnabled }})
4646
{{ end }}
47+
48+
UNIT_HOSTNAME = '{{ or (.deis_controller_unitHostname) "default" }}'

docs/customizing_deis/controller_settings.rst

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,18 @@ Settings set by controller
1919
--------------------------
2020
The following etcd keys are set by the controller component, typically in its /bin/boot script.
2121

22-
=========================== =================================================================================
22+
============================= =================================================================================
2323
setting description
24-
=========================== =================================================================================
24+
============================= =================================================================================
2525
/deis/controller/host IP address of the host running controller
2626
/deis/controller/port port used by the controller service (default: 8000)
2727
/deis/controller/protocol protocol for controller (default: http)
2828
/deis/controller/secretKey used for secrets (default: randomly generated)
2929
/deis/controller/builderKey used by builder to authenticate with the controller (default: randomly generated)
30+
/deis/controller/unitHostname See `Unit hostname`_. (default: "default")
3031
/deis/builder/users/* stores user SSH keys (used by builder)
3132
/deis/domains/* domain configuration for applications (used by router)
32-
=========================== =================================================================================
33+
============================= =================================================================================
3334

3435
Settings used by controller
3536
---------------------------
@@ -73,3 +74,35 @@ Be sure that your custom image functions in the same way as the `stock controlle
7374
Deis. Specifically, ensure that it sets and reads appropriate etcd keys.
7475

7576
.. _`stock controller image`: https://github.com/deis/deis/tree/master/controller
77+
78+
Unit hostname
79+
-------------
80+
Per default, Docker automatically generates a hostname for your application unit, such as:
81+
``5c149b397cd6``. Auto generated hostnames is not always preferred. For instance,
82+
New Relic would classify each Docker container as an unique server since they use hostname
83+
for grouping applications running on the same server together.
84+
85+
Deis supports configuring hostname assignment through the ``unitHostname`` setting.
86+
You can change the assignment solution using the following command:
87+
88+
.. code-block:: console
89+
90+
$ deisctl config controller set unitHostname=application
91+
92+
The valid ``unitHostname`` values are:
93+
94+
default
95+
Docker will generate the hostname. Example: ``5c149b397cd6``
96+
97+
application
98+
The hostname is assigned based on the unit name. Example: ``dancing-cat.v2.web.1``
99+
100+
server
101+
The hostname is assigned based on the CoreOS hostname. Example:
102+
``ip-10-21-2-168.eu-west-1.compute.internal``
103+
104+
.. note::
105+
106+
Changes to ``/deis/controller/unitHostname`` requires either pushing a new build to
107+
every application or scaling them down and up.
108+
The change is only detected when a container unit is deployed.

0 commit comments

Comments
 (0)