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:
  - nginx-gateway
  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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章