Memcached Kubernetes Client Golang

9/21/2018

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

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.

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).

func TestMemcached(t *testing.T) {
    mc := memcache.New("10.40.1.18:11211", "10.40.2.16:11211")
    mc.Set(&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())
    }
}

kubectl describe svc memcached:

Name:              memcached
Namespace:         redfalcon
Labels:            app=memcached
                   chart=memcached-2.3.1
                   heritage=Tiller
                   release=memcached
Annotations:       <none>
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:            <none>
-- mornindew
go
kube-dns
kubernetes
memcached

2 Answers

9/21/2018

You can expose your memcache instance(s) as a [Service](https://kubernetes.io/docs/concepts/services-networking/service/ in Kubernetes.

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

In your code you would need something like this:

mc := memcache.New("memcached.redfalcon.svc.cluster.local:11211")

You can also use ConfigMaps to specify this configuration for your application.

Hope it helps!

-- Rico
Source: StackOverflow

9/26/2018

Ok, I figured it out! It had to do with telepresence (https://www.telepresence.io/) that I am using for local development. It gives me access to all K8s resources from my developmnet environment. Unfortunately there is a defect in Linux where it cannot use K8s FQDN for lookup.

https://github.com/telepresenceio/telepresence/issues/161

Fixing is is sort of a hack, I port forwarded my K8s pod and when running in local development mode I changed my URL to "localhost:port". This worked but it isn't really testing Memcached as K8s would use it.

When I deploy into K8s it works fine.

-- mornindew
Source: StackOverflow