专栏首页CNCF技术漫谈 | 为Kubernetes构建自定义admission webhook

技术漫谈 | 为Kubernetes构建自定义admission webhook

本文包含admission webhook的server端demo、证书制作以及验证,欢迎各位前来围观。demo中定义了webhook server, 通过jsonpatch的方式修改带有特定标签deployment的resources。

为什么使用 admission webhook

Kubernetes的admission webhook为开发者提供了非常灵活的插件模式,在Kubernetes资源持久化之前用户可以对指定资源做校验、修改等操作。应用场景 eg:接入sidecare、设置默认的serviceaccount、设置quota等等。

webhook如何工作的

  • 注册webhook server
  • 资源操作请求通过API Server Auth验证
  • 根据注册信息回调对应的webhook server

webhook注册信息说明

apiVersion: admissionregistration.k8s.io/v1beta1kind: ValidatingWebhookConfigurationmetadata:  name: configwebhooks:- name: lb-webhook.default.svc ①  rules: ②  - apiGroups:    - "*"    apiVersions:    - "*"    operations:    - CREATE    resources:    - deployments   clientConfig:    service:      namespace: default ③      name: lb-webhook ④      path: /deployments/mutate ⑤    ⑥caBundle: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUNwakNDQVk0Q0NRQ0pXMWhxTnBXVVpEQU5CZ2txaGtpRzl3MEJBUXNGQURBVU1SSXdFQVlEVlFRRERBbFgKYVhObE1tTWdRMEV3SUJjTk1Ua3dOekUxTURZMU5EVXhXaGdQTWpFeE9UQTJNakV3TmpVME5URmFNQlF4RWpBUQpCZ05WQkFNTUNWZHBjMlV5WXlCRFFUQ0NBU0l3RFFZSktvWklodmNOQVFFQkJRQURnZ0VQQURDQ0FRb0NnZ0VCCkFPalpHM2tZcGZCMjFkNmREY2RRcU1IQ0NPRTU5NDlTeU5GTEhWWWpkYk52djRHQnlFOUJHVitCVzFxOS9rRHMKYmFFUTVZWnB3Q3NwS2lpL04zbEZEdTZRN2RqM0d1YnhOQlc5YWRmN0JrcFQreVhJRUVuRU9jTnViekhwbm9PNwpYdlVxMnpKQlYzd1FPbTgxMnBZeDlzckprYnAvclF1MjVqTktPZC9tZDEyMG1SRXN5Q3VLOTBIaXFqTjlLYlJICjVXaEh0R09SUjB1QWlTMGJoc0g0aElYRHhqMWhqenZEK21BZ2FPa0xRYjBBNDFZNGo2bE5GNUp6Q2h6eUJZYmYKbTFFVzRFdGdjZElvZ0hDRWVYekdXMEVHMWYvMVJuNDhGOXJMeFJ6ZTQyODFNbTJWZUh4ZGhBdXZSbGxPL3lHYgpnbEphSjlQTDFwdldNR2JMWC9jSmE4TUNBd0VBQVRBTkJna3Foa2lHOXcwQkFRc0ZBQU9DQVFFQUtONUtLVndEClZ1VzBTQmVtbE9ENnRDUFowWmVlMEtITUliMUh4T3kwblM3akYxRm84Ukw0K25lMDB4ZEhmelNZcm5JSWZpSy8KL3UraVNpTk80RjhNQlo5RzI1OFhzcS9zVkpJWkhCRE9ad3M2emZkMklvdUdubFF5QU5tdEpHeTFTNDNOaFcvbwp0aTJQTlVzQ2hMdy9hQ21COEVlU3BRb0FZeHhVTW1SK3pHUEY2VGszQ3Z6T1c2ekczT1g3bWhzMVdtU1orTS9tCjJRbElKRWpQQVYzWDJMOUN5UTRucW5MSks5dDltcWpaOFJyRmcwNFR0QnhEYUdYazAvQ2JlSURibUxPRitOdzMKOWhUNm9QNlhoaGNHb0h2dG91MndUOU5GTjdxY1R5cC9GaDFRaktxdTJLOWNyUWs3Wk52QUdGVWdISnBhb0UxRAp2NzV3cGp3bG1TSU5Bdz09Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K

① webhook名称

② 描述api-server操作什么资源什么动作时调用webhook插件

③ webhook service所在的namespace

④ webhook service name

⑤ 调用webhook api的地址

⑥ 提供和webhook通信的TLS链接信息, 生成的证书必须支持<svc_name>.<svc_namespace>.svc

准备

  • 准备一个Kubernetes集群必须为v1.9或以上的版本(本人基于v1.13.6的版本测试的)。
  • api server需要开启MutatingAdmissionWebhook ValidatingAdmissionWebhook,通过以下命令可查看。
kubectl api-versions | grep admissionregistration> admissionregistration.k8s.io/v1beta1

证书制作

手动制作证书

  • 生成密钥位数为 2048 的 ca.key
openssl genrsa -out ca.key 2048
  • 依据 ca.key 生成 ca.crt (使用 -days 参数来设置证书有效时间):
penssl req -x509 -new -nodes -key ca.key -subj "/CN=lb-webhook.default.svc" -days 10000 -out ca.crt
  • 生成密钥位数为 2048 的 server.key
openssl genrsa -out server.key 2048
  • 创建用于生成证书签名请求(CSR)的配置文件。确保在将其保存至文件(如csr.conf)。
[ req ]default_bits = 2048prompt = nodefault_md = sha256req_extensions = req_extdistinguished_name = dn
[ dn ]C = CNST = SiChuanL = SZO = Wise2cOU = Wise2cCN = lb-webhook.default.svc
[ req_ext ]subjectAltName = @alt_names
[ alt_names ]DNS.1 = lb-webhook.default.svc
[ v3_ext ]authorityKeyIdentifier=keyid,issuer:alwaysbasicConstraints=CA:FALSEkeyUsage=keyEncipherment,dataEnciphermentextendedKeyUsage=serverAuth,clientAuthsubjectAltName=@alt_names
  • 基于配置文件生成证书签名请求:
openssl req -new -key server.key -out server.csr -config csr.conf
  • 使用 ca.key、ca.crt 和 server.csr 生成服务器证书:
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key \-CAcreateserial -out server.crt -days 10000 \-extensions v3_ext -extfile csr.conf
  • 查看证书
openssl x509  -noout -text -in ./server.crt

自动生成证书可用脚本文件

执行make URL=lb-webhook.default.svc 相关说明请参考该项目readme.md。

部署

通过上面的操作,已经生成好了部署前的准备工作(证书)。接下来我们需要使用证书。

部署文件定义

  • admissionregistration.yaml,文件中的caBundle使用的是上面生成ca.crt文件内容的base64值。
apiVersion: admissionregistration.k8s.io/v1beta1kind: MutatingWebhookConfigurationmetadata:  name: configwebhooks:- name: lb-webhook.default.svc  rules:  - apiGroups:    - "*"    apiVersions:    - "*"    operations:    - CREATE    resources:    - deployments  clientConfig:    service:      namespace: default      name: lb-webhook      path: /deployments/mutate    caBundle: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUNwakNDQVk0Q0NRQ0pXMWhxTnBXVVpEQU5CZ2txaGtpRzl3MEJBUXNGQURBVU1SSXd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lhoaGNHb0h2dG91MndUOU5GTjdxY1R5cC9GaDFRaktxdTJLOWNyUWs3Wk52QUdGVWdISnBhb0UxRAp2NzV3cGp3bG1TSU5Bdz09Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K
  • secret.yaml, 文件中的data内容分别对应生成证书的三个文件内容的base64值。
