<p>I want to connect a docker container running locally to a service running on a Kubernetes cluster. To do so I have exposed a service through reserving some static IP addresses.</p>

<p>I have also saved those IP addresses in local DNS, in the <code>/etc/hosts/</code> file:</p>

<pre><code>123.123.123.12 host1
456.456.456.45 host2 
</code></pre>

<p>I want to link my container to that such that all the traffic is routed to those addresses so that it can be processed by the cluster. I am using the link feature in the docker container but it isn't working. </p>

<p>I want to connect directly using IP? How should I do this?</p>

kush
<p>There's no difference doing this if the client is or isn't in Docker.  However you have the <a href="https://kubernetes.io/docs/tasks/access-application-cluster/service-access-application-cluster/" rel="nofollow noreferrer">service exposed from Kubernetes</a>, you'd make the same connection to it from a process running on an external host or from a process running in a Docker container on that host.</p>

<p>Say, as in the example in the Kubernetes documentation, you're running a NodePort service that's accessible on port 31496 on every node in the cluster, and you're trying to connect to it from outside the cluster.  Maybe as in the question 123.123.123.12 is some node in the cluster.  A typical setup would be to get the location of the service from an environment variable (JavaScript <code>process.env.THE_SERVICE_URL</code>; Ruby <code>ENV['THE_SERVICE_URL']</code>; Python <code>os.environ['THE_SERVICE_URL']</code>; ...).</p>

<p>When you're developing, you could set that variable in your local shell:</p>

<pre class="lang-sh prettyprint-override"><code>export THE_SERVICE_URL=http://123.123.123.12:31496
cd here &amp;&amp; ./kubernetes_client_script.py
</code></pre>

<p>When you go to deploy your application, you can set the same environment variable:</p>

<pre class="lang-sh prettyprint-override"><code>docker run -e THE_SERVICE_URL=http://123.123.123.12:31496 me:k8s-client
</code></pre>
