如何从外部访问规范的 kubernetes 仪表板?

sub*_*s87 1 ubuntu kubernetes kubectl

如何从外部网络/IP 访问规范的 kubernetes 仪表板?有没有办法在外部公开仪表板服务,而不是从规范 k8s 集群节点的本地浏览器访问?

Jos*_*sto 5

该文档有一个关于如何做到这一点的指南

使用 kubectl 代理

kubectl proxy在您的机器和 Kubernetes API 服务器之间创建代理服务器。默认情况下,它只能在本地访问(从启动它的机器)。启动本地代理服务器:

$ kubectl proxy

Starting to serve on 127.0.0.1:8001
Run Code Online (Sandbox Code Playgroud)

代理服务器启动后,您应该能够从浏览器访问仪表板。

要访问仪表板的 HTTPS 端点,请转到: http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/

注意:不应使用 kubectl proxy 命令公开显示仪表板,因为它只允许 HTTP 连接。对于 localhost 和 127.0.0.1 以外的域,将无法登录。单击登录页面上的“登录”按钮后不会发生任何事情。

使用节点端口

这种访问仪表板的方式仅推荐用于单节点设置中的开发环境。编辑kubernetes-dashboard服务。

$ kubectl -n kube-system edit service kubernetes-dashboard
Run Code Online (Sandbox Code Playgroud)

您应该会看到服务的 yaml 表示。将类型:ClusterIP 更改为类型:NodePort 并保存文件。如果已经更改,请转到下一步。

# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
...
  name: kubernetes-dashboard
  namespace: kube-system
  resourceVersion: "343478"
  selfLink: /api/v1/namespaces/kube-system/services/kubernetes-dashboard-head
  uid: 8e48f478-993d-11e7-87e0-901b0e532516
spec:
  clusterIP: 10.100.124.90
  externalTrafficPolicy: Cluster
  ports:
  - port: 443
    protocol: TCP
    targetPort: 8443
  selector:
    k8s-app: kubernetes-dashboard
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}
Run Code Online (Sandbox Code Playgroud)

接下来,我们需要检查 Dashboard 暴露的端口。

$ kubectl -n kube-system get service kubernetes-dashboard
NAME                   CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes-dashboard   10.100.124.90   <nodes>       443:31707/TCP   21h
Run Code Online (Sandbox Code Playgroud)

仪表板已在端口 31707 (HTTPS) 上公开。现在您可以通过浏览器访问它:https://<master-ip>:31707master-ip可以通过执行找到kubectl cluster-info。通常它是 127.0.0.1 或您机器的 IP,假设您的集群直接运行在执行这些命令的机器上。

如果您尝试在多节点集群上使用 NodePort 公开仪表板,则必须找出运行仪表板的节点的 IP 以访问它。而不是访问https://<master-ip>:<nodePort>你应该访问https://<node-ip>:<nodePort>.

接口服务器

如果 Kubernetes API 服务器已公开并可从外部访问,您可以直接访问仪表板: https://<master-ip>:<apiserver-port>/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/

入口

仪表板也可以使用 Ingress 资源公开。例如

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
 name: kubernetes-dashboard-ingress
 namespace: kube-system
spec:
 rules:
   — host: kubernetes
     http:
       paths:
         — path: /ui
           backend:
             serviceName: kubernetes-dashboard
             servicePort: 80
Run Code Online (Sandbox Code Playgroud)