前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊如何变更pod的流量路由

聊聊如何变更pod的流量路由

原创
作者头像
code4it
发布2024-02-13 10:53:16
1200
发布2024-02-13 10:53:16
举报
文章被收录于专栏:码匠的流水账

本文主要研究一下如何变更pod的流量路由

配置

代码语言:javascript
复制
# 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: v1
kind: Service
metadata:
  name: ratings
  labels:
    app: ratings
    service: ratings
spec:
  ports:
  - port: 8080
    name: http
  selector:
    app: ratings
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: ratings-v1
  labels:
    app: ratings
    version: v1
spec:
  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          
---

kind load docker-image jvm-tools-demo kind create -f ratings.yaml

查看

endpoint

代码语言:javascript
复制
kubectl get ep
NAME         ENDPOINTS                                           AGE
kubernetes   192.168.228.2:6443                                  43m
ratings      10.244.0.10:8080,10.244.0.8:8080,10.244.0.9:8080   6m18s

svc

代码语言:javascript
复制
kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP    46m
ratings      ClusterIP   10.96.170.159   <none>        8080/TCP   9m3s

pods

代码语言:javascript
复制
kubectl get pods
NAME                         READY   STATUS    RESTARTS   AGE
ratings-v1-676f4d994-8xp7j   1/1     Running   0          9m22s
ratings-v1-676f4d994-9gbkh   1/1     Running   0          9m22s
ratings-v1-676f4d994-tg49h   1/1     Running   0          9m22s

更新label

代码语言:javascript
复制
kubectl label pod ratings-v1-676f4d994-tg49h app=ratings2 --overwrite

查看变更

代码语言:javascript
复制
kubectl describe pod ratings-v1-676f4d994-tg49h
Name:             ratings-v1-676f4d994-tg49h
Namespace:        default
Priority:         0
Service Account:  default
Node:             kind-control-plane/192.168.228.2
Start Time:       Tue, 13 Feb 2024 10:27:11 +0800
Labels:           app=ratings2
                  pod-template-hash=676f4d994
                  version=v1
Annotations:      <none>
Status:           Running
IP:               10.244.0.8
IPs:
  IP:  10.244.0.8
Containers:
  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      True
Volumes:
  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:             true
QoS Class:                   Burstable
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  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

可以看到label变更了

查看ep

代码语言:javascript
复制
kubectl get ep
NAME         ENDPOINTS                                           AGE
kubernetes   192.168.228.2:6443                                  50m
ratings      10.244.0.10:8080,10.244.0.11:8080,10.244.0.9:8080   12m

可以看到原来10.244.0.8:8080的pod由于label被更新了,所以被移除了,但因为需要保持3个副本,因而点多生成了一个pod(10.244.0.11:8080)

查看pod

代码语言:javascript
复制
kubectl get pods
NAME                         READY   STATUS    RESTARTS   AGE
ratings-v1-676f4d994-8xp7j   1/1     Running   0          13m
ratings-v1-676f4d994-9gbkh   1/1     Running   0          13m
ratings-v1-676f4d994-hpfg8   1/1     Running   0          9m6s
ratings-v1-676f4d994-tg49h   1/1     Running   0          13m

可以看到由于ratings-v1-676f4d994-tg49h的label被更新了,因而又重新生成了一个pod

小结

通过更新pod的label可以将该pod从endpoint中移除,从而使得该pod不会被svc的流量路由到。但是因为更新了label,原来app=ratings需要保持3个副本,因而会重新创建一个pod来补充。

doc

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 配置
  • 查看
    • endpoint
      • svc
        • pods
        • 更新label
          • 查看变更
            • 查看ep
              • 查看pod
              • 小结
              • doc
              相关产品与服务
              容器服务
              腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档