{"id":2712,"date":"2020-12-25T09:48:54","date_gmt":"2020-12-25T01:48:54","guid":{"rendered":"http:\/\/blog.ywdevops.cn\/?p=2712"},"modified":"2026-01-07T11:52:41","modified_gmt":"2026-01-07T03:52:41","slug":"service","status":"publish","type":"post","link":"https:\/\/blog.ywdevops.cn\/index.php\/2020\/12\/25\/service\/","title":{"rendered":"k8s\u4e4bservice\u7528\u6cd5"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Service<\/strong>\u662fKubernetes\u7684\u6838\u5fc3\u6982\u5ff5\uff0c\u901a\u8fc7\u521b\u5efaService\uff0c<strong>\u53ef\u4ee5\u4e3a\u4e00\u7ec4\u5177\u6709\u76f8\u540c\u529f\u80fd\u7684\u5bb9\u5668\u5e94\u7528\u63d0\u4f9b\u4e00\u4e2a\u7edf\u4e00\u7684\u5165\u53e3\u5730\u5740<\/strong>\uff0c\u5e76\u4e14\u5c06\u8bf7\u6c42\u8d1f\u8f7d\u5206\u53d1\u5230\u540e\u7aef\u7684\u5404\u4e2a\u5bb9\u5668\u5e94\u7528\u4e0a\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4e00\u3001 Service\u7684\u57fa\u672c\u7528\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u822c\u6765\u8bf4\uff0c\u5bf9\u5916\u63d0\u4f9b\u670d\u52a1\u7684\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u901a\u8fc7\u67d0\u79cd\u673a\u5236\u6765\u5b9e\u73b0\uff0c\u5bf9\u4e8e\u5bb9\u5668\u5e94\u7528\u6700\u7b80\u4fbf\u7684\u65b9\u5f0f\u5c31\u662f\u901a\u8fc7TCP\/IP\u673a\u5236\u53ca\u76d1\u542cIP\u548c\u7aef\u53e3\u53f7\u6765\u5b9e\u73b0\u3002 <strong> <\/strong> <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u867d\u7136\u76f4\u63a5\u901a\u8fc7Pod\u7684IP\u5730\u5740\u548c\u7aef\u53e3\u53f7\u53ef\u4ee5\u8bbf\u95ee\u5230\u5bb9\u5668\u5e94\u7528\u5185\u7684\u670d\u52a1\uff0c\u4f46\u662fPod\u7684IP\u5730\u5740\u662f\u4e0d\u53ef\u9760\u7684\uff0c\u4f8b\u5982\u5f53Pod\u6240\u5728\u7684Node\u53d1\u751f\u6545\u969c\u65f6\uff0cPod\u5c06\u88abKubernetes\u91cd\u65b0\u8c03\u5ea6\u5230\u53e6\u4e00\u4e2aNode\uff0c Pod\u7684IP\u5730\u5740\u5c06\u53d1\u751f\u53d8\u5316\u3002\u66f4\u91cd\u8981\u7684\u662f\uff0c\u5982\u679c\u5bb9\u5668\u5e94\u7528\u672c\u8eab\u662f\u5206\u5e03\u5f0f\u7684\u90e8\u7f72\u65b9\u5f0f\uff0c\u901a\u8fc7\u591a\u4e2a\u5b9e\u4f8b\u5171\u540c\u63d0\u4f9b\u670d\u52a1\uff0c\u5c31\u9700\u8981\u5728\u8fd9\u4e9b\u5b9e\u4f8b\u7684\u524d\u7aef\u8bbe\u7f6e\u4e00\u4e2a\u8d1f\u8f7d\u5747\u8861\u5668\u6765\u5b9e\u73b0\u8bf7\u6c42\u7684\u5206\u53d1\u3002 Kubernetes\u4e2d\u7684Service\u5c31\u662f\u7528\u4e8e\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u7684\u6838\u5fc3\u7ec4\u4ef6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Service \u4e0ePod \u4e4b\u95f4\u662f\u901a\u8fc7Label \u548cLabel \u7b5b\u9009\u5668\uff08selector\uff09\u677e\u8026\u5408\u5728\u4e00\u8d77\u7684\u3002Deployment<br>\u548cPod \u4e4b\u95f4\u4e5f\u662f\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u8fdb\u884c\u5173\u8054\u7684\uff0c\u8fd9\u79cd\u677e\u8026\u5408\u65b9\u5f0f\u662fKubernetes \u5177\u5907\u8db3\u591f\u7684\u7075\u6d3b\u6027<br>\u7684\u5173\u952e<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u968f\u7740Pod \u7684\u65f6\u5e38\u8fdb\u51fa\uff08\u6269\u5bb9\u548c\u7f29\u5bb9\u3001\u6545\u969c\u3001\u6eda\u52a8\u5347\u7ea7\u7b49\uff09\uff0cService \u4f1a\u52a8\u6001\u66f4\u65b0\u5176\u7ef4\u62a4\u7684<br>\u76f8\u5339\u914d\u7684\u5065\u5eb7Pod \u5217\u8868\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5176\u4e2d\u7684\u5339\u914d\u5173\u7cfb\u662f\u901a\u8fc7Label \u7b5b\u9009\u5668\u548c\u540d\u4e3aEndpoint<br>\u5bf9\u8c61\u7684\u7ed3\u6784\u5171\u540c\u5b8c\u6210\u7684\uff0c\u6bcf\u4e00\u4e2a Service \u5728\u88ab\u521b\u5efa\u7684\u65f6\u5019\uff0c\u90fd\u4f1a\u5f97\u5230\u4e00\u4e2a\u5173\u8054\u7684Endpoint \u5bf9\u8c61\u3002\u6574\u4e2aEndpoint\u5bf9\u8c61\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a\u52a8\u6001\u7684\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u96c6\u7fa4\u4e2d\u6240\u6709\u7684\u5339\u914dService Label \u7b5b\u9009\u5668\u7684\u5065\u5eb7<br>Pod\u3002Kubernetes \u4f1a\u4e0d\u65ad\u5730\u68c0\u67e5Service \u7684Label \u7b5b\u9009\u5668\u548c\u5f53\u524d\u96c6\u7fa4\u4e2d\u5065\u5eb7Pod \u7684\u5217\u8868\u3002\u5982\u679c\u6709<br>\u65b0\u7684\u80fd\u591f\u5339\u914dLabel \u7b5b\u9009\u5668\u7684Pod \u51fa\u73b0\uff0c\u5b83\u5c31\u4f1a\u88ab\u52a0\u5165Endpoint \u5bf9\u8c61\uff0c\u800c\u6d88\u5931\u7684Pod \u5219\u4f1a\u88ab<br>\u5254\u9664\u3002\u4e5f\u5c31\u662f\u8bf4\uff0cEndpoint \u5bf9\u8c61\u59cb\u7ec8\u662f\u4fdd\u6301\u66f4\u65b0\u7684\u3002\u8fd9\u65f6\uff0c\u5f53Service \u9700\u8981\u5c06\u6d41\u91cf\u8f6c\u53d1\u5230Pod<br>\u7684\u65f6\u5019\uff0c\u5c31\u4f1a\u5230Endpoint \u5bf9\u8c61\u4e2d\u6700\u65b0\u7684Pod \u7684\u5217\u8868\u4e2d\u8fdb\u884c\u67e5\u627e\uff0c\u4e0b\u56fe\u4e2d\u53ef\u4ee5\u770b\u5230server\u548cendpoint\u7684\u5bf9\u5e94\u5173\u7cfb<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"656\" height=\"311\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/11.png\" alt=\"\" class=\"wp-image-18181\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/11.png 656w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/11-300x142.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/11-600x284.png 600w\" sizes=\"auto, (max-width: 656px) 100vw, 656px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4e8c\u3001service\u7c7b\u578b\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> Kubernetes\u7684Service\u5171\u6709\u56db\u79cd\u7c7b\u578b\uff1a ClusterIP \u3001 NodePort\u3001 LoadBalancer\u548cExternalName <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001 ClusterIP \uff1a\u62e5\u6709\u56fa\u5b9a\u7684IP \u5730\u5740\u548c\u7aef\u53e3\u53f7\uff0c\u53ea\u8981\u521b\u5efa\u540e\u540d\u5b57\u3001IP\u3001\u7aef\u53e3\u90fd\u4fdd\u6301\u4e0d\u53d8\uff0c\u76f4\u5230\u5220\u9664\uff0c\u5e76\u4e14\u4ec5\u80fd\u591f\u4ece\u96c6\u7fa4\u5185\u90e8\u8bbf\u95ee\u5f97\u5230\uff0c\u96c6\u7fa4\u5916\u90e8\u5ba2\u6237\u7aef\u65e0\u6cd5\u8bbf\u95ee(\u9002\u5408\u5404\u4e2apod\u4e4b\u95f4\u901a\u4fe1\uff0c\u53ef\u5728service\u6587\u4ef6\u4e2d\u5b9a\u4e49\u591a\u670d\u52a1\u7aef\u53e3\u7684\u65b9\u5f0f)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001 NodePort \uff1a\u6b64\u7c7b\u578b\u5efa\u7acb\u5728clusterip\u4e4b\u4e0a\uff0c\u5728\u6bcf\u4e2a\u8282\u70b9\u7684IP\u5730\u5740\u4e0a\u7684\u67d0\u9759\u6001\u7aef\u53e3\u66b4\u9732\u670d\u52a1\uff0c\u7aef\u53e3\u8303\u56f4(<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">30000-32767<\/mark><\/strong>)\uff0c\u4e5f\u4f1a\u4e3a\u96c6\u7fa4\u5206\u914dClusterIP\u5730\u5740\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\uff0c\u5916\u90e8\u5ba2\u6237\u7aef\u901a\u8fc7NodePort\u8bbf\u95ee\u8fdb\u6765\uff0c\u901a\u8fc7ClusterIP\u5730\u5740\u505a\u8def\u7531\u8f6c\u53d1\u5230\u540e\u53f0\u7684pod\u670d\u52a1\u4e2d\uff0c\u540c\u65f6NodePort\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u96c6\u7fa4\u5185\u90e8\u4e4b\u95f4\u901a\u4fe1\u4f7f\u7528\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"621\" height=\"458\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2020\/12\/8-1.png\" alt=\"\" class=\"wp-image-2714\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2020\/12\/8-1.png 621w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2020\/12\/8-1-300x221.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2020\/12\/8-1-600x443.png 600w\" sizes=\"auto, (max-width: 621px) 100vw, 621px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">3\u3001 LoadBalancer \uff1a\u6b64\u7c7b\u578b\u6784\u5efa\u5728 ClusterIP  \u4e4b\u4e0a\uff0c\u901a\u8fc7cloud provider\u63d0\u4f9b\u7684\u8d1f\u8f7d\u5747\u8861\u5668\u5c06\u670d\u52a1\u66b4\u9732\u5728\u96c6\u7fa4\u5916\u90e8\uff0c LoadBalancer\u5177\u6709 ClusterIP \u548c NodePort <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"638\" height=\"440\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2020\/12\/9-1.png\" alt=\"\" class=\"wp-image-2715\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2020\/12\/9-1.png 638w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2020\/12\/9-1-300x207.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2020\/12\/9-1-600x414.png 600w\" sizes=\"auto, (max-width: 638px) 100vw, 638px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">4\u3001 ExternalName \uff1a\u901a\u8fc7\u5c06Service\u6620\u5c04\u81f3\u7531 ExternalName \u5b57\u6bb5\u7684\u5185\u5bb9\u6307\u5b9a\u7684\u4e3b\u673a\u540d\u6765\u66b4\u9732\u670d\u52a1\uff0c\u6b64\u4e3b\u673a\u540d\u9700\u8981\u88abDNS\u89e3\u6790\u5230CNAME\u7c7b\u578b\u7684\u8bb0\u5f55\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u5c06\u96c6\u7fa4\u5916\u90e8\u7684\u670d\u52a1\u901a\u8fc7DNS CNAME\u7684\u65b9\u5f0f\u6620\u5c04\u5230\u96c6\u7fa4\u5185\u90e8\uff0c\u6b64\u7c7b\u578b\u6ca1\u6709 ClusterIP \u548c NodePort \uff0c\u4e5f\u4e0d\u5b58\u5728\u6807\u7b7e\u9009\u62e9\u5668\u6765\u9009\u62e9pod\u8d44\u6e90\uff0c\u56e0\u6b64\u4e0d\u4f1a\u6709Endpoints\u7684\u5b58\u5728\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"608\" height=\"357\" src=\"http:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2020\/12\/10-1.png\" alt=\"\" class=\"wp-image-2716\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2020\/12\/10-1.png 608w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2020\/12\/10-1-300x176.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2020\/12\/10-1-600x352.png 600w\" sizes=\"auto, (max-width: 608px) 100vw, 608px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u56e0\u6b64\uff0c\u5982\u679c\u8981\u5c06\u5185\u90e8\u7684\u8d44\u6e90\u66b4\u9732\u5230\u5916\u90e8\u5ba2\u6237\u7aef\u4f7f\u7528\uff0c\u9700\u8981\u914d\u7f6e NodePort \u6216\u8005  LoadBalancer \u7c7b\u578b\uff0c\u5982\u679c\u8981\u5c06\u5916\u90e8\u8d44\u6e90\u53d1\u5e03\u5230\u96c6\u7fa4\u5185\u90e8\u4f9bpod\u8c03\u7528 \uff0c\u5e94\u8be5\u914d\u7f6e ExternalName \u7c7b\u578b<\/p>\n\n\n\n<p class=\"has-text-align-center has-medium-font-size wp-block-paragraph\"><strong>\u670d\u52a1\u66b4\u9732<\/strong>\uff1a <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u3001<strong>\u901a\u8fc7  NodePort \u65b9\u5f0f\u5b9e\u73b0<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1\u3001\u901a\u8fc7deployment\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684pod\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"657\" height=\"425\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/3-4.png\" alt=\"\" class=\"wp-image-18191\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/3-4.png 657w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/3-4-300x194.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/3-4-600x388.png 600w\" sizes=\"auto, (max-width: 657px) 100vw, 657px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001\u5b9a\u4e49service.yaml\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"654\" height=\"307\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/4-1.png\" alt=\"\" class=\"wp-image-18192\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/4-1.png 654w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/4-1-300x141.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/4-1-600x282.png 600w\" sizes=\"auto, (max-width: 654px) 100vw, 654px\" \/><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u8bf4\u660e\uff1a\u5b9a\u4e49\u670d\u52a1\u53d1\u73b0\u65b9\u5f0f\u4e3a<strong>NodePort<\/strong>\uff0c\u8282\u70b9\u7aef\u53e3\u4e3a<strong>3000<\/strong>0\uff0c\u6839\u636e\u6807\u7b7enginx-pod\u53bb\u5bfb\u627epod\uff0c\u6b64\u6587\u4ef6\u7684\u5185\u5bb9\u53ef\u4ee5\u7406\u89e3\u4e3a\uff1a<strong>\u901a\u8fc7\u96c6\u7fa4\u7684\u4efb\u610f\u8282\u70b9\u768430000\u7aef\u53e3\u8bbf\u95ee\u5230ClusterIP\u5730\u5740\u76848088\u7aef\u53e3\uff0c\u8fdb\u800c\u8f6c\u53d1\u5230pod\u4e2d\u768480\u7aef\u53e3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001\u6267\u884c\u547d\u4ee4<strong>kubectl create -f service.yaml<\/strong>\uff0c\u67e5\u770bservice\u72b6\u6001\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"718\" height=\"156\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/5-3.png\" alt=\"\" class=\"wp-image-18194\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/5-3.png 718w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/5-3-300x65.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/5-3-600x130.png 600w\" sizes=\"auto, (max-width: 718px) 100vw, 718px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u4e0a\u56fe\u53ef\u4ee5\u770b\u5230\u521b\u5efaNodePort\u7c7b\u578b\u7684Service\u7684\u65f6\u5019\uff0c\u4e5f\u521b\u5efa\u4e86ClusterIP\u7684IP\u5730\u5740\u548c\u7aef\u53e3(8088)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3\u3001\u6b64\u65f6\u901a\u8fc7\u96c6\u7fa4\u4e2d\u7684<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u4efb\u610f\u8282\u70b9\u52a0\u7aef\u53e3<\/mark><\/strong>\u90fd\u53ef\u4ee5\u8bbf\u95eepod\u8d44\u6e90\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"672\" height=\"296\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/6-3.png\" alt=\"\" class=\"wp-image-18195\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/6-3.png 672w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/6-3-300x132.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/6-3-600x264.png 600w\" sizes=\"auto, (max-width: 672px) 100vw, 672px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001<strong>ClusterIP\u65b9\u5f0f\u5b9e\u73b0\u5185\u90e8\u8bbf\u95ee<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ClusterIP\u662f\u4e0d\u80fd\u901a\u8fc7\u96c6\u7fa4\u5916\u90e8\u8bbf\u95ee\u7684\uff0c\u53ea\u9002\u7528\u4e0e\u96c6\u7fa4\u5185\u90e8\u7684pod\u4e4b\u95f4\u901a\u8baf\uff0c\u4f8b\u5982\uff1a\u73b0\u5728\u96c6\u7fa4\u5185\u90e8\u5df2\u7ecf\u5b58\u5728\u4e86nginx\u5e94\u7528\uff0c\u7aef\u53e3\u662f80\uff0c\u96c6\u7fa4\u5185\u7684\u5176\u4ed6pod\u60f3\u8bbf\u95ee\u8fd9\u4e2a80\u7aef\u53e3\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7ClusterIP \u6765\u8bbf\u95ee\uff0c\u5b9a\u4e49yml\u6587\u4ef6\u521b\u5efaClusterIP\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apiVersion: v1\nkind: Service\nmetadata:\n  name: service-nginx-cluster\nspec:\n  type: ClusterIP\n  ports:\n  - port: 8081\n    targetPort: 80\n    protocol: TCP\n  selector:\n    app: nginx-pod<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u5b9a\u4e49\u7684Service\u7c7b\u578b\u4e3aClusterIP\uff0c\u8fd9\u4e2aClusterIP\u7684\u7aef\u53e3\u662f8081\uff0c\u6620\u5c04\u5230\u76ee\u6807pod\u7684\u7aef\u53e3\u4e3a80\uff0c\u5e76\u6839\u636eselector\u6765\u627e\u5230\u76ee\u6807pod\uff0c\u521b\u5efa\u540e\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"666\" height=\"189\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/1-3.png\" alt=\"\" class=\"wp-image-18185\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/1-3.png 666w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/1-3-300x85.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/1-3-600x170.png 600w\" sizes=\"auto, (max-width: 666px) 100vw, 666px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u540e\u5728\u4efb\u610f\u8282\u70b9\u901a\u8fc7ClusterIP:8081\u7aef\u53e3\u8bbf\u95ee\u5373\u53ef\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"660\" height=\"432\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/2-4.png\" alt=\"\" class=\"wp-image-18186\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/2-4.png 660w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/2-4-300x196.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/05\/2-4-600x393.png 600w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-orange-background-color has-background wp-block-paragraph\">\u6ce8\uff1a\u53ef\u4ee5\u5b9a\u4e49\u591a\u4e2aClusterIP\u7c7b\u578b\u7684Service,\u65b9\u4fbfpod\u4e4b\u95f4\u8fdb\u884c\u901a\u4fe1<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3\u3001ExternalName\u7c7b\u578b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ExternalName\u7528\u4e8e\u5c06\u96c6\u7fa4\u5916\u7684\u670d\u52a1\u5b9a\u4e49\u4e3aK8S\u96c6\u7fa4\u5185\u7684service\uff0c\u5e76\u901a\u8fc7ExternalName\u5b57\u6bb5\u6307\u5b9a\u5916\u90e8\u670d\u52a1\u7684\u5730\u5740\uff0c\u53ef\u4f7f\u7528\u57df\u540d\u6216\u8005IP\u5f62\u5f0f\uff0c\u96c6\u7fa4\u5185\u5ba2\u6237\u7aef\u901a\u8fc7\u8bbf\u95ee\u6b64Service\u5c31\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u670d\u52a1\u4e86\uff0c\u6b64Service\u6ca1\u6709\u540e\u7aefPod\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u8bbe\u7f6e\u6807\u7b7e\u9009\u62e9\u5668\uff0c\u4e0b\u9762\u4e3e\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apiVersion: v1\r\nkind: Service\r\nmetadata:\r\n  name: ex-service\r\n  namespace: prod\r\nspec:\r\n  type: ExternalName\r\n  externalName: www.test.com\n  ports:\n  - port: 443\n    targetPort: 443<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">service\u7684\u540d\u79f0\u4e3aex-service\uff0c\u547d\u540d\u7a7a\u95f4\u4e3aprod\uff0c\u5ba2\u6237\u7aef\u8bbf\u95ee\u670d\u52a1\u7684DNS\u57df\u540d\u5730\u5740ex-service.prod.svc.cluster.local\u7684\u65f6\u5019\uff0c\u4f1a\u81ea\u52a8\u8bf7\u6c42\u5230https:\/\/www.test.com\u5730\u5740\uff0c\u76f8\u5f53\u4e8e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl https:\/\/ex-service.prod.svc.cluster.local >> curl https:\/\/www.test.com<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6ce8\uff1a\u53ef\u6839\u636e\u5b9e\u9645\u9700\u8981\u4fee\u6539\u7aef\u53e3<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Service\u7684\u4ef7\u503c\u5728\u54ea\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\u6700\u5e38\u89c1\u7684\u5c31\u662f\u5bf9\u5e94\u7528\u8fdb\u884c\u66f4\u65b0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Service\u662fKubernetes\u7684\u6838\u5fc3\u6982\u5ff5\uff0c\u901a\u8fc7\u521b\u5efaService\uff0c\u53ef\u4ee5\u4e3a\u4e00\u7ec4\u5177\u6709\u76f8\u540c\u529f\u80fd\u7684\u5bb9\u5668\u5e94\u7528\u63d0\u4f9b\u4e00 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":2728,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93,23],"tags":[],"class_list":["post-2712","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kubernetes","category-rong"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":8}},"_links":{"self":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/2712","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2712"}],"version-history":[{"count":29,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/2712\/revisions"}],"predecessor-version":[{"id":19464,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/2712\/revisions\/19464"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media\/2728"}],"wp:attachment":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2712"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2712"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2712"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}