方法1:kube-proxy

1.集群内:
启动proxy端口9999,访问该端口将proxy到api-server

# kubectl proxy --address='178.104.163.243' --accept-hosts='^*$' --port=9999

2.集群外:

# curl http://178.104.163.243:9999/api/v1/nodes

方法2: Token

1.集群内:创建sa

kubectl create serviceaccount sa-test

2.集群内:rbac给sa赋权

将clusterrole:cluster-admin绑定给sa,该sa拥有了管理员的权限:

kubectl create clusterrolebinding sa-test-cluster-admin --clusterrole='cluster-admin' --serviceaccount=default:sa-test

3.集群内:获得token
刚创建的sa带一个secret,secret中存放了token:

TOKEN=$(kubectl get secrets -o jsonpath="{.items[?(@.metadata.annotations['kubernetes\.io/service-account\.name']=='sa-test')].data.token}"|base64 -d)

4.集群外:使用token访问API

curl --header "Authorization: Bearer $TOKEN" --insecure  -X GET https://178.104.163.38:6443/api/v1/namespaces/monitoring/pods?limit=1

方法3: 证书

要求使用apiserver的key文件和cert文件:

curl -k --key /etc/kubernetes/pki/apiserver-kubelet-client.key --cert /etc/kubernetes/pki/apiserver-kubelet-client.crt  https://178.104.163.38:6443/api/v1/nodes

参考

1.Access Clusters Using the Kubernetes API


a朋
63 声望38 粉丝