无法访问外部具有负载均衡器的 Kubernetes 集群内的文件
Cannot access file inside Kubernetes cluster that has load balancer externally
我在 AKS 中设置了以下集群
apiVersion: apps/v1
kind: Deployment
metadata:
name: hpa-example
spec:
replicas: 3
selector:
matchLabels:
app: hpa-example
template:
metadata:
labels:
app: hpa-example
spec:
containers:
- name: hpa-example
image: gcr.io/google_containers/hpa-example
ports:
- name: http-port
containerPort: 80
resources:
requests:
cpu: 200m
---
apiVersion: v1
kind: Service
metadata:
name: hpa-example
spec:
ports:
- port: 31001
nodePort: 31001
targetPort: http-port
protocol: TCP
selector:
app: hpa-example
type: NodePort
---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: hpa-example-autoscaler
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: hpa-example
minReplicas: 1
maxReplicas: 10
targetCPUUtilizationPercentage: 50
这样做的想法是检查AutoScaling
我需要外部可用,所以我添加了
apiVersion: v1
kind: Service
metadata:
name: load-balancer-autoscaler
spec:
selector:
app: hpa-example
ports:
- port: 31001
targetPort: 31001
type: LoadBalancer
这现在给了我一个外部 IP,但是我无法在 Postman 中或通过浏览器连接到它
我错过了什么?
我曾尝试更改 80 和 31001 之间的端口,但这没有什么区别
由用户@David Maze 发布:
What's the exact URL you're trying to connect to? What error do you get? (On the load-balancer-autoscaler service, the targetPort needs to match the name or number of a ports: in the pod, or you could just change the hpa-example service to type: LoadBalancer.)
我重现了您的场景,发现您的配置中存在问题,可能会导致您无法连接到此 Deployment
。
从 Deployment
和 Service
类型 NodePort
的角度来看,一切似乎都正常。
如果涉及类型 LoadBalancer
的 Service
另一方面:
apiVersion: v1
kind: Service
metadata:
name: load-balancer-autoscaler
spec:
selector:
app: hpa-example
ports:
- port: 31001
targetPort: 31001 # <--- CULPRIT
type: LoadBalancer
此定义会将您的流量直接发送到端口 31001
上的 pods 并且 它应该将其发送到端口 80
(这是您的应用响应的端口)。您可以通过以下任一方式更改它:
targetPort: 80
targetPort: http-port
You could also change the Service
of the NodePort
(hpa-example
) to LoadBalancer
as pointed by user @David Maze!
更改此定义后,您将能够 运行:
$ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
load-balancer-autoscaler LoadBalancer 10.4.32.146 AA.BB.CC.DD 31001:31497/TCP 9m41s
curl AA.BB.CC.DD:31001
得到OK!
的回复
我鼓励您查看有关 Kubernetes 服务的其他资源:
我在 AKS 中设置了以下集群
apiVersion: apps/v1
kind: Deployment
metadata:
name: hpa-example
spec:
replicas: 3
selector:
matchLabels:
app: hpa-example
template:
metadata:
labels:
app: hpa-example
spec:
containers:
- name: hpa-example
image: gcr.io/google_containers/hpa-example
ports:
- name: http-port
containerPort: 80
resources:
requests:
cpu: 200m
---
apiVersion: v1
kind: Service
metadata:
name: hpa-example
spec:
ports:
- port: 31001
nodePort: 31001
targetPort: http-port
protocol: TCP
selector:
app: hpa-example
type: NodePort
---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: hpa-example-autoscaler
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: hpa-example
minReplicas: 1
maxReplicas: 10
targetCPUUtilizationPercentage: 50
这样做的想法是检查AutoScaling
我需要外部可用,所以我添加了
apiVersion: v1
kind: Service
metadata:
name: load-balancer-autoscaler
spec:
selector:
app: hpa-example
ports:
- port: 31001
targetPort: 31001
type: LoadBalancer
这现在给了我一个外部 IP,但是我无法在 Postman 中或通过浏览器连接到它
我错过了什么?
我曾尝试更改 80 和 31001 之间的端口,但这没有什么区别
由用户@David Maze 发布:
What's the exact URL you're trying to connect to? What error do you get? (On the load-balancer-autoscaler service, the targetPort needs to match the name or number of a ports: in the pod, or you could just change the hpa-example service to type: LoadBalancer.)
我重现了您的场景,发现您的配置中存在问题,可能会导致您无法连接到此 Deployment
。
从 Deployment
和 Service
类型 NodePort
的角度来看,一切似乎都正常。
如果涉及类型 LoadBalancer
的 Service
另一方面:
apiVersion: v1
kind: Service
metadata:
name: load-balancer-autoscaler
spec:
selector:
app: hpa-example
ports:
- port: 31001
targetPort: 31001 # <--- CULPRIT
type: LoadBalancer
此定义会将您的流量直接发送到端口 31001
上的 pods 并且 它应该将其发送到端口 80
(这是您的应用响应的端口)。您可以通过以下任一方式更改它:
targetPort: 80
targetPort: http-port
You could also change the
Service
of theNodePort
(hpa-example
) toLoadBalancer
as pointed by user @David Maze!
更改此定义后,您将能够 运行:
$ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
load-balancer-autoscaler LoadBalancer 10.4.32.146 AA.BB.CC.DD 31001:31497/TCP 9m41s
curl AA.BB.CC.DD:31001
得到OK!
的回复
我鼓励您查看有关 Kubernetes 服务的其他资源: