我有一个简单的入口网络,我想从该入口网络访问位于不同名称空间的服务。
我该怎么做? 我的入口网络Yaml文件:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress
spec:
rules:
- host: api.myhost.com
http:
paths:
- backend:
serviceName: bookapi-2
servicePort: 8080
path: /booking-service/
我已将ExternalNames服务类型设置为yaml文件:
apiVersion: v1
kind: Service
metadata:
name: bookapi-2
namespace: booking-namespace
spec:
type: ExternalName
externalName: bookapi-2
ports:
- name: app
protocol: TCP
port: 8080
targetPort: 8080
selector:
app: bookapi-2
tier: backend-2
答案 0 :(得分:4)
ExternalName服务是不提供服务的特殊情况 有选择器,而是使用DNS名称。
您可以从官方Kubernetes documentation中找到有关ExternalName服务的更多信息:
当您要从其他命名空间访问服务时,对于example,您的Yaml可能如下所示:
kind: Service
apiVersion: v1
metadata:
name: test-service-1
namespace: namespace-a
spec:
type: ExternalName
externalName: test-service-2.namespace-b.svc.cluster.local
ports:
- port: 80
对于您的Ingress yaml文件,请重新检查并确保它与官方示例兼容,对于example,此文件包含一些不一致之处:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: my-ingress
spec:
rules:
- host: www.mysite.com
http:
paths:
- backend:
serviceName: website
servicePort: 80
- host: forums.mysite.com
http:
paths:
- path:
backend:
serviceName: forums
servicePort: 80
也请重新检查ExternalName yaml,因为它具有在此类Service中未使用的TargetPort和选择器,并确保:
外部名称服务仅在
kube-dns
1.7版中可用 及以后。
如果您无法成功,请分享您遇到的问题。
答案 1 :(得分:1)
ExternaNames 服务应该在没有任何选择器选项的情况下创建。因此,在创建 入口资源 的命名空间中创建 ExternaNames 服务,并指向外部名称服务以解析托管在不同命名空间中的应用程序的名称。
答案 2 :(得分:1)
注意:演示代码不能在生产环境中运行。只是想了解它如何跨命名空间工作
---
#1
apiVersion: v1
kind: Namespace
metadata:
name: service-ns
---
#2
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx
name: nginx-internal
namespace: service-ns
spec:
ports:
- name: "80"
port: 80
targetPort: 80
selector:
app: nginx
---
#3
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx
name: nginx
namespace: service-ns
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
image: nginx
name: nginx
ports:
- containerPort: 80
restartPolicy: Always
---
#4
apiVersion: v1
kind: Namespace
metadata:
name: ingress-ns
---
#5
kind: Service
apiVersion: v1
metadata:
name: nginx
namespace: ingress-ns
spec:
type: ExternalName
externalName: nginx-internal.service-ns.svc.cluster.local
ports:
- port: 80
---
#6
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: main-ingress
annotations:
kubernetes.io/ingress.class: nginx
namespace: ingress-ns
spec:
rules:
- host: whatever.domain.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx
port:
number: 80