几个月前我开始使用 Kubernetes,实际上我将微服务迁移到我的牧场主集群 (RKE(。一切正常,我的部署很好,服务也很好。我想使用入口。
一切看起来都不错,服务由入口找到,pod 由服务找到。但是,当我尝试访问该网站时,我有一个来自入口控制器的 404 错误页面。
你可以看到我对两条路径的配置:一条nginx和grafana。 有人知道如何修复它并使用入口来执行反向代理?
非常感谢您的帮助。
我尝试使用没有结果的重写目标,并且不推荐使用添加基网址。
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
field.cattle.io/creatorId: user-cg5r7
field.cattle.io/ingressState: '{"bXktaW5ncmVzcy9kZWZhdWx0L3d3dy5zY29sLWVhLm92aC8vbmdpbngvNDI=":""}'
field.cattle.io/publicEndpoints: '[{"addresses":["51.68.226.21"],"port":80,"protocol":"HTTP","serviceName":"default:nginx-services","ingressName":"default:my-ingress","hostname":"www.scol-ea.ovh","path":"/nginx","allNodes":true}]'
creationTimestamp: "2019-08-31T10:54:25Z"
generation: 2
labels:
cattle.io/creator: or antoine
name: my-ingress
namespace: default
resourceVersion: "106239"
selfLink: /apis/extensions/v1beta1/namespaces/default/ingresses/my-ingress
uid: b27b7b20-cbdd-11e9-b16b-fa163ea73397
spec:
rules:
- host: www.scol-ea.ovh
http:
paths:
- backend:
serviceName: nginx-sample
servicePort: 80
path: /nginx
- backend:
serviceName: prometheus-grafana
servicePort: http
path : /grafana
------------------------
apiVersion: v1
kind: Service
metadata:
annotations:
field.cattle.io/targetWorkloadIds: '["deployment:default:nginx-sample"]'
workload.cattle.io/targetWorkloadIdNoop: "true"
workload.cattle.io/workloadPortBased: "true"
creationTimestamp: "2019-08-31T10:03:47Z"
labels:
cattle.io/creator: norman
name: nginx-sample
namespace: default
ownerReferences:
- apiVersion: apps/v1beta2
controller: true
kind: deployment
name: nginx-sample
uid: 57af9603-cb2a-11e9-b16b-fa163ea73397
resourceVersion: "102071"
selfLink: /api/v1/namespaces/default/services/nginx-sample
uid: 9fffe98c-cbd6-11e9-b16b-fa163ea73397
spec:
clusterIP: 10.43.183.187
ports:
- name: 80tcp02
port: 80
protocol: TCP
targetPort: 80
selector:
workload.user.cattle.io/workloadselector: deployment-default-nginx-sample
sessionAffinity: None
type: ClusterIP
----------------------------
apiVersion: v1
kind: Service
metadata:
creationTimestamp: "2019-08-30T13:44:00Z"
labels:
app: prometheus-grafana
chart: grafana-0.0.31
heritage: Tiller
io.cattle.field/appId: prometheus
release: prometheus
name: prometheus-grafana
namespace: default
resourceVersion: "2536"
selfLink: /api/v1/namespaces/default/services/prometheus-grafana
uid: 38ebd878-cb2c-11e9-b16b-fa163ea73397
spec:
clusterIP: 10.43.142.143
ports:
- name: http
port: 80
protocol: TCP
targetPort: 3000
selector:
app: prometheus-grafana
sessionAffinity: None
type: ClusterIP
实际上,Nginx 入口控制器处理所有无法到达目标位置的请求,并将它们发送到默认后端,导致每个请求都位于default backend - 404
页面中。
由于您已经决定实现基于路径的路由方案,因此重写规则可以通过nginx.ingress.kubernetes.io/rewrite-target
注释来完成:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
labels:
cattle.io/creator: or antoine
name: my-ingress
namespace: default
spec:
rules:
- host: www.scol-ea.ovh
http:
paths:
- backend:
serviceName: nginx-sample
servicePort: 80
path: /nginx(/|$)(.*)
- backend:
serviceName: prometheus-grafana
servicePort: http
path : /grafana(/|$)(.*)
应用上述清单Ingress
后,可以在以下重写示例中提供基于路径的路由:
www.scol-ea.ovh/nginx -> www.scol-ea.ovh/
www.scol-ea.ovh/grafana -> www.scol-ea.ovh/
我不建议跨已发布的清单或任何其他数据对象(如公共终结点(共享任何用户敏感数据。
如果您在 kubernetes 中使用 nginx 作为部署代理并遇到问题,那么以下配置可以解决重定向问题。
您可以在 grafan.ini 文件中包含以下变量,也可以公开为 env 变量,如下所示,
GF_SERVER_DOMAIN=abc.google.com
GF_SERVER_ROOT_URL=%(protocol)s://%(domain)s:/grafana
因此,Grafana 部署将如下所示。
apiVersion: apps/v1
kind: Deployment
metadata:
name: grafana
namespace: monitoring
spec:
replicas: 1
selector:
matchLabels:
app: grafana
spec:
containers:
- name: grafana
image: grafana/grafana:latest
env:
- name: GF_SERVER_DOMAIN
value: "abc.google.com"
- name: GF_SERVER_ROOT_URL
value: "%(protocol)s://%(domain)s:/grafana"
ports:
- name: grafana
containerPort: 3000
resources:
limits:
memory: "2Gi"
cpu: "1000m"
requests:
memory: "1Gi"
cpu: "500m"
volumeMounts:
- mountPath: /var/lib/grafana
name: grafana-storage-volume
- mountPath: /etc/grafana/provisioning/datasources
name: grafana-datasources
readOnly: false
并且您必须更新nginx服务器位置块,如下所示,
location /grafana {
proxy_pass http://grafana.monitoring.svc.cluster.local:3000;
rewrite ^/grafana/(.*) /$1 break;
proxy_set_header Host $host;
}
我相信你应该在你的yaml中使用nginx.ingress.kubernetes.io/backend-protocol: HTTPS
注释
我偶然发现的一件事是,确保在您的规范规则中,后端服务名称是正确的。
取而代之的是:
- path: /app-ui
pathType: Prefix
backend:
service:
name: app-ui-service
port:
number: 8080
我有这个:
- path: /app-ui
pathType: Prefix
backend:
service:
name: app-ui
port:
number: 8080
另一件事,请确保如果您使用的是 Nginx 入口,请指定入口类名。
spec:
ingressClassName: nginx