<article class=“article fmt article-content”><h2>序</h2><p>本文次要钻研一下如何变更pod的流量路由</p><h2>配置</h2><pre><code># Copyright Istio Authors## Licensed under the Apache License, Version 2.0 (the “License”);# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an “AS IS” BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.################################################################################################### Ratings service##################################################################################################apiVersion: v1kind: Servicemetadata: name: ratings labels: app: ratings service: ratingsspec: ports: - port: 8080 name: http selector: app: ratings—apiVersion: apps/v1kind: Deploymentmetadata: name: ratings-v1 labels: app: ratings version: v1spec: replicas: 3 selector: matchLabels: app: ratings version: v1 template: metadata: labels: app: ratings version: v1 spec: containers: - name: ratings image: jvm-tools-demo imagePullPolicy: IfNotPresent ports: - containerPort: 8080 securityContext: runAsUser: 1000 resources: # keep request = limit to keep this container in guaranteed class requests: cpu: 50m memory: 128Mi —</code></pre><blockquote>kind load docker-image jvm-tools-demo<br/>kind create -f ratings.yaml</blockquote><h2>查看</h2><h3>endpoint</h3><pre><code>kubectl get epNAME ENDPOINTS AGEkubernetes 192.168.228.2:6443 43mratings 10.244.0.10:8080,10.244.0.8:8080,10.244.0.9:8080 6m18s</code></pre><h3>svc</h3><pre><code>kubectl get svcNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEkubernetes ClusterIP 10.96.0.1 <none> 443/TCP 46mratings ClusterIP 10.96.170.159 <none> 8080/TCP 9m3s</code></pre><h3>pods</h3><pre><code>kubectl get podsNAME READY STATUS RESTARTS AGEratings-v1-676f4d994-8xp7j 1/1 Running 0 9m22sratings-v1-676f4d994-9gbkh 1/1 Running 0 9m22sratings-v1-676f4d994-tg49h 1/1 Running 0 9m22s</code></pre><h2>更新label</h2><pre><code>kubectl label pod ratings-v1-676f4d994-tg49h app=ratings2 –overwrite</code></pre><h3>查看变更</h3><pre><code>kubectl describe pod ratings-v1-676f4d994-tg49hName: ratings-v1-676f4d994-tg49hNamespace: defaultPriority: 0Service Account: defaultNode: kind-control-plane/192.168.228.2Start Time: Tue, 13 Feb 2024 10:27:11 +0800Labels: app=ratings2 pod-template-hash=676f4d994 version=v1Annotations: <none>Status: RunningIP: 10.244.0.8IPs: IP: 10.244.0.8Containers: ratings: Container ID: containerd://fe1d8ddc2d27c557a51181f0b4df8187fb1c06c71d8e564fe9f1ceebb480e156 Image: registry.cn-hangzhou.aliyuncs.com/springcloud-cn/jvm-tools-demo Image ID: docker.io/library/import-2024-02-13@sha256:4ed39c8b931585c67e28def544117913fddf929cff8c3062ae19c3d15fffebe7 Port: 8080/TCP Host Port: 0/TCP State: Running Started: Tue, 13 Feb 2024 10:27:12 +0800 Ready: True Restart Count: 0 Requests: cpu: 50m memory: 128Mi Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-2f9mt (ro)Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled TrueVolumes: kube-api-access-2f9mt: Type: Projected (a volume that contains injected data from multiple sources) TokenExpirationSeconds: 3607 ConfigMapName: kube-root-ca.crt ConfigMapOptional: <nil> DownwardAPI: trueQoS Class: BurstableNode-Selectors: <none>Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s node.kubernetes.io/unreachable:NoExecute op=Exists for 300sEvents: Type Reason Age From Message —- —— —- —- ——- Normal Scheduled 5m42s default-scheduler Successfully assigned default/ratings-v1-676f4d994-tg49h to kind-control-plane Normal Pulled 5m41s kubelet Container image “registry.cn-hangzhou.aliyuncs.com/springcloud-cn/jvm-tools-demo” already present on machine Normal Created 5m41s kubelet Created container ratings Normal Started 5m41s kubelet Started container ratings</code></pre><blockquote>能够看到label变更了</blockquote><h3>查看ep</h3><pre><code>kubectl get epNAME ENDPOINTS AGEkubernetes 192.168.228.2:6443 50mratings 10.244.0.10:8080,10.244.0.11:8080,10.244.0.9:8080 12m</code></pre><blockquote>能够看到原来10.244.0.8:8080的pod因为label被更新了,所以被移除了,但因为须要放弃3个正本,因此点多生成了一个pod(<code>10.244.0.11:8080</code>)</blockquote><h3>查看pod</h3><pre><code>kubectl get podsNAME READY STATUS RESTARTS AGEratings-v1-676f4d994-8xp7j 1/1 Running 0 13mratings-v1-676f4d994-9gbkh 1/1 Running 0 13mratings-v1-676f4d994-hpfg8 1/1 Running 0 9m6sratings-v1-676f4d994-tg49h 1/1 Running 0 13m</code></pre><blockquote>能够看到因为ratings-v1-676f4d994-tg49h的label被更新了,因此又从新生成了一个pod</blockquote><h2>小结</h2><p>通过更新pod的label能够将该pod从endpoint中移除,从而使得该pod不会被svc的流量路由到。然而因为更新了label,原来app=ratings须要放弃3个正本,因此会从新创立一个pod来补充。</p><h2>doc</h2><ul><li>应用kind在mac本地搭建k8s及istio</li><li>istio流量路由小试牛刀</li><li>Kubernetes之Label</li></ul></article>