当我尝试从本地笔记本电脑访问kubernetes仪表板服务时,我收到一条消息,提示无法访问该站点。
遵循的程序
我遵循以下链接提供的文档
https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/
我在本地计算机上创建了具有一个主节点和一个工作节点的集群。每台机器都是ubuntu 16.04。然后我安装了kubectl并从我运行ci / cd管道的Jenkins的控制vm访问该集群。从该控制虚拟机开始,我按照文档中的说明绑定了clusterrole并部署了kubernetes仪表板
我运行以下命令,使用kuectl命令(在群集外部)从控制vm部署默认仪表板服务,
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时,出现类似“您的连接不是私有的”错误。我要在下面添加屏幕截图,
任何人都可以帮助解决此问题,并帮助我找出我走错路了吗?
答案 0 :(得分:4)
您需要将服务类型更改为 NodePort 才能从本地访问。
NodePort
仅在单节点设置中的开发环境中才建议使用这种访问Dashboard的方式。
编辑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: {}
下一步,我们需要检查显示板所在的端口。
$ 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
。可以通过执行kubectl cluster-info找到master-ip。假设您的集群直接在执行这些命令的机器上运行,通常它是127.0.0.1或您机器的IP。
如果要在多节点群集上使用NodePort公开Dashboard,则必须找出运行Dashboard的节点的IP才能访问它。而不是访问https://<master-ip>:<nodePort>
,您应该访问https://<node-ip>:<nodePort>
。
答案 1 :(得分:2)
只能从执行命令(kubectl代理)的机器上访问UI。在那台机器上尝试
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服务器访问仪表板
在浏览器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或令牌才能访问。可以通过上述机制创建令牌。