Skip to content
This repository was archived by the owner on May 7, 2025. It is now read-only.

K8s headless svc plugin #150

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open

Conversation

gsmini
Copy link

@gsmini gsmini commented Nov 7, 2024

I am very honored to use go-micro and to be involved in this project.
when i study go-micro v4 and deploy it with k8s , i found the registry plugin kubernets plugin, it inspired me.

i found the kubernets plugin use client.go to call apiServer which make it not very friendly to use. Coincidentally, I've been studying kubenetes recently, and i found when the Service.ClusterIP is None we can take the Service as a DNS server, so i
try to make this happen in registry.

apiVersion: v1
kind: Service
metadata:
  name: user-svc
  namespace: default
spec:
  clusterIP: None #attention here
  ports:
    - port: 8080
  selector:
    app:  user

  sessionAffinity: ClientIP
  sessionAffinityConfig:
    clientIP:
        timeoutSeconds: 3600

so, this kubernetes-headless-svc is to resolve Service name to get the endpoints ip which are also podIP u call.

root@hecs-410147:~# kubectl get endpoints
NAME                  ENDPOINTS             AGE
frontend-svc          10.32.0.4:8080       100s
user-svc              10.32.0.9:8080        200s

the plugin return 10.32.0.9:8080 to fronted applition to call
it is so easy finish this work ,just use a net package like this:

package main
import (
	"fmt"
	"net"
)

func main() {
	//we just use net.LookupIP to get ip address of www.twitter.com
	ipRecords, err := net.LookupIP("www.twitter.com")
	if err != nil {
		panic(err)
	}
	for _, value := range ipRecords {
		fmt.Println(value.String())
	}
}

you can take a look of README.md to get the detail document about it(https://github.com/gsmini/plugins/tree/k8s-headless-svc/v4/registry/kubernetes-headless-svc) , and i give a example to use this plugin in examples folder.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants