[英]Site can not be reached problem while accessing on local laptop from deployed kubernetes dashboard service on remote machine
當我嘗試從本地筆記本電腦訪問 Kubernetes 儀表板服務時,收到無法訪問站點的消息。
程序如下:
我按照以下鏈接中的文檔進行操作,
https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/
我在我的本地機器上創建了一個帶有一個主節點和一個工作節點的集群。 每台機器都是ubuntu 16.04。 我安裝了 kubectl 並從我的控制虛擬機訪問這個集群,我在那里運行 Jenkins for ci/cd 管道。 從這個控制虛擬機中,我按照文檔中的說明綁定了集群角色並部署了 Kubernetes 儀表板。
我運行以下命令,通過使用 kuectl 命令(在集群外)從我的控制虛擬機部署默認儀表板服務:
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta8/aio/deploy/recommended.yaml
我使用以下內容創建了角色綁定 yaml dashboard-adminuser.yaml
,
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: admin-user
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: admin-user
namespace: kubernetes-dashboard
並使用以下命令創建了它:
kubectl apply -f dashboard-adminuser.yaml
使用以下命令訪問令牌:
kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')
並運行以下命令來提供儀表板服務:
kubectl proxy
當我運行顯示“在 127.0.0.1:8001 上開始服務”的命令時。
我嘗試通過將以下 URL 放在瀏覽器上來訪問儀表板,
http://192.168.16.170:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/
但我只收到無法訪問該站點的消息。
更新
現在我試圖通過將儀表板服務類型編輯為NodePort
類型來使用NodePort 機制進行訪問。 當我嘗試訪問 URL 時,出現“您的連接不是私有的”之類的錯誤。 我正在添加下面的屏幕截圖,
我哪里錯了?
您需要將服務類型更改為NodePort才能從本地訪問它。
節點端口
這種訪問儀表板的方式僅推薦用於單節點設置中的開發環境。
編輯 kubernetes-dashboard 服務。
$ kubectl -n kubernetes-dashboard 編輯服務 kubernetes-dashboard
您應該會看到服務的 yaml 表示。 將類型:ClusterIP 更改為類型:NodePort 並保存文件。
apiVersion: v1
...
name: kubernetes-dashboard
namespace: kubernetes-dashboard
resourceVersion: "343478"
selfLink: /api/v1/namespaces/kubernetes-dashboard/services/kubernetes-
dashboard
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: {}
接下來,我們需要檢查 Dashboard 暴露的端口。
$ kubectl -n kubernetes-dashboard get service kubernetes-dashboard
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S)
AGE
kubernetes-dashboard NodePort 10.100.124.90 <nodes> 443:31707/TCP
21h
儀表板已在端口 31707 (HTTPS) 上公開。 現在您可以從瀏覽器訪問它: https://<master-ip>:31707
。 master-ip 可以通過執行 kubectl cluster-info 找到。 通常它是 127.0.0.1 或您機器的 IP,假設您的集群直接運行在執行這些命令的機器上。
如果您嘗試在多節點集群上使用 NodePort 公開 Dashboard,那么您必須找出運行 Dashboard 的節點的 IP 以訪問它。 您應該訪問https://<node-ip>:<nodePort>
而不是訪問https://<master-ip>:<nodePort>
https://<node-ip>:<nodePort>
。
UI 只能從執行命令(kubectl 代理)的機器上訪問。 在那台機器上試試
http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/
編輯:
否則使用 nodeport 機制訪問它而不使用 kubectl 代理
更新:
使用 kubectl 代理訪問儀表板
運行kubectl proxy
然后訪問
我使用令牌進行身份驗證,現在我創建了令牌:
# Create the service account in the current namespace
# (we assume default)
kubectl create serviceaccount my-dashboard-sa
# Give that service account root on the cluster
kubectl create clusterrolebinding my-dashboard-sa \
--clusterrole=cluster-admin \
--serviceaccount=default:my-dashboard-sa
# Find the secret that was created to hold the token for the SA
kubectl get secrets
# Show the contents of the secret to extract the token
kubectl describe secret my-dashboard-sa-token-xxxxx
通過公開的 API 服務器訪問儀表板
在瀏覽器中使用此 url https://<master-ip>:<apiserver-port>/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/
這會給你以下錯誤:
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {
},
"status": "Failure",
"message": "services \"https:kubernetes-dashboard:\" is forbidden: User \"system:anonymous\" cannot get resource \"services/proxy\" in API group \"\" in the namespace \"kube-system\"",
"reason": "Forbidden",
"details": {
"name": "https:kubernetes-dashboard:",
"kind": "services"
},
"code": 403
}
要解決上述錯誤,請在 yaml 下應用以配置 RBAC:
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: kubernetes-dashboard-anonymous
rules:
- apiGroups: [""]
resources: ["services/proxy"]
resourceNames: ["https:kubernetes-dashboard:"]
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
- nonResourceURLs: ["/ui", "/ui/*", "/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/*"]
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: kubernetes-dashboard-anonymous
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kubernetes-dashboard-anonymous
subjects:
- kind: User
name: system:anonymous
您仍然需要 kubeconfig 或令牌才能訪問。 令牌可以通過上述機制創建。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.