gpt4 book ai didi

docker - Nginx和Ingress与Kubernetes无法路由我的请求

转载 作者:行者123 更新时间:2023-12-02 12:22:15 25 4
gpt4 key购买 nike

我的Docker,Kubernetes(1.7)和Nginx都在我的RHEL7服务器上运行,我自己的服务位于Docker容器中并被Kubernetes占用。我知道Kubernetes正在与docker一起使用,因为我可以使用其自己的IP:PORT地址调用Kubernete pod的get请求,并且它可以工作。我用默认的后端设置了Nginx并完成了所有这些工作。我通过调用get podsget svc命令知道了这一点,一切都在按需运行。创建入口时,我知道Nginx会选择它,因为当我使用kubectl describe pods {NGNIX-CONTROLLER}命令时,我会看到它更新了入口,甚至记录了我命名的入口。现在,我使用kubectl clusterinfo获得了Kubernetes master的IP地址,并使用此IP地址尝试调用我的服务,类似于http://KUBEIPADDRESS/PATH/TO/MY/SERVICE的内容,没有端口号,但是它不起作用。我不知道发生了什么。有人可以帮我为什么Ingress和/或Nnginx无法正确路由到我的服务吗?我将在下面给出我的入口和nginx文件。

(请注意,对于nginx yaml文件,nginx Controller 的部署始终在底部。)

入口Yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: gateway-ingress
annotations:
kubernetes.io/ingress.class: nginx
ingress.kubernetes.io/rewrite-target: /
spec:
backend:
serviceName: default-http-backend
servicePort: 80
rules:
- host: testhost
http:
paths:
- path: /customer
backend:
serviceName: customer
servicePort: 9001

Nginx Controller Yaml
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
name: ingress
rules:
- apiGroups:
- ""
- "extensions"
resources:
- configmaps
- secrets
- services
- endpoints
- ingresses
- nodes
- pods
verbs:
- list
- watch
- apiGroups:
- "extensions"
resources:
- ingresses
verbs:
- get
- apiGroups:
- ""
resources:
- events
- services
verbs:
- create
- list
- update
- get
- apiGroups:
- "extensions"
resources:
- ingresses/status
- ingresses
verbs:
- update
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: Role
metadata:
name: ingress-ns
namespace: kube-system
rules:
- apiGroups:
- ""
resources:
- pods
verbs:
- list
- apiGroups:
- ""
resources:
- services
verbs:
- get
- apiGroups:
- ""
resources:
- endpoints
verbs:
- get
- create
- update
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:
name: ingress-ns-binding
namespace: kube-system
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: ingress-ns
subjects:
- kind: ServiceAccount
name: ingress
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: ingress-binding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: ingress
subjects:
- kind: ServiceAccount
name: ingress
namespace: kube-system
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: default-http-backend
labels:
k8s-app: default-http-backend
namespace: kube-system
spec:
replicas: 1
template:
metadata:
labels:
k8s-app: default-http-backend
spec:
terminationGracePeriodSeconds: 60
containers:
- name: default-http-backend
# Any image is permissable as long as:
# 1. It serves a 404 page at /
# 2. It serves 200 on a /healthz endpoint
image: gcr.io/google_containers/defaultbackend:1.0
livenessProbe:
httpGet:
path: /healthz
port: 8080
scheme: HTTP
initialDelaySeconds: 30
timeoutSeconds: 5
ports:
- containerPort: 8080
resources:
limits:
cpu: 10m
memory: 20Mi
requests:
cpu: 10m
memory: 20Mi
---
apiVersion: v1
kind: Service
metadata:
name: default-http-backend
namespace: kube-system
labels:
k8s-app: default-http-backend
spec:
ports:
- port: 80
targetPort: 8080
selector:
k8s-app: default-http-backend
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: ingress
namespace: kube-system
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-ingress-controller
labels:
k8s-app: nginx-ingress-controller
namespace: kube-system
spec:
replicas: 1
template:
metadata:
labels:
k8s-app: nginx-ingress-controller
spec:
# hostNetwork makes it possible to use ipv6 and to preserve the source IP correctly regardless of docker configuration
# however, it is not a hard dependency of the nginx-ingress-controller itself and it may cause issues if port 10254 already is taken on the host
# that said, since hostPort is broken on CNI (https://github.com/kubernetes/kubernetes/issues/31307) we have to use hostNetwork where CNI is used
# like with kubeadm
hostNetwork: true
terminationGracePeriodSeconds: 60
serviceAccountName: ingress
containers:
- image: gcr.io/google_containers/nginx-ingress-controller:0.9.0-beta.3
name: nginx-ingress-controller
readinessProbe:
httpGet:
path: /healthz
port: 10254
scheme: HTTP
livenessProbe:
httpGet:
path: /healthz
port: 10254
scheme: HTTP
initialDelaySeconds: 10
timeoutSeconds: 1
ports:
- containerPort: 80
hostPort: 80
- containerPort: 443
hostPort: 443
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
args:
- /nginx-ingress-controller
- --default-backend-service=$(POD_NAMESPACE)/default-http-backend

当我做 kubectl describe ing
Name:                   gateway-ingress
Namespace: default
Address:
Default backend: default-http-backend:80 (<none>)
Rules:
Host Path Backends
---- ---- --------
testhost

/customer customer:9001 ({IP}:9001,{IP}:9001)
Annotations:
rewrite-target: /
Events: <none>

如果有人需要,这是我对客户的部署和服务
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: customer
labels:
run: customer
spec:
replicas: 2
template:
metadata:
labels:
run: customer
spec:
containers:
- name: customer
image: customer
imagePullPolicy: Always
ports:
- containerPort: 9001
protocol: TCP
---
kind: Service
apiVersion: v1
metadata:
name: customer
spec:
selector:
run: customer
type: NodePort
ports:
- name: port1
protocol: TCP
port: 9001
targetPort: 9001

最佳答案

据我所知,您的设置存在一些问题:

您调用的网址中的

  • KUBEIPADDRESS:IP地址不起作用,因为您将Ingress配置为侦听testhost。因此,您需要调用http://testhost/customer,并配置网络以将testhost解析为正确的IP地址
  • ,但是正确的IP地址是什么?您正在尝试在端口80上使用k8s master。如果没有进一步的配置,它将无法正常工作。为此,您需要为Ingress Controller使用NodePort服务,该服务将其公开在端口80(可能是433)上。为了使用较低的端口,您需要允许使用kube-apiserver选项,请参阅https://kubernetes.io/docs/admin/kube-apiserver/上的--service-node-port-range。一旦可行,您可以将k8s集群的任何节点的任何IP地址用于testhost。注意:请确保没有其他应用程序在任何节点上使用这些端口!
  • 关于docker - Nginx和Ingress与Kubernetes无法路由我的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46221090/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com