Go Grpc部署到 k8s【端口共享】

书接上文Go Grpc部署到 k8s【负载均衡】 grpc server端我们暴露了9090和8080端口,这次我们的http服务用iris,并且绑定到9090端口。

GO文件

服务端代码main.go:

package main

import (
	"context"
	"fmt"
	pb "grpcdemo/protos"
	"net"

	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/middleware/logger"
	"github.com/kataras/iris/v12/middleware/recover"
	"github.com/soheilhy/cmux"
	"google.golang.org/grpc"
)

func main() {
	l, err := net.Listen("tcp", ":9090")
	fmt.Println(err)
	m := cmux.New(l)

	// grpc
	grpcL := m.MatchWithWriters(cmux.HTTP2MatchHeaderFieldSendSettings("content-type", "application/grpc"))
	grpcServer := grpc.NewServer()                  // 创建GRPC
	pb.RegisterGreeterServer(grpcServer, &server{}) // 在GRPC服务端注册服务

	go grpcServer.Serve(grpcL)

	// http
	app := iris.New()
	app.Use(recover.New())
	app.Use(logger.New())

	app.Handle("GET", "/", func(ctx iris.Context) {
		ctx.WriteString("pong")
	})

	go func() {
		httpL := m.Match(cmux.HTTP1Fast())
		app.Run(iris.Listener(httpL))
	}()

	// Start serving!
	m.Serve()
}

type server struct{}

func NewServer() *server {
	return &server{}
}

func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
	msg := "Resuest By:" + in.Name + " Response By :" + LocalIp()
	fmt.Println("GRPC Send: ", msg)
	return &pb.HelloReply{Message: msg}, nil
}

func LocalIp() string {
	addrs, _ := net.InterfaceAddrs()
	var ip string = "localhost"
	for _, address := range addrs {
		if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
			if ipnet.IP.To4() != nil {
				ip = ipnet.IP.String()
			}
		}
	}
	return ip
}

服务端代码deploy.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: grpcserver
  namespace: go
  labels:
    app: grpcserver
    version: v1
spec:
  replicas: 1
  minReadySeconds: 10 
  selector:
    matchLabels:
      app: grpcserver
      version: v1
  template:
    metadata:
      labels:
        app: grpcserver
        version: v1
    spec:
      imagePullSecrets:
      - name: regsecret
      containers:
      - name: grpcserver
        image: 192.168.100.30:8080/go/grpcserver:2022
        ports:
        - containerPort: 9090
        imagePullPolicy: Always

---
apiVersion: v1 
kind: Service 
metadata:
  name: grpcserver
  namespace: go 
  labels:
    app: grpcserver
    version: v1
spec:
  ports:
    - port: 9090 
      targetPort: 9090 
      name: grpcserver
      protocol: TCP
  selector:
    app: grpcserver

服务端build.sh

#!/bin/bash
#cd $WORKSPACE
export GOPROXY=https://goproxy.io
 
 #根据 go.mod 文件来处理依赖关系。
go mod tidy
 
# linux环境编译
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main
 
# 构建docker镜像,项目中需要在当前目录下有dockerfile,否则构建失败

docker build -t grpcserver .
docker tag  grpcserver 192.168.100.30:8080/go/grpcserver:2022

docker login -u admin -p '123456' 192.168.100.30:8080
docker push 192.168.100.30:8080/go/grpcserver
 
docker rmi  grpcserver
docker rmi 192.168.100.30:8080/go/grpcserver:2022

服务端Dockerfile

FROM golang:1.15.6

RUN mkdir -p /app

WORKDIR /app
 
ADD main /app/main

EXPOSE 9090
 
CMD ["./main"]

客户端 default.go

package controllers

import (
	"context"
	"fmt"
	pb "grpcclient/protos"
	"io/ioutil"
	"net"
	"net/http"
	"time"

	"github.com/astaxie/beego"
	"google.golang.org/grpc"
)

