<p>I have a Django deployment for a frontend service in my Azure Kubernetes cluster with some basic configuration. But note that the same question applies for my local Minikube cluster. I fetch my Django frontend container image from my remote container registry and expose <code>port 8010</code>. My service configuration is quite simple as well.</p>

<p><strong>frontend.deployment.yaml</strong></p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend-v1
  labels:
    app: frontend-v1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: frontend-v1
  template:
    metadata:
      labels:
        app: frontend-v1
    spec:
      containers:
        - name: frontend-v1
          imagePullPolicy: Always
          image: yourremotename.azurecr.io/frontend-remote:v1
          ports:
          - containerPort: 8010
      imagePullSecrets:
        - name: acr-secret
</code></pre>

<p><strong>frontend.service.yaml</strong></p>

<pre><code>kind: Service
apiVersion: v1
metadata:
  name: frontend-v1
spec:
  selector:
    app: frontend-v1
  ports:
  - NodePort:
    protocol: TCP
    port: 8010
    targetPort: 8010
  type: NodePort
</code></pre>

<p>Now, when I access my deployed frontend service in the browser <em>i.e.  <a href="http://172.17.194.253:31436" rel="nofollow noreferrer">http://172.17.194.253:31436</a></em> with Django's setting <code>DEBUG = True</code>, I get the error:</p>

<pre><code>Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.6/dist-packages/django/utils/deprecation.py", line 93, in __call__
    response = self.process_request(request)
  File "/usr/local/lib/python3.6/dist-packages/django/middleware/common.py", line 48, in process_request
    host = request.get_host()
  File "/usr/local/lib/python3.6/dist-packages/django/http/request.py", line 122, in get_host
    raise DisallowedHost(msg)

Exception Type: DisallowedHost at /
Exception Value: Invalid HTTP_HOST header: '172.17.194.253:31436'. You may need to add '172.17.194.253' to ALLOWED_HOSTS.
</code></pre>

<p>But how can I bind the dynamically created HostIp of the pod to Django's <code>ALLOWED_HOSTS</code>?</p>

marcuse
<p>Since Kubernetes 1.7 it is possible to request the HostIp of the pod in your kubernetes deployment file.<a href="https://github.com/kubernetes/kubernetes/issues/24657" rel="noreferrer">(1)</a></p>

<p>First adjust the deployment file to set the required environment variable for the HostIp. In the beneath scenario I set the POD_IP and the HOST_IP, as they are different. You can inject a variety of Kubernetes application data variables using environment variables in your Kubernetes deployment files, for more info about this topic look <a href="https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/" rel="noreferrer">here</a>.</p>

<p><strong>frontend.service.yaml</strong></p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend-v1
  labels:
    app: frontend-v1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: frontend-v1
  template:
    metadata:
      labels:
        app: frontend-v1
    spec:
      containers:
        - name: frontend-v1
          imagePullPolicy: Always
          image: yourremotename.azurecr.io/frontend-remote:v1
          ports:
          - containerPort: 8010
          env:
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: HOST_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.hostIP
      imagePullSecrets:
        - name: acr-secret
</code></pre>

<p>Now in you Django settings adjust the <code>ALLOWED_HOSTS</code> configuration to point to the <code>HOST_IP</code> environment variable.</p>

<p><strong>settings.py</strong></p>

<pre><code>import os
...
ALLOWED_HOSTS = [os.environ.get('HOST_IP'), '127.0.0.1']
....
</code></pre>

<p><em>Note that this allows the pod's HostIP as well als localhost for local development purposes.</em></p>

<p><strong>Warning!</strong> Some blog posts or tutorials advise you to set <code>ALLOWED_HOSTS = ['*']</code> to accept all host IP's, but this is a serious security loophole. Don't do this!</p>

<p>Now redeploy your pod and your Django application should run smoothly again.</p>
