{"id":76945,"date":"2024-03-07T14:19:21","date_gmt":"2024-03-07T06:19:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/?p=76945"},"modified":"2024-03-07T14:19:21","modified_gmt":"2024-03-07T06:19:21","slug":"kubernetes%e7%9a%84yaml%e6%96%87%e4%bb%b6%e6%80%8e%e4%b9%88%e5%86%99","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/76945.html","title":{"rendered":"Kubernetes\u7684YAML\u6587\u4ef6\u600e\u4e48\u5199"},"content":{"rendered":"<p>\u5728\u7f16\u5199Kubernetes\u7684YAML\u6587\u4ef6\u65f6\uff0c\u5173\u952e\u662f\u8981\u7406\u89e3\u5176\u4f5c\u7528\u3001\u7ed3\u6784\u548c\u8bed\u6cd5\u3002<strong>Kubernetes YAML\u6587\u4ef6\u4e3b\u8981\u7528\u4e8e\u63cf\u8ff0\u8d44\u6e90\u7684\u671f\u671b\u72b6\u6001<\/strong>\u3001<strong>\u5305\u542b\u5fc5\u8981\u7684\u5143\u6570\u636e<\/strong>\u3001<strong>\u4ee5\u53ca\u6307\u5b9a\u8d44\u6e90\u7684\u914d\u7f6e\u4fe1\u606f<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u9075\u5faa\u6b63\u786e\u7684\u7f29\u8fdb\u98ce\u683c<\/strong>\u662f\u7f16\u5199\u6709\u6548YAML\u6587\u4ef6\u7684\u5fc5\u8981\u6761\u4ef6\uff0c\u56e0\u4e3aYAML\u4e2d\u7684\u5c42\u6b21\u7ed3\u6784\u662f\u901a\u8fc7\u7f29\u8fdb\u8868\u793a\u7684\uff0c\u4e00\u4e2a\u5e38\u89c1\u7684\u9519\u8bef\u5c31\u662f\u5c42\u6b21\u5173\u7cfb\u548c\u7f29\u8fdb\u7684\u6df7\u4e71\u3002<\/p>\n<p><strong>\u4e00\u4e2a\u57fa\u672c\u7684YAML\u6587\u4ef6\u81f3\u5c11\u5e94\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u90e8\u5206<\/strong>: apiVersion\u3001kind\u3001metadata\u4ee5\u53caspec\u3002apiVersion\u5b9a\u4e49\u4e86\u4f7f\u7528\u7684Kubernetes API\u7248\u672c\uff0ckind\u6307\u660e\u8d44\u6e90\u7c7b\u578b\uff0cmetadata\u63d0\u4f9b\u8d44\u6e90\u7684\u547d\u540d\u548c\u5176\u4ed6\u8f85\u52a9\u4fe1\u606f\uff0c\u800cspec\u5219\u8be6\u7ec6\u9610\u8ff0\u4e86\u8d44\u6e90\u7684\u671f\u671b\u72b6\u6001\u3002<\/p>\n<p>### <strong>\u4e00\u3001YAML\u6587\u4ef6\u57fa\u7840\u7ed3\u6784\u4ecb\u7ecd<\/strong><\/p>\n<p>#### <strong>API\u7248\u672c\u548c\u8d44\u6e90\u7c7b\u578b<\/strong><\/p>\n<p>\u5728YAML\u6587\u4ef6\u7684\u5f00\u59cb\u90e8\u5206\uff0c\u60a8\u9700\u8981\u6307\u5b9a\u4f7f\u7528\u7684API\u7248\u672c\uff08\u4f8b\u5982`apiVersion: v1`\u6216`apiVersion: apps\/v1`\u7b49\uff09\uff0c\u4ee5\u53ca\u60a8\u60f3\u8981\u521b\u5efa\u7684\u8d44\u6e90\u7c7b\u578b\uff08kind\uff09\uff0c\u5982Deployment\u3001Service\u7b49\u3002<\/p>\n<p>#### <strong>\u5143\u6570\u636e<\/strong><\/p>\n<p>\u5143\u6570\u636e\u90e8\u5206\uff08metadata\uff09\u5305\u62ec\u8d44\u6e90\u7684\u540d\u79f0\u3001\u547d\u540d\u7a7a\u95f4\u548c\u6807\u7b7e\uff0c\u8fd9\u4e9b\u5bf9\u4e8e\u8d44\u6e90\u7684\u7ba1\u7406\u548c\u8bc6\u522b\u81f3\u5173\u91cd\u8981\u3002\u8fd9\u91cc\u7684\u540d\u79f0\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u6807\u7b7e\u5219\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u9009\u62e9\u548c\u7ba1\u7406\u64cd\u4f5c\u3002<\/p>\n<p>### <strong>\u4e8c\u3001\u914d\u7f6e\u8be6\u7ec6\u89c4\u683c\uff08Spec\uff09<\/strong><\/p>\n<p>#### <strong>\u7406\u89e3Spec\u7ed3\u6784<\/strong><\/p>\n<p>Spec\u662fYAML\u6587\u4ef6\u6700\u91cd\u8981\u7684\u90e8\u5206\uff0c\u4e0d\u540c\u79cd\u7c7b\u7684\u8d44\u6e90\u5c06\u62e5\u6709\u4e0d\u540c\u7684spec\u7ed3\u6784\u3002\u5bf9\u4e8e\u4e00\u4e2aPod\u6765\u8bf4\uff0c\u53ef\u80fd\u5305\u62ec\u5bb9\u5668\u7684\u540d\u79f0\u3001\u955c\u50cf\u3001\u7aef\u53e3\u7b49\u914d\u7f6e\u4fe1\u606f\u3002<\/p>\n<p>#### <strong>\u8bbe\u7f6e\u8d44\u6e90\u9700\u6c42\u548c\u9650\u5236<\/strong><\/p>\n<p>\u5728spec\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7requests\u548climits\u5b57\u6bb5\u6765\u5b9a\u4e49\u5bb9\u5668\u9700\u8981\u7684\u8d44\u6e90\u6570\u91cf\uff0c\u4f8b\u5982CPU\u548c\u5185\u5b58\u3002\u8fd9\u4e9b<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a>\u53c2\u6570\u5bf9\u4e8eKubernetes\u6765\u786e\u4fdd\u8d44\u6e90\u6709\u6548\u5206\u914d\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<p>### <strong>\u4e09\u3001\u914d\u7f6e\u9009\u62e9\u5668\u548c\u6807\u7b7e<\/strong><\/p>\n<p>#### <strong>\u4f7f\u7528\u6807\u7b7e\u548c\u9009\u62e9\u5668<\/strong><\/p>\n<p>\u9009\u62e9\u5668\uff08selector\uff09\u901a\u5e38\u5728Deployment\u6216Service\u914d\u7f6e\u4e2d\u51fa\u73b0\uff0c\u7528\u6765\u9009\u62e9\u4e00\u7ec4\u6807\u7b7e\uff08labels\uff09\u76f8\u5339\u914d\u7684Pods\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06Deployment\u4e0e\u5176\u521b\u5efa\u7684Pods\u8054\u7cfb\u8d77\u6765\u3002<\/p>\n<p>#### <strong>\u7ba1\u7406\u6807\u7b7e<\/strong><\/p>\n<p>\u6807\u7b7e\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u9009\u62e9\u7279\u5b9a\u7684Pods\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u884c\u8d44\u6e90\u7684\u5206\u7ec4\u3001\u8fc7\u6ee4\u548c\u7b56\u7565\u5e94\u7528\u3002\u5408\u7406\u5730\u4f7f\u7528\u6807\u7b7e\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u8d44\u6e90\u7ba1\u7406\u7684\u7075\u6d3b\u6027\u548c\u6548\u7387\u3002<\/p>\n<p>### <strong>\u56db\u3001\u5b9a\u4e49\u5bb9\u5668\u548c\u5377<\/strong><\/p>\n<p>#### <strong>\u5bb9\u5668\u914d\u7f6e<\/strong><\/p>\n<p>\u5728\u5b9a\u4e49Pod\u65f6\uff0c\u5fc5\u987b\u6307\u5b9a\u8fd0\u884c\u7684\u5bb9\u5668\u548c\u5bb9\u5668\u4f7f\u7528\u7684\u955c\u50cf\u3002\u60a8\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u5bb9\u5668\u8fd0\u884c\u65f6\u9700\u8981\u7684\u5404\u79cd\u53c2\u6570\uff0c\u6bd4\u5982\u73af\u5883\u53d8\u91cf\u3001\u542f\u52a8\u547d\u4ee4\u53ca\u53c2\u6570\u7b49\u3002<\/p>\n<p>#### <strong>\u914d\u7f6e\u5377<\/strong><\/p>\n<p>\u5377\uff08Volumes\uff09\u5728Pod\u4e2d\u7528\u4e8e\u5b58\u50a8\u6570\u636e\uff0c\u4fdd\u8bc1\u6570\u636e\u7684\u6301\u4e45\u6027\u3002Kubernetes\u7684YAML\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u5b9a\u4e49\u591a\u79cd\u7c7b\u578b\u7684\u5377\uff0c\u5e76\u6307\u5b9a\u5377\u7684\u6302\u8f7d\u70b9\u3002<\/p>\n<p>### <strong>\u4e94\u3001\u9ad8\u7ea7\u914d\u7f6e<\/strong><\/p>\n<p>#### <strong>\u751f\u547d\u5468\u671f\u7ba1\u7406<\/strong><\/p>\n<p>Kubernetes\u5141\u8bb8\u7528\u6237\u901a\u8fc7\u5b9a\u4e49\u751f\u547d\u5468\u671f\u7684\u94a9\u5b50\uff0c\u6765\u7ba1\u7406\u5bb9\u5668\u7684\u751f\u547d\u5468\u671f\u4e8b\u4ef6\uff0c\u5982PostStart\u6216PreStop\u3002<\/p>\n<p>#### <strong>\u5065\u5eb7\u68c0\u67e5<\/strong><\/p>\n<p>\u901a\u8fc7\u8bbe\u7f6eliveness\u548creadiness\u63a2\u9488\uff0cKubernetes\u53ef\u4ee5\u68c0\u6d4b\u5bb9\u5668\u4f55\u65f6\u51c6\u5907\u597d\u670d\u52a1\u8bf7\u6c42\uff0c\u4ee5\u53ca\u5b83\u662f\u5426\u4ecd\u5728\u8fd0\u884c\u3002<\/p>\n<p>### <strong>\u516d\u3001\u76d1\u63a7\u548c\u65e5\u5fd7\u7ba1\u7406<\/strong><\/p>\n<p>#### <strong>\u8d44\u6e90\u72b6\u6001\u76d1\u63a7<\/strong><\/p>\n<p>Kubernetes\u7684\u76d1\u63a7\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u76d1\u63a7\u8d44\u6e90\u7684\u72b6\u6001\uff0c\u5e76\u786e\u4fdd\u7cfb\u7edf\u7a33\u5065\u8fd0\u884c\u3002\u901a\u8fc7\u9002\u5f53\u5730\u914d\u7f6e\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u53ca\u65f6\u4e86\u89e3\u7cfb\u7edf\u7684\u5065\u5eb7\u72b6\u51b5\u3002<\/p>\n<p>#### <strong>\u65e5\u5fd7\u7ba1\u7406<\/strong><\/p>\n<p>\u5408\u7406\u914d\u7f6e\u65e5\u5fd7\u7ba1\u7406\uff0c\u5bf9\u4e8e\u8c03\u8bd5\u5e94\u7528\u548cKubernetes\u96c6\u7fa4\u81f3\u5173\u91cd\u8981\u3002YAML\u6587\u4ef6\u4e2d\u53ef\u4ee5\u914d\u7f6e\u65e5\u5fd7\u7684\u7ea7\u522b\u548c\u8f93\u51fa\uff0c\u6709\u52a9\u4e8e\u5f00\u53d1\u8005\u5728\u9047\u5230\u95ee\u9898\u65f6\u5feb\u901f\u5b9a\u4f4d\u3002<\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u7684\u4ecb\u7ecd\u548c\u7ed3\u6784\u5206\u6790\uff0c\u60a8\u5e94\u8be5\u5bf9\u5982\u4f55\u7f16\u5199\u4e00\u4e2aKubernetes\u7684YAML\u6587\u4ef6\u6709\u4e86\u57fa\u672c\u7684\u4e86\u89e3\u3002\u8bb0\u5f97\uff0c\u7f16\u5199YAML\u65f6\u52a1\u5fc5\u7ec6\u5fc3\u4e14\u9075\u5faa\u6587\u6863\u89c4\u8303\uff0c\u4ee5\u514d\u56e0\u683c\u5f0f\u9519\u8bef\u6216\u8005\u8bed\u6cd5\u758f\u5ffd\u5bfc\u81f4\u914d\u7f6e\u65e0\u6548\u3002<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u6b63\u786e\u7f16\u5199Kubernetes\u7684YAML\u6587\u4ef6\uff1f<\/strong><\/p>\n<p>\u7f16\u5199Kubernetes\u7684YAML\u6587\u4ef6\u662f\u5b9a\u4e49\u548c\u914d\u7f6eKubernetes\u8d44\u6e90\u7684\u5173\u952e\u6b65\u9aa4\u3002\u5728\u7f16\u5199YAML\u6587\u4ef6\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<p>1. <strong>\u6b63\u786e\u7684\u6587\u4ef6\u7ed3\u6784<\/strong>\uff1aYAML\u6587\u4ef6\u4f7f\u7528\u7f29\u8fdb\u548c\u5192\u53f7\u6765\u5b9a\u4e49\u5c42\u7ea7\u5173\u7cfb\uff0c\u4fdd\u8bc1\u6b63\u786e\u7684\u7f29\u8fdb\u548c\u683c\u5f0f\u975e\u5e38\u91cd\u8981\uff0c\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\uff08\u5982YAML Lint\uff09\u6765\u9a8c\u8bc1\u6587\u4ef6\u683c\u5f0f\u662f\u5426\u6b63\u786e\u3002<\/p>\n<p>2. <strong>\u5b9a\u4e49API\u7248\u672c\u548cKind<\/strong>\uff1a\u5728YAML\u6587\u4ef6\u4e2d\u9700\u8981\u6307\u5b9a\u6240\u8981\u521b\u5efa\u7684\u8d44\u6e90\u7c7b\u578b\uff0c\u6bd4\u5982Pod\u3001Deployment\u7b49\uff0c\u540c\u65f6\u4e5f\u9700\u8981\u6307\u5b9aAPI\u7248\u672c\uff08\u5982v1\u3001apps\/v1\u7b49\uff09\u3002<\/p>\n<p>3. <strong>\u6307\u5b9a\u5143\u6570\u636e<\/strong>\uff1a\u6bcf\u4e2aKubernetes\u8d44\u6e90\u90fd\u9700\u8981\u5305\u542b\u5143\u6570\u636e\uff0c\u6bd4\u5982\u540d\u79f0\uff08metadata.name\uff09\u3001\u547d\u540d\u7a7a\u95f4\uff08metadata.namespace\uff09\u7b49\u3002<\/p>\n<p>4. <strong>\u6307\u5b9a\u89c4\u683c\uff08Spec\uff09<\/strong>\uff1a\u6839\u636e\u6240\u9700\u7684\u8d44\u6e90\u7c7b\u578b\u4e0d\u540c\uff0c\u9700\u8981\u5728YAML\u6587\u4ef6\u4e2d\u6307\u5b9a\u76f8\u5e94\u7684\u89c4\u683c\uff0c\u6bd4\u5982Pod\u7684\u5bb9\u5668\u89c4\u683c\uff08spec.cont<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ners\uff09\u3001Deployment\u7684\u526f\u672c\u6570\u91cf\uff08spec.replicas\uff09\u7b49\u3002<\/p>\n<p>5. <strong>\u6807\u7b7e\uff08Labels\uff09\u548c\u6ce8\u89e3\uff08Annotations\uff09<\/strong>\uff1a\u4e3a\u4e86\u66f4\u597d\u7684\u7ba1\u7406\u548c\u7b5b\u9009\u8d44\u6e90\uff0c\u53ef\u4ee5\u5728YAML\u6587\u4ef6\u4e2d\u6dfb\u52a0\u6807\u7b7e\u548c\u6ce8\u89e3\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u7ec4\u7ec7\u548c\u63cf\u8ff0\u8d44\u6e90\u3002<\/p>\n<p>6. <strong>\u73af\u5883\u53d8\u91cf\u548c\u6570\u636e\u5377<\/strong>\uff1a\u5982\u679c\u9700\u8981\u5728\u5bb9\u5668\u4e2d\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u6216\u6302\u8f7d\u6570\u636e\u5377\uff0c\u4e5f\u9700\u8981\u5728YAML\u6587\u4ef6\u4e2d\u8fdb\u884c\u76f8\u5e94\u7684\u914d\u7f6e\u3002<\/p>\n<p>7. <strong>\u670d\u52a1\u53d1\u73b0\u548c\u7f51\u7edc<\/strong>\uff1a\u5982\u679c\u8d44\u6e90\u9700\u8981\u66b4\u9732\u670d\u52a1\u6216\u914d\u7f6e\u7f51\u7edc\uff0c\u4e5f\u9700\u8981\u5728YAML\u6587\u4ef6\u4e2d\u6dfb\u52a0\u76f8\u5e94\u7684\u914d\u7f6e\uff0c\u6bd4\u5982Service\u8d44\u6e90\u3001Ingress\u8d44\u6e90\u7b49\u3002<\/p>\n<p>8. <strong>\u5b58\u50a8\u548c\u5377<\/strong>\uff1a\u5bf9\u4e8e\u9700\u8981\u6301\u4e45\u5316\u5b58\u50a8\u7684\u8d44\u6e90\uff0c\u9700\u8981\u5728YAML\u6587\u4ef6\u4e2d\u6dfb\u52a0\u5b58\u50a8\u5377\u548c\u6301\u4e45\u5377\u58f0\u660e\u7684\u76f8\u5173\u914d\u7f6e\u3002<\/p>\n<p>\u603b\u4e4b\uff0c\u7f16\u5199Kubernetes\u7684YAML\u6587\u4ef6\u9700\u8981\u7ed3\u5408\u5b9e\u9645\u9700\u6c42\u548c\u5bf9Kubernetes\u8d44\u6e90\u7684\u7406\u89e3\uff0c\u786e\u4fdd\u6587\u4ef6\u683c\u5f0f\u6b63\u786e\u3001\u914d\u7f6e\u5408\u7406\uff0c\u4ee5\u786e\u4fdd\u8d44\u6e90\u53ef\u4ee5\u6b63\u786e\u90e8\u7f72\u548c\u8fd0\u884c\u5728Kubernetes\u96c6\u7fa4\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728\u7f16\u5199Kubernetes\u7684YAML\u6587\u4ef6\u65f6\uff0c\u5173\u952e\u662f\u8981\u7406\u89e3\u5176\u4f5c\u7528\u3001\u7ed3\u6784\u548c\u8bed\u6cd5\u3002Kubernetes YAML\u6587\u4ef6 [&hellip;]","protected":false},"author":1,"featured_media":76946,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/76945"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=76945"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/76945\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/76946"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=76945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=76945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=76945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}