【问题标题】:Kubernetes Ingress same with with master-slave architectureKubernetes Ingress 与主从架构相同
【发布时间】:2022-10-08 21:24:42
【问题描述】:

我正在尝试创建一个遵循垂直复制的服务-

在此架构中,请求转到主节点。为此,我可以使用 kubernetes 入口。

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: / 
        backend:
          serviceName: master-node
          servicePort: http

现在我的要求是如果主节点关闭,那么请求应该转到从节点。 我可以通过创建三个路径/master/slave-1/slave-2 来实现这一点。但限制是请求的路径必须保持不变.因此,路径必须始终为/

我怎样才能创建一个入口,如果master-node 关闭,那么所有请求都应该转发到slave-1-node

我想实现以下目标 -

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: / 
        priority: 1
        backend:
          serviceName: master-node
          servicePort: http
  - host: example.com
    http:
      paths:
      - path: / 
        priority: 2
        backend:
          serviceName: slave-1-node
          servicePort: http
  - host: example.com
    http:
      paths:
      - path: / 
        priority: 3
        backend:
          serviceName: slave-2-node
          servicePort: http

【问题讨论】:

    标签: kubernetes kubernetes-ingress nginx-ingress distributed-system ingress-controller


    【解决方案1】:

    我不确定如何使用只是一个入口资源,但是如果您要在您的服务前面部署一个 haproxy pod,那将非常容易,因此您的架构如下所示:

    使用这样的 haproxy 配置,您将获得所需的行为:

    global
        log         stdout format raw local0
        maxconn     4000
        user        haproxy
        group       haproxy
    
    defaults
        mode    http
        log global
        option  httplog
        option  dontlognull
        option  http-server-close
        option  forwardfor  except 127.0.0.0/8
        option  redispatch
        retries 3
        timeout connect     10s
        timeout client      1m
        timeout server      1m
    
    frontend  example_fe
        bind 0.0.0.0:8080
        default_backend example_be
    
    backend example_be
        option httpchk GET /healthz
    
        server alpha example-alpha:80 check
        server beta example-beta:80 check backup
        server gamma example-gamma:80 check backup
    

    这会将所有请求发送到alpha,只要它正在运行。如果alpha 离线,请求将转到beta,如果beta 未运行,则请求将转到gamma。我发现this article 在查找有关如何设置的信息时很有用。

    您创建一个运行 haproxy 的部署:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app: haproxy
      name: haproxy
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: haproxy
      template:
        metadata:
          labels:
            app: haproxy
        spec:
          containers:
          - image: docker.io/haproxy:latest
            name: haproxy
            ports:
            - containerPort: 8080
              name: http
            volumeMounts:
            - mountPath: /usr/local/etc/haproxy
              name: haproxy-config
          volumes:
          - configMap:
              name: haproxy-config-ddc898c5f5
            name: haproxy-config
    
    

    指向该部署的服务:

    apiVersion: v1
    kind: Service
    metadata:
      labels:
        app: haproxy
      name: haproxy
    spec:
      ports:
      - name: http
        port: 80
        targetPort: http
      selector:
        app: haproxy
    

    然后将入口指向该服务:

    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: example
    spec:
      rules:
      - host: example.com
        http:
          paths:
          - backend:
              service:
                name: haproxy
                port:
                  name: http
            path: /
            pathType: Prefix
    
    

    如果您想尝试一下,我已经整理了完整的配置here

    【讨论】:

      猜你喜欢
      • 2023-03-26
      • 1970-01-01
      • 2017-09-26
      • 1970-01-01
      • 2011-07-11
      • 2020-02-06
      • 2017-12-26
      • 2021-10-07
      • 1970-01-01
      相关资源
      最近更新 更多