什么是 Sidecar 模式?
Sidecar 模式是一种软件架构设计模式,尤其适用于构建和部署微服务。其主要表现为在主应用容器旁部署附加服务,称为"边车(Sidecar)",在不改变主应用程序功能的情况下增强其功能,这种模式常用于容器化应用程序。
服务
- Sidecar 认证服务(Go)
- 主服务(Go)
1.主服务
主服务非常简单,只有一个 API 端点,该端点以 HTTP 响应的形式返回 JSON 消息。
package main
import (
"fmt"
"github.com/gin-gonic/gin"
)
func main() {
fmt.Println("Api Go!")
r := gin.Default()
r.GET("/ping", ping)
r.Run(":8080")
}
func ping(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
}
2.Dockefile
FROM golang:1.22-alpine as builder
WORKDIR /go/app
COPY . .
RUN go build -v -o app cmd/api/main.go
FROM alpine
COPY --from=builder /go/app/ .
EXPOSE 8080
CMD ["/app"]
运行以下命令编译 Docker 镜像。
docker build -t mertcakmak2/go-container .
3.Sidecar 服务
Sidecar 服务会将传入的 HTTP 请求转发给主服务。
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"net/http/httputil"
"net/url"
)
func main() {
fmt.Println("Sidecar Go!")
r := gin.Default()
// Reverse Proxy
r.Any("/*proxyPath", authProxy)
r.Run(":8081")
}
// Simulate Auth
func authProxy(c *gin.Context) {
// Bearer Token Check...
// MAIN CONTAINER URL
remote, err := url.Parse("http://localhost:8080")
if err != nil {
panic(err)
}
proxy := httputil.NewSingleHostReverseProxy(remote)
proxy.Director = func(req *http.Request) {
req.Header = c.Request.Header
req.Host = remote.Host
req.URL.Scheme = remote.Scheme
req.URL.Host = remote.Host
req.URL.Path = c.Param("proxyPath")
}
proxy.ServeHTTP(c.Writer, c.Request)
}
4.Dockerfile
FROM golang:1.22-alpine as builder
WORKDIR /go/app
COPY . .
RUN go build -v -o app cmd/sidecar/main.go
FROM alpine
COPY --from=builder /go/app/ .
EXPOSE 8081
CMD ["/app"]
运行以下命令编译 Docker 镜像。
docker build -f Dockerfile.sidecar -t mertcakmak2/go-sidecar .
Kubernetes 部署
Kubernetes 部署文件:
apiVersion: v1
kind: Service
metadata:
name: go-container-sidecar
spec:
selector:
app: go-container-sidecar
ports:
- protocol: "TCP"
name: main-container-port
port: 8080
targetPort: 8080
- protocol: "TCP"
name: sidecar-container-port
port: 8081
targetPort: 8081
type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: go-container-sidecar
spec:
selector:
matchLabels:
app: go-container-sidecar
replicas: 1
template:
metadata:
labels:
app: go-container-sidecar
spec:
containers:
- name: go-container
image: mertcakmak2/go-container:latest
imagePullPolicy: Always
ports:
- containerPort: 8080
- name: go-sidecar
image: mertcakmak2/go-sidecar:latest
imagePullPolicy: Always
ports:
- containerPort: 8081
创建部署文件,在不同端口上公开两个服务。
主服务 => 8080 Sidecar 服务 => 8081
运行以下命令进行部署。
kubectl apply -f k8s-deployment.yaml
1.Kubernetes 控制面板
包含两个容器的 Pod。
2.发送 HTTP 请求
(1) 访问 Minikube 服务:
minikube service go-container-sidecar --url
生成了两个 URL,第一个 URL 是主服务,第二个 URL 是 Sidecar 服务。
主服务 => http://127.0.0.1:57496 Sidecar 服务 => http://127.0.0.1:57497
(2) 用 Curl 命令发送请求。
(3) API 返回了 JSON 消息,我们看一下容器日志。
Sidecar 服务将这些传入的 HTTP 请求转发给主服务。
参考资料:
- Kubernetes Sidecar Container - Best Practices and Examples[2]
- Sidecar Container: What is it and How to use it (Examples)[3]
参考资料:
- [1]Sidecar Pattern with Kubernetes and Go: https://medium.com/@mertcakmak2/sidecar-pattern-with-kubernetes-and-go-68e0278c5260
- [2]Kubernetes Sidecar Container - Best Practices and Examples: https://spacelift.io/blog/kubernetes-sidecar-container
- [3]Sidecar Container: What is it and How to use it (Examples): https://kodekloud.com/blog/kubernetes-sidecar-container