type MainController struct {
	beego.Controller
}

func (c *MainController) Get() {
	address := "grpcserver:9090"
	// GRPC
	conn, _ := grpc.Dial(address, grpc.WithInsecure())
	defer conn.Close()
	grpcClient := pb.NewGreeterClient(conn)
	req := pb.HelloRequest{Name: "gavin_" + LocalIp()}

	res, err := grpcClient.SayHello(context.Background(), &req)
	if err != nil {
		msg := fmt.Sprintf("grpc client  client.SayHello  has err:%v\r\n", err)
		c.Ctx.WriteString(msg)
		return
	}

	//http
	var httpClient = http.Client{
		Timeout: 10 * time.Second,
	}
	resp, err := httpClient.Get("http://" + address)
	if err != nil {
		msg := fmt.Sprintf("http get  has err:%v\r\n", err)
		c.Ctx.WriteString(msg)
		return
	}

	defer resp.Body.Close()
	bytes, _ := ioutil.ReadAll(resp.Body)
	message := fmt.Sprintf("GRPC Clinet Received:%v ,Http Get:%v ", res.Message, string(bytes))
	//
	c.Ctx.WriteString(message)
}

func LocalIp() string {
	addrs, _ := net.InterfaceAddrs()
	var ip string = "localhost"
	for _, address := range addrs {
		if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
			if ipnet.IP.To4() != nil {
				ip = ipnet.IP.String()
			}
		}
	}
	return ip
}

客户端 deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: grpcclient
  namespace: go
  labels:
    app: grpcclient
    version: v1
spec:
  replicas: 1
  minReadySeconds: 10 
  selector:
    matchLabels:
      app: grpcclient
      version: v1
  template:
    metadata:
      labels:
        app: grpcclient
        version: v1
    spec:
      imagePullSecrets:
      - name: regsecret
      containers:
      - name: grpcclient
        image: 192.168.100.30:8080/go/grpcclient:2022
        ports:
        - containerPort: 8080
        imagePullPolicy: Always

---

apiVersion: v1 
kind: Service 
metadata:
  name: grpcclient
  namespace: go 
  labels:
    app: grpcclient
    version: v1
spec:
  type: ClusterIP
  ports:
    - port: 8080 
      targetPort: 8080 
      protocol: TCP
  selector:
    app: grpcclient

客户端 build.sh

#!/bin/bash
#cd $WORKSPACE

export GOPROXY=https://goproxy.io
 
 #根据 go.mod 文件来处理依赖关系。
go mod tidy
 
# linux环境编译
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main
 
# 构建docker镜像,项目中需要在当前目录下有dockerfile,否则构建失败

docker build -t grpcclient .
docker tag  grpcclient 192.168.100.30:8080/go/grpcclient:2022

docker login -u admin -p '123456' 192.168.100.30:8080
docker push 192.168.100.30:8080/go/grpcclient
 
docker rmi  grpcclient
docker rmi 192.168.100.30:8080/go/grpcclient:2022

客户端 Dockerfile

FROM golang:1.15.6

RUN mkdir -p /app
RUN mkdir -p /app/conf
RUN mkdir -p /app/logs

WORKDIR /app
 
ADD main /app/main

EXPOSE 8080
 
CMD ["./main"]

Deploy

默认的k8s 部署运行结果:

Go Grpc部署到 k8s【端口共享】

Istio部署

需要先启用拦截 kubectl label namespace go istio-injection=enabled

Istio想要启用的话 就必须修改svc的 端口名称如:

Go Grpc部署到 k8s【端口共享】

 但是这样http返回失败:

Go Grpc部署到 k8s【端口共享】

所以用一般的方式是不能能搞定grpc和http协议.我后面尝试了很多方法 还是没搞定。。。。,估计只能放弃了

 

上一篇:C# 实现gRPC通信


下一篇:.Net Core gRPC入门(二)——Web客户端