Ami*_*war 3 nginx kubernetes minikube kubernetes-ingress
我正在尝试从服务访问我的 ingress-nginx 服务,但它拒绝连接。这是我的入口
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress-service
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/use-regex: "true"
spec:
rules:
- host: ticketing.dev
http:
paths:
- path: /api/users/?(.*)
backend:
serviceName: auth-srv
servicePort: 3000
- path: /api/tickets/?(.*)
backend:
serviceName: tickets-srv
servicePort: 3000
- path: /?(.*)
backend:
serviceName: client-srv
servicePort: 3000
Run Code Online (Sandbox Code Playgroud)
apiVersion: v1
kind: Namespace
metadata:
name: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
---
kind: Service
apiVersion: v1
metadata:
name: ingress-nginx
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
spec:
externalTrafficPolicy: Local
type: LoadBalancer
selector:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
ports:
- name: http
port: 80
protocol: TCP
targetPort: http
- name: http
port: 443
protocol: TCP
targetPort: https
Run Code Online (Sandbox Code Playgroud)
? kubectl get services -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx LoadBalancer 10.101.124.218 10.101.124.218 80:30634/TCP,443:30179/TCP 15m
Run Code Online (Sandbox Code Playgroud)
ingress-nginx 在命名空间 ingress-nginx 上运行。所以它应该可以通过http://ingress-nginx.ingress-nginx.svc.cluster.local. 但是当我访问它时,它说connection refused 10.101.124.218:80。我能够从外部访问入口,即从ingressip。
我正在使用 minikube 并通过运行使用 ingress minikube addons enable ingress。是的,我正在运行隧道minikube tunnel
我测试了您的环境并发现了相同的行为,外部访问但内部连接被拒绝,这就是我解决的方法:
kube-system命名空间中部署控制器。如果您尝试在新创建的命名空间中部署服务,它将无法到达kube-system命名空间中的部署。nginx-ingress部署使用ingress-nginx您尝试的命名空间。我发现的另一个问题是您的服务没有分配给控制器部署的所有选择器。
使部署工作的最简单方法是kubectl expose在 nginx 控制器上运行:
kubectl expose deployment ingress-nginx-controller --target-port=80 --type=NodePort -n kube-system
Run Code Online (Sandbox Code Playgroud)
再生产:
ingress和metallb插件。v1and v2,两个 pod 都侦听端口8080并作为节点端口公开,如下所示:$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello1-svc NodePort 10.110.211.119 <none> 8080:31243/TCP 95m
hello2-svc NodePort 10.96.9.66 <none> 8080:31316/TCP 93m
Run Code Online (Sandbox Code Playgroud)
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress-service
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/use-regex: "true"
spec:
rules:
- host: ticketing.dev
http:
paths:
- path: /api/users/?(.*)
backend:
serviceName: hello1-svc
servicePort: 8080
- path: /?(.*)
backend:
serviceName: hello2-svc
servicePort: 8080
Run Code Online (Sandbox Code Playgroud)
nginx-ingress暴露控制器部署的服务,这样所有标签和设置都将被继承:$ kubectl expose deployment ingress-nginx-controller --target-port=80 --type=NodeP
ort -n kube-system
service/ingress-nginx-controller exposed
Run Code Online (Sandbox Code Playgroud)
$ kubectl apply -f ingress.yaml
ingress.networking.k8s.io/ingress-service created
$ kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
ingress-service <none> ticketing.dev 172.17.0.4 80 56s
$ minikube ip
172.17.0.4
Run Code Online (Sandbox Code Playgroud)
$ tail -n 1 /etc/hosts
172.17.0.4 ticketing.dev
$ curl http://ticketing.dev/?foo
Hello, world!
Version: 2.0.0
Hostname: hello2-67bbbf98bb-s78c4
$ curl http://ticketing.dev/api/users/?foo
Hello, world!
Version: 1.0.0
Hostname: hello-576585fb5f-67ph5
Run Code Online (Sandbox Code Playgroud)
alpinepod 来测试从集群内部的访问:$ kubectl run --generator=run-pod/v1 -it alpine --image=alpine -- /bin/sh
/ # nslookup ingress-nginx-controller.kube-system.svc.cluster.local
Server: 10.96.0.10
Address: 10.96.0.10:53
Name: ingress-nginx-controller.kube-system.svc.cluster.local
Address: 10.98.167.112
/ # apk update
/ # apk add curl
/ # curl -H "Host: ticketing.dev" ingress-nginx-controller.kube-system.svc.cluster.local/?foo
Hello, world!
Version: 2.0.0
Hostname: hello2-67bbbf98bb-s78c4
/ # curl -H "Host: ticketing.dev" ingress-nginx-controller.kube-system.svc.cluster.local/api/users/?foo
Hello, world!
Version: 1.0.0
Hostname: hello-576585fb5f-67ph5
Run Code Online (Sandbox Code Playgroud)
如您所见,所有请求均已完成。
笔记:
正如@suren所指出的,在卷曲入口时,我必须用 -H 指定主机
服务名称需要完全是 FQDN,因为我们正在处理托管在另一个命名空间中的服务,使用格式<SVC_NAME>.<NAMESPACE>.svc.cluster.local.
在您的 JS 应用程序中,您必须传递Host参数才能到达入口。
如果您有任何问题,请在评论中告诉我。
| 归档时间: |
|
| 查看次数: |
2712 次 |
| 最近记录: |