[TOC]
在创建好
kubernetes
集群后, 内网访问便是稀松平常的事情。 由于使用了http_proxy
代理,但是未设置no_proxy
导致各种错误。为什么设置代理,因为github
头像老是不显示。
排查思路
内网的另外一台主机(这台主机用的是provixy
代理)使用curl
命令进行排查,查看集群version
信息,没有任何异常返回200
。
$ curl -ik https://apiserver.cluster.local:6443/version
HTTP/2 200
content-type: application/json
content-length: 263
date: Wed, 08 Jul 2020 10:00:59 GMT
{
"major": "1",
"minor": "18",
"gitVersion": "v1.18.0",
"gitCommit": "9e991415386e4cf155a24b1da15becaa390438d8",
"gitTreeState": "clean",
"buildDate": "2020-03-25T14:50:46Z",
"goVersion": "go1.13.8",
"compiler": "gc",
"platform": "linux/amd64"
}%
使用curl
命令进行资源查看, 此时已经出现了401错误。Unauthorized
, 其实这里我很懵逼的, 想了想怎么会出现这个问题, 在kubernetes集群内部访问是没有问题的。
$ curl -H 'Authorization: Bearer ***' https://apiserver.cluster.local:6443/api --insecure
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {
},
"status": "Failure",
"message": "Unauthorized",
"reason": "Unauthorized",
"code": 401
}
继续使用curl命令访问kuboard
的api接口, 这个在集群内访问和内网的主机访问都没有问题。
$ curl 'http://192.168.0.179:6443/api/apps/v1/namespaces/kube-system/deployments' \
-H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0' \
-H 'Accept: application/json, text/plain, */*' \
-H 'Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2' \
--compressed \
-H 'Authorization: Bearer ×××' \
-H 'Connection: keep-alive' \
-H 'Referer: http://192.168.0.179:32567/namespace/kube-system' \
-H 'Cookie: _ga=GA1.1.240841703.1594199764; _gid=GA1.1.812371467.1594199764; _gat=1'
{
... //数据了太多。没有复制了
}
使用kubectl
命令行访问kubernetes
集群
使用自己创建的ServiceAccount
,获取资源信息
$ kubectl --kubeconfig=./kubectl-config.yaml get pods
error: You must be logged in to the server (Unauthorized)
使用admin.conf
配置文件,获取资源信息
$ kubectl get pods
Unable to connect to the server: x509: certificate signed by unknown authority (possibly because of "crypto/rsa: verification error" while trying to verify candidate authority certificate "kubernetes")
越访问,心越凉。 咋内网的就各种问题。 出现的还不一样, google
后基本是admin.conf
配置文件忘记复制了。 但是我这边是集群内部是可以访问的, 在master
节点执行是全ok的。
另外。 使用的cow
代理的一台内网主机曾经也发生过问题。 但是被我忽略了, 大概的错误是
$ kubectl get pods
Unable to connect to the server: Forbidden
然后curl
命令返回的是403.
$ curl -ik https://apiserver.cluster.local:6443/version
HTTP/1.1 403 Forbidden
Connection: keep-alive
Cache-Control: no-cache
Pragma: no-cache
Content-Type: text/html
Content-Length: 346
curl: (56) Received HTTP code 403 from proxy after CONNECT
这里灵机一闪, proxy问题。 因为我开发的主机设置, 没有设置no_proxy
,全部走的cow
这个自动分发流量的代理上。
$ cat ~/.zshrc
export http_proxy="http://127.0.0.1:7777"
export https_proxy="http://127.0.0.1:7777"
解决方法.
$ export no_proxy="apiserver.cluster.local,192.168.0.179,127.0.0.1"
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
web-hello-world-6c7f79b76d-5mzjp 1/1 Running 0 10d
web-hello-world-6c7f79b76d-8psvs 1/1 Running 0 173m
web-hello-world-6c7f79b76d-mqp9j 1/1 Running 0 173m
而内网服务器的代理设置是provixy
, 出现的问题是不一样的。
$ cat ~/.zshrc
export http_proxy=http://192.168.0.23:8118
export https_proxy=http://192.168.0.23:8118
同理, 设置no_proxy
即可, no_proxy暂时不支持通配符 比如192.168.0.×
$ printf -v no_proxy '%s,' 192.168.0.{1..255},;
$ export no_proxy="${no_proxy%,},apiserver.cluster.local,127.0.0.1"
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
web-hello-world-6c7f79b76d-5mzjp 1/1 Running 0 10d
web-hello-world-6c7f79b76d-8psvs 1/1 Running 0 174m
web-hello-world-6c7f79b76d-mqp9j 1/1 Running 0 174m
记录一下。 一次http_proxy
引起的问题。