<p>I am attempting to connect to my memcached instance via golang.  Both my app and Memcached will be in Kubernetes.</p>

<p>I couldn't find a good way (or example) on how to dynamically discover memcached and ensure that it will update if the pods scale.</p>

<p>I can manually create a client with the IP but not sure the best way to dynamically create clients in K8s with Golang.  Any help would be appreciated, below is my test function that I am using (if it helps).</p>

<pre><code>func TestMemcached(t *testing.T) {
    mc := memcache.New("10.40.1.18:11211", "10.40.2.16:11211")
    mc.Set(&amp;memcache.Item{Key: "foo", Value: []byte("my value"), Expiration: 1})

    it, err := mc.Get("foo")
    if err != nil {
        t.Error("Error pulling from cache:" + err.Error())
    }
    fmt.Println("Key: " + string(it.Key) + "Value: " + string(it.Value))
    //Sleep for a second and a half to let the key expire
    time.Sleep(1500 * time.Millisecond)
    it, err = mc.Get("foo")
    if err == nil {
        t.Error("Got a hit when I didn't expect it:" + err.Error())
    }
}
</code></pre>

<p>kubectl describe svc memcached:</p>

<pre><code>Name:              memcached
Namespace:         redfalcon
Labels:            app=memcached
                   chart=memcached-2.3.1
                   heritage=Tiller
                   release=memcached
Annotations:       &lt;none&gt;
Selector:          app=memcached
Type:              ClusterIP
IP:                None
Port:              memcache  11211/TCP
TargetPort:        memcache/TCP
Endpoints:         10.40.1.23:11211,10.40.2.24:11211
Session Affinity:  None
Events:            &lt;none&gt;
</code></pre>

mornindew
<p>You can expose your memcache instance(s) as a [Service](<a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/services-networking/service/</a> in Kubernetes. </p>

<p>This way in the client you only specify the internal DNS entry for your service which would be something like <code>&lt;service-name&gt;.&lt;k8s-namespace&gt;.svc.cluster.local</code>.</p>

<p>In your code you would need something like this:</p>

<pre><code>mc := memcache.New("memcached.redfalcon.svc.cluster.local:11211")
</code></pre>

<p>You can also use <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/" rel="nofollow noreferrer">ConfigMaps</a> to specify this configuration for your application.</p>

<p>Hope it helps!</p>
