问题描述
在Kubernetes中如何像访问内部服务那样访问外部服务。
问题分析
Kubernetes在service中提供了ExternalName字段,可以将集群外的服务映射到集群内。
问题解决
1.创建测试service
kind: Service
apiVersion: v1
metadata:
name: ext-test
namespace: default
spec:
type: ExternalName
externalName: httpbin.org
ports:
- name: http
port: 80
protocol: TCP
2.创建service并查看
$ kubectl apply -f externalName-service.yaml
$ kubectl get svc -n default|grep baidu
ext-baidu ExternalName <none> baidu.com <none> 1s
3.验证
$ kubectl exec -it <pod-name> -- bash -c "curl http://ext-test -I"
HTTP/1.1 200 OK
Date: Fri, 29 Apr 2022 07:21:47 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 9593
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
参考链接
https://kubernetes.io/docs/concepts/services-networking/service/#externalname 如果您有其他问题,欢迎您联系火山引擎技术支持服务