kubenetes - greeter-web call greeter-srv 500 error?

6/6/2019

I'm trying to get this go-micro greeter example working on Kubernetes https://github.com/micro/examples/tree/master/greeter

I can run this locally in docker fine. However when I attempt to access the greeter api service via Kubernetes (http://{{external-ip}}/greeter/say/hello), I get the error:

{"id":"go.micro.api","code":500,"detail":"not found","status":"Internal Server Error"}

For the sake of troubleshooting I've simplified the scenario, I simply want to be able to make a call via the micro api to a go-micro api service. Below is my setup:

micro api call greeter-web successful,but greeter-web call greeter-srv error.

error message

{ "id": "go.micro.client", "code": 500, "detail": "error selecting greeter node: not found", "status": "Internal Server Error" }

my yaml is here:

greeter-web

package main

import (
    "fmt"
    "log"
    "net/http"

    hello "github.com/micro/examples/greeter/srv/proto/hello"
    "github.com/micro/go-micro/client"
    "github.com/micro/go-web"
    k8s "github.com/micro/kubernetes/go/web"

    "context"
)

func main() {
    service := k8s.NewService(
        web.Name("go.micro.web.greeter"),
    )

    service.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        if r.Method == "POST" {
            r.ParseForm()

            name := r.Form.Get("name")
            if len(name) == 0 {
                name = "World"
            }

            cl := hello.NewSayService("greeter", client.DefaultClient)
            rsp, err := cl.Hello(context.Background(), &hello.Request{
                Name: name,
            })

            if err != nil {
                http.Error(w, err.Error(), 500)
                return
            }

            w.Write([]byte(`<html><body><h1>` + rsp.Msg + `</h1></body></html>`))
            return
        }

        fmt.Fprint(w, `<html><body><h1>Enter Name<h1><form method=post><input name=name type=text /></form></body></html>`)
    })

    if err := service.Init(); err != nil {
        log.Fatal(err)
    }

    if err := service.Run(); err != nil {
        log.Fatal(err)
    }
}

greeter-srv

package main

import (
    "log"
    "time"

    hello "github.com/micro/examples/greeter/srv/proto/hello"
    "github.com/micro/go-micro"
    k8s "github.com/micro/kubernetes/go/micro"
    "context"
)

type Say struct{}

func (s *Say) Hello(ctx context.Context, req *hello.Request, rsp *hello.Response) error {
    log.Print("Received Say.Hello request")
    rsp.Msg = "Hello " + req.Name
    return nil
}

func main() {
    service := k8s.NewService(
        micro.Name("greeter"),
        micro.RegisterTTL(time.Second*30),
        micro.RegisterInterval(time.Second*10),
    )

    // optionally setup command line usage
    service.Init()

    // Register Handlers
    hello.RegisterSayHandler(service.Server(), new(Say))

    // Run server
    if err := service.Run(); err != nil {
        log.Fatal(err)
    }
}

greeter-srv service

apiVersion: v1
kind: Service
metadata:
  name: greeter
  namespace: micro
  labels:
    app: greeter
spec:
  ports:
    - port: 8080
      protocol: TCP
  selector:
    name: greeter-srv

greater-srv pod

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: micro
  name: greeter
spec:
  replicas: 1
  selector:
    matchLabels:
      name: greeter-srv
  template:
    metadata:
      labels:
        name: greeter-srv
    spec:
      containers:
        - name: greeter
          command: [
            "/greeter-srv",
            "--selector=static",
            "--server_address=0.0.0.0:8080",
            "--broker_address=0.0.0.0:10001"
          ]
          image: harbor.my.work.com/micro-test/greeter-srv:latest
          imagePullPolicy: Always
          ports:
            - containerPort: 8080
              name: greeter-port
        - name: health
          command: [
            "/health",
            "--health_address=0.0.0.0:8081",
            "--server_name=greeter",
            "--server_address=0.0.0.0:8080"
          ]
          image: harbor.my.work.com/micro-test/health:latest
          imagePullPolicy: Always
          livenessProbe:
            httpGet:
              path: /health
              port: 8081
            initialDelaySeconds: 3
            periodSeconds: 3
-- watermelon-brother
go-micro
kubernetes

1 Answer

