<p>I'm using a dockerized microservice architecture running on Kubernetes with Nginx, and am encountering an issue with hostnames. How do you correctly add the hostname to Kubernetes (or perhaps Nginx too)?</p>

<p>The problem: When microservice A called <code>admin</code> tries to talk to microservice B called <code>session</code>, <code>admin</code> logs the following error and <code>session</code> is not reached: </p>

<pre><code>{ Error [ERR_TLS_CERT_ALTNAME_INVALID]: Hostname/IP does not match certificate's 
altnames: Host: session. is not in the cert's altnames: DNS:*.example.com, example.com
at Object.checkServerIdentity (tls.js:225:17)
at TLSSocket.onConnectSecure (_tls_wrap.js:1051:27)
at TLSSocket.emit (events.js:160:13)
at TLSSocket._finishInit (_tls_wrap.js:638:8)
  reason: 'Host: session. is not in the cert\'s altnames: 
DNS:*.example.com, example.com',
  host: 'session',
  cert:
   { subject: { OU: 'Domain Control Validated', CN: 
'*.example.com' },
     issuer: ...
</code></pre>

<p>In response to this error, I tried to update the hostname in the kubernetes config yaml file unsuccessfully (based on <a href="https://stackoverflow.com/questions/34609572/is-it-possible-to-set-a-hostname-in-a-kubernetes-replication-controller">this</a>). See the added <code>hostname</code> below.</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: session
  namespace: demo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: session
      component: demo
  template:
    metadata:
      labels:
        app: session
        component: demo
    spec:
      hostname: session.example.com . ----&gt; added host name here
      imagePullSecrets:
        - name: docker-secret 
      containers:
      - name: session
       ...
</code></pre>

<p>However, when I try to apply this updated config file in Kubernetes, an error emerges that I cannot use a period.  If I cannot use a period, and the hostname is <code>*.example.com</code> (i.e. <code>session.example.com</code>), where/how should the hostname be updated.</p>

<pre><code>The Deployment "session" is invalid: spec.template.spec.hostname: 
Invalid value: "session.example.com": a DNS-1123 label must 
consist of lower case alphanumeric characters or '-', and must start and 
end with an alphanumeric character (e.g. 'my-name',  or '123-abc', regex 
used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?')
</code></pre>

<p>Meanwhile, the server name in the nginx config file is indeed updated with <code>session.example.com</code>.</p>

<pre><code>upstream session {
  server 127.0.0.1:3000;
  keepalive 32;
}

server {
  listen 443 ssl http2 default_server;
  listen [::]:443 ssl http2 default_server;

  server_name "session.example.com";  ---&gt; updated for hostname 

  ssl_certificate      /etc/ssl/nginx/certificate.pem;
  ssl_certificate_key  /etc/ssl/nginx/key.pem;

  location / {
      proxy_pass http://session/;
      proxy_http_version 1.1;
      proxy_set_header Connection "";
  }
}


server {
  listen 80 default_server;
  listen [::]:80 default_server;
  server_name "session.example.com";    ---&gt; updated for hostname 

  return 301 https://$host$request_uri;
}
</code></pre>

<p>How do you suggest fixing this? My goal is for <code>admin</code> to successfully communicate with <code>session</code>.</p>

tskittles
<p>You can use kubernetes own dns.</p>
<p><a href="https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/</a></p>
<p>So you can access your pod using pod dns;</p>
<blockquote>
<p>When enabled, pods are assigned a DNS A record in the form of</p>
<p><strong>“pod-ip-address.my-namespace.pod.cluster.local”</strong></p>
</blockquote>
<p>With service you can use</p>
<blockquote>
<p><strong>my-svc.my-namespace.svc.cluster.local</strong></p>
</blockquote>
