无法访问外部具有负载均衡器的Kubernetes集群中的文件

时间:2020-10-04 10:52:13

标签: kubernetes azure-aks

我在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之间更改端口,但这没有区别

1 个答案:

答案 0 :(得分:1)

由用户@David Maze发布:

您要连接的确切URL是什么?你得到什么错误? (在负载平衡器自动缩放器服务上,targetPort需要匹配端口的名称或端口号:在pod中,或者您可以将hpa-example服务更改为键入:LoadBalancer。)

我转载了您的情况,发现您的配置中存在问题,可能使您无法连接到此Deployment

Deployment类型的ServiceNodePort的角度来看,一切似乎都可以正常工作。

另一方面,如果涉及类型为Service的{​​{1}}:

LoadBalancer

此定义会将您的流量直接发送到端口 apiVersion: v1 kind: Service metadata: name: load-balancer-autoscaler spec: selector: app: hpa-example ports: - port: 31001 targetPort: 31001 # <--- CULPRIT type: LoadBalancer 上的Pod,并且应将其发送到端口31001 (这是您的应用响应的端口)。您可以通过以下方式更改它:

  • 80
  • targetPort: 80

您还可以按照用户@David Maze的指示,将targetPort: http-portService)中的NodePort更改为hpa-example

更改此定义后,您将可以运行:

LoadBalancer

$ 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的回复

我鼓励您查看有关Kubernetes服务的其他资源: