douzhun8615 2018-09-21 15:57
浏览 89

Memcached Kubernetes客户端Golang

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>
  • 写回答

2条回答 默认 最新

  • dpcwz210393 2018-09-21 16:13
    关注

    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!

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?