6/6/2019
[root@k8s-deploy services]# kubectl get pods,svc,ep --all-namespaces
NAMESPACE     NAME                                           READY   STATUS    RESTARTS   AGE
kube-system   pod/calico-kube-controllers-7fd84c849f-vwxgk   1/1     Running   0          40m
kube-system   pod/calico-node-cdbkr                          1/1     Running   8          12d
kube-system   pod/calico-node-l2kh2                          1/1     Running   9          12d
kube-system   pod/calico-node-xr2cd                          1/1     Running   8          12d
kube-system   pod/calico-node-xwbmk                          1/1     Running   8          12d
kube-system   pod/coredns-55f46dd959-hft6f                   1/1     Running   0          40m
kube-system   pod/coredns-55f46dd959-nx6qx                   1/1     Running   8          12d
kube-system   pod/heapster-fdb7596d6-7l97g                   1/1     Running   8          12d
kube-system   pod/kubernetes-dashboard-68ddcc97fc-kvxfw      1/1     Running   2          40m
kube-system   pod/metrics-server-6c898b5b8b-bk7cz            1/1     Running   16         12d
micro         pod/greeter-79cf6cd6bb-rh47x                   2/2     Running   0          9m25s
micro         pod/greeter-web-77b96d849-fc7n8                1/1     Running   0          23m
micro         pod/micro-web-5cb474c8cc-n42gq                 1/1     Running   0          23m
NAMESPACE     NAME                           TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)  AGE
default       service/kubernetes             ClusterIP   10.68.0.1       <none>        443/TCP  12d
kube-system   service/heapster               ClusterIP   10.68.240.195   <none>        80/TCP  12d
kube-system   service/kube-dns               ClusterIP   10.68.0.2       <none>        53/UDP,53/TCP,9153/TCP  12d
kube-system   service/kubernetes-dashboard   NodePort    10.68.40.70     <none>        443:29495/TCP  12d
kube-system   service/metrics-server         ClusterIP   10.68.91.180    <none>        443/TCP  12d
micro         service/greeter                ClusterIP   10.68.157.133   <none>        8080/TCP                 9m4s
micro         service/greeter-web            NodePort    10.68.63.126    <none>        8688:20003/TCP           27h
micro         service/micro-web              NodePort    10.68.108.247   <none>        8086:20002/TCP           26h
NAMESPACE     NAME                                ENDPOINTS                                                           AGE
default       endpoints/kubernetes                192.168.0.220:6443,192.168.0.221:6443                               12d
kube-system   endpoints/heapster                  176.26.169.159:8082                                                 12d
kube-system   endpoints/kube-controller-manager   <none>                                                              12d
kube-system   endpoints/kube-dns                  176.26.169.161:53,176.26.169.162:53,176.26.169.161:53 + 3 more...   12d
kube-system   endpoints/kube-scheduler            <none>                                                              12d
kube-system   endpoints/kubernetes-dashboard      176.26.169.146:8443                                                 12d
kube-system   endpoints/metrics-server            176.26.169.141:443                                                  12d
micro         endpoints/greeter                   176.26.169.171:8080                                                 9m4s
micro         endpoints/greeter-web               176.26.107.200:8688                                                 27h
micro         endpoints/micro-web                 176.26.107.201:8082                                                 26h
[root@k8s-deploy services]# kubectl get pods -n micro
NAME                          READY   STATUS    RESTARTS   AGE
greeter-79cf6cd6bb-rh47x      2/2     Running   0          16m
greeter-web-77b96d849-fc7n8   1/1     Running   0          30m
micro-web-5cb474c8cc-n42gq    1/1     Running   0          30m
[root@k8s-deploy services]# kubectl logs greeter-79cf6cd6bb-rh47x greeter -n micro
2019/06/06 17:04:07 Listening on [::]:8080
2019/06/06 17:04:07 Broker Listening on [::]:10001
2019/06/06 17:04:07 Broker [grpc] Listening on [::]:10001
2019/06/06 17:04:07 Registering node: greeter-a54b5993-3422-410d-b548-e1bc629ce182

[root@k8s-deploy services]# kubectl logs greeter-79cf6cd6bb-rh47x health -n micro
[root@k8s-deploy services]#
[root@k8s-deploy services]# kubectl logs greeter-79cf6cd6bb-rh47x health -n micro
[root@k8s-deploy services]# kubectl logs greeter-web-77b96d849-fc7n8  -n micro
2019/06/06 16:50:02 Listening on [::]:8688
[root@k8s-deploy services]#
[root@k8s-deploy services]# kubectl logs micro-web-5cb474c8cc-n42gq   -n micro
2019/06/06 16:50:13 HTTP API Listening on [::]:8082
2019/06/06 16:50:13 Listening on [::]:35315
2019/06/06 16:50:13 Broker Listening on [::]:36389
2019/06/06 16:50:13 Broker [grpc] Listening on [::]:36389
2019/06/06 16:50:13 Registering node: go.micro.web-65751383-2126-4df0-8059-7ea14094f7de
176.26.114.128 - - [06/Jun/2019:16:50:34 +0000] "GET /greeter HTTP/1.1" 200 98 "" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
176.26.114.128 - - [06/Jun/2019:16:50:34 +0000] "GET /favicon.ico HTTP/1.1" 200 0 "http://192.168.0.220:20002/greeter" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
176.26.114.128 - - [06/Jun/2019:16:50:36 +0000] "POST /greeter HTTP/1.1" 500 120 "http://192.168.0.220:20002/greeter" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
-- watermelon-brother
Source: StackOverflow