Istio组件IngressGateway

创建Deployment,SVC

1.创建Deployment

注:它们Pod标签都有app: nginx,service服务发现根据这个标签选择,version是为后面定义版本设置的

kubectl apply -f nginx-deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-v1
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
      version: v1
  template:
    metadata:
      name: nginx-v1
      labels:
        app: nginx
        version: v1
    spec:
      containers:
      - name: nginx-v1
        image: linuxwei/nginx_test:v1
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        env:
        - name: VERSION
          value: v3
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-v2
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
      version: v2
  template:
    metadata:
      name: nginx-v2
      labels:
        app: nginx
        version: v2
    spec:
      containers:
      - name: nginx-v2
        image: linuxwei/nginx_test:v1-2
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        env:
        - name: VERSION
          value: v3
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-v3
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
      version: v3
  template:
    metadata:
      name: nginx-v3
      labels:
        app: nginx
        version: v3
    spec:
      containers:
      - name: nginx-v3
        image: linuxwei/nginx_test:v3
        imagePullPolicy: IfNotPresent
        env:
        - name: VERSION
          value: v3
        ports:
        - containerPort: 80

 

2.创建svc

kubectl apply -f nginx-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  selector:
    app: nginx
  type: ClusterIP
  ports:
    - targetPort: 80
      port: 80
      name: web

 

创建Gateway

1.创建HTTPS证书的secret

kubectl create -n istio-system secret generic all-test.com-credential --from-file=key=private.key --from-file=cert=full_chain.pem

2.创建网关

kubectl apply -f test-gateway.yaml

apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
  name: nginx-gateway
  namespace: istio-system
spec:
  selector:
    istio: ingressgateway
  servers:
  - hosts:
    - '*.test.com'
    port:
      name: http
      number: 80
      protocol: HTTP
  - hosts:
    - '*.test.com'
    port:
      name: https
      number: 443
      protocol: HTTPS
    tls:
      credentialName: all-test.com-credential
      mode: SIMPLE

根据域名进行路由分发

1.根据Pod的version标签进行版本分类(v1,v2,v3)

v3访问路径project/index.html

kubectl apply -f nginx-destinationrule.yaml

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: nginx
spec:
  host: nginx
  trafficPolicy:
    loadBalancer:
      simple: RANDOM
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2
  - name: v3
    labels:
      version: v3

2.创建路由分发策略

kubectl apply -f test1-virtualservice.yaml

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: nginxvs
spec:
  hosts:
  - "test1.test.com"
  gateways:
  - istio-system/nginx-gateway       #注意不在一个namespace时前面要加gateway所在的namesapce
  http:
  - match:
    - uri:
        prefix: /
    route:
    - destination:
        host: nginx
        port:
          number: 80
        subset: v1

kubectl apply -f test2-virtualservice.yaml

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: nginxvs2
spec:
  hosts:
  - "test2.test.com"
  gateways:
  - nginx-gateway
  http:
  - match:
    - uri:
        prefix: /
    route:
    - destination:
        host: nginx
        port:
          number: 80
        subset: v2

3.配置本地hosts解析

vim /etc/hosts

(ingressgatewayIP)        test1.test.com

(ingressgatewayIP)         test2.test.com

分别访问这两个域名流量对应转到不同版本的Pod

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Istio IngressGateway 的范例: ```yaml apiVersion: networking.istio.io/v1alpha3 kind: Gateway metadata: name: my-gateway spec: selector: istio: ingressgateway # 使用默认的 Istio IngressGateway servers: - port: number: 80 name: http protocol: HTTP hosts: - myapp.example.com # 定义需要路由的域名 ``` 在这个范例中,我们定义了一个名为 `my-gateway` 的 `Gateway` 对象,使用默认的 Istio IngressGateway。我们通过 `servers` 字段定义了一个监听 `80` 端口的 HTTP 服务,并且指定了需要路由的域名 `myapp.example.com`。 接下来,我们需要创建一个 `VirtualService` 对象,来定义路由规则: ```yaml apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: my-virtual-service spec: hosts: - myapp.example.com gateways: - my-gateway http: - match: - uri: prefix: / route: - destination: host: myapp-service # 定义后端的服务名 port: number: 8080 # 定义后端的端口 ``` 在这个范例中,我们定义了一个名为 `my-virtual-service` 的 `VirtualService` 对象,使用了之前定义的 `my-gateway`。我们通过 `hosts` 字段指定了需要路由的域名 `myapp.example.com`,并且通过 `http` 字段定义了路由规则,匹配所有的 URL 前缀 `/`,并且将这些请求转发到名为 `myapp-service` 的后端服务的 `8080` 端口。 总的来说,Istio IngressGateway 的范例需要定义两个对象:`Gateway` 和 `VirtualService`,其中 `Gateway` 定义了监听的端口和需要路由的域名,而 `VirtualService` 定义了具体的路由规则和转发的后端服务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值