为了自学一些 Kubernetes(Noob-Alert),我在 Raspberry PI 上的私人家庭实验室中设置了一个 K3S Kubernetes 环境,使用 NGINX 作为入口控制器,并且我有点无法将请求的真实 IP 传递给目标 Pod,在我的情况下是 Nextcloud 实例。K3S 的版本是v1.22.5+k3s1
。
K3S 使用 Docker 作为容器运行时并带有--no-deploy traefik
选项进行设置。
之后我使用以下方式部署了 NGINX Ingress Controller
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.0/deploy/static/provider/baremetal/deploy.yaml
然后,在部署 Nextcloud pod 之后,我部署了 Ingress:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
annotations:
nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
spec:
tls:
- hosts:
- my.own-dns.org
secretName: very-secret-ssl-secret
ingressClassName: nginx
rules:
- host: my.own-dns.org
http:
paths:
- path: /somepath
pathType: Prefix
backend:
service:
name: someservice-service
port:
number: 8081
- path: /
pathType: Prefix
backend:
service:
name: nextcloud-service
port:
number: 80
在IngressController的部署中,我在ConfigMap中添加了以下条目:
apiVersion: v1
kind: ConfigMap
metadata:
labels:
helm.sh/chart: ingress-nginx-4.0.10
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/instance: ingress-nginx
app.kubernetes.io/version: 1.1.0
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/component: controller
name: ingress-nginx-controller
namespace: ingress-nginx
data:
allow-snippet-annotations: 'true'
compute-full-forwarded-for: 'true'
use-forwarded-headers: 'true'
enable-real-ip: 'true'
proxy-add-original-uri-header: 'true'
forwarded-for-header: 'X-Forwarded-For'
并将 ServiceType 从 http 服务更改为LoadBalancer
,因此我的 IngressController 服务如下所示:
apiVersion: v1
kind: Service
metadata:
annotations:
labels:
helm.sh/chart: ingress-nginx-4.0.10
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/instance: ingress-nginx
app.kubernetes.io/version: 1.1.0
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/component: controller
name: ingress-nginx-controller
namespace: ingress-nginx
spec:
type: LoadBalancer
ipFamilyPolicy: SingleStack
ipFamilies:
- IPv4
ports:
- name: http
port: 80
protocol: TCP
targetPort: http
appProtocol: http
- name: https
port: 443
protocol: TCP
targetPort: https
appProtocol: https
selector:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/instance: ingress-nginx
app.kubernetes.io/component: controller
到目前为止一切顺利,从互联网访问 Nextcloud 实例运行良好,包括重定向到 https 等。但 Nextcloud 审计日志仅获取内部集群 IP 作为源 IP(令人惊讶的是,没有来自我在集群内部运行的任何服务的 IP),而不是来自外部世界的真实 IP。
那么我遗漏了什么?我尝试设置use-proxy-protocol
为 true,但结果却是ERR_CONNECTION_RESET
。
答案1
您是否尝试过将 spec.externalTrafficPolicy 设置为本地?请查看kubernetes 文档关于其含义。