Kubernetes Dashboard is very useful when managing Kubernetes cluster. If your app is deployed to a Kubernetes cluster, you may want users to directly access Kubernetes dashboard through your app, instead of creating extra load balancer for the dashboard itself. We can create a proxy for Kubernetes dashboard.

The goal is to allow users to access Kubernetes dashboard using a different context path, for example, http://myapp.example.com/kubernetes_dashboard. Normal users can still the app, while administrators can access the dashboard.

If you try to proxy the request to Kubernetes dashboard, you may find that the dashboard doesn't work. This is because no base URL is set to default /, which causes dashboard page to load JavaScript and CSS files from the root path. Kubernetes dashboard doesn't provide a way to customise the base URL, so you have to do that yourself.

I'm going to use Java proxy mitre/HTTP-Proxy-Servlet as an example. You can do the similar thing when using other proxies. The key point is to rewrite the dashboard HTML page to include a <base> tag.

KubernetesDashboardProxyServlet class below extends from ProxyServlet and overrides the copyResponseEntity() method. If the response content type is text/html, then replace the HTML content to add a &lt;base&gt; tag with href set to specified baseHref.

class KubernetesDashboardProxyServlet(private val baseHref: String) : ProxyServlet() {
  override fun copyResponseEntity(proxyResponse: HttpResponse,
      servletResponse: HttpServletResponse,
      proxyRequest: HttpRequest,
      servletRequest: HttpServletRequest) {
    if (proxyResponse.getFirstHeader("Content-Type")?.value?.contains("text/html") == true) {
      if (proxyResponse.entity != null) {
        val content = IOUtils.toString(proxyResponse.entity.content, Charsets.UTF_8)
        val data = content.replace("<head>", "<head><base href=\"$baseHref\">").toUtf8Bytes()
        IOUtils.write(data, servletResponse.outputStream)
        return
      }
    }
    super.copyResponseEntity(proxyResponse, servletResponse, proxyRequest, servletRequest)
  }
}

Then you need to register the KubernetesDashboardProxyServlet in the context /kubernetes_dashboard/* and use /kubernetes_dashboard/ as the baseHref to create KubernetesDashboardProxyServlet.

Now the Kubernetes dashboard should work when you access /kubernetes_dashboard/.