簡體   English   中英

如何在集群外訪問/公開 kubernetes-dashboard 服務?

[英]How to access/expose kubernetes-dashboard service outside of a cluster?

我有以下服務:

ubuntu@master:~$ kubectl get services --all-namespaces
NAMESPACE     NAME                   CLUSTER-IP      EXTERNAL-IP   PORT(S)         AGE
default       kubernetes             100.64.0.1      <none>        443/TCP         48m
kube-system   kube-dns               100.64.0.10     <none>        53/UDP,53/TCP   47m
kube-system   kubernetes-dashboard   100.70.83.136   <nodes>       80/TCP          47m

我正在嘗試訪問 kubernetes 儀表板。 考慮到 curl 不是瀏覽器,以下響應似乎是合理的。

ubuntu@master:~$ curl 100.70.83.136
 <!doctype html> <html ng-app="kubernetesDashboard"> <head> <meta charset="utf-8"> <title>Kubernetes Dashboard</title> <link rel="icon" type="image/png" href="assets/images/kubernetes-logo.png"> <meta name="viewport" content="width=device-width"> <link rel="stylesheet" href="static/vendor.36bb79bb.css"> <link rel="stylesheet" href="static/app.d2318302.css"> </head> <body> <!--[if lt IE 10]>
      <p class="browsehappy">You are using an <strong>outdated</strong> browser.
      Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your
      experience.</p>
    <![endif]--> <kd-chrome layout="column" layout-fill> </kd-chrome> <script src="static/vendor.633c6c7a.js"></script> <script src="api/appConfig.json"></script> <script src="static/app.9ed974b1.js"></script> </body> </html> 

根據文檔,正確的訪問點是https://localhost/ui 所以,我正在嘗試並收到有點令人擔憂的結果。 是預期的反應嗎?

ubuntu@master:~$ curl https://localhost/ui
curl: (60) server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle"
 of Certificate Authority (CA) public keys (CA certs). If the default
 bundle file isn't adequate, you can specify an alternate file
 using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
 the bundle, the certificate verification probably failed due to a
 problem with the certificate (it might be expired, or the name might
 not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
 the -k (or --insecure) option.

在沒有證書驗證的情況下嘗試相同的方法。 對於 curl 可能沒問題。 但我在瀏覽器中得到了相同的結果,它通過 vagrant forwarded_port 選項通過端口轉發進行連接。

ubuntu@master:~$ curl -k https://localhost/ui
Unauthorized

我做錯了什么? 以及如何確保我可以訪問 UI? 目前它以未授權響應。

儀表板的文檔告訴密碼在配置中:

ubuntu@master:~$ kubectl config view
apiVersion: v1
clusters: []
contexts: []
current-context: ""
kind: Config
preferences: {}
users: []

但似乎我什么都沒有......這是預期的行為嗎? 如何使用 UI 進行授權?

官方維基有點混亂,所以我在這里重新排序:

如果使用推薦的yaml 部署儀表板,則只能通過 https 訪問儀表板,並且應生成證書,請參閱指南 然后你可以運行kubectl proxy --address='0.0.0.0' --accept-hosts='^*$'來訪問“http://localhost:8001/ui”上的儀表板。 此頁面需要使用令牌登錄。 要生成它,請參閱此頁面 您也可以將NodePort添加到您的 yaml 並使用<nodeip>:<port>訪問它。

如果使用http 替代方法進行部署,則只能通過 nodeip:port 訪問儀表板 記得先添加到yaml中!! 部署后,您還應該生成您的令牌並為每個請求添加標頭Authorization: Bearer <token>

我認為這可以幫助您和其他想要使用 kube-dashboard 的人。

你可以參考文檔:

https://github.com/kubernetes/dashboard/blob/master/docs/user/accessing-dashboard/README.md

簡單的方法是

$ kubectl -n kube-system edit service kubernetes-dashboard

.spec.type更改為NodePort

您可以通過點擊 master 上儀表板的 nodePort 來訪問

kubectl describe services kubernetes-dashboard --namespace=kube-system
NodePort:       <unset> 30042/TCP

http://MASTER:30042

您需要在本地運行 kubectl proxy 才能訪問 kubernetes 集群外的儀表板。 這是因為身份驗證機制。 運行以下命令后,您將能夠在瀏覽器上的http://localhost/ui上查看儀表板。 admin.conf 文件是 kubernetes master 上的文件,位於 /etc/kubernetes/admin.conf 您必須將該文件 scp 到要訪問儀表板的機器上,並將其傳遞給 kubectl 命令。

kubectl --kubeconfig=./admin.conf proxy -p 80

如果以下兩個條件之一有效,@user2363318 提到的 nodePort 方法將適用:

  1. 您的 http 客戶端(瀏覽器或 curl)能夠發送身份驗證令牌
  2. 你在 kubernetes 集群中的服務沒有 auth

你有沒有試過這個:
\n kubectl port-forward -n kubernetes-dashboard service/kubernetes-dashboard 10443:443 --address 0.0.0.0\n
在您的情況下,命名空間不同,因此:
\n kubectl port-forward -n kube-system service/kubernetes-dashboard 10443:443 --address 0.0.0.0

現在您應該能夠在端口 10443 訪問儀表板。

在我的筆記本電腦上使用終端應用程序執行以下操作:

sudo ssh  -i “MYAMOZONHOSTKEYPAIR.pem" -L 8001:127.0.0.1:8001 ubuntu@MYAMAZONHOST

MYAMAZONHOST 是我運行 k3s 的地方。

在 SSH 上執行以下操作:

sudo k3s kubectl proxy

然后用瀏覽器打開url http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/

Kubernetes 儀表板已成功打開。

只需為上述回復添加一點清晰度,

將類型:ClusterIP 更改為類型:NodePort 並保存文件。

儀表板已在端口 31707 (HTTPS) 上公開。 現在您可以通過瀏覽器訪問它: https://master-ip:31707

注意:提到的端口可能與您的情況有所不同,只需運行以下 kubectl 命令即可從 master 獲取端口。

kubectl -n kube-system 獲取服務 kubernetes-dashboard

創建一個新服務,將端口 8001 暴露給 kubernetes-dashboard 的 8443。

kubectl expose deployment kubernetes-dashboard -n kubernetes-dashboard --type=LoadBalancer --name=kdash --port=8001 --target-port=8443

現在,您可以使用https://<ip>:8001訪問您的 kubernetes-dashboard。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM