{"id":323,"date":"2024-10-11T12:34:00","date_gmt":"2024-10-11T04:34:00","guid":{"rendered":"http:\/\/zhaoyanqi.cn\/?p=323"},"modified":"2025-01-16T13:31:18","modified_gmt":"2025-01-16T05:31:18","slug":"%e7%94%a8webhook-%e8%87%aa%e5%8a%a8%e7%bb%99%e6%b3%a8%e5%85%a5sidecar%e6%94%b6%e9%9b%86%e6%97%a5%e5%bf%97","status":"publish","type":"post","link":"https:\/\/zhaoyanqi.cn\/?p=323","title":{"rendered":"\u7528Webhook \u81ea\u52a8\u7ed9\u6ce8\u5165sidecar\u6536\u96c6\u65e5\u5fd7"},"content":{"rendered":"\n<p id=\"u7755a410\">\u9700\u6c42\uff1a\u4f7f\u7528 Python \u7f16\u5199\u4e00\u4e2a Mutating Admission Webhook\uff0c\u4ee5\u4fbf\u5728\u521b\u5efa\u65b0\u7684 Pod \u65f6\u81ea\u52a8\u6ce8\u5165 Sidecar \u5bb9\u5668\uff08\u4f8b\u5982\u7528\u4e8e\u65e5\u5fd7\u6536\u96c6\u7684 Fluentd\uff09<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"ySRWU\"><strong>\u80cc\u666f\u77e5\u8bc6<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Admission Webhook<\/strong>\uff1aKubernetes \u4e2d\u7684\u4e00\u79cd\u673a\u5236\uff0c\u5141\u8bb8\u4f60\u5728\u5bf9\u8c61\u88ab\u521b\u5efa\u6216\u66f4\u65b0\u65f6\u5bf9\u5176\u8fdb\u884c\u62e6\u622a\u548c\u4fee\u6539\u3002<\/li>\n\n\n\n<li><strong>Mutating Webhook<\/strong>\uff1a\u4e00\u79cd Admission Webhook\uff0c\u53ef\u4ee5\u4fee\u6539\uff08\u53d8\u5f02\uff09\u4f20\u5165\u7684 Kubernetes \u5bf9\u8c61\uff08\u5982 Pod\uff09\u3002<\/li>\n\n\n\n<li><strong>Sidecar \u5bb9\u5668<\/strong>\uff1a\u4e0e\u4e3b\u5bb9\u5668\u4e00\u8d77\u8fd0\u884c\u7684\u8f85\u52a9\u5bb9\u5668\uff0c\u901a\u5e38\u7528\u4e8e\u65e5\u5fd7\u6536\u96c6\u3001\u76d1\u63a7\u7b49\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"e50Z3\"><strong>\u603b\u4f53\u6b65\u9aa4<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7f16\u5199\u4e00\u4e2a Python Web \u670d\u52a1\u5668<\/strong>\uff0c\u5904\u7406 AdmissionReview \u8bf7\u6c42\u5e76\u6ce8\u5165 Sidecar\u3002<\/li>\n\n\n\n<li><strong>\u90e8\u7f72 Webhook \u670d\u52a1<\/strong>\u5230 Kubernetes \u96c6\u7fa4\u4e2d\uff0c\u5e76\u786e\u4fdd\u5b83\u53ef\u4ee5\u88ab API Server \u8bbf\u95ee\u3002<\/li>\n\n\n\n<li><strong>\u914d\u7f6e MutatingWebhookConfiguration<\/strong>\uff0c\u4f7f\u5f97 Kubernetes \u5728\u521b\u5efa Pod \u65f6\u8c03\u7528\u4f60\u7684 Webhook\u3002<\/li>\n\n\n\n<li><strong>\u9a8c\u8bc1<\/strong>\u4f60\u7684 Webhook \u662f\u5426\u6309\u9884\u671f\u5de5\u4f5c\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" id=\"dr0o3\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"iMR8s\"><strong>\u7f16\u5199 Mutating Webhook \u670d\u52a1\u5668<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"dTeEo\"><strong>1. \u521b\u5efa Webhook \u670d\u52a1\u5668<\/strong><\/h4>\n\n\n\n<p id=\"u17783253\">\u9996\u5148\uff0c\u4f7f\u7528 Python \u7684 <code>flask<\/code> \u6216 <code>aiohttp<\/code> \u7b49\u6846\u67b6\u521b\u5efa\u4e00\u4e2a HTTPS \u670d\u52a1\u5668\u3002\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528 <code>flask<\/code> \u4f5c\u4e3a\u793a\u4f8b\u3002<\/p>\n\n\n\n<p id=\"ue752b1c9\"><strong>\u5b89\u88c5\u4f9d\u8d56\uff1a<\/strong><\/p>\n\n\n\n<pre id=\"hTPRB\" class=\"wp-block-code\"><code>pip install flask flask-jsonschema-validator cryptography<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"SHJIL\"><strong>2. \u5904\u7406 AdmissionReview \u8bf7\u6c42<\/strong><\/h4>\n\n\n\n<p id=\"u9cf5eb98\">\u7f16\u5199\u670d\u52a1\u5668\u4ee3\u7801\uff0c\u5904\u7406 Kubernetes \u53d1\u9001\u7684 AdmissionReview \u8bf7\u6c42\uff0c\u5e76\u6839\u636e\u9700\u8981\u4fee\u6539 Pod \u914d\u7f6e\u3002<\/p>\n\n\n\n<pre id=\"r2HNz\" class=\"wp-block-code\"><code>from flask import Flask, request, jsonify\nimport json\nimport base64\nimport ssl\n \napp = Flask(__name__)\n \n@app.route('\/mutate', methods=&#91;'POST'])\ndef mutate():\n    admission_review = request.get_json()\n    request_uid = admission_review&#91;'request']&#91;'uid']\n    pod = admission_review&#91;'request']&#91;'object']\n \n    # \u68c0\u67e5\u662f\u5426\u9700\u8981\u8df3\u8fc7\u6ce8\u5165\n    if 'sidecar-injected' in pod&#91;'metadata'].get('annotations', {}):\n        return admission_response(request_uid, allowed=True)\n \n    # \u6784\u5efa Patch \u64cd\u4f5c\n    patch = get_pod_patch()\n \n    admission_response_data = {\n        'uid': request_uid,\n        'allowed': True,\n        'patch': base64.b64encode(json.dumps(patch).encode()).decode(),\n        'patchType': 'JSONPatch'\n    }\n \n    response = {\n        'apiVersion': 'admission.k8s.io\/v1',\n        'kind': 'AdmissionReview',\n        'response': admission_response_data\n    }\n \n    return jsonify(response)\n \ndef admission_response(uid, allowed, patch=None, patch_type=None):\n    response = {\n        'uid': uid,\n        'allowed': allowed\n    }\n    if patch and patch_type:\n        response&#91;'patch'] = base64.b64encode(json.dumps(patch).encode()).decode()\n        response&#91;'patchType'] = patch_type\n    return jsonify({\n        'apiVersion': 'admission.k8s.io\/v1',\n        'kind': 'AdmissionReview',\n        'response': response\n    })\n \ndef get_pod_patch():\n    # \u5b9a\u4e49\u8981\u6ce8\u5165\u7684 Sidecar \u5bb9\u5668\n    sidecar_container = {\n        'name': 'fluentd-sidecar',\n        'image': 'registry.cn-shanghai.aliyuncs.com\/oversea_speedup\/fluentd:latest',\n        'resources': {},\n        'volumeMounts': &#91;\n            {\n                'name': 'varlog',\n                'mountPath': '\/var\/log'\n            }\n        ]\n    }\n \n    # \u5b9a\u4e49\u8981\u6dfb\u52a0\u7684 Volume\n    volume = {\n        'name': 'varlog',\n        'emptyDir': {}\n    }\n \n    # JSON Patch \u64cd\u4f5c\n    patch = &#91;\n        {'op': 'add', 'path': '\/spec\/containers\/-', 'value': sidecar_container},\n        {'op': 'add', 'path': '\/spec\/volumes\/-', 'value': volume},\n        {'op': 'add', 'path': '\/metadata\/annotations', 'value': {}},\n        {'op': 'add', 'path': '\/metadata\/annotations\/sidecar-injected', 'value': 'true'}\n    ]\n    return patch\n \nif __name__ == '__main__':\n    context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)\n    context.load_cert_chain('\/tls\/tls.crt', '\/tls\/tls.key')\n    app.run(host='0.0.0.0', port=443, ssl_context=context)<\/code><\/pre>\n\n\n\n<p id=\"uff4dbc4b\"><strong>\u89e3\u91ca\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>mutate()<\/strong>\uff1a\u63a5\u6536 AdmissionReview \u8bf7\u6c42\uff0c\u89e3\u6790 Pod \u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><strong>get_pod_patch()<\/strong>\uff1a\u5b9a\u4e49\u5982\u4f55\u4fee\u6539 Pod\uff0c\u5305\u62ec\u6dfb\u52a0 Sidecar \u5bb9\u5668\u3001\u5377\u548c\u6ce8\u89e3\u3002<\/li>\n\n\n\n<li><strong>admission_response()<\/strong>\uff1a\u6784\u5efa AdmissionReview \u54cd\u5e94\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"Q9Er5\"><strong>3. \u6ce8\u5165 Sidecar \u5bb9\u5668<\/strong><\/h4>\n\n\n\n<p id=\"uae94a960\">\u5728 <code>get_pod_patch()<\/code> \u51fd\u6570\u4e2d\uff0c\u5b9a\u4e49\u4e86\u8981\u6ce8\u5165\u7684 Sidecar \u5bb9\u5668\u548c\u9700\u8981\u6dfb\u52a0\u7684\u5377\u3002\u4f7f\u7528 JSON Patch \u683c\u5f0f\u6307\u5b9a\u5bf9 Pod \u7684\u4fee\u6539\u3002<\/p>\n\n\n\n<p id=\"u794708c1\"><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Volume \u548c VolumeMounts<\/strong>\uff1a\u786e\u4fdd Sidecar \u5bb9\u5668\u548c\u4e3b\u5bb9\u5668\u5171\u4eab\u65e5\u5fd7\u76ee\u5f55\u3002<\/li>\n\n\n\n<li><strong>Annotations<\/strong>\uff1a\u4f7f\u7528\u6ce8\u89e3\u6807\u8bb0\u5df2\u6ce8\u5165\u7684 Pod\uff0c\u9632\u6b62\u91cd\u590d\u6ce8\u5165\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"YGPWH\"><strong>4. \u6784\u5efa\u548c\u8fd0\u884c Docker \u955c\u50cf<\/strong><\/h4>\n\n\n\n<p id=\"u48bcc192\"><strong>Dockerfile\uff1a<\/strong><\/p>\n\n\n\n<pre id=\"l4Hti\" class=\"wp-block-code\"><code>FROM registry.cn-shanghai.aliyuncs.com\/joezhao\/python:3.9-slim\n \nRUN pip install flask flask-jsonschema-validator cryptography\n \nCOPY server.py \/server.py\n \nCMD &#91;\"python\", \"\/server.py\"]<\/code><\/pre>\n\n\n\n<p id=\"u0510063a\"><strong>\u6784\u5efa\u955c\u50cf\uff1a<\/strong><\/p>\n\n\n\n<pre id=\"vCdib\" class=\"wp-block-code\"><code>docker build -t your-registry\/sidecar-injector:latest .<\/code><\/pre>\n\n\n\n<p id=\"u4d326fe5\"><strong>\u63a8\u9001\u955c\u50cf\uff1a<\/strong><\/p>\n\n\n\n<pre id=\"vYJZ6\" class=\"wp-block-code\"><code>docker push your-registry\/sidecar-injector:latest<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" id=\"SKT24\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"ZnfDS\"><strong>\u90e8\u7f72\u5230 Kubernetes \u96c6\u7fa4<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"g1Kcg\"><strong>\u8bc1\u4e66\u751f\u6210\u7684\u5b8c\u6574\u6b65\u9aa4<\/strong><\/h4>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"kZ0fP\"><strong>1. \u751f\u6210 CA\uff08\u81ea\u7b7e\u540d\u6839\u8bc1\u4e66\uff09<\/strong><\/h5>\n\n\n\n<p id=\"u369bfe6c\">\u9996\u5148\uff0c\u751f\u6210 CA \u7684\u79c1\u94a5\u548c\u8bc1\u4e66\uff0c\u8fd9\u4e2a\u8bc1\u4e66\u7528\u4e8e\u7b7e\u7f72 Webhook \u670d\u52a1\u7684\u8bc1\u4e66\uff1a<\/p>\n\n\n\n<pre id=\"KyoIK\" class=\"wp-block-code\"><code># \u751f\u6210 CA \u79c1\u94a5\nopenssl genrsa -out ca.key 2048\n\n# \u751f\u6210\u81ea\u7b7e\u540d CA \u8bc1\u4e66\nopenssl req -x509 -new -nodes -key ca.key -sha256 -days 365 -out ca.crt -subj \"\/CN=Webhook CA\"<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ca.key<\/code>\uff1aCA \u7684\u79c1\u94a5\u3002<\/li>\n\n\n\n<li><code>ca.crt<\/code>\uff1aCA \u7684\u81ea\u7b7e\u540d\u8bc1\u4e66\uff0c\u540e\u7eed\u4f1a\u7528\u6765\u9a8c\u8bc1\u670d\u52a1\u7aef\u8bc1\u4e66\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" id=\"s5Pwj\"\/>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"tKpAD\"><strong>2. \u751f\u6210 Webhook \u670d\u52a1\u7684\u79c1\u94a5\u548c CSR<\/strong><\/h5>\n\n\n\n<p id=\"u83fac372\">Webhook \u670d\u52a1\u7684\u8bc1\u4e66\u5fc5\u987b\u5305\u542b <strong>SAN<\/strong> \u5b57\u6bb5\uff0c\u5339\u914d Kubernetes \u670d\u52a1\u57df\u540d <code>sidecar-injector.default.svc<\/code>\u3002<\/p>\n\n\n\n<p id=\"ud41098d2\">\u521b\u5efa\u4e00\u4e2a\u914d\u7f6e\u6587\u4ef6 <code>csr.conf<\/code>\uff1a<\/p>\n\n\n\n<pre id=\"aHPYE\" class=\"wp-block-code\"><code>conf\n\n\n\u590d\u5236\u4ee3\u7801\n&#91; req ]\ndefault_bits = 2048\nprompt = no\ndefault_md = sha256\nreq_extensions = req_ext\ndistinguished_name = dn\n\n&#91; dn ]\nCN = sidecar-injector.default.svc\n\n&#91; req_ext ]\nsubjectAltName = @alt_names\n\n&#91; alt_names ]\nDNS.1 = sidecar-injector\nDNS.2 = sidecar-injector.default\nDNS.3 = sidecar-injector.default.svc<\/code><\/pre>\n\n\n\n<p id=\"u3dcf099e\"><strong>\u751f\u6210 Webhook \u670d\u52a1\u7684\u79c1\u94a5\u548c CSR<\/strong>\uff1a<\/p>\n\n\n\n<pre id=\"MTn5w\" class=\"wp-block-code\"><code># \u751f\u6210\u79c1\u94a5\u548c CSR\nopenssl req -new -nodes -keyout tls.key -out tls.csr -config csr.conf<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tls.key<\/code>\uff1aWebhook \u670d\u52a1\u7684\u79c1\u94a5\u3002<\/li>\n\n\n\n<li><code>tls.csr<\/code>\uff1a\u8bc1\u4e66\u7b7e\u540d\u8bf7\u6c42\u6587\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" id=\"XpWyB\"\/>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"BJpkE\"><strong>3. \u4f7f\u7528 CA \u7b7e\u53d1\u670d\u52a1\u7aef\u8bc1\u4e66<\/strong><\/h5>\n\n\n\n<p id=\"u87469b0d\">\u4f7f\u7528 <code>ca.key<\/code> \u548c <code>ca.crt<\/code> \u7b7e\u53d1 Webhook \u670d\u52a1\u7684\u8bc1\u4e66\uff1a<\/p>\n\n\n\n<pre id=\"OUR0w\" class=\"wp-block-code\"><code>openssl x509 -req -in tls.csr -CA ca.crt -CAkey ca.key -CAcreateserial \\\n  -out tls.crt -days 365 -extensions req_ext -extfile csr.conf<\/code><\/pre>\n\n\n\n<p id=\"u84fb374e\">\u8f93\u51fa\u6587\u4ef6\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tls.crt<\/code>\uff1a\u670d\u52a1\u7aef\u7684\u8bc1\u4e66\u3002<\/li>\n\n\n\n<li><code>ca.crt<\/code>\uff1aCA \u6839\u8bc1\u4e66\uff0c\u7528\u4e8e\u9a8c\u8bc1 <code>tls.crt<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" id=\"eFYyY\"\/>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"QstH6\"><strong>4. \u5c06\u8bc1\u4e66\u6dfb\u52a0\u5230 Kubernetes<\/strong><\/h5>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u521b\u5efa Kubernetes Secret<\/strong>\uff0c\u5305\u542b <code>tls.crt<\/code> \u548c <code>tls.key<\/code>\uff1a<\/li>\n<\/ol>\n\n\n\n<pre id=\"wXTNN\" class=\"wp-block-code\"><code>kubectl create secret tls sidecar-injector-certs \\\n  --cert=tls.crt --key=tls.key -n default<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u83b7\u53d6 <\/strong><code><strong>ca.crt<\/strong><\/code><strong> \u7684 Base64 \u7f16\u7801<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<pre id=\"gNSkB\" class=\"wp-block-code\"><code>cat ca.crt | base64 -w 0<\/code><\/pre>\n\n\n\n<p id=\"u78084b61\">\u5c06\u7f16\u7801\u540e\u7684\u5185\u5bb9\u7528\u4e8e <code>MutatingWebhookConfiguration<\/code> \u4e2d\u7684 <code>caBundle<\/code> \u5b57\u6bb5\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" id=\"QPSTw\"\/>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"ClnGZ\"><strong>5. \u603b\u7ed3\u8bc1\u4e66\u7684\u5173\u7cfb<\/strong><\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>CA \u8bc1\u4e66 (<\/strong><code><strong>ca.crt<\/strong><\/code><strong>)<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7528\u4e8e\u9a8c\u8bc1\u670d\u52a1\u7aef\u8bc1\u4e66 (<code>tls.crt<\/code>)\u3002<\/li>\n\n\n\n<li>\u5176 Base64 \u7f16\u7801\u5185\u5bb9\u5199\u5165 <code>MutatingWebhookConfiguration<\/code> \u7684 <code>caBundle<\/code>\u3002<\/li>\n\n\n\n<li><strong>Webhook \u670d\u52a1\u8bc1\u4e66 (<\/strong><code><strong>tls.crt<\/strong><\/code><strong> \u548c <\/strong><code><strong>tls.key<\/strong><\/code><strong>)<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7528\u4e8e Webhook \u670d\u52a1\u5668\u7684 HTTPS \u901a\u4fe1\u3002<\/li>\n\n\n\n<li>\u5fc5\u987b\u5305\u542b SAN\uff0c\u786e\u4fdd\u4e0e <code>sidecar-injector.default.svc<\/code> \u5339\u914d\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"qk3GA\"><strong>2. \u521b\u5efa Kubernetes \u670d\u52a1\u548c\u90e8\u7f72<\/strong><\/h4>\n\n\n\n<p id=\"uf7dc6d47\"><strong>\u90e8\u7f72\u6587\u4ef6\uff1a<\/strong><\/p>\n\n\n\n<pre id=\"y4qLk\" class=\"wp-block-code\"><code>apiVersion: apps\/v1\nkind: Deployment\nmetadata:\n  name: sidecar-injector\n  labels:\n    app: sidecar-injector\nspec:\n  replicas: 1\n  selector:\n    matchLabels:\n      app: sidecar-injector\n  template:\n    metadata:\n      labels:\n        app: sidecar-injector\n    spec:\n      containers:\n      - name: sidecar-injector\n        #\u4e0b\u9762\u6362\u6210\u521a\u624d\u81ea\u5df1push\u7684\u955c\u50cf\n        image: registry.cn-shanghai.aliyuncs.com\/joezhao\/sidecar-fluentd:latest\n        ports:\n        - containerPort: 443\n        volumeMounts:\n        - name: webhook-certs\n          mountPath: \"\/tls\"\n          readOnly: true\n      volumes:\n      - name: webhook-certs\n        secret:\n          secretName: sidecar-injector-certs\n \n---\n\napiVersion: v1\nkind: Service\nmetadata:\n  name: sidecar-injector\n  labels:\n    app: sidecar-injector\nspec:\n  ports:\n  - port: 443\n    targetPort: 443\n  selector:\n    app: sidecar-injector<\/code><\/pre>\n\n\n\n<p id=\"ufd2f785e\"><strong>\u5e94\u7528\u90e8\u7f72\uff1a<\/strong><\/p>\n\n\n\n<pre id=\"TpMzZ\" class=\"wp-block-code\"><code>kubectl apply -f deployment.yaml<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"dscDh\"><strong>3. \u521b\u5efa MutatingWebhookConfiguration<\/strong><\/h4>\n\n\n\n<p id=\"uf3513431\"><strong>\u83b7\u53d6 CA Bundle\uff1a<\/strong><\/p>\n\n\n\n<pre id=\"Fomng\" class=\"wp-block-code\"><code># \u5982\u679c\u4f7f\u7528\u81ea\u7b7e\u540d\u8bc1\u4e66\nCA_BUNDLE=$(cat tls.crt | base64 | tr -d '\\n')\n# \u67e5\u51fa\u6765\u76f4\u63a5\u8d34\u8fdb\u53bb\ncat ca.crt | base64 -w 0<\/code><\/pre>\n\n\n\n<p id=\"u8deaf721\"><strong>Webhook \u914d\u7f6e\u6587\u4ef6\uff1a<\/strong><\/p>\n\n\n\n<pre id=\"f3p1Y\" class=\"wp-block-code\"><code>apiVersion: admissionregistration.k8s.io\/v1\nkind: MutatingWebhookConfiguration\nmetadata:\n  name: sidecar-injector-webhook\nwebhooks:\n  - name: sidecar-injector.k8s.io\n    clientConfig:\n      service:\n        name: sidecar-injector\n        namespace: default\n        path: \"\/mutate\"\n      # \u4e0b\u9762\u522b\u7528\u53d8\u91cf\uff0c\u76f4\u63a5\u8d34\u521a\u521a\u67e5\u51fa\u6765\u7684\u8bc1\u4e66\u7f16\u7801base64\u89e3\u7801 \n      caBundle: \"${CA_BUNDLE}\"\n    rules:\n      - operations: &#91;\"CREATE\"]\n        apiGroups: &#91;\"\"]\n        apiVersions: &#91;\"v1\"]\n        resources: &#91;\"pods\"]\n    admissionReviewVersions: &#91;\"v1\", \"v1beta1\"]\n    sideEffects: None\n    timeoutSeconds: 5<\/code><\/pre>\n\n\n\n<p id=\"u44b7e61a\"><strong>\u5e94\u7528\u914d\u7f6e\uff1a<\/strong><\/p>\n\n\n\n<pre id=\"HSewJ\" class=\"wp-block-code\"><code>kubectl apply -f webhook-configuration.yaml<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" id=\"MBjPz\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"Ugjdo\"><strong>\u9a8c\u8bc1 Webhook \u529f\u80fd<\/strong><\/h3>\n\n\n\n<p id=\"u86f6a10b\">\u521b\u5efa\u4e00\u4e2a\u6d4b\u8bd5 Pod\uff1a<\/p>\n\n\n\n<pre id=\"r6pAt\" class=\"wp-block-code\"><code>apiVersion: v1\nkind: Pod\nmetadata:\n  name: test-pod\n  labels:\n    app: test\nspec:\n  containers:\n  - name: nginx\n    image: nginx<\/code><\/pre>\n\n\n\n<p id=\"uda9db740\"><strong>\u521b\u5efa Pod\uff1a<\/strong><\/p>\n\n\n\n<pre id=\"u5uTH\" class=\"wp-block-code\"><code>kubectl apply -f test-pod.yaml<\/code><\/pre>\n\n\n\n<p id=\"u9f4a3478\"><strong>\u68c0\u67e5 Pod \u662f\u5426\u88ab\u6ce8\u5165 Sidecar\uff1a<\/strong><\/p>\n\n\n\n<pre id=\"pvZgp\" class=\"wp-block-code\"><code>kubectl describe pod test-pod<\/code><\/pre>\n\n\n\n<p id=\"ub05f3a0a\">\u5728\u8f93\u51fa\u4e2d\uff0c\u4f60\u5e94\u8be5\u770b\u5230 <code>fluentd-sidecar<\/code> \u5bb9\u5668\u5df2\u88ab\u6ce8\u5165\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" id=\"f9J4i\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"VXD5k\"><strong>\u53c2\u8003\u8d44\u6599<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/kubernetes.io\/zh\/docs\/reference\/access-authn-authz\/extensible-admission-controllers\/\" target=\"_blank\" rel=\"noreferrer noopener\">Kubernetes \u5b98\u65b9\u6587\u6863\uff1a\u4f7f\u7528 Admission Webhooks<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/flask.palletsprojects.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python Flask \u5b98\u65b9\u6587\u6863<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/morvencao\/kube-mutating-webhook-tutorial\" target=\"_blank\" rel=\"noreferrer noopener\">Admission Webhook \u793a\u4f8b<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42\uff1a\u4f7f\u7528 Python \u7f16\u5199\u4e00\u4e2a Mutating Admission Webhook\uff0c\u4ee5\u4fbf\u5728\u521b\u5efa\u65b0\u7684 Po [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-323","post","type-post","status-publish","format-standard","hentry","category-k8s"],"_links":{"self":[{"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts\/323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=323"}],"version-history":[{"count":2,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts\/323\/revisions"}],"predecessor-version":[{"id":328,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts\/323\/revisions\/328"}],"wp:attachment":[{"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}