selector

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 4, 2022 License: Apache-2.0 Imports: 12 Imported by: 0

README

About

Memcache server discovery based on Kubernetes endpoints for Go memcache client

How it works:

  • Watches endpoints containing memcache pod ips using tiny k8s client
  • Implements ServerPicker interface to dynamically update memcache ip addresses.

Use cases

In dynamic environments like Kubernetes both clients (pods calling memcache) and memcache pods could change. There are few issues:

  1. Calling memcache using clusterIP service is quite useless (unless you have one replica) as kernel will perform round-robin across service endpoints.
  2. You can enable sessionAffinity. This will ensure that client pod always goes to the same memcache instance. But what happens if pods are redeployed?
  3. Using StatefulSet with headless services and passing each endpoint is kind of static too.

Installing

go get github.com/castai/k8s-memcache-selector

Usage

package main

import (
	"context"
	"log"

	"github.com/bradfitz/gomemcache/memcache"

	selector "github.com/castai/k8s-memcache-selector"
)

func main() {
	ss, err := selector.NewServerList(context.Background(), "memcache-headless:11211")
	if err != nil {
		log.Fatalf("creating server selector: %v", err)
	}
	cache := memcache.NewFromSelector(ss)

	// Use cache..
}

Permissions

You application needs permission to get and watch endpoints. Example manifest:

apiVersion: v1
kind: ServiceAccount
metadata:
  name: endpoints-watcher
  namespace: e2e
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: endpoints-watcher
rules:
  - apiGroups: [ "" ]
    resources: [ "endpoints" ]
    verbs: [ "get", "watch" ]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: endpoints-watcher
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: endpoints-watcher
subjects:
  - kind: ServiceAccount
    name: endpoints-watcher
    namespace: e2e
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-service
  namespace: e2e
spec:
  template:
    metadata:
      labels:
        app: my-service
    spec:
      serviceAccount: endpoints-watcher
      containers:
        - name: nginx
          image: nginx
      restartPolicy: Never
  backoffLimit: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoServers = errors.New("k8s-memcache-selector: no servers configured or available")
)

Functions

This section is empty.

Types

type Logger

type Logger interface {
	Errorf(format string, args ...any)
	Infof(format string, args ...any)
}

type Option

type Option func(o *options)

func WithLogger

func WithLogger(log Logger) Option

type ServerList

type ServerList struct {
	// contains filtered or unexported fields
}

ServerList implements memcache ServerSelector interface. Under the hood kubernetes endpoints are used for discovery of memcache server ips. See https://github.com/bradfitz/gomemcache/blob/master/memcache/selector.go

func NewServerList

func NewServerList(ctx context.Context, addr string, opt ...Option) (*ServerList, error)

func (*ServerList) Each

func (ss *ServerList) Each(f func(net.Addr) error) error

Each iterates over each server calling the given function

func (*ServerList) PickServer

func (ss *ServerList) PickServer(key string) (net.Addr, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL