如何通过https访问kubernetes服务?

时间:2017-06-22 19:55:04

标签: kubernetes

这是我的群集信息

kubectl cluster-info
Kubernetes master is running at https://129.146.10.66:6443
Heapster is running at https://129.146.10.66:6443/api/v1/proxy/namespaces/kube-system/services/heapster
KubeDNS is running at https://129.146.10.66:6443/api/v1/proxy/namespaces/kube-system/services/kube-dns

所以,我有一个作为NodePort运行的服务(mysqlbrokerservice),配置看起来像这样

kubectl describe svc mysqlbrokerservice
Name:                        mysqlbrokerservice
Namespace:                mysqlbroker
Labels:                        <none>
Annotations:                <none>
Selector:                app=mysqlbroker
Type:                        NodePort
IP:                        10.99.194.191
Port:                        mysqlbroker        8080/TCP
NodePort:                mysqlbroker        30000/TCP
Endpoints:                10.244.1.198:8080
Session Affinity:        None
Events:                        <none>

我可以通过运行pod的节点的公共IP访问该服务,如http://129.146.34.181:30000/v2/catalog

然后我想看看我是否可以通过https访问该服务。我按照https://kubernetes.io/docs/tasks/access-application-cluster/access-cluster/#manually-constructing-apiserver-proxy-urls

中的指示进行操作

我按照示例使用curl获取资源。 这是命令。 129.146.10.66:6443是我的主人ip。

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET  https://129.146.10.66:6443/api/v1/namespaces/mysqlbroker/services/mysqlbrokerservice:8080/proxy/v2/catalog --header "Authorization: Bearer $TOKEN" --insecure
HTTP/1.0 200 Connection established
卷曲只是坐在那里没有回应。然后我查看了我的pod日志,但没有显示收到任何请求。

有人可以解释我在这里做错了吗?如果我想通过https公开服务,那么理想的解决方案是什么?

2 个答案:

答案 0 :(得分:0)

文档说它期望端口名称而不是数字。你试过以下这个吗?

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET  https://129.146.10.66:6443/api/v1/namespaces/mysqlbroker/services/mysqlbrokerservice:mysqlbroker/proxy/v2/catalog --header "Authorization: Bearer $TOKEN" --insecure

答案 1 :(得分:0)

如果您点击kubectl cluster-info提供的任何网址,您会看到浏览器提示您接受不安全的TLS连接。

要使HTTP适用于此特定地址,您需要购买为主机名颁发的TLS证书(在这种情况下,IP地址,您无法购买IP地址的证书)。另一种选择是将Kubernetes群集的根证书添加到您的计算机的受信任根目录中,但这不会使其在其他计算机上运行。

所以我假设您只是尝试通过HTTPs使外部世界可以在Kubernetes上运行应用程序:

为此,我建议实际购买域名(或重用子域名),为该主机名购买SSL / TLS证书,并使用Ingress配置具有HTTP终止的负载均衡器。 https://kubernetes.io/docs/concepts/services-networking/ingress/#tls(如果您使用的是GKE,Google Load Balancer,否则它会配置本地nginx实例来执行此任务。)