有了kubeadm以后,Kubernetes的安装过程已经简单了很多,安装过程可以参考我的另一篇文章《基于kubeadm安装k8s 1.12.2和dashboard(国内网络环境)》。
这里主要对安装完成后,访问apiserver和dashboard时遇到的问题提供简化处理办法。
apiserver的访问地址可以通过"kubectl cluster-info"获取,一般默认是https://xxx.xxx.xxx.xxx:6443。
问题现象如下:
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {
},
"status": "Failure",
"message": "forbidden: User \"system:anonymous\" cannot get path \"/\"",
"reason": "Forbidden",
"details": {
},
"code": 403
}
对于正式环境,需要创建一个用户并授权,在上一篇文章里介绍过,多少还是烦了点。
对于测试环境,我们不需要那么麻烦,不就是匿名用户被禁用了吗,直接给匿名用户授权就可以了。
kubectl create clusterrolebinding test:anonymous --clusterrole=cluster-admin --user=system:anonymous
问题现象:
1、通过https://xxx.xxx.xxx.xxx:6443/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/,url太长
2、需要kubeconfig或者token,有点烦
对于测试环境,我们这里简化处理。
编辑kubernetes-dashboard.yaml,改为http访问,并开放外部端口,一共需要4处修改。
spec:
containers:
- name: kubernetes-dashboard
image: k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1
# 1、下面改成http的9090端口
ports:
- containerPort: 9090
protocol: TCP
args:
# 2、这里注释掉
# - --auto-generate-certificates
# Uncomment the following line to manually specify Kubernetes API server Host
# If not specified, Dashboard will attempt to auto discover the API server and connect
# to it. Uncomment only if the default does not work.
# - --apiserver-host=http://my-address:port
volumeMounts:
- name: kubernetes-dashboard-certs
mountPath: /certs
# Create on-disk volume to store exec logs
- mountPath: /tmp
name: tmp-volume
livenessProbe:
# 3、这里同样改成http的9090端口
httpGet:
scheme: HTTP
path: /
port: 9090
initialDelaySeconds: 30
timeoutSeconds: 30
......
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kube-system
spec:
# 4、修改服务端口,外部端口设置为31000,并设置为NodePort
ports:
- port: 80
targetPort: 9090
nodePort: 31000
type: NodePort
selector:
k8s-app: kubernetes-dashboard
重新部署kubernetes-dashboard,然后通过http://xxx.xxx.xxx.xxx:31000即可直接访问,然后会遇到下面的问题:
configmaps is forbidden: User "system:serviceaccount:kube-system:kubernetes-dashboard" cannot list resource "configmaps" in API group "" in the namespace "default"
persistentvolumeclaims is forbidden: User "system:serviceaccount:kube-system:kubernetes-dashboard" cannot list resource "persistentvolumeclaims" in API group "" in the namespace "default"
问题很清楚,就是kube-system:kubernetes-dashboard这个serviceaccount没有权限,同上一个的问题的解决办法:
kubectl create clusterrolebinding test:kubernetes-dashboard --clusterrole=cluster-admin --serviceaccount=kube-system:kubernetes-dashboard
重新访问就可以了。