通过 Istio 入口网关公开服务

时间:2021-05-19 09:06:46

标签: kubernetes istio

我是 istio 的新手,我想公开三个服务,并根据传递给“website.com:port”或子域的端口号将流量路由到这些服务。

服务部署配置文件:

    apiVersion: v1
kind: Service
metadata:
  name: visitor-service 
  labels:
    app: visitor-service
spec:
  ports:
    - port: 8000
      nodePort: 30800
      targetPort: 8000
  selector:
   app: visitor-service
---   
apiVersion: apps/v1
kind: Deployment
metadata:
  name: visitor-service 
spec:
  replicas: 1
  selector:
    matchLabels:
      app: visitor-service
  template:
    metadata:
      labels:
        app: visitor-service
    spec:
      containers:
        - name: visitor-service
          image: visitor-service
          ports:
            - containerPort: 8000

二次服务:

apiVersion: v1
kind: Service
metadata:
  name: auth-service 
  labels:
    app: auth-service
spec:
  ports:
    - port: 3004
      nodePort: 30304
      targetPort: 3004
  selector:
   app: auth-service
---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: auth-service
spec:
  replicas: 1
  selector:
    matchLabels:
      app: auth-service
  template:
    metadata:
      labels:
        app: auth-service
    spec:
      containers:
        - name: auth-service
          image: auth-service
          ports:
            - containerPort: 3004

第三个:

apiVersion: v1
kind: Service
metadata:
  name: gateway 
  labels:
    app: gateway
spec:
  ports:
    - port: 8080
      nodePort: 30808
      targetPort: 8080
  selector:
   app: gateway

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: gateway
spec:
  replicas: 1
  selector:
    matchLabels:
      app: gateway
  template:
    metadata:
      labels:
        app: gateway
    spec:
      containers:
        - name: gateway
          image: gateway
          ports:
            - containerPort: 8080

如果有人可以帮助设置网关和虚拟服务配置,那就太好了。

1 个答案:

答案 0 :(得分:2)

似乎您只是想公开您的应用程序,因此 istio 似乎完全是矫枉过正,因为它带来了很多您不会使用的开销。 无论您是想使用 istio 作为默认入口还是任何其他入口控制器(nginx、traefik 等),以下构造都适用于所有这些: 根据您的基础架构,通过 NodePortLoadBalancer 类型的服务公开入口控制器。在云环境中,后者很可能最适合您(如果在 GKE、AKS、EKS 上,...)。 一旦暴露,设置一个 DNS A 记录以指向外部 IP 地址。之后你就可以开始配置你的入口了,这取决于你选择了哪个入口控制器,下面的 YAML 可能需要一些调整(例子是为 istio 给出的):

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: istio
  name: ingress
spec:
  rules:
  - host: httpbin.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          serviceName: httpbin
          servicePort: 8000

如果对 httpbin.example.com 之类的请求进入您的入口控制器,它会将请求发送到端口 httpbin 上名为 8000 的服务。 从上面发布的 YAML 中可以看出,rulespaths 字段采用一个列表(由下一行中的 - 表示)。要公开多个服务,只需在列表中添加一个新条目,例如:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: istio
  name: ingress
spec:
  rules:
  - host: httpbin.example.com
    http:
      paths:
      - path: /httpbin
        pathType: Prefix
        backend:
          serviceName: httpbin
          servicePort: 8000
      - path: /apache
        pathType: Prefix
        backend:
          serviceName: apache
          servicePort: 8080

这会将 httpbin.example.com/httpbin/ 之类的请求发送到 httpbin,将 httpbin.example.com/apache/ 之类的请求发送到 apache

有关更多信息,请参阅: