istio Gateway 设置路由

总索引目录:istio 从入门到放弃系列

1、命名空间注入sidecar

kubectl label namespaces test istio-injection=enabled

kubectl get ns test --show-labels

2、资源文件准备

2.1  Deployment和Service

apiVersion: v1
kind: Service
metadata:
  name: test-service
  namespace: test
  labels:
    app: test
spec:
  ports:
  - name: http
    nodePort: 
    port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    app: test
  sessionAffinity: None
  type: NodePort
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: test-v1
  namespace: test
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: test
        version: v1
    spec:
      containers:
      - name: test
        image: docker.io/kubeguide/tomcat-app:v1
        imagePullPolicy: IfNotPresent #Always
        ports:
        - containerPort: 8080

创建deployment和service

2.2 gateway

首先,需要为服务网格启用HTTP/HTTPS流量。 为此,我们需要创建一个Gateway。 Gateway描述了在网络边缘运行的负载均衡器,用于接收传入或传出的HTTP / TCP连接。

让我们创建一个test-gateway.yaml文件:

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: test-gateway
  namespace: test
spec:
  selector:
    istio: ingressgateway # use istio default controller
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "*"

创建Gateway

已经为集群启用了HTTP流量。 需要将之前创建的Kubernetes服务映射到Gateway。将使用VirtualService执行此操作。

2.3 VirtualService

    VirtualService实际上将Kubernetes服务连接到Istio网关。它还可以执行更多操作,例如定义一组流量路由规则,以便在主机被寻址时应用

创建一个test-virtualservice.yaml文件:

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: test-virtualservice
  namespace: test
spec:
  hosts:
  - "*"
  gateways:
  - test-gateway
  http:
  - route:
    - destination:
        host: test-service

创建VirtualService,请注意,VirtualService与特定网关绑定,并定义引用Kubernetes服务的主机。

测试app v1版本

现在可以开始测试应用了,首先需要拿到Istio Ingress Gateway的外部端口。

kubectl get svc istio-ingressgateway -n istio-system

image-20200330111235863.png

image-20200330111255489.png

2.4 DestinationRule

在某些时候,希望将应用更新为新版本。 也许想分割两个版本之间的流量。 您需要创建一个DestinationRule来定义那些版本,在Istio中称为子集。

首先,更新test.yaml文件,用v2版本的容器来定义v2的部署(Deployment)

apiVersion: v1
kind: Service
metadata:
  name: test-service
  namespace: test
  labels:
    app: test
spec:
  ports:
  - port: 8080
    name: http
  selector:
    app: test
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: test-v1
  namespace: test
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: test
        version: v1
    spec:
      containers:
      - name: test
        image: docker.io/kubeguide/tomcat-app:v1
        imagePullPolicy: Always #IfNotPresent
        ports:
        - containerPort: 8080
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: test-v2
  namespace: test
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: test
        version: v2
    spec:
      containers:
      - name: test
        image: tomcat:8.0
        imagePullPolicy: IfNotPresent #Always
        ports:
        - containerPort: 8080

创建一个新的部署(Deployment),如果刷新浏览器,你可以看到VirtualService 在v1 和v2 版本之间切换:

image-20200330114434642.png

image-20200330114459072.png

如果您想将服务仅限于v2该怎么办? 可以通过在VirtualService中指定子集来完成,但需要首先在DestinationRules中定义这些子集。 DestinationRule本质上将标签映射到Istio子集。

创建一个test-destinationrule.yaml文件:

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: test-destinationrule
  namespace: test
spec:
  host: test-service
  trafficPolicy:
    tls:
      mode: ISTIO_MUTUAL
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2

创建DestinnationRule,可以在VirtualService指向v2子集:

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: test-virtualservice
  namespace: test
spec:
  hosts:
  - "*"
  gateways:
  - test-gateway
  http:
  - route:
    - destination:
        host: test-service
        subset: v2

更新VirtualService,现在再刷新浏览器,你应该只会看到v2版本的内容了

image-20200330114936181.png

猜你喜欢

转载自blog.51cto.com/14268033/2485599