<p><strong>Problem:</strong></p>
<p>how to resolve host name of kubernetes pod?</p>
<p>I have the Following requirement we are using grpc with java where we have one app where we are running out grpc server other app where we are creating grpc client and connecting to grpc server (that is running on another pod).</p>
<hr />
<p>We have three kubernetes pod running where our grpc server is running.</p>
<p>lets say :
my-service-0, my-service-1, my-service-2</p>
<p>my-service has a cluster IP as: 10.44.5.11</p>
<hr />
<p>We have another three kubernetes pod running where our gprc client is running.</p>
<p>lets say:
my-client-0, my-client-1, my-client-2</p>
<hr />
<p><strong>Without Security:</strong></p>
<p>i am try to connect grpc server pod with grpc client pod and it work fine.</p>
<pre><code>grpc client (POD -&gt; my-client) ----------------&gt; groc server(POD -&gt; my-service)
</code></pre>
<p>So without security i am giving host name as my-service and it's working fine without any problem..</p>
<pre><code>ManagedChannel channel = ManagedChannelBuilder.forAddress(&quot;my-service&quot;, 50052)
                .usePlaintext()
                .build();
</code></pre>
<hr />
<p><strong>With SSL Security:</strong></p>
<p>if i try to connect grpc server it will throw host name not match.
we have created a certificate with wild card *.default.pod.cluster.local</p>
<p>it will throw the below error:</p>
<pre><code>java.security.cert.CertificateException: No name matching my-service found
    at java.base/sun.security.util.HostnameChecker.matchDNS(HostnameChecker.java:225) ~[na:na]
    at java.base/sun.security.util.HostnameChecker.match(HostnameChecker.java:98) ~[na:na]
    at java.base/sun.security.ssl.X509TrustManagerImpl.checkIdentity(X509TrustManagerImpl.java:455) ~[na:na]

Not Working Code:
     ManagedChannel channel = NettyChannelBuilder.forAddress(&quot;my-service&quot;, 50052)
                    .sslContext(GrpcSslContexts.forClient().trustManager(new File(System.getenv(&quot;GRPC_CLIENT_CA_CERT_LOCATION&quot;))).build())
                    .build();
</code></pre>
<p>but if i give the host name as like this ==&gt; <strong>10-44-5-11.default.pod.cluster.local</strong> it will work fine correctly.</p>
<pre><code>Working Code
      ManagedChannel channel = NettyChannelBuilder.forAddress(&quot;10-44-5-11.default.pod.cluster.local&quot;, 50052)
                        .sslContext(GrpcSslContexts.forClient().trustManager(new File(System.getenv(&quot;GRPC_CLIENT_CA_CERT_LOCATION&quot;))).build())
                        .build();
</code></pre>
<p>Now my problem is cluster ip of pod is dynamic and it will change every time during app deploy. what is the right way to resolve this host name?</p>
<p>is it possible if i give host name and it will return me the ip then i will append default.pod.cluster.local to hostname and try to connect to grpc server?</p>

Mohit Singh
<p>Addressing your pod directly is not a good solution since Kubernetes may need to move your pods around the cluster. This can occur for example because of the failing node.</p>
<p>To allow you clients/traffic to easy find desired containers you can place them behind a service with single static IP address.  Service IP can be look up through DNS.</p>
<p>This is how you can connect to the service through it`s FQDN:</p>
<pre><code>my-service.default.svc.cluster.local 
</code></pre>
<p>Where <code>my-service</code>  is your service name, <code>default</code> for your namespace and <code>svc.cluster.local</code> is a configurable cluster domain suffix used in all cluster services.</p>
<p>It's worth to know that you can skip <code>svc.cluster.local</code> suffix and even the namespace if the pods are in the same namespace. So you'll just refer to the service as <code>my-service</code>.</p>
<p>For more you can check K8s <a href="https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/" rel="nofollow noreferrer">documents</a> about DNS.</p>
