Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

develop a regisry with kubernetes headless service #106

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

Conversation

gsmini
Copy link

@gsmini gsmini commented Apr 13, 2023

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 , and i give a example to use this plugin in examples folder.

@Davincible
Copy link
Contributor

Thanks a lot for your PR! Interesting approach!

Could you please have a look at the lining issues, and translate all Chinese to English?

Then I can review the PR in more detail.

@gsmini
Copy link
Author

gsmini commented May 9, 2023

Thanks a lot for your PR! Interesting approach!

Could you please have a look at the lining issues, and translate all Chinese to English?

Then I can review the PR in more detail.

I'm sorry that it took me so long to get your message, because I was delayed by some things. According to your requirements and the error prompt in test.sh, I made relevant modifications and changed the Chinese commnets in the code into English. Please kindly help to approve this pr again.
Look forward to your reply, I am always online!

@gsmini
Copy link
Author

gsmini commented Jul 25, 2023

this repo is for demo how to use this plugin :https://github.com/orgs/go-micro-v4-demo/repositories
the "frontend" service call " user " and "helloword" service, especially in frontend/main.go showed the whole code :

const UserSvcName = "user-svc"        //the name of k8s service the micoservice "user"  used 
const HelloWordSvcName = "helloworld" //the name of k8s service the micoservice "helloworld" used
func main() {
        // u can use this plugin so easily like this
	UserSvc := &k8sHeadlessSvc.Service{Namespace: "default", SvcName: UserSvcName, PodPort: 8080}
	//HelloWordSvc := &k8sHeadlessSvc.Service{Namespace: "default", SvcName: HelloWordSvcName, PodPort: 9090}
	reg := k8sHeadlessSvc.NewRegistry([]*k8sHeadlessSvc.Service{UserSvc})
	srv := micro.NewService(
		micro.Server(mhttp.NewServer()), 
		micro.Client(mgrpc.NewClient())) 
	srv.Init(
		micro.Name(service),
		micro.Version(version),
		micro.Address("0.0.0.0:8080"), 
		micro.Registry(reg),
	)
       .....

@gsmini
Copy link
Author

gsmini commented Jul 26, 2023

@Davincible When you have time, you can help me with the code review thanks .

@gsmini gsmini closed this Sep 19, 2023
@gsmini gsmini reopened this Sep 19, 2023
@jochumdev jochumdev self-assigned this Nov 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants