美文网首页
聊聊如何变更pod的流量路由

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

作者: go4it | 来源:发表于2024-02-12 10:49 被阅读0次

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

配置

# 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

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

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

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

kubectl label pod ratings-v1-676f4d994-tg49h app=ratings2 --overwrite

查看变更

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

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

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

相关文章

  • 通过JS拦截 pushState 和 replaceState

    写在前面前几天遇到一个需求,原生js拦截vue项目中的路由变更。目的是单项目应用中,拦截路由变更,发送用户路由变更...

  • k8s网络-pod(calico)

    1. pod IP生成 分配原则:calico写入etcd,每个节点一个IP段 2. pod路由 查看pod的路由...

  • 5月05-07日工作

    字段变化 collect变更为favorite 变更路由 get_merchandise_list_by_topi...

  • 异地多活

    背景 目标 整体架构 核心技术 流量路由 前端流量路由 无Region路由 SET内流量封闭 异地容灾 SET间数...

  • 组件化设计

    路由(URL/Target-Acton/BeeHive) pod 的使用 依赖注入 pod会帮我们生成Framew...

  • 组件化思路整理

    组件化设计 1.路由(URL/Target-Action/BeeHive)2.pod的使用3.依赖注入 pod->...

  • k8s 之 kubelet 源码简单分析

    简介 kubelet 监听资源的变更,并通过容器运行时运行pod对Pod进行健康检查,并且把状态通过apiserv...

  • k8s 网络

    k8s通过cluster vip解决pod到service的路由问题,具体kube-proxy的路由引擎负责 那么...

  • 如何在Openshift中让Router Pod独占Router

    概念 什么是Router Pod? Router Pod是Openshift中管理外部流量访问集群服务的重要的入口...

  • Service Mesh - Istio流量控制篇(上)

    动态路由:用Virtual Service和Destination Rule设置路由规则 路由这个功能是流量控制里...

网友评论

      本文标题:聊聊如何变更pod的流量路由

      本文链接:https://www.haomeiwen.com/subject/ssodadtx.html