Kubernetes WebHook 入门 -- 入门案例: apiserver 接入 github

2024-01-10 05:56:12

在这里插入图片描述

博客原文

k8s 集群配置

IPHost配置
11.0.1.150master1 (keepalived+haproxy)2C 4G 30G
11.0.1.151master2 (keepalived+haproxy)2C 4G 30G
11.0.1.152node12C 4G 30G

vip 地址: https://11.0.1.100:16443

介绍

Webhook就是一种HTTP回调,用于在某种情况下执行某些动作,Webhook不是K8S独有的,很多场景下都可以进行Webhook,比如在提交完代码后调用一个Webhook自动构建docker镜像

K8S中提供了自定义资源类型自定义控制器来扩展功能,还提供了动态准入控制,其实就是通过Webhook来实现准入控制,分为两种:验证性质的准入 Webhook (Validating Admission Webhook) 和 修改性质的准入 Webhook (Mutating Admission Webhook)

Admission Webhook有哪些使用场景?如下

  • 在资源持久化到ETCD之前进行修改(Mutating Webhook),比如增加init Container或者sidecar Container
  • 在资源持久化到ETCD之前进行校验(Validating Webhook),不满足条件的资源直接拒绝并给出相应信息

现在非常火热的的 Service Mesh 应用istio就是通过 mutating webhooks 来自动将Envoy这个 sidecar 容器注入到 Pod 中去的:
https://istio.io/docs/setup/kubernetes/sidecar-injection/。

更多详情介绍可参考:
https://kubernetes.io/zh/docs/reference/access-authn-authz/extensible-admission-controllers/

Admission Webhook

上面提到K8S的动态准入控制是通过Webhook来实现的,那么它到底是在哪个环节执行的?请看下图

在这里插入图片描述

综上, k8s webhook 可分为两类:

  1. Mutating Webhook: 这类 webhook 可以更改请求对象
  2. Validating Webhook: 这类 webhook 不能去更改请求对象, 就算更改也会被忽略, 用于写入 etcd 前的验证逻辑

WebHook 入门实践: github 认证接入

源码地址: https://github.com/Ai-feier/k8s-webhook/tree/main/auth-github

写一个用于认证校验的 mutating webhook

请求流程:

  1. 拦截 apiserver 请求
  2. 将请求(TokenReview)中的 token 发送给 github 用于验证
  3. 将 github 返回的用户信息填入 TokenReview

web 服务器

package main

import (
	"context"
	"encoding/json"
	"github.com/google/go-github/github"
	"golang.org/x/oauth2"
	authentication "k8s.io/api/authentication/v1beta1"
	"log"
	"net/http"
)

func main() {
	http.HandleFunc("/authenticate", func(writer http.ResponseWriter, request *http.Request) {
		// 获取请求中的 TokonReview
		decoder := json.NewDecoder(request.Body)
		var tokenReview authentication.TokenReview
		if err := decoder.Decode(&tokenReview); err != nil {
			log.Println("decode error: ", err)
			// 响应返回错误
			writer.WriteHeader(http.StatusBadRequest)
			_ = json.NewEncoder(writer).Encode(map[string]interface{}{
				"apiVersion": "authentication.k8s.io/v1beta1",
				"kind":       "TokenReview",
				"status": authentication.TokenReviewStatus{
					Authenticated: false,
				},
			})
			return
		}

		log.Println("receive request")

		// 检查用户
		// 创建 oauth2 对象, 发送给 github
		oauthToken := oauth2.StaticTokenSource(&oauth2.Token{
			AccessToken: tokenReview.Spec.Token, // tokenReview 的 token
		})
		oauthClient := oauth2.NewClient(context.Background(), oauthToken)
		client := github.NewClient(oauthClient)
		user, _, err := client.Users.Get(context.Background(), "")
		if err != nil {
			log.Println("github auth error: ", err)
			writer.WriteHeader(http.StatusUnauthorized)
			_ = json.NewEncoder(writer).Encode(map[string]any{
				"apiVersion": "authentication.k8s.io/v1beta1",
				"kind":       "TokenReview",
				"status": authentication.TokenReviewStatus{
					Authenticated: false, // 返回认证失败
				},
			})
			return
		}

		// 认证成功
		log.Println("github login success, as: ", *user.Login)
		status := authentication.TokenReviewStatus{
			Authenticated: true,
			User: authentication.UserInfo{
				Username: *user.Login,
				UID: *user.Login,
			},
		}
		json.NewEncoder(writer).Encode(map[string]any{
			"apiVersion": "authentication.k8s.io/v1beta1",
			"kind":       "TokenReview",
			"status": status,
		})
	})
	log.Println(http.ListenAndServe(":3000", nil))
}

Dockerfile 镜像制作

本项目是使用 go 作为 web 服务器, go 提供一种非常好用的编译特性 – 交叉编译

在 dockerfile 中我们选择的 busybox 作为基础镜像, 但默认 busybox 架构为 x86_64(uname -m查看), 所以可以使用 amd64/busybox 或交叉编译 go 代码

当出现这类问题 exec /usr/bin/sh: exec format error 多半是架构问题

推荐: Golang交叉编译详解CSDN

amd64
FROM golang:1.20 as builder

WORKDIR /app

COPY . .

# go 交叉编译: amd64
RUN go env -w  GOPROXY=https://goproxy.cn,direct && go mod tidy && \
    CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main main.go 

RUN chmod +x /app/main

FROM amd64/busybox

COPY --from=builder /app/main .

EXPOSE 3000/tcp

CMD ["/main"]
x86_64
FROM golang:1.20 as builder

WORKDIR /app

COPY . .

# go 交叉编译: x86
RUN go env -w  GOPROXY=https://goproxy.cn,direct && go mod tidy && \
    CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -o main main.go 

RUN chmod +x /app/main

FROM busybox

COPY --from=builder /app/main .

EXPOSE 3000/tcp

CMD ["/main"]
构造镜像
$ docker build -t aifeierwithinmkt/auth-github-webhook:amd64 -f Dockerfile_amd64 .

$ docker build -t aifeierwithinmkt/auth-github-webhook:x86 -f Dockerfile_x86 .

$ docker tag aifeierwithinmkt/auth-github-webhook:amd64 aifeierwithinmkt/auth-github-webhook
检验镜像
$ docker run --rm -p 3000:3000 -d aifeierwithinmkt/auth-github-webhook:amd64
$ docker run --rm -p 3001:3000 -d aifeierwithinmkt/auth-github-webhook:x86

$ curl 127.0.0.1:3000/authenticate
{"apiVersion":"authentication.k8s.io/v1beta1","kind":"TokenReview","status":{"user":{}}}
$ curl 127.0.0.1:3001/authenticate
{"apiVersion":"authentication.k8s.io/v1beta1","kind":"TokenReview","status":{"user":{}}}

镜像服务正常

Makefile

build_amd64:
	mkdir -p bin/amd64
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/amd64/main .

build_x86:
	mkdir -p bin/x86
	CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -o bin/x86/main .	

webhook 接入 apiserver

webhook.yaml
apiVersion: v1
clusters:
- cluster:
    server: http://192.168.1.7:3000/authenticate
  name: github-authn
contexts:
- context:
    cluster: github-authn
    user: authn-apiserver
  name: webhook
current-context: webhook
kind: Config
preferences: {}
users:
- name: authn-apiserver
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QoyR29yOXJMaENnSStMR2ZWMmZvYllOMGdONkVZYnVoMjkrK3FvOE4wUk5KMi9IWkVyQ2o3bjlCVk5yZXVhWi9FClJKUFFDNFRoWXhEcll0WVdhMkpseHdLQmdRRFh3SjU0LzNtVk5DTkFucnVOZzNmYkNla21SZ29veDRmT2hCbncKdkxHYmx4S0ZBQjBraStyRDVuU2xZWjI3cm9uOXpmOGdtNmd6K2hPSWk4OWtoMHFSZEY2Z29GYUNXQlZvanFuYwo3WDR5N2hYOTFKS1phMmlVVllGMHJYZUlaSkI1bTdFVm9iYmJxZGo0ZTA5dXlncktkbXprNWpEbzNVenBIQThoCk5WdnJZUUtCZ1FDNDRreXNsRlBUVkNlbkp5SDFHLy82VmQ0aXRFaFlYQVllYWJUdU15eVlkZFlxcnJYTTFUS1QKL24wRHQrQXFIa0dmU2pvUExCa3NITkNDZWZnZSt0dDRxVmFPNjJyWkhRRTQ0TXVueHhxRXBNdmxFeUNEVUM4cApTaUF6bW9XaDR4R2tUdkpLUlBLWGlPNEdjbk5wZVNuUVY5RjJSY01ocllCUVZjTzJLb2N1Umc9PQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo=

该配置与 kubeconfig 配置相同, 直接在 kubeconfig 的基础上修改就行, 重要的是 cluster 的 server (即提供服务的地址

apiserver 挂载 webconfig
# 修改 apiserver 配置参数, 增加:
- --authentication-token-webhook-config-file=/etc/config/webhook.yaml

# 相应增加挂载卷
    volumes:
    - hostPath:
        path: /etc/config
        type: DirectoryOrCreate
      name: auth-webhook
    
    volumeMounts:
    - mountPath: /etc/config
      name: auth-webhook
      readOnly: true
  1. 修改 manifest 后 apiserver 理论上会自动重启, 如果没有则 systemctl restart kubelet
  2. 本文实验主题为认证 webhook 可直接使用 --authentication-token-webhook-config-file, 如需自定义 mutatingwebhook 请参考: mutatingwebhook
在 github 中创建认证 token

setting -> Developer Settings -> Personal access tokens (classic)

将 token 添加到 kubeconfig
# 在 kubeconfig 下增加一个 user
- name: githubuser
  user:
    token: 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

验证

$ k get po --user githubuser
Error from server (Forbidden): pods is forbidden: User "Ai-feier" cannot list resource "pods" in API group "" in the namespace "default"
授权验证
$ k create clusterrolebinding testview --clusterrole view --user Ai-feier
$ k get po --user githubuser
NAME                         READY   STATUS    RESTARTS      AGE
nginx-kusc00401              0/1     Pending   0             4d23h
testnginx-5cdb847cd9-7nm4k   1/1     Running   1 (29h ago)   43h
testnginx-5cdb847cd9-llmxf   1/1     Running   1 (29h ago)   43h
testnginx-5cdb847cd9-qfsdn   1/1     Running   1 (29h ago)   43h
web-server                   0/1     Pending   0             4d22h

参考

  1. https://kubernetes.io/zh-cn/docs/reference/access-authn-authz/extensible-admission-controllers/

文章来源:https://blog.csdn.net/m0_51964671/article/details/135459895
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。