{"id":1206,"date":"2023-03-28T11:02:30","date_gmt":"2023-03-28T03:02:30","guid":{"rendered":"https:\/\/www.linuxdevops.cn\/?p=1206"},"modified":"2023-04-07T09:52:50","modified_gmt":"2023-04-07T01:52:50","slug":"examples-of-commonly-used-yaml-templates-in-kubernetes","status":"publish","type":"post","link":"https:\/\/www.linuxdevops.cn\/2023\/03\/examples-of-commonly-used-yaml-templates-in-kubernetes\/","title":{"rendered":"Kubernetes\u4e2d\u5e38\u7528YAML\u6a21\u677f\u793a\u4f8b"},"content":{"rendered":"
Kubernetes\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u5bb9\u5668\u7f16\u6392\u5e73\u53f0\uff0c\u5b83\u53ef\u4ee5\u7ba1\u7406\u5bb9\u5668\u5316\u5e94\u7528\u7a0b\u5e8f\u7684\u90e8\u7f72\u3001\u6269\u5c55\u548c\u7ba1\u7406\u3002\u5728Kubernetes\u4e2d\uff0cDeployment\u548cService\u662f\u4e24\u4e2a\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u4ed6\u4eec\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b8c\u6210\u5e94\u7528\u7a0b\u5e8f\u7684\u90e8\u7f72\u548c\u670d\u52a1\u53d1\u73b0\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecdKubernetes\u4e2d\u7684Deployment\u548cService\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5e38\u7528\u7684YAML\u6a21\u677f\uff0c\u4ee5\u5e2e\u52a9\u60a8\u5feb\u901f\u90e8\u7f72\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n
Deployment<\/p>\n
Deployment\u662fKubernetes\u4e2d\u7684\u4e00\u4e2a\u8d44\u6e90\u5bf9\u8c61\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u5b9a\u4e49Pod\u7684\u526f\u672c\u6570\u76ee\u548c\u66f4\u65b0\u7b56\u7565\u3002Deployment\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b8c\u6210\u65e0\u7f1d\u7684\u5e94\u7528\u7a0b\u5e8f\u90e8\u7f72\u548c\u66f4\u65b0\u3002\u4f8b\u5982\uff0c\u5728\u66f4\u65b0\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0cDeployment\u53ef\u4ee5\u9010\u6b65\u5730\u5c06\u65b0\u7248\u672c\u7684Pod\u66ff\u6362\u4e3a\u65e7\u7248\u672c\u7684Pod\uff0c\u4ece\u800c\u907f\u514d\u4e86\u5e94\u7528\u7a0b\u5e8f\u7684\u4e2d\u65ad\u3002<\/p>\n
\u5728Kubernetes\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0bYAML\u6a21\u677f\u6765\u521b\u5efa\u4e00\u4e2aDeployment\uff1a<\/p>\n
apiVersion: apps\/v1\nkind: Deployment\nmetadata:\n name: my-deployment\nspec:\n replicas: 3\n selector:\n matchLabels:\n app: my-app\n template:\n metadata:\n labels:\n app: my-app\n spec:\n containers:\n - name: my-container\n image: my-image\n ports:\n - containerPort: 80<\/code><\/pre>\n\u5728\u8fd9\u4e2aYAML\u6a21\u677f\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3amy-deployment\u7684Deployment\u5bf9\u8c61\uff0c\u5b83\u5c06\u521b\u5efa3\u4e2aPod\u526f\u672c\u3002selector\u5b57\u6bb5\u7528\u4e8e\u6307\u5b9a\u54ea\u4e9bPod\u5c06\u53d7\u5230Deployment\u7684\u7ba1\u7406\uff0c\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a\u540d\u4e3amy-app\u7684\u6807\u7b7e\u3002template\u5b57\u6bb5\u7528\u4e8e\u5b9a\u4e49\u521b\u5efa\u7684Pod\u7684\u6a21\u677f\uff0c\u8fd9\u91cc\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3amy-container\u7684\u5bb9\u5668\uff0c\u5b83\u4f7f\u7528\u4e86\u4e00\u4e2a\u540d\u4e3amy-image\u7684\u955c\u50cf\uff0c\u5e76\u76d1\u542c\u4e8680\u7aef\u53e3\u3002<\/p>\n
Service<\/p>\n
Service\u662fKubernetes\u4e2d\u7684\u53e6\u4e00\u4e2a\u8d44\u6e90\u5bf9\u8c61\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b8c\u6210\u670d\u52a1\u53d1\u73b0\u548c\u8d1f\u8f7d\u5747\u8861\u3002\u5728Kubernetes\u4e2d\uff0cPod\u7684IP\u5730\u5740\u662f\u52a8\u6001\u5206\u914d\u7684\uff0c\u56e0\u6b64\u6211\u4eec\u65e0\u6cd5\u76f4\u63a5\u4f7f\u7528Pod\u7684IP\u5730\u5740\u6765\u8bbf\u95ee\u5b83\u4eec\u3002\u800cService\u53ef\u4ee5\u4e3a\u4e00\u7ec4Pod\u63d0\u4f9b\u4e00\u4e2a\u7a33\u5b9a\u7684IP\u5730\u5740\u548c\u7aef\u53e3\uff0c\u4ece\u800c\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7Service\u6765\u8bbf\u95ee\u8fd9\u4e9bPod\u3002<\/p>\n
\u5728Kubernetes\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0bYAML\u6a21\u677f\u6765\u521b\u5efa\u4e00\u4e2aService\uff1a<\/p>\n
apiVersion: v1\nkind: Service\nmetadata:\n name: my-service\nspec:\n selector:\n app: my-app\n ports:\n - name: http\n port: 80\n targetPort: 80\n type: ClusterIP<\/code><\/pre>\n\u5728\u8fd9\u4e2aYAML\u6a21\u677f\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3amy-service\u7684Service\u5bf9\u8c61\uff0c\u5b83\u5c06\u4e3a\u4e00\u7ec4\u4f7f\u7528\u4e86my-app\u6807\u7b7e\u7684Pod\u63d0\u4f9b\u4e00\u4e2a\u7a33\u5b9a\u768480\u7aef\u53e3\u3002type\u5b57\u6bb5\u7528\u4e8e\u6307\u5b9aService\u7684\u7c7b\u578b\uff0c\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528\u4e86ClusterIP\u7c7b\u578b\uff0c\u5b83\u5c06\u4e3aService\u5206\u914d\u4e00\u4e2aClusterIP\u5730\u5740\uff0c\u8be5\u5730\u5740\u53ea\u80fd\u5728Kubernetes\u96c6\u7fa4\u5185\u90e8\u4f7f\u7528\u3002<\/p>\n
\u9664\u4e86Deployment\u548cService\u5728Kubernetes\u4e2d\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u8d44\u6e90\u5bf9\u8c61\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b8c\u6210\u5e94\u7528\u7a0b\u5e8f\u7684\u90e8\u7f72\u548c\u7ba1\u7406\uff0c\u4f8b\u5982Horizontal Pod Autoscaler\uff08HPA\uff09\u548cIngress\u3002<\/p>\n
Horizontal Pod Autoscaler\uff08HPA\uff09<\/p>\n
HPA\u53ef\u4ee5\u6839\u636eCPU\u5229\u7528\u7387\u3001\u5185\u5b58\u5229\u7528\u7387\u7b49\u6307\u6807\u81ea\u52a8\u6269\u5bb9\u6216\u7f29\u5bb9Pod\u7684\u526f\u672c\u6570\u91cf\uff0c\u4ece\u800c\u4fdd\u8bc1\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u53ef\u7528\u6027\u3002\u5728Kubernetes\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0bYAML\u6a21\u677f\u6765\u521b\u5efa\u4e00\u4e2aHPA\uff1a<\/p>\n
apiVersion: autoscaling\/v1\nkind: HorizontalPodAutoscaler\nmetadata:\n name: my-hpa\nspec:\n scaleTargetRef:\n apiVersion: apps\/v1\n kind: Deployment\n name: my-deployment\n minReplicas: 1\n maxReplicas: 10\n targetCPUUtilizationPercentage: 50<\/code><\/pre>\n\u5728\u8fd9\u4e2aYAML\u6a21\u677f\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3amy-hpa\u7684HPA\u5bf9\u8c61\uff0c\u5b83\u5c06\u81ea\u52a8\u6269\u5bb9\u6216\u7f29\u5bb9my-deployment\u7684Pod\u526f\u672c\u6570\u91cf\u3002minReplicas\u548cmaxReplicas\u5b57\u6bb5\u7528\u4e8e\u5b9a\u4e49Pod\u526f\u672c\u6570\u91cf\u7684\u4e0a\u4e0b\u9650\uff0ctargetCPUUtilizationPercentage\u5b57\u6bb5\u7528\u4e8e\u5b9a\u4e49CPU\u5229\u7528\u7387\u7684\u76ee\u6807\u503c\u3002<\/p>\n
Ingress<\/p>\n
Ingress\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5c06\u5916\u90e8\u6d41\u91cf\u8def\u7531\u5230Kubernetes\u96c6\u7fa4\u5185\u90e8\u7684Service\u6216Pod\u4e2d\u3002\u5728Kubernetes\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0bYAML\u6a21\u677f\u6765\u521b\u5efa\u4e00\u4e2aIngress\uff1a<\/p>\n
apiVersion: networking.k8s.io\/v1\nkind: Ingress\nmetadata:\n name: my-ingress\n annotations:\n nginx.ingress.kubernetes.io\/rewrite-target: \/\nspec:\n rules:\n - host: example.com\n http:\n paths:\n - path: \/my-app\n pathType: Prefix\n backend:\n service:\n name: my-service\n port:\n name: http<\/code><\/pre>\n\u5728\u8fd9\u4e2aYAML\u6a21\u677f\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3amy-ingress\u7684Ingress\u5bf9\u8c61\uff0c\u5b83\u5c06example.com\u57df\u540d\u4e0b\u7684\/my-app\u8def\u5f84\u8def\u7531\u5230\u540d\u4e3amy-service\u7684Service\u4e2d\u3002annotations\u5b57\u6bb5\u7528\u4e8e\u5b9a\u4e49Ingress\u7684\u4e00\u4e9b\u7279\u6b8a\u914d\u7f6e\uff0c\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528\u4e86nginx.ingress.kubernetes.io\/rewrite-target\u6ce8\u89e3\uff0c\u5c06\u8bf7\u6c42\u7684\u8def\u5f84\u91cd\u5199\u4e3a\/\uff0c\u4ece\u800c\u907f\u514d\u4e86\u8bf7\u6c42\u7684\u8def\u5f84\u4e2d\u5305\u542b\/my-app\u3002<\/p>\n
\u603b\u7ed3<\/p>\n
\u5728Kubernetes\u4e2d\uff0cDeployment\u548cService\u662f\u4e24\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u5b83\u4eec\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b8c\u6210\u5e94\u7528\u7a0b\u5e8f\u7684\u90e8\u7f72\u548c\u670d\u52a1\u53d1\u73b0\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528YAML\u6a21\u677f\u6765\u521b\u5efaDeployment\u548cService\uff0c\u9664\u4e86Deployment\u548cService\uff0cKubernetes\u4e2d\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u975e\u5e38\u6709\u7528\u7684\u8d44\u6e90\u5bf9\u8c61\uff0c\u4f8b\u5982HPA\u548cIngress\uff0c\u5b83\u4eec\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b8c\u6210\u5e94\u7528\u7a0b\u5e8f\u7684\u81ea\u52a8\u6269\u7f29\u5bb9\u548c\u5916\u90e8\u6d41\u91cf\u7684\u8def\u7531\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528YAML\u6a21\u677f\u6765\u521b\u5efaDeployment\u548cService\uff0c\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528YAML\u6a21\u677f\u6765\u521b\u5efaHPA\u548cIngress\uff0c\u5e0c\u671b\u8fd9\u4e9b\u6a21\u677f\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7ba1\u7406\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
Kubernetes\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u5bb9\u5668\u7f16\u6392\u5e73\u53f0\uff0c\u5b83\u53ef\u4ee5\u7ba1\u7406\u5bb9\u5668\u5316\u5e94\u7528\u7a0b\u5e8f\u7684\u90e8\u7f72\u3001\u6269\u5c55\u548c\u7ba1\u7406\u3002\u5728Kubernete<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[121],"yoast_head":"\n
Kubernetes\u4e2d\u5e38\u7528YAML\u6a21\u677f\u793a\u4f8b - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n