ExternalName类型的Service在Kubernetes中用于将外部服务(不是Kubernetes集群内的服务)映射到Kubernetes集群内的Service。
样例
其创建方法如下:
kind: Service
apiVersion: v1
metadata:
name: my-external-service
namespace: cv-console-dev
labels:
app: my-external-service
annotations:
kubesphere.io/creator: sunjq
kubesphere.io/serviceType: externalservice
spec:
type: ExternalName
sessionAffinity: None
externalName: www.httpclient.cn
在上述配置中,我们创建了一个名为my-external-service的Service,其类型为ExternalName,并将www.httpclient.cn映射到该Service。这就相当于在Kubernetes集群内部创建了一个别名,可以通过访问my-external-service来访问www.httpclient.cn。
需要注意的是,使用ExternalName类型的Service需要确保目标外部服务已经存在,并且可以通过域名进行访问。
测试过程
root@5c856df78-5ml9f:/# curl -k test-ex.cv-console-dev -vvv
* Trying 47.244.242.115:80...
* Connected to test-ex.cv-console-dev (47.244.242.115) port 80 (#0) ---------------------外部的域名对应的ip
> GET / HTTP/1.1
> Host: test-ex.cv-console-dev
> User-Agent: curl/7.74.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: nginx/1.15.12
< Date: Thu, 31 Aug 2023 08:49:05 GMT
< Content-Type: text/html
< Content-Length: 612
< Last-Modified: Fri, 21 Jun 2019 06:42:16 GMT
< Connection: keep-alive
< ETag: "5d0c7c48-264"
< Accept-Ranges: bytes
<
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
* Connection #0 to host test-ex.cv-console-dev left intact