是否可以根据服务标签而不是服务名称来路由Ingress请求?
我希望有两个名称相同但标签值不同的服务。 Ingress可以这样路由流量吗?
service-name.labelA.com -> service with label = A
service-name.labelB.com -> service with label = A
是否可以这样做? 因为在Docs中,它使用服务名称和端口(没有标签)进行路由:
spec:
rules:
- http:
paths:
- path: /testpath
backend:
serviceName: test
servicePort: 80
答案 0 :(得分:2)
Kubernets不允许您创建两个具有相同名称的服务
$ cat service_A.yaml apiVersion: v1
kind: Service
metadata:
name: portfwd-srv-a
spec:
ports:
-
nodePort: 30010
port: 88
name: knp1
selector:
app: nginx
type: NodePort
cat service_B.yaml apiVersion: v1
kind: Service
metadata:
name: portfwd-srv-a
spec:
ports:
-
nodePort: 30011
port: 88
name: knp1
selector:
app: nginx
type: NodePort
$ kubectl create -f service_A.yaml
service/portfwd-srv-a created
$ kubectl create -f service_B.yaml
Error from server (AlreadyExists): error when creating "service_B.yaml": services "portfwd-srv-a" already exists
$ kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 6d3h <none>
portfwd-srv-a NodePort 10.0.5.233 <none> 88:30010/TCP 3m app=nginx
web NodePort 10.0.4.73 <none> 8080:31881/TCP 3d run=web
但是,可以将同一服务(或Ingress选择的任何服务)作为目标。 以下示例受Ingress文档的影响。
$ cat ingress_namebased.yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: name-virtual-host-ingress
spec:
rules:
- host: foo.bar.com
http:
paths:
- backend:
serviceName: web
servicePort: 8080
- host: bar.foo.com
http:
paths:
- backend:
serviceName: web
servicePort: 8080
在这里,我通过foo.bar.com
服务处理发送到bar.foo.com
和web
的请求:
$ kubectl get ingress
NAME HOSTS ADDRESS PORTS AGE
name-virtual-host-ingress foo.bar.com,bar.foo.com 35.186.*.* 80 134m
$ kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
web NodePort 10.0.4.73 <none> 8080:31881/TCP 3d run=web
...
指向一些豆荚/端点:
$ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web-ddb799d85-7x2v4 1/1 Running 0 3d 10.12.0.6 cluster-1-pool-1-6v3n <none> <none>
web-ddb799d85-p2cq8 1/1 Running 0 2d21h 10.12.1.8 cluster-1-pool-1-m7z8 <none> <none>
$ kubectl get ep -o wide
NAME ENDPOINTS AGE
web 10.12.0.6:8080,10.12.1.8:8080 3d
这里是从“最终用户”预期中如何工作的示例(因为我有2个端点,所以进行了循环):
$ curl foo.bar.com
Hello, world!
Version: 1.0.0
Hostname: web-ddb799d85-7x2v4
$ curl foo.bar.com
Hello, world!
Version: 1.0.0
Hostname: web-ddb799d85-p2cq8
curl bar.foo.com
Hello, world!
Version: 1.0.0
Hostname: web-ddb799d85-p2cq8
$ curl bar.foo.com
Hello, world!
Version: 1.0.0
Hostname: web-ddb799d85-7x2v4