Nginx控制器为本地中的任何入口资源设置引发404错误。
nginx-ingress控制器设置是根据以下文档中详细介绍的步骤进行的,并且在本地设置中将入口控制器公开为NodePort Service
以下示例入口资源用于测试入口 banana.yaml
kind: Pod
apiVersion: v1
metadata:
name: banana-app
namespace: nginx-ingress
labels:
app: banana
spec:
containers:
- name: banana-app
image: hashicorp/http-echo
args:
- "-text=banana"
---
kind: Service
apiVersion: v1
metadata:
name: banana-service
namespace: nginx-ingress
spec:
selector:
app: banana
ports:
- port: 5678
apple.yaml
kind: Pod
apiVersion: v1
metadata:
name: apple-app
namespace: nginx-ingress
labels:
app: apple
spec:
containers:
- name: apple-app
image: hashicorp/http-echo
args:
- "-text=apple"
---
kind: Service
apiVersion: v1
metadata:
name: apple-service
namespace: nginx-ingress
spec:
selector:
app: apple
ports:
- port: 5678 #
ingressfile.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: example-ingress
namespace: nginx-ingress
annotations:
ingress.kubernetes.io/rewrite-target: /
kubernetes.io/ingress.class: nginx
spec:
rules:
- http:
paths:
- path: /apple
backend:
serviceName: apple-service
servicePort: 5678
- path: /banana
backend:
serviceName: banana-service
servicePort: 5678
以下是服务列表
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-ingress apple-service ClusterIP 10.111.230.109 <none> 5678/TCP 95m
nginx-ingress banana-service ClusterIP 10.102.139.127 <none> 5678/TCP 95m
nginx-ingress nginx-ingress NodePort 10.97.65.187 <none> 80:30207/TCP,443:31031/TCP 6d23h
[root@kube01 ingress]# kubectl get ing
NAME HOSTS ADDRESS PORTS AGE
example-ingress * 80 131m
[root@kube01 ingress]# kubectl describe ing example-ingress
Name: example-ingress
Namespace: default
Address:
Default backend: default-http-backend:80 (<none>)
Rules:
Host Path Backends
---- ---- --------
*
/apple apple-service:5678 (10.244.2.7:5678)
/banana banana-service:5678 (10.244.1.11:5678)
Annotations:
ingress.kubernetes.io/rewrite-target: /
Events: <none>
如果我按如下所示直接卷曲豆荚,则它按预期工作
[root@kube01 ingress]# curl http://10.244.2.7:5678/apple
apple
[root@kube01 ingress]# curl http://10.244.1.11:5678/banana
banana
[root@kube01 ingress]#
但是,如果我尝试通过Nodeport入口控制器访问它,总是找不到如下所示的
[root@kube01 ingress]# curl http://xx.xx.xx.193:30207/apple
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.17.2</center>
</body>
</html>
[root@kube01 ingress]# curl http://xx.xx.xx.193:30207/banana
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.17.2</center>
</body>
</html>
[root@kube01 ingress]#
卷曲http://xx.xx.xx.193:30207/apple 卷曲http://xx.xx.xx.193:30207/banana 从外部或通过浏览器访问时,上述语句应分别显示苹果和香蕉
更新:我进入了运行入口控制器的Pod,检查了它的配置文件,发现入口资源未应用于控制器。我没有运气就试图重新启动该容器。我也尝试过重新应用入口资源,但Pod中的配置文件未更改。进入控制器的所有内容都将404抛出,就像配置文件中的位置标记一样。感谢您提供任何关于为什么资源没有被应用到控制器的帮助
root@nginx-ingress-685d7964cd-djvgf:/etc/nginx# cat nginx.conf
user nginx;
worker_processes auto;
daemon off;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65s;
keepalive_requests 100;
#gzip on;
server_names_hash_max_size 512;
variables_hash_bucket_size 256;
variables_hash_max_size 1024;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80 default_server;
listen 443 ssl default_server;
ssl_certificate /etc/nginx/secrets/default;
ssl_certificate_key /etc/nginx/secrets/default;
server_name _;
server_tokens "on";
access_log off;
location / {
return 404;
}
}
# stub_status
server {
listen 8080;
allow 127.0.0.1;
deny all;
location /stub_status {
stub_status;
}
}
include /etc/nginx/config-version.conf;
include /etc/nginx/conf.d/*.conf;
server {
listen unix:/var/run/nginx-502-server.sock;
access_log off;
location / {
return 502;
}
}
}
stream {
log_format stream-main '$remote_addr [$time_local] '
'$protocol $status $bytes_sent $bytes_received '
'$session_time';
access_log /var/log/nginx/stream-access.log stream-main;
}
root@nginx-ingress-685d7964cd-djvgf:/etc/nginx#
答案 0 :(得分:0)
我的评论很少(我已经注意到):
1 ,您的入口正在默认名称空间中工作:
UIInputView
2 ,在您的true
中没有类似的注释:
Namespace: default
instead of nginx-ingress namespace
3 ,我遇到了同样的问题,但无法访问该服务,但是当您使用kubectl describe ing example-ingress
选项更改入口控制器的configmap时,它将起作用。然后它将在入口资源中没有kubernetes.io/ingress.class: nginx
的情况下工作。
希望获得帮助
答案 1 :(得分:0)
我在我的情况下修复了它。
我的入口看起来像这样(ingress.yaml
):
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: app-ingress
namespace: sample
spec:
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: web-service
port:
number: 80
我为路径添加了通配符,如下::
- path: /*
并重新应用此入口资源:kubectl apply -f ingress.yaml
然后就成功了!