备考ICA----Istio实验2---Istio Gateway 和 VirtualService 实验

备考ICA----Istio实验2---Istio Gateway 和 VirtualService 实验

1. 部署helloworld app

hellowworld app主要是由一个svc加2个deployment组成

2个deployment都拥有一个app=helloworld的标签,还各自拥有自己版本的version,分别是v1和v2

service通过app=helloworl将流量分发给v1和v2

当两个deployment中pod个数一样时,那么大概会各自获取到50%的流量.当放大或缩小某个deployment中pod数量,可以大致的实现不同比例的流量分发到2个版本上,这也是早期金丝雀的一种用法.

yaml内容如下

yaml 复制代码
apiVersion: v1
kind: Service
metadata:
  name: helloworld
  labels:
    app: helloworld
    service: helloworld
spec:
  ports:
  - port: 5000
    name: http
  selector:
    app: helloworld
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: helloworld-v1
  labels:
    app: helloworld
    version: v1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: helloworld
      version: v1
  template:
    metadata:
      labels:
        app: helloworld
        version: v1
    spec:
      containers:
      - name: helloworld
        image: docker.io/istio/examples-helloworld-v1
        resources:
          requests:
            cpu: "100m"
        imagePullPolicy: IfNotPresent #Always
        ports:
        - containerPort: 5000
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: helloworld-v2
  labels:
    app: helloworld
    version: v2
spec:
  replicas: 1
  selector:
    matchLabels:
      app: helloworld
      version: v2
  template:
    metadata:
      labels:
        app: helloworld
        version: v2
    spec:
      containers:
      - name: helloworld
        image: docker.io/istio/examples-helloworld-v2
        resources:
          requests:
            cpu: "100m"
        imagePullPolicy: IfNotPresent #Always
        ports:
        - containerPort: 5000
bash 复制代码
kubectl apply -f istio/samples/helloworld/helloworld.yaml
bash 复制代码
kubectl get pods --show-labels |grep helloworld

测试通过访问svc的地址可以看到和我们预期的结果差不多

bash 复制代码
kubectl get svc
for i in {1..20};do curl $(kubectl get svc helloworld|grep helloworld|awk '{print $3}'):5000/hello;sleep 0.5 ;done

2. 发布 Gateway 和 VirtualService

istio/samples/helloworld/helloworld-gateway.yaml

在ingressgateway的80端口监听,当所有hosts的流量匹配uri: /hello如果匹配上那么将所有流量转发给helloworld:5000的endpoint进行响应

yaml 复制代码
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: helloworld-gateway
spec:
  selector:
    istio: ingressgateway # use istio default controller
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: helloworld
spec:
  hosts:
  - "*"
  gateways:
  - helloworld-gateway
  http:
  - match:
    - uri:
        exact: /hello
    route:
    - destination:
        host: helloworld
        port:
          number: 5000
bash 复制代码
kubectl apply -f istio/samples/helloworld/helloworld-gateway.yaml

Gateway和Virtualservice被创建

3. 访问测试

此时可以通过ingressgateway访问到hello的内容,通过刷新可以分别读取到v1和v2

v1

v2

当多次curl时可以看到流量被随机分配到2个版本上

bash 复制代码
for i in {1..20};do curl http://192.168.126.220/hello ;sleep .5 ;done

当访问量足够大时,流量分配趋近于1:1

相关推荐
眠りたいです26 分钟前
基于脚手架微服务的视频点播系统-播放控制部分
c++·qt·ui·微服务·云原生·架构·播放器
程序猿阿伟5 小时前
《云原生微服务治理进阶:隐性风险根除与全链路能力构建》
微服务·云原生·架构
Sweety丶╮7948 小时前
【Ansible】实施 Ansible Playbook知识点
服务器·云原生·ansible
wp909011 小时前
Docker命令大全
docker·云原生·eureka
xiao-xiang11 小时前
kubernetes-lxcfs解决资源可见性问题
云原生·容器·kubernetes
向上的车轮12 小时前
云原生TodoList Demo 项目,验证云原生核心特性
云原生
喂完待续12 小时前
【序列晋升】28 云原生时代的消息驱动架构 Spring Cloud Stream的未来可能性
spring cloud·微服务·云原生·重构·架构·big data·序列晋升
jzzy_hony12 小时前
云原生:微服务与Serverless指南
微服务·云原生·serverless
roman_日积跬步-终至千里12 小时前
【软件架构设计(23)】云计算与云原生技术
云原生·云计算
静若繁花_jingjing14 小时前
云原生部署_k8s入门
云原生·容器·kubernetes