Sidekiq UI在基于Kubernetes路径的路由中加载错误的资产路径

时间:2018-04-04 06:21:28

标签: ruby-on-rails kubernetes sidekiq devops kubernetes-ingress

我有一个网站:

xyz.com - >独立的Angular4 APP(容器)

xyz.com/api/ - >独立Rails API APP(容器 - ruby​​-2.4.0)

xyz.com/api/sidekiq - >安装到Rails api路由。

当我尝试访问/ api / sidekiq时,它会加载数据,但资源仍然指向xyz.com/sidekiq而不是xyz.com/api/sidekiq。所有当我点击重试时,它会尝试提交到xyz.com/sidekiq而不是xyz.com/api/sidekiq。有没有办法强制sidekiq使用不同的资产路径和基本URL路径?

有没有办法让sidekiq网页视图成为一个单独的独立应用程序容器?

我使用Kubernetes作为我的编排工具。我正在使用Nginx Ingress来进行基于路径的路由。当我访问xyz.com/api/sidekiq时,它会加载数据,但网址和资源指向xyz.com/sidekiq,这就是为什么我无法重试sidekiq工作,因为sidekiq网络无法正常工作。

1 个答案:

答案 0 :(得分:1)

您可以看看我向sidekiq团队“ Sidekiq Route Mount doesn't rewrite URL in Rails API”提出的主题,该主题帮助我了解了sidekiq mount "/sidekiq"的工作原理。

我对此问题有一个简单的解决方法,发现它是解决基于路径的路由问题的最佳方法。您可以通过编写2个Ingress规则,在Ingress配置中使用nginx.ingress.kubernetes.io/rewrite-target批注来做到这一点,如下所示:

  1. 将“ / ”路径路由到rails-app-service的根路径( xyz.com/* -> rails-app-service/*

  2. 将“ / sidekiq ”路径路由到rails-app-service的sidekiq Web安装路径( xyz.com/sidekiq/* -> rails-app-service/sidekiq/*

要了解有关Nginx Ingress Controller重写目标注释的更多信息,请查看官方存储库以获取详细信息:https://github.com/kubernetes/ingress-nginx/tree/master/docs/examples/rewrite#rewrite-target

这是解决方案的最终代码片段:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: "rails-app-ingress"
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: xyz.com
    http:
      paths:
      - path: /
        backend:
          serviceName: "rails-app-service"
          servicePort: 80
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: "rails-sidekiq-ingress"
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/rewrite-target: /sidekiq
spec:
  rules:
  - host: xyz.com
    http:
      paths: 
      - path: /sidekiq
        backend:
          serviceName: "rails-app-service"
          servicePort: 80