Myluzh Blog

K8S istio-ingressgateway 简单的路由实例演示

发布时间: 2024-7-8 文章作者: myluzh 分类名称: Kubernetes 朗读文章


0x01 前置条件
默认已经安装好了istio,并且已经有了确保已经安装 Istio 并配置了 istio-ingressgateway。由于我已经之前安装了ingress-nginx 已占用 80 和 443 端口。所以我 istio-ingressgateway 从默认的 LoadBalancer 类型改为 NodePort 类型。istio-ingressgateway 使用端口 http 30058 和 https 30605
root@iZbp12bkuvg20e1j3y9gtxZ:~/# kubectl get svc -n istio-system | grep ingress
istio-ingressgateway   NodePort    10.43.160.137   <none>        15021:31016/TCP,80:30058/TCP,443:30605/TCP,31400:32117/TCP,15443:32636/TCP   87d

0x02 实例演示
1、创建命名空间并自动注入边车代理
kubectl create ns myluzh
kubectl label namespace default istio-injection=enabled
2、部署应用和服务
部署名为 "hellok8s" 的web应用,访问页面会输出 "hellok8s!"。
# kubectl apply -f hellok8s -n myluzh
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hellok8s
spec:
  replicas: 4
  selector:
    matchLabels:
      app: hellok8s
  template:
    metadata:
      labels:
        app: hellok8s
    spec:
      containers:
        - name: hellok8s
          imagePullPolicy: Always
          image: registry.cn-hangzhou.aliyuncs.com/myluzh/hellok8s:v3
---
apiVersion: v1
kind: Service
metadata:
  name: hellok8s
spec:
  selector:
    app: hellok8s
  type: NodePort
  ports:
    - port: 80
      targetPort: 80
      nodePort: 31000
查看应用是否正常运行:
root@iZbp12bkuvg20e1j3y9gtxZ:~/myluzh# kubectl get deployment,svc -n myluzh
NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/hellok8s   3/4     4            3           4m36s

NAME               TYPE       CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
service/hellok8s   NodePort   10.43.189.6   <none>        80:31000/TCP   4m36s
3、配置Gateway跟VirtualService
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: gw-hellok8s
  namespace: myluzh
spec:
  selector:
    istio: ingressgateway # 使用 Istio IngressGateway
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: vs-hellok8s
  namespace: myluzh
spec:
  hosts:
  - "*"
  gateways:
  - gw-hellok8s
  http:
  - match:
    - uri:
        prefix: "/"
    route:
    - destination:
        host: hellok8s # 应用svc名称
        port:
          number: 80
          protocol: HTTP
验证是否生效:
root@iZbp12bkuvg20e1j3y9gtxZ:~/myluzh# kubectl  get gw,vs -n myluzh
NAME                                      AGE
gateway.networking.istio.io/gw-hellok8s   24m

NAME                                             GATEWAYS          HOSTS   AGE
virtualservice.networking.istio.io/vs-hellok8s   ["gw-hellok8s"]   ["*"]   24m
使用curl测试:
root@iZbp12bkuvg20e1j3y9gtxZ:~/myluzh# curl 127.0.0.1:30058
...
                <title>Hello, K8S!</title>
...

标签: k8s Ingress istio istio-ingressgateway gateway virtualservice

发表评论