{"id":100,"date":"2026-08-24T00:11:10","date_gmt":"2026-08-23T16:11:10","guid":{"rendered":"https:\/\/www.xiaoxindong.cloud\/?p=100"},"modified":"2026-09-06T19:57:26","modified_gmt":"2026-09-06T11:57:26","slug":"5-k8s-%e6%9c%8d%e5%8a%a1%e5%8f%91%e7%8e%b0%e4%b8%8e%e8%b4%9f%e8%bd%bd%e5%9d%87%e8%a1%a1%e5%a4%8d%e4%b9%a0%e8%b5%84%e6%96%99","status":"publish","type":"post","link":"https:\/\/www.xiaoxindong.cloud\/?p=100","title":{"rendered":"5. K8s \u670d\u52a1\u53d1\u73b0\u4e0e\u8d1f\u8f7d\u5747\u8861\u590d\u4e60\u8d44\u6599"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4e00\u3001Service \u6838\u5fc3\u6982\u5ff5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u4ec0\u4e48\u662f Service<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Service \u662f K8s \u4e3a\u4e00\u7ec4 Pod \u63d0\u4f9b<strong>\u7a33\u5b9a\u7f51\u7edc\u5165\u53e3\u3001\u670d\u52a1\u53d1\u73b0\u3001\u8d1f\u8f7d\u5747\u8861<\/strong>\u7684\u6838\u5fc3\u8d44\u6e90\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">Pod \u662f\u52a8\u6001\u7684\uff1a\u9500\u6bc1\u91cd\u5efa\u3001\u6f02\u79fb\u540e Pod\u2011IP \u4f1a\u53d8\u5316\uff1bService \u5c4f\u853d Pod \u53d8\u5316\uff0c\u63d0\u4f9b\u56fa\u5b9a\u8bbf\u95ee\u5165\u53e3\u3002<\/p>\n<\/blockquote>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u901a\u8fc7<strong>\u6807\u7b7e selector<\/strong>\u5339\u914d\u540e\u7aef Pod<\/li>\n\n\n\n<li>\u81ea\u52a8\u7ef4\u62a4\u540e\u7aef\u53ef\u7528 Pod \u5217\u8868\uff0cPod \u589e\u51cf\u81ea\u52a8\u66f4\u65b0<\/li>\n\n\n\n<li>\u6838\u5fc3\u4e2d\u95f4\u8d44\u6e90\uff1a<strong>Endpoints<\/strong>\uff0c\u4fdd\u5b58\u5339\u914d selector \u7684 Pod \u7684<code>IP+\u7aef\u53e3<\/code>\uff0cService \u548c Pod \u4e4b\u95f4\u7684\u6865\u6881\uff0c\u81ea\u52a8\u751f\u6210\uff0c\u4e0d\u7528\u624b\u52a8\u4fee\u6539\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Service \u5b57\u6bb5\u8bf4\u660e<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>port<\/code>\uff1aService \u81ea\u8eab\u66b4\u9732\u7aef\u53e3\uff0c\u5ba2\u6237\u7aef\u8bbf\u95ee\u8fd9\u4e2a\u7aef\u53e3<\/li>\n\n\n\n<li><code>targetPort<\/code>\uff1a\u540e\u7aef Pod \u5bb9\u5668\u5b9e\u9645\u76d1\u542c\u7aef\u53e3\uff0c\u6d41\u91cf\u8f6c\u53d1\u5230\u6b64\u7aef\u53e3<\/li>\n\n\n\n<li><code>selector<\/code>\uff1a\u6807\u7b7e\u9009\u62e9\u5668\uff0c\u7b5b\u9009\u540e\u7aef Pod<\/li>\n\n\n\n<li><code>type<\/code>\uff1aservice \u7c7b\u578b<\/li>\n\n\n\n<li>\u4f1a\u8bdd\u4fdd\u6301<code>sessionAffinity:ClientIP<\/code>\uff1a\u540c\u4e00\u4e2a\u5ba2\u6237\u7aef IP \u59cb\u7ec8\u8f6c\u53d1\u5230\u540c\u4e00\u4e2a Pod\uff1b\u9ed8\u8ba4 None \u8f6e\u8be2\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u96c6\u7fa4\u5185\u90e8 DNS \u57df\u540d\u683c\u5f0f\uff1a <code>\u670d\u52a1\u540d.\u547d\u540d\u7a7a\u95f4.svc.cluster.local<\/code> \u4f8b\uff1a<code>my\u2011app\u2011service.default.svc.cluster.local<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001Service \u4e94\u79cd\u7c7b\u578b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u8868\u683c<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7c7b\u578b<\/th><th>\u4f5c\u7528\u57df<\/th><th>\u7279\u70b9<\/th><th>\u9002\u7528\u573a\u666f<\/th><\/tr><\/thead><tbody><tr><td><strong>ClusterIP\uff08\u9ed8\u8ba4\uff09<\/strong><\/td><td>\u4ec5\u96c6\u7fa4\u5185\u90e8\u8bbf\u95ee<\/td><td>\u5206\u914d\u865a\u62df\u96c6\u7fa4 IP\uff1bkube\u2011proxy (iptables\/ipvs) \u505a\u56db\u5c42\u8d1f\u8f7d\u5747\u8861\uff1b\u5916\u90e8\u65e0\u6cd5\u8bbf\u95ee<\/td><td>\u5fae\u670d\u52a1\u4e4b\u95f4\u5185\u90e8\u8c03\u7528\uff0c<strong>\u751f\u4ea7\u540e\u7aef\u670d\u52a1\u9996\u9009<\/strong><\/td><\/tr><tr><td><strong>NodePort<\/strong><\/td><td>\u96c6\u7fa4\u5185\u5916<\/td><td>\u5728<strong>\u6240\u6709\u8282\u70b9\u6253\u5f00\u540c\u4e00\u4e2a\u7aef\u53e3 (30000\u201132767)<\/strong>\uff1b\u4fdd\u7559 ClusterIP \u80fd\u529b\uff1b\u5916\u90e8\u8bbf\u95ee<code>\u8282\u70b9IP:NodePort<\/code><\/td><td>\u6d4b\u8bd5\u73af\u5883\uff0c\u4e34\u65f6\u66b4\u9732\u670d\u52a1\uff0c<strong>\u751f\u4ea7\u4e0d\u5efa\u8bae\u76f4\u63a5\u4f7f\u7528<\/strong><\/td><\/tr><tr><td><strong>Headless (\u65e0\u5934\u670d\u52a1\uff0cclusterIP:None)<\/strong><\/td><td>\u96c6\u7fa4\u5185\u90e8<\/td><td><strong>\u4e0d\u5206\u914d ClusterIP\uff0c\u65e0 kube\u2011proxy \u4ee3\u7406\u3001\u65e0\u5185\u7f6e\u8d1f\u8f7d\u5747\u8861<\/strong>\uff1bDNS \u76f4\u63a5\u8fd4\u56de\u5168\u90e8 Pod \u771f\u5b9e IP\uff1b\u914d\u5408 StatefulSet \u6709\u56fa\u5b9a Pod \u57df\u540d<code>pod\u540d.svc\u540d.ns.svc.cluster.local<\/code><\/td><td>\u6709\u72b6\u6001\u5e94\u7528 MySQL\u3001Redis \u96c6\u7fa4\uff1b\u5ba2\u6237\u7aef\u76f4\u8fde Pod \u5b9e\u4f8b<\/td><\/tr><tr><td>LoadBalancer<\/td><td>\u5916\u90e8\u8bbf\u95ee<\/td><td>\u4f9d\u8d56\u4e91\u5382\u5546\uff0c\u4e91\u5e73\u53f0\u5206\u914d\u516c\u7f51 IP<\/td><td>\u516c\u6709\u4e91\u751f\u4ea7\u5bf9\u5916\u66b4\u9732\u670d\u52a1<\/td><\/tr><tr><td>ExternalName<\/td><td>\u96c6\u7fa4\u5185\u90e8<\/td><td>CNAME\uff0c\u628a service \u6620\u5c04\u96c6\u7fa4\u5916\u90e8\u57df\u540d<\/td><td>\u8bbf\u95ee\u96c6\u7fa4\u5916\u90e8\u7b2c\u4e09\u65b9\u670d\u52a1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">1.ClusterIP \u793a\u4f8b yaml<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>apiVersion: v1\nkind: Service\nmetadata:\n  name: nginx-svc\nspec:\n  type: ClusterIP #\u9ed8\u8ba4\uff0c\u53ef\u7701\u7565\n  selector:\n    app: nginx\n  ports:\n  - port: 80\n    targetPort: 80\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u9a8c\u8bc1\uff1a\u96c6\u7fa4\u5185\u90e8 Pod \u4f7f\u7528 ClusterIP \u6216\u8005 DNS \u57df\u540d\u8bbf\u95ee\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">2.NodePort \u793a\u4f8b yaml<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>apiVersion: v1\nkind: Service\nmetadata:\n  name: nginx-np-svc\nspec:\n  type: NodePort\n  selector:\n    app: nginx\n  ports:\n  - port: 80\n    targetPort: 80\n    nodePort: 30080 #\u53ef\u9009\uff0c\u4e0d\u5199\u81ea\u52a8\u5206\u914d30000\u201132767\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u8bbf\u95ee\uff1a<code>\u4efb\u610f\u8282\u70b9IP:30080<\/code>\uff1b\u96c6\u7fa4\u5185\u90e8\u4f9d\u65e7\u53ef\u4ee5\u7528 ClusterIP \u8bbf\u95ee\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">3.Headless \u65e0\u5934\u670d\u52a1\u793a\u4f8b yaml<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>apiVersion: v1\nkind: Service\nmetadata:\n  name: mysql-headless\nspec:\n  clusterIP: None #\u5173\u952e\uff0c\u5f00\u542f\u65e0\u5934\u670d\u52a1\n  selector:\n    app: mysql\n  ports:\n  - port: 3306\n    targetPort: 3306\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u642d\u914d StatefulSet\uff0c\u6bcf\u4e2a Pod \u56fa\u5b9a\u57df\u540d\uff1a<code>mysql\u20110.mysql\u2011headless.default.svc.cluster.local<\/code>\uff1bDNS \u89e3\u6790\u76f4\u63a5\u8fd4\u56de Pod \u771f\u5b9e IP\uff0c\u5ba2\u6237\u7aef\u81ea\u5df1\u505a\u8d1f\u8f7d\u5747\u8861\u3002<\/p>\n<\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u2728\u9762\u8bd5\uff1aHeadless \u548c\u666e\u901a ClusterIP \u533a\u522b<\/p>\n<\/blockquote>\n\n\n\n<ol class=\"wp-block-list\">\n<li>ClusterIP \u5206\u914d\u865a\u62df IP\uff0ckube\u2011proxy \u4ee3\u7406\u505a\u8d1f\u8f7d\u5747\u8861\uff1bHeadless <code>clusterIP:None<\/code>\uff0c\u65e0\u865a\u62df IP\uff0c\u65e0\u4ee3\u7406\u3002<\/li>\n\n\n\n<li>DNS\uff1a\u666e\u901a Service \u89e3\u6790\u8fd4\u56de ClusterIP\uff1bHeadless DNS \u8fd4\u56de\u5168\u90e8\u540e\u7aef PodIP \u5217\u8868\u3002<\/li>\n\n\n\n<li>Headless \u9002\u5408\u6709\u72b6\u6001\u7ec4\u4ef6\uff0c\u5ba2\u6237\u7aef\u76f4\u8fde\u6307\u5b9a Pod\uff1b\u666e\u901a Service \u9002\u5408\u65e0\u72b6\u6001\uff0c\u968f\u673a\u8d1f\u8f7d\u5747\u8861\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u5e38\u7528 service \u547d\u4ee4<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl get svc\nkubectl describe svc xxx   #\u67e5\u770bselector\u3001Endpoints\nkubectl get endpoints      #\u76f4\u63a5\u67e5\u770b\u540e\u7aef\u7aef\u70b9\u5217\u8868\n#\u96c6\u7fa4\u5185\u6d4b\u8bd5\u8bbf\u95ee\nkubectl run test-pod --image=busybox --rm -it --restart=Never -- sh\nwget -q -O - http:\/\/nginx-svc.default.svc.cluster.local:80\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u26a0\ufe0f\u751f\u4ea7\u4e3a\u4ec0\u4e48\u4e0d\u76f4\u63a5\u7528 NodePort\uff1f<\/p>\n<\/blockquote>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u7aef\u53e3\u8303\u56f4\u56fa\u5b9a 30000\u201132767\uff0c\u670d\u52a1\u591a\u5bb9\u6613\u7aef\u53e3\u51b2\u7a81\uff0c\u4e0d\u80fd\u4f7f\u7528 80\/443 \u6807\u51c6\u7aef\u53e3\uff1b<\/li>\n\n\n\n<li>\u76f4\u63a5\u66b4\u9732\u8282\u70b9\u7aef\u53e3\uff0c\u5b89\u5168\u98ce\u9669\u9ad8\uff1b<\/li>\n\n\n\n<li>NodePort \u56db\u5c42\u8f6c\u53d1\uff0c<strong>\u4e0d\u652f\u6301 HTTP\/HTTPS \u4e03\u5c42\u57df\u540d\u3001\u8def\u5f84\u8def\u7531<\/strong>\uff1b<\/li>\n\n\n\n<li>\u591a\u8282\u70b9\u9ad8\u53ef\u7528\u9700\u8981\u5916\u90e8\u8d1f\u8f7d\u5747\u8861\u914d\u5408\uff0c\u8fd0\u7ef4\u590d\u6742\uff1b<\/li>\n<\/ol>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u2705\u751f\u4ea7\u5bf9\u5916\u66b4\u9732\uff1a<strong>Ingress + ClusterIP<\/strong>\u3002<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001Ingress \u4e03\u5c42\u7f51\u5173\uff08HTTP\/HTTPS\uff09\u2b50\u91cd\u70b9<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u57fa\u672c\u6982\u5ff5<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Ingress\uff08\u8d44\u6e90\uff09<\/strong>\uff1a\u53ea\u662f\u8def\u7531\u914d\u7f6e\u89c4\u5219\u5bf9\u8c61\uff0c\u5b9a\u4e49\u57df\u540d\u3001\u8def\u5f84\u8f6c\u53d1\u89c4\u5219\uff0c<strong>\u672c\u8eab\u4e0d\u80fd\u5904\u7406\u6d41\u91cf<\/strong>\u3002<\/li>\n\n\n\n<li><strong>Ingress\u2011Controller\uff08\u63a7\u5236\u5668\uff09<\/strong>\uff1a\u771f\u6b63\u5e72\u6d3b\u7ec4\u4ef6\uff08nginx\u2011ingress\uff09\uff0c\u76d1\u542c Ingress \u89c4\u5219\uff0c\u751f\u6210 nginx \u914d\u7f6e\uff0c\u5904\u7406\u4e03\u5c42 HTTP\/HTTPS \u6d41\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u67b6\u6784\uff1aIngress \u89c4\u5219 \u2192 Ingress\u2011Controller (Pod) \u2192 \u540e\u7aef ClusterIP Service \u2192 Pod\u3002<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">Ingress \u6838\u5fc3\u4ef7\u503c\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7edf\u4e00\u5165\u53e3\uff0c\u591a\u4e2a\u670d\u52a1\u5171\u4eab 80\/443 \u7aef\u53e3\uff0c\u4e0d\u7528\u6bcf\u4e2a\u670d\u52a1\u5360\u7528 NodePort<\/strong><\/li>\n\n\n\n<li>\u4e03\u5c42\u8def\u7531\uff1a<strong>\u57fa\u4e8e\u57df\u540d host\u3001URL \u8def\u5f84 path \u5206\u53d1\u6d41\u91cf<\/strong><\/li>\n\n\n\n<li>\u539f\u751f\u652f\u6301 HTTPS \u8bc1\u4e66\u3001\u91cd\u5b9a\u5411\u3001\u8def\u5f84\u91cd\u5199\u3001\u9650\u6d41\u3001\u9ed1\u767d\u540d\u5355\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">2.IngressClass<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">K8s1.19 + \u6807\u51c6\u8d44\u6e90\uff1b\u96c6\u7fa4\u53ef\u4ee5\u90e8\u7f72\u591a\u5957\u7f51\u5173\uff08nginx\u2011ingress\u3001traefik\uff09\uff0cIngressClass \u7528\u6765\u533a\u5206\u4e0d\u540c\u63a7\u5236\u5668\uff1b\u521b\u5efa Ingress \u8d44\u6e90\u5fc5\u987b\u6307\u5b9a<code>ingressClassName<\/code>\uff0c\u544a\u8bc9\u96c6\u7fa4\u4ea4\u7ed9\u54ea\u4e2a\u7f51\u5173\u5904\u7406\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.Ingress \u4e09\u79cd\u8def\u5f84\u5339\u914d pathType<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Prefix\uff08\u524d\u7f00\u5339\u914d\uff0c\u6700\u5e38\u7528\uff09<\/strong> \u6309<code>\/<\/code>\u5206\u5272\u5b8c\u6574\u8def\u5f84\u5143\u7d20\u524d\u7f00\u5339\u914d\uff1b\u533a\u5206\u5927\u5c0f\u5199\uff1b <code>\/api<\/code>\u53ef\u4ee5\u5339\u914d<code>\/api<\/code>\u3001<code>\/api\/<\/code>\u3001<code>\/api\/v1<\/code>\uff1b<strong>\u4e0d\u80fd\u5339\u914d<code>\/ap<\/code>\u3001<code>\/api\u2011test<\/code><\/strong>\u3002<\/li>\n\n\n\n<li><strong>Exact\uff08\u7cbe\u786e\u5339\u914d\uff09<\/strong> \u8bf7\u6c42\u8def\u5f84\u5fc5\u987b\u548c\u914d\u7f6e\u5b8c\u5168\u4e00\u6a21\u4e00\u6837\uff0c\u5927\u5c0f\u5199\u3001\u672b\u5c3e\u659c\u6760\u4e25\u683c\u5339\u914d\uff1b\u9002\u5408\u767b\u5f55\u3001\u56de\u8c03\u63a5\u53e3\u3002<\/li>\n\n\n\n<li><strong>ImplementationSpecific<\/strong> \u4ea4\u7ed9 Ingress \u63a7\u5236\u5668\u81ea\u5df1\u5b9e\u73b0\u903b\u8f91\uff0c\u4e0d\u540c\u7f51\u5173\u884c\u4e3a\u4e0d\u4e00\u6837\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">4.Ingress \u5e38\u7528\u6ce8\u89e3 annotations<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u8def\u5f84\u91cd\u5199\uff0c\u628a\u5339\u914d\u5230\u7684\u524d\u7f00\u66ff\u6362\u6210\/\nnginx.ingress.kubernetes.io\/rewrite-target: \/\n#http\u81ea\u52a8\u8df3\u8f6chttps\nnginx.ingress.kubernetes.io\/ssl\u2011redirect: \"true\"\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u793a\u4f8b 1\uff1a\u5355\u57df\u540d\uff0c\u4e0d\u540c\u8def\u5f84\u8f6c\u53d1<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>apiVersion: networking.k8s.io\/v1\nkind: Ingress\nmetadata:\n  name: nginx\u2011ab\n  annotations:\n    nginx.ingress.kubernetes.io\/rewrite\u2011target: \/\nspec:\n  ingressClassName: nginx #\u6307\u5b9aingressclass\n  rules:\n  - host: xx.scyclass.com\n    http:\n      paths:\n      - path: \/wr\n        pathType: Prefix\n        backend:\n          service:\n            name: nginx\u2011a\u2011service\n            port:\n              number: 80\n      - path: \/tan\n        pathType: Prefix\n        backend:\n          service:\n            name: nginx\u2011b\u2011service\n            port:\n              number: 80\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. \u793a\u4f8b 2\uff1a\u591a\u57df\u540d host \u8f6c\u53d1<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>apiVersion: networking.k8s.io\/v1\nkind: Ingress\nmetadata:\n  name: ingress\u2011multi\u2011host\nspec:\n  ingressClassName: nginx\n  rules:\n  - host: xx.scyclass.com\n    http:\n      paths:\n      - path: \/\n        pathType: Prefix\n        backend:\n          service:\n            name: nginx\u2011a\u2011service\n            port: {number:80}\n  - host: yy.scyclass.com\n    http:\n      paths:\n      - path: \/\n        pathType: Prefix\n        backend:\n          service:\n            name: nginx\u2011b\u2011service\n            port: {number:80}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7. \u793a\u4f8b 3\uff1aHTTPS TLS \u52a0\u5bc6\u8bbf\u95ee<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">TLS \u8bc1\u4e66\u5b58\u653e\u5728<code>kubernetes.io\/tls<\/code>\u7c7b\u578b Secret\uff1bIngress \u901a\u8fc7<code>tls.secretName<\/code>\u5f15\u7528 secret\u3002<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>apiVersion: networking.k8s.io\/v1\nkind: Ingress\nmetadata:\n  name: ingress\u2011https\n  annotations:\n    nginx.ingress.kubernetes.io\/ssl\u2011redirect: \"true\"\nspec:\n  ingressClassName: nginx\n  tls:\n  - hosts:\n    - xx.scyclass.com\n    secretName: example\u2011tls\u2011secret #tls secret\u540d\u79f0\n  rules:\n  - host: xx.scyclass.com\n    http:\n      paths:\n      - path: \/fanlin\n        pathType: Prefix\n        backend:\n          service:\n            name: nginx\u2011a\u2011service\n            port: {number:80}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u751f\u6210\u81ea\u7b7e\u540d\u8bc1\u4e66\u547d\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>openssl genrsa -out tls.key 2048\nopenssl req -new -key tls.key -out tls.csr -subj \"\/C=CN\/ST=BJ\/L=BJ\/O=test\/CN=xx.scyclass.com\"\nopenssl x509 -req -days 365 -in tls.csr -signkey tls.key -out tls.crt\n#\u521b\u5efatls\u7c7b\u578bsecret\nkubectl create secret tls example\u2011tls\u2011secret --cert=tls.crt --key=tls.key\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">8.nginx\u2011ingress\u2011controller \u90e8\u7f72\u7ec4\u4ef6\u89e3\u6790<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u90e8\u7f72 yaml \u5305\u542b\u5168\u5957\u8d44\u6e90\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Namespace ingress\u2011nginx<\/strong>\uff1a\u7f51\u5173\u8d44\u6e90\u72ec\u7acb\u547d\u540d\u7a7a\u95f4<\/li>\n\n\n\n<li>ServiceAccount \u00d72\uff1a\u63a7\u5236\u5668\u8d26\u53f7\u3001webhook \u8bc1\u4e66\u4efb\u52a1\u8d26\u53f7<\/li>\n\n\n\n<li>Role\/ClusterRole + RoleBinding\/ClusterRoleBinding\uff1aRBAC \u6743\u9650\n<ul class=\"wp-block-list\">\n<li>ClusterRole \u5168\u5c40\u6743\u9650\uff1a\u76d1\u542c\u96c6\u7fa4\u6240\u6709 ns \u7684 Ingress\u3001Service\u3001Pod\u3001ConfigMap\uff0c\u5b9e\u73b0\u8de8\u547d\u540d\u7a7a\u95f4\u8def\u7531\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>ConfigMap <code>ingress\u2011nginx\u2011controller<\/code>\uff1anginx \u5168\u5c40\u914d\u7f6e\u4e2d\u5fc3\uff0c\u53ef\u4ee5\u4fee\u6539\u8d85\u65f6\u3001\u9650\u6d41\u53c2\u6570\u3002<\/li>\n\n\n\n<li>Service\uff1a\n<ul class=\"wp-block-list\">\n<li><code>ingress\u2011nginx\u2011controller<\/code>\uff1aNodePort \u7c7b\u578b\uff0c\u5916\u90e8\u6d41\u91cf\u5165\u53e3 80\/443<\/li>\n\n\n\n<li><code>ingress\u2011nginx\u2011controller\u2011admission<\/code>\uff1aClusterIP\uff0c\u51c6\u5165\u6821\u9a8c webhook<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Deployment ingress\u2011nginx\u2011controller\uff1a\u6838\u5fc3\u7f51\u5173 Pod\uff0c\u5185\u7f6e nginx\uff1b\u914d\u7f6e liveness\/readiness \u63a2\u9488\u3001\u5b89\u5168\u4e0a\u4e0b\u6587\u3001\u9886\u5bfc\u8005\u9009\u4e3e\u3001\u4f18\u96c5\u5173\u95ed preStop\u3002<\/li>\n\n\n\n<li>Job \u00d72\uff1a\u4e00\u6b21\u6027 Job\uff0c\u81ea\u52a8\u751f\u6210 webhook \u81ea\u7b7e\u8bc1\u4e66\uff0c\u6ce8\u5165 ValidatingWebhookConfiguration\u3002<\/li>\n\n\n\n<li>IngressClass <code>nginx<\/code>\uff1a\u7f51\u5173\u6807\u8bc6\u3002<\/li>\n\n\n\n<li>ValidatingWebhookConfiguration\uff1a\u51c6\u5165\u6821\u9a8c\uff0c\u62e6\u622a\u975e\u6cd5 Ingress \u914d\u7f6e\uff0c\u9632\u6b62\u7f51\u5173\u762b\u75ea\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u90e8\u7f72\u547d\u4ee4<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl apply -f ingress\u2011nginx\u2011v1.15.0.yaml\n#\u9a8c\u8bc1\nkubectl get pod,svc,ingressclasses -n ingress\u2011nginx\nkubectl get ingress\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u6d4b\u8bd5\u8bbf\u95ee\u6ce8\u610f\uff1a\u4fee\u6539\u672c\u5730 hosts\uff0c\u628a\u57df\u540d\u89e3\u6790\u5230 ingress controller \u8282\u70b9 IP\u3002<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u6c47\u603b<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Service \u548c Ingress \u533a\u522b<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Service\uff1a\u56db\u5c42 TCP\/UDP\uff1b\u57fa\u4e8e\u7aef\u53e3\u8f6c\u53d1\uff1b\u4e3b\u8981\u7528\u4e8e\u96c6\u7fa4\u5185\u90e8\u901a\u4fe1\uff1bClusterIP\/NodePort\/LoadBalancer\u3002<\/li>\n\n\n\n<li>Ingress\uff1a\u4e03\u5c42 HTTP\/HTTPS\uff1b\u57fa\u4e8e\u57df\u540d\u3001URL \u8def\u5f84\u8def\u7531\uff1b\u7edf\u4e00\u5bf9\u5916\u5165\u53e3\uff0c\u5b9e\u73b0\u57df\u540d\u8bbf\u95ee\u3001HTTPS\uff1b<strong>Ingress \u672c\u8eab\u53ea\u662f\u89c4\u5219\uff0c\u9700\u8981 Ingress\u2011Controller \u6267\u884c\u6d41\u91cf\u8f6c\u53d1<\/strong>\u3002<\/li>\n<\/ul>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Endpoints \u662f\u4ec0\u4e48\uff1f<\/strong> Endpoints \u8d44\u6e90\u4fdd\u5b58 Service selector \u5339\u914d\u5230\u7684\u540e\u7aef Pod <code>IP+port<\/code>\uff0c\u662f Service \u5230 Pod \u4e4b\u95f4\u6865\u6881\uff1b\u6839\u636e Pod \u72b6\u6001\u81ea\u52a8\u66f4\u65b0\uff0c\u7528\u6237\u4e00\u822c\u4e0d\u624b\u52a8\u4fee\u6539\u3002<\/li>\n\n\n\n<li><strong>Headless \u65e0\u5934\u670d\u52a1\u539f\u7406\u548c\u4f7f\u7528\u573a\u666f<\/strong><code>clusterIP:None<\/code>\uff0c\u4e0d\u5206\u914d\u865a\u62df ClusterIP\uff0c\u6ca1\u6709 kube\u2011proxy \u4ee3\u7406\uff1bDNS \u76f4\u63a5\u8fd4\u56de\u540e\u7aef Pod \u771f\u5b9e IP\uff1b\u914d\u5408 StatefulSet \u5b9e\u73b0\u6bcf\u4e2a Pod \u7a33\u5b9a\u57df\u540d\uff1b\u7528\u4e8e MySQL\u3001Redis \u7b49\u6709\u72b6\u6001\u96c6\u7fa4\uff0c\u5ba2\u6237\u7aef\u76f4\u8fde Pod \u5b9e\u4f8b\u3002<\/li>\n\n\n\n<li><strong>\u7528\u6237\u8bbf\u95ee K8s \u670d\u52a1\u5b8c\u6574\u6d41\u7a0b\uff08\u751f\u4ea7 Ingress \u67b6\u6784\uff09<\/strong><\/li>\n\n\n\n<li>\u7528\u6237\u6d4f\u89c8\u5668\u8f93\u5165\u57df\u540d\uff0cDNS \u89e3\u6790\u516c\u7f51 IP\uff1b<\/li>\n\n\n\n<li>\u8bf7\u6c42\u5230\u8fbe SLB\/Ingress\u2011Controller\uff1b<\/li>\n\n\n\n<li>Ingress Controller \u5339\u914d Ingress \u7684 host\/path \u8def\u7531\u89c4\u5219\uff1b<\/li>\n\n\n\n<li>\u6d41\u91cf\u8f6c\u53d1\u7ed9\u540e\u7aef ClusterIP Service\uff1b<\/li>\n\n\n\n<li>Service \u6839\u636e Endpoints\uff0ckube\u2011proxy \u56db\u5c42\u8d1f\u8f7d\u5747\u8861\u8f6c\u53d1\u5230\u540e\u7aef\u5065\u5eb7 Pod\uff1b<\/li>\n\n\n\n<li>Pod \u6267\u884c\u4e1a\u52a1\u903b\u8f91\u8fd4\u56de\u54cd\u5e94\u3002<\/li>\n\n\n\n<li><strong>\u4e3a\u4ec0\u4e48\u751f\u4ea7\u73af\u5883\u4e0d\u76f4\u63a5\u4f7f\u7528 NodePort \u66b4\u9732\u4e1a\u52a1\uff1f<\/strong> \u7aef\u53e3\u8303\u56f4\u53d7\u9650\u3001\u4e0d\u80fd\u7528\u6807\u51c6 80\/443\uff1b\u76f4\u63a5\u66b4\u9732\u8282\u70b9\u7aef\u53e3\u5b89\u5168\u98ce\u9669\uff1b\u53ea\u6709\u56db\u5c42\u8f6c\u53d1\uff0c\u4e0d\u652f\u6301\u57df\u540d\u8def\u5f84\u4e03\u5c42\u8def\u7531\uff1b\u7ba1\u7406\u6df7\u4e71\u3002\u751f\u4ea7\u4f7f\u7528 <strong>Ingress + ClusterIP Service<\/strong>\u3002<\/li>\n\n\n\n<li><code>rewrite\u2011target<\/code>\u6ce8\u89e3\u7684\u4f5c\u7528 \u5c06 Ingress \u5339\u914d\u5230\u7684 URL \u8def\u5f84\u524d\u7f00\u91cd\u5199\u4e3a<code>\/<\/code>\uff0c\u628a<code>xx.scyclass.com\/wr<\/code>\u8bf7\u6c42\u8f6c\u53d1\u7ed9\u540e\u7aef service \u7684\u65f6\u5019\u8def\u5f84\u53d8\u6210<code>\/<\/code>\uff0c\u9002\u914d\u540e\u7aef web \u670d\u52a1\u6839\u8def\u5f84\u3002<\/li>\n\n\n\n<li>IngressClass \u4f5c\u7528 \u96c6\u7fa4\u652f\u6301\u591a\u5957 Ingress \u63a7\u5236\u5668\uff0cIngressClass \u4f5c\u4e3a\u6807\u8bc6\uff1bIngress \u8d44\u6e90\u6307\u5b9a<code>ingressClassName<\/code>\uff0c\u786e\u5b9a\u4ea4\u7ed9\u54ea\u4e2a Ingress\u2011Controller \u5904\u7406\u8def\u7531\u89c4\u5219\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u6838\u5fc3\u547d\u4ee4\u6c47\u603b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#service\nkubectl get svc\nkubectl describe svc &lt;svc\u2011name&gt;\nkubectl get endpoints\n\n#ingress\nkubectl get ingress\nkubectl describe ingress &lt;ingress\u2011name&gt;\nkubectl get ingressclasses -A\n\n#\u67e5\u770bingress\u2011nginx\u7ec4\u4ef6\nkubectl get all -n ingress\u2011nginx\n\n#tls secret\u7ba1\u7406\nkubectl get secrets\nkubectl create secret tls xxx --cert=xxx.crt --key=xxx.key<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001Service \u6838\u5fc3\u6982\u5ff5 1. \u4ec0\u4e48\u662f Service Service \u662f K8s \u4e3a\u4e00\u7ec4 Pod \u63d0\u4f9b\u7a33 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-100","post","type-post","status-publish","format-standard","hentry","category-kubernetes"],"_links":{"self":[{"href":"https:\/\/www.xiaoxindong.cloud\/index.php?rest_route=\/wp\/v2\/posts\/100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xiaoxindong.cloud\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xiaoxindong.cloud\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xiaoxindong.cloud\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xiaoxindong.cloud\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=100"}],"version-history":[{"count":2,"href":"https:\/\/www.xiaoxindong.cloud\/index.php?rest_route=\/wp\/v2\/posts\/100\/revisions"}],"predecessor-version":[{"id":190,"href":"https:\/\/www.xiaoxindong.cloud\/index.php?rest_route=\/wp\/v2\/posts\/100\/revisions\/190"}],"wp:attachment":[{"href":"https:\/\/www.xiaoxindong.cloud\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xiaoxindong.cloud\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xiaoxindong.cloud\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}