我正在尝试使用Kubernetes Python 客户端 https://github.com/kubernetes-client/python连接到我的 Kubernetes 集群。该 API 位于我的 CA 签署的 SSL 证书后面。如果我尝试访问任何 API,则会收到有关证书验证失败的 SSL 错误。
我找到了一个v1beta1_api_service_spec.py https://github.com/kubernetes-client/python/blob/master/kubernetes/client/models/v1beta1_api_service_spec.py有一个 ca_bundle 参数来验证证书的库,但是core_v1_api.py https://github.com/kubernetes-client/python/blob/master/kubernetes/client/apis/core_v1_api.py and api_client.py https://github.com/kubernetes-client/python/blob/master/kubernetes/client/api_client.pyca_bundle 没有参数选项。
如何通过 CA 证书以便通过 HTTPS 访问 API?
** 解决方案 **
根据马修的指点,我能够找出问题所在。最初,我使用 Kubernetes 配置模块从 ~/.kube/config 文件加载配置。
from kubernetes import client, config
config.load_kube_config()
这在我正在测试的客户端上不起作用,但 kubectl 在我的 PC 上工作,所以我检查了,发现 .kube/config 文件没有指定 CA 证书。我添加了它,然后就成功了。
apiVersion: v1
clusters:
- cluster:
api-version: v1
certificate-authority: /path/to/ca_chain.crt
server: "https://my-kubernetes-cluster"
...
如果您不想在主机上创建 .kube/config 文件,我还能够弄清楚如何手动构建配置。
from kubernetes import client
from kubernetes.client import Configuration, ApiClient
config = Configuration()
config.api_key = {'authorization': 'Bearer <api_key>'}
config.host = 'https://my-kubernetes-cluster'
config.ssl_ca_cert = "/path/to/ca_chain.crt"
api_client = ApiClient(configuration=config)
v1 = client.CoreV1Api(api_client)
v1.list_pod_for_all_namespaces(watch=False)