apiVersion: v1data:  ca.crt: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUNwakNDQVk0Q0NRQ0pXMWhxTnBXVVpEQU5CZ2txaGtpRzl3MEJBUXNGQURBVU1SSXdFQVlEVlFRRERBbFgKYVhObE1tTWdRMEV3SUJjTk1Ua3dOekUxTURZMU5EVXhXaGdQTWpFeE9UQTJNakV3TmpVME5URmFNQlF4RWpBUQpCZ05WQkFNTUNWZHBjMlV5WXlCRFFUQ0NBU0l3RFFZSktvWklodmNOQVFFQkJRQURnZ0VQQURDQ0FRb0NnZ0VCCkFPalpHM2tZcGZCMjFkNmREY2RRcU1IQ0NPRTU5NDlTeU5GTEhWWWpkYk52djRHQnlFOUJHVitCVzFxOS9rRHMKYmFFUTVZWnB3Q3NwS2lpL04zbEZEdTZRN2RqM0d1YnhOQlc5YWRmN0JrcFQreVhJRUVuRU9jTnViekhwbm9PNwpYdlVxMnpKQlYzd1FPbTgxMnBZeDlzckprYnAvclF1MjVqTktPZC9tZDEyMG1SRXN5Q3VLOTBIaXFqTjlLYlJICjVXaEh0R09SUjB1QWlTMGJoc0g0aElYRHhqMWhqenZEK21BZ2FPa0xRYjBBNDFZNGo2bE5GNUp6Q2h6eUJZYmYKbTFFVzRFdGdjZElvZ0hDRWVYekdXMEVHMWYvMVJuNDhGOXJMeFJ6ZTQyODFNbTJWZUh4ZGhBdXZSbGxPL3lHYgpnbEphSjlQTDFwdldNR2JMWC9jSmE4TUNBd0VBQVRBTkJna3Foa2lHOXcwQkFRc0ZBQU9DQVFFQUtONUtLVndEClZ1VzBTQmVtbE9ENnRDUFowWmVlMEtITUliMUh4T3kwblM3ak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kovTk9uaVFjUG5GV29KVmZjMFVkTk9nT1Z0akw2YnU1SwpRRlVOUmFzdHNkaDdLSHQyWHNYaWlnNzNyRktjQ3JCS1BITlpKUWRrNjh5VENKNmRUZkxzeXc9PQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo=kind: Secretmetadata:  name: lb-webhook-tls  namespace: defaulttype: Opaque
  • deployment.yaml

webhook server部署文件除了部署了server,还定义了server端相关的rbac模型。

apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata:  name: webhookrules:- apiGroups: ["*"]  resources: ["deployments", "resourcequotas"]  verbs: ["*"]---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata:  name: webhook  namespace: defaultsubjects:- kind: ServiceAccount  name: webhook  namespace: defaultroleRef:  kind: ClusterRole  name: webhook  apiGroup: rbac.authorization.k8s.io---apiVersion: v1kind: ServiceAccountmetadata:  name: webhook  namespace: default---apiVersion: extensions/v1beta1kind: Deploymentmetadata:  labels:    com.wise2c.service: lb-webhook  name: lb-webhook  namespace: defaultspec:  replicas: 1  selector:    matchLabels:      com.wise2c.service: lb-webhook  template:    metadata:      labels:        com.wise2c.service: lb-webhook    spec:      containers:      - image: registry.cn-hangzhou.aliyuncs.com/mojo/lb-webhook:master        imagePullPolicy: IfNotPresent        name: lb-webhook        args:        - "--memory=100Mi"        - "--cpu=200m"        - "--tls-cert-file=/etc/certs/server.crt"        - "--tls-private-key-file=/etc/certs/server.key"        volumeMounts:        - mountPath: /etc/certs          name: config      serviceAccount: webhook      volumes:      - name: config        secret:          secretName: lb-webhook-tls---apiVersion: v1kind: Servicemetadata:  labels:    com.wise2c.service: lb-webhook  name: lb-webhook  namespace: defaultspec:  ports:  - name: https    port: 443    protocol: TCP    targetPort: 443  selector:    com.wise2c.service: lb-webhook

测试文件定义

在指定的namespace中创建resourcequota。通过两个test文件,一个包含webhook server指定的标签文件test-success.yaml, 另一个不带有指定标签文件test-fail.yaml, apply到对应的namespace中。期望看到test-success.yaml下发以后pod成功启动,test-fail.yaml未能看到相应pod启动。并且edit test-success.yaml的deployment对象发现该对象自动加上了对应的resources。

  • demo-namespace.yaml
