部署Dotnet Core应用到Kubernetes(二)
<div id="output_wrapper_id" class="output_wrapper" style="font-size: 16px; color: rgba(62, 62, 62, 1); line-height: 1.6; word-spacing: 0; letter-spacing: 0; font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif"><blockquote style="line-height: inherit; display: block; padding: 15px 15px 15px 1rem; font-size: 0.9em; margin: 1em 0; color: rgba(129, 145, 152, 1); border-left: 6px solid rgba(220, 230, 240, 1); background-color: rgba(242, 247, 251, 1); overflow: auto; word-wrap: normal; word-break: normal">
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 0">前一篇文章,概念性地介绍了K8s的一些基础组件,如Pod、部署和服务。这篇文章,我打算写写如何使用YAML清单定义和配置这些资源。</p>
</blockquote>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">实际上,在K8s集群中创建对象有几种方式 - 命令,或声明。两种方式区别不大。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">不过实际应用中,一旦开始真正部署应用,最终都会走到YAML配置文件方式。这种方式也叫配置清单。每个资源类型的清单,例如部署、服务、入口,他们的格式会略有不同,尽管他们之间存在着共性。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这篇文章,我就着重写写如何使用YAML清单定义和配置这些资源。当然,涉及内容太多,我们还是面向实际应用,写写通用的,和最有针对性的配置格式,而不详细解释所有的配置选项和排列。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"><em style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; font-style: italic"> <span style="font-size: small; color: inherit; line-height: inherit; margin: 0; padding: 0">为防止非授权转发,这儿给出本文的原文链接:https://www.cnblogs.com/tiger-wang/p/13996662.html</span></em></p>
<h1 id="hpod" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">Pod和部署的配置清单</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">前文我们说过,Pod是K8s最小的可部署单元。我们部署Pod,不是部署单个容器,而是部署包含一个或多个容器的Pod。这个概念一定要清楚。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">K8s中部署独立的Pod是可以的。但在实际应用中,最常见的做法是将Pod部署成为“部署资源”的一部分。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">所以,通常我们的做法,是在部署清单中定义Pod资源,而不是创建一个Pod,然后创建一个部署来管理他们。换句话说,我们是创建了一个知道如何为我们创建Pod的部署。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">我们先来看一个相对基础的部署清单:</p>
<pre><code class="yaml language-yaml" style="padding: 2px 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1); line-height: 15px; font-size: 11px; font-weight: normal; word-spacing: -3px; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br> name: nginx-deployment<br> labels:<br> app: nginx<br>spec:<br> replicas: 3<br> strategy: <br> type: RollingUpdate<br> rollingUpdate:<br> maxUnavailable: 1<br> maxSurge: 1<br> maxUnavailable: 1<br> selector:<br> matchLabels:<br> app: nginx<br> template:<br> metadata:<br> labels:<br> app: nginx<br> spec:<br> containers:<br> - name: nginx<br> image: nginx:1.19.3<br> ports:<br> - containerPort: 80<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">YAML对空格敏感,所以写的时候一定要注意缩进。</p>
<h2 id="h1apiversionkindmetadata" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.4em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">1. apiVersion、kind、metadata</span></h2>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这是K8s配置清单的开头,每个清单都会有这三个项。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<pre><code class="yaml language-yaml" style="padding: 2px 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1); line-height: 15px; font-size: 11px; font-weight: normal; word-spacing: -3px; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">apiVersion: apps/v1<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">apiVersion用来指示K8s API的版本。K8s有很多个API版本。以下命令可以查询已经安装的全部版本。</p>
<pre><code class="bash language-bash hljs" style="margin: 0 2px; line-height: 15px; font-size: 11px; font-weight: normal; word-spacing: -3px; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; overflow-x: auto; background-color: rgba(254, 251, 236, 1); color: rgba(110, 107, 94, 1); padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">% kubectl api-versions<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">K8s的每个版本都包含了不同的API资源版本。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">大多数情况下,配置应用时不需要太多关注这个版本号,对应好就成。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<pre><code class="yaml language-yaml" style="padding: 2px 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1); line-height: 15px; font-size: 11px; font-weight: normal; word-spacing: -3px; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">kind: Deployment<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">定义清单的类型。前面说了,各种资源都有不同的配置清单。本例中是部署。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<pre><code class="yaml language-yaml" style="padding: 2px 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1); line-height: 15px; font-size: 11px; font-weight: normal; word-spacing: -3px; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">metadata:<br> name: nginx-deployment<br> labels:<br> app: nginx<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">元数据提供了诸如资源名称以及附加到资源上的标签等细节。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">需要注意一下:标签是与资源相关的键值对,后面我们会用到。本例中,我们只有一个标签app,值是nginx。</p>
<h2 id="h2spec" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.4em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">2. spec</span></h2>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这是K8s清单中的规范资源部分,这部分才是实际指定类型的资源定义,在本例中,是部署(kind: Deployment)的配置。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<pre><code class="yaml language-yaml" style="padding: 2px 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1); line-height: 15px; font-size: 11px; font-weight: normal; word-spacing: -3px; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"> replicas: 3<br> strategy: <br> type: RollingUpdate<br> rollingUpdate:<br> maxUnavailable: 1<br> maxSurge: 1<br> maxUnavailable: 1<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">副本和策略,定义了部署应该创建多少个Pod实例,以及创建他们应该使用的方法。本例中,我们希望K8s同时运行三个Pod,并且部署新版本时使用滚动更新。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">在策略中,我们设置maxUnavilable为1,这是更新策略的一个额外设置,表示我们希望在发布部署新版本时,在需要用新版本替换旧版本的Pod过程中,或者开始新Pod之前,每次只删除一个旧Pod。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">maxSurge和maxUnavailable也是额外设置。maxSurge控制滚动更新时一次应该添加多少额外的容器,而maxUnavilable则定义了一个容器要运行多少秒后才可被认为是可用的。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<pre><code class="yaml language-yaml" style="padding: 2px 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1); line-height: 15px; font-size: 11px; font-weight: normal; word-spacing: -3px; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"> selector:<br> matchLabels:<br> app: nginx<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">selector部分定义这个部署管理哪些Pod。这里面涉及到元数据和选择器的相互配合,我们后面会说到。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<pre><code class="yaml language-yaml" style="padding: 2px 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1); line-height: 15px; font-size: 11px; font-weight: normal; word-spacing: -3px; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"> template:<br> metadata:<br> labels:<br> app: nginx<br> spec:<br> containers:<br> - name: nginx<br> image: nginx:1.19.3<br> ports:<br> - containerPort: 80<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">部署的spec节的最后一个部分是模板。这是一个清单内的清单,里面也同样有metadata和spec。这里的内容实际是一个Pod的定义配置清单。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">因此,这整个部署清单定义了两种类型的资源:Pod,和用于管理Pod扩展和生命周期的部署。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">上面这个Pod模板,定义了一个叫nginx的容器,是由nginx 1.19.3镜像构建的。这里containerPort定义了容器内公开的端口。默认80,可不写。不过我自己觉得,写出来是一个好习惯。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">如果我们想在Pod中部署两个容器,则需要在containers下添加另一个:</p>
<pre><code class="yaml language-yaml" style="padding: 2px 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1); line-height: 15px; font-size: 11px; font-weight: normal; word-spacing: -3px; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">template:<br> metadata:<br> labels:<br> app: nginx<br> spec:<br> containers:<br><br> - name: nginx<br> image: nginx:1.19.3<br> ports:<br> - containerPort: 80<br><br> - name: nginx1<br> image: nginx:1.19.3<br> ports:<br> - containerPort: 8080<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">例子中,我们添加的第二个容器,名称叫nginx1,也是nginx 1.19.3镜像,端口在8080。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">上面就是部署清单的基本内容。这部分内容很多,需要了可以查K8s相关的部分。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">前面说了,K8s清单不同类型的规范会稍有不同,但他们的共同之处也很多,比如元数据和选择器。所以学通了一个,其它基本上也就通了。</p>
<h1 id="h" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">标签和选择器的配合</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">标签是K8s中最广泛使用的一个概念,是与对象关联的一个键值对。每个对象都可以有几个不同的标签,但每个键只能定义一次。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">看前边的例子:</p>
<pre><code class="yaml language-yaml" style="padding: 2px 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1); line-height: 15px; font-size: 11px; font-weight: normal; word-spacing: -3px; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">metadata:<br> name: nginx-deployment<br> labels:<br> app: nginx<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">有一个键名叫app,值是nginx。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">同样,在spec的template中:</p>
<pre><code class="yaml language-yaml" style="padding: 2px 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1); line-height: 15px; font-size: 11px; font-weight: normal; word-spacing: -3px; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">spec:<br> template:<br> metadata:<br> labels:<br> app: nginx<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这里,我们向Pod添加了同样的键值对。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">在需要的情况下,我们还可以添加其它的标签,例如:</p>
<pre><code class="yaml language-yaml" style="padding: 2px 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1); line-height: 15px; font-size: 11px; font-weight: normal; word-spacing: -3px; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">metadata:<br> labels:<br> app: nginx<br> environment: staging<br> system: frontend<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这两个标签的用处,可以自己去查。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">标签在命令行管理的时候非常有用。例如下面的命令:</p>
<pre><code class="bash language-bash hljs" style="margin: 0 2px; line-height: 15px; font-size: 11px; font-weight: normal; word-spacing: -3px; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; overflow-x: auto; background-color: rgba(254, 251, 236, 1); color: rgba(110, 107, 94, 1); padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">kubectl get pods -l environment=staging,system=frontend<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">可以查出所有具有staging和frontend标签的Pod。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">标签在这里,主要是提供附加信息。所以,实际中可以加上各种全适的标签。同样,在选择器定义时,也可以通过这些标签来区分不同的资源。例如:</p>
<pre><code class="yaml language-yaml" style="padding: 2px 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1); line-height: 15px; font-size: 11px; font-weight: normal; word-spacing: -3px; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">selector:<br> matchLabels:<br> app: nginx<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这里面,选择器定义了所选资源的条件。满足条件的资源,才会被匹配。例如,上面这个选择器将匹配所有具有app标签且值为nginx的Pod。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">同样,对应上面的命令行,选择器可以写为:</p>
<pre><code class="yaml language-yaml" style="padding: 2px 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1); line-height: 15px; font-size: 11px; font-weight: normal; word-spacing: -3px; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">selector:<br> matchLabels:<br> environment: staging<br> system: frontend<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这儿要注意:这个匹配的全条件的,条件之间是与的关系。当然,还有一些资源可以支持更多的匹配表达式,例如:In、NotIn、Exists或DoesNotExist。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">除了部署外,服务也使用相同的机制来定义它将数据传递给哪个Pod。</p>
<h1 id="h-1" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">服务的配置清单</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">在前文中说过,服务可以看作集群内的负载均衡,负责隔离并转发向Pod发出的请求。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">我们来看一个清单的例子:</p>
<pre><code class="yaml language-yaml" style="padding: 2px 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1); line-height: 15px; font-size: 11px; font-weight: normal; word-spacing: -3px; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">apiVersion: v1<br>kind: Service<br>metadata:<br> name: my-shop-backend<br> labels: <br> app: my-shop<br> system: backend<br>spec:<br> type: ClusterIP<br> selector:<br> app: my-shop<br> system: backend<br> ports:<br> - port: 80<br> targetPort: 8080<br> protocol: TCP<br></code></pre>
<h2 id="h1apiversionkindmetadata-1" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.4em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">1. apiVersion、kind、metadata</span></h2>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">跟前边一样,先定义清单的资源类型,这回是服务,以及所需的API版本。元数据为服务指定了一个名称,并加了两个标签用来记录和提供信息。</p>
<h2 id="h2spec-1" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.4em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">2. spec</span></h2>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">首先,需要定义服务类型。根据K8s的版本,这儿有4种选择:</p>
<ul style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0 0 0 32px; list-style-type: disc">
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0 0 0.5em; padding: 0"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">ClusterIP,默认值,定义服务只能从内部访问;</span></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0 0 0.5em; padding: 0"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">NodePort,在每个节点自动生成的端口上公开服务;</span></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0 0 0.5em; padding: 0"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">loadbalance,在连接AWS、Azure等云服务时,提供负载均衡;</span></li>
<li style="font-size: inherit; color: inherit; line-height: inherit; margin: 0 0 0.5em; padding: 0"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">ExternalName,这个有点特殊,它不做代理或转发,而只提供内部的DNS服务。</span></li>
</ul>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">实际应用中,通常就用默认的ClusterIP,把服务作为内部负载均衡器,并使用入口将外部请求转发到服务的结构就OK了。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">下一个是选择器,这个配置具体控制负载均衡的实现配置。如前一节所写的,选择器将选择所有具有所需标签的Pod,在本例中是app:my-shop和system:backend。服务将把所有传入的请求路由到这些服务。看下面的部分:</p>
<pre><code class="yaml language-yaml" style="padding: 2px 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1); line-height: 15px; font-size: 11px; font-weight: normal; word-spacing: -3px; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">spec:<br> selector:<br> app: my-shop<br> system: backend<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">最后,是清单中的端口配置。例子中定义了服务将被公开的端口80以及要使用的协议,本例中是TCP。targetPort是Pod上的端口,表示请求将会被传发到8080。这一段单摘出来如下:</p>
<pre><code class="yaml language-yaml" style="padding: 2px 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1); line-height: 15px; font-size: 11px; font-weight: normal; word-spacing: -3px; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">spec:<br> ports:<br> - port: 80<br> targetPort: 8080<br> protocol: TCP<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这个部分因为涉及到网络,所以配置可以很复杂。必要时,可以参考K8s的文档。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">在应用中,通常需要向外部公开某种HTTP服务。部署部分处理应用程序Pod的管理,服务提供多个Pod内部的负载均衡,而入口将应用暴露给外部。</p>
<h1 id="h-2" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">入口的配置清单</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">前文中说过,入口充当内部K8s服务的外部负载均衡或反向代理。下面的例子展示了如何公开内部服务到外网:</p>
<pre><code class="yaml language-yaml" style="padding: 2px 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1); line-height: 15px; font-size: 11px; font-weight: normal; word-spacing: -3px; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">apiVersion: extensions/v1beta1<br>kind: Ingress<br>metadata:<br> name: my-shop-backend-ingress<br> labels: <br> app: my-shop<br> system: backend<br> annotations:<br> nginx.ingress.kubernetes.io/rewrite-target: /<br>spec:<br> rules:<br> - http:<br> host: api.my-shop.com<br> paths:<br> - path: /my-shop<br> backend:<br> serviceName: my-shop-backend<br> servicePort: 80<br></code></pre>
<h2 id="h1apiversionkindmetadata-2" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.4em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">1. apiVersion、kind、metadata</span></h2>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">同样,入口清单也包含API版本、类型和元数据。不同的是,入口的元数据还包含入口的名称以及关联对象的标签。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">此外,还多了一个注释标签annotations。注释标签的工作原理与前面提到的标签很相似,也都是键值对。不同的是注释标签不能用来选择对象。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">本例中,我们添加了注释nginx.ingress.kubernetes.io/rewrite-target,值为/。这个注释用到了Nginx入口控制器的一个特殊键值,用来告诉控制器重写传入请求的匹配路径为“/”。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">例如,在以下路径收到一个请求:</p>
<pre><code class="http language-http hljs" style="margin: 0 2px; line-height: 15px; font-size: 11px; font-weight: normal; word-spacing: -3px; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; overflow-x: auto; background-color: rgba(254, 251, 236, 1); color: rgba(110, 107, 94, 1); padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">/<span class="hljs-attribute" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(215, 55, 55, 1); word-wrap: inherit !important; word-break: inherit !important">my-shop/orders/123<br></span></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">入口控制器将重写到:</p>
<pre><code class="http language-http hljs" style="margin: 0 2px; line-height: 15px; font-size: 11px; font-weight: normal; word-spacing: -3px; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; overflow-x: auto; background-color: rgba(254, 251, 236, 1); color: rgba(110, 107, 94, 1); padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">/<span class="hljs-attribute" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(215, 55, 55, 1); word-wrap: inherit !important; word-break: inherit !important">orders/123<br></span></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">另外,为了使用入口,集群中需要部署一个入口控制器。每个入口控制器会有不同的特性,都需要不同的注释来配置。因此,入口配置清单可能会根据不同的作用而有所不同。</p>
<h2 id="h2spec-2" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.4em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">2. spec</span></h2>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">入口清单的spec部分包含配置由集群的入口控制器管理的反向代理所需要的全部规则。如果你用过Nginx、IIS或其它类似的反代,那应该会容易理解,规则很相似,都是定义了一组传入请求的规则,以决定将他们路由到什么路径。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">入口清单可以定义多种规则,但一定要记着可部署单元的概念。我们应该为部署到K8s的每个应用配置单一入口,而不是跨应用去配置入口,尽管这样做技术上支持。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">看上面例子的spec段:</p>
<pre><code class="yaml language-yaml" style="padding: 2px 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1); line-height: 15px; font-size: 11px; font-weight: normal; word-spacing: -3px; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">spec:<br> rules:<br> - http:<br> host: api.my-shop.com<br> paths:<br> - path: /my-shop<br> backend:<br> serviceName: my-shop-backend<br> servicePort: 80<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">通常我会习惯用主机来区分不同的应用,像例子中的api.my-shop.com。当然,您也可以通过匹配路径来定义,例如/my-shop。而这个会根据元数据中的重写定义注释被重写。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">除了匹配传入的路径和主机外,清单中还需要定义请求转发的内部服务和端口。本例中,我们用了同样的80端口去路由到上一节中定义的后端服务中。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">上面这个例子是最常见的一个入口清单定义,只将传入的请求与单个服务匹配。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">有时候,可能会几种不同类型的Pod组成一个服务“微单元”。这种情况下,也可能会有几个K8s服务,并且基于路径将几个服务路由到单一入口内,这时候可以这样配置:</p>
<pre><code class="yaml language-yaml" style="padding: 2px 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1); line-height: 15px; font-size: 11px; font-weight: normal; word-spacing: -3px; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">spec:<br> rules:<br> - http:<br> paths:<br> - path: /orders<br> backend:<br> serviceName: my-shop-orders-service<br> servicePort: 80<br> - http:<br> paths:<br> - path: /history<br> backend:<br> serviceName: my-shop-order-history-backend<br> servicePort: 80<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">在K8s配置时,入口中可以加入很多配置。这部分如果有问题,可以去翻K8s文档。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">如果只是需要将应用部署到K8s,那上边的内容基本够用了。唯一要注意的是集群中用了哪个入口控制器。</p>
<h1 id="h-3" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">总结一下</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这篇文章是这个系列的第二篇。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这篇文章介绍了我们必须要知道的K8s的主要资源:Pod、部署、服务、入口的YAML基本配置,并列出了一些最重要的配置值。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">K8s这个东西,做为一个大的分布式架构,肯定是比较复杂的。但实际应用中,不是每个内容都需要理解和应用。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">做多了,就是套路。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">蹚过了,就是经验。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">(未完待续)</p>
<p> </p>
<hr>
<p> </p>
<table border="0">
<tbody>
<tr>
<td><img src="https://img2020.cnblogs.com/blog/907112/202005/907112-20200527230728396-985375280.jpg"></td>
<td>
<p>微信公众号:老王Plus</p>
<p>扫描二维码,关注个人公众号,可以第一时间得到最新的个人文章和内容推送</p>
<p>本文版权归作者所有,转载请保留此声明和原文链接</p>
</td>
</tr>
</tbody>
</table>
</div><br><br>
来源:https://www.cnblogs.com/tiger-wang/p/13996662.html
頁:
[1]