apiVersion: v1kind: Namespacemetadata:  name: webhook-demo
  • quota.yaml
apiVersion: v1kind: ResourceQuotametadata:  name: compute-resources  namespace: webhook-demospec:  hard:    limits.memory: 2Gi
  • test-success.yaml
apiVersion: extensions/v1beta1kind: Deploymentmetadata:  labels:    run: web-success    io.wise2c.service.type: lb # 上文提到的特定标签  name: web-success  namespace: webhook-demospec:  selector:    matchLabels:      run: web-success  template:    metadata:      labels:        run: web-success    spec:      containers:        - image: nginx          imagePullPolicy: Always          name: web          ports:            - containerPort: 80              protocol: TCP
  • test-fail.yaml
apiVersion: extensions/v1beta1kind: Deploymentmetadata:  labels:    run: web  name: web-fail  namespace: webhook-demospec:  selector:    matchLabels:      run: web-fail  template:    metadata:      labels:        run: web-fail    spec:      containers:        - image: nginx          imagePullPolicy: Always          name: web          ports:            - containerPort: 80              protocol: TCP

测试

kubectl apply -f admissionregistration.yamlkubectl apply -f secret.yamlkubectl apply -f deployment.yaml
kubectl apply -f demo-namespace.yamlkubectl apply -f quota.yamlkubectl apply -f test-fail.yamlkubectl apply -f test-success.yaml 

结果如下:

[root@dev-7 webhook]# kubectl apply -f test-fail.yamldeployment.extensions/web-fail created[root@dev-7 webhook]# kubectl apply -f test-success.yamldeployment.extensions/web-success created[root@dev-7 webhook]# kubectl get po -n webhook-demoNAME                           READY     STATUS              RESTARTS   AGEweb-success-85fd64db95-wl9xx   0/1       ContainerCreating   0          9s

上述结果和期望的一致, webhook到此结束。

本文分享自微信公众号 - CNCF(lf_cncf)

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2019-07-18

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Node.js究竟是什么?Node.js工作原理解析

    Node.js 是一个 JavaScript 运行时环境。听起来还不错,不过这究竟意味着什么?它又是如何运作的?

    德顺
  • Java基础-脚本引擎

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 ...

    cwl_java
  • 从零开始开发一个 React - 实现Context API

    Context 提供了一个无需为每层组件手动添加 props,就能在组件树间进行数据传递的方法。

    lucifer210
  • CentOS7和Ubuntu18.10下运行Qt Creator出现cannot find -lGL的问题的解决方案

    原文链接:https://blog.csdn.net/songrotek/article/details/48023461

    ccf19881030
  • 前端工程化(一)NPM如何管理依赖包版本?

    Nodejs成功离不开 npm 优秀的依赖管理系统。在介绍整个依赖系统之前,必须要了解 npm如何管理依赖包的版本,本文将介绍 npm包 的版本发布规范以、何管...

    ConardLi
  • REST API URI 设计的七准则

    在了解 REST API URI 设计的规则之前,让我们快速过一下我们将要讨论的一些术语。

    用户5807183
  • 怎么用 Python 来朗读网页 ?

    之所以用 Python,就是因为 Python 有着丰富的库,网页正文识别也不在话下。这里我尝试了 readability、goose3。

    小小詹同学
  • 超越Cookie,当今的客户端数据存储技术有哪些

    当 cookie 被首次引入时,它是浏览器保存数据的唯一方式。之后又有了很多新的选择:Web Storage API、IndexedDB 和 Cache API...

    德顺
  • 还在用JDK8?我都开始上手JDK 13了!

    目标:提高应用程序类 - 数据共享(AppCDS)的可用性。消除了用户进行试运行以创建每个应用程序的类列表的需要。

    黄泽杰
  • 官方解读:TensorFlow 2.0中即将到来的所有新特性

    本文经机器之心(微信公众号:almosthuman2014)授权转载,禁止二次转载

    小小詹同学

扫码关注云+社区

领取腾讯云代金券