Digital Ocean:负载均衡器在 Flask 应用程序的 Pod 上无法正常工作。

huangapple go评论53阅读模式
英文:

digital ocean: load balancer not working for flask app pods

问题

我正在尝试在Digital Ocean Kubernetes集群中部署一个简单的Python Flask应用程序,使用以下部署和服务配置。Flask应用程序在运行代码时使用8080端口,并通过负载均衡器暴露相同的端口。

Flask应用程序

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=8080)

部署

apiVersion: apps/v1
kind: Deployment
metadata:
  name: unit7-app-deploy
spec:
  replicas: 2
  selector:
    matchLabels:
      app: unit-app
  template:
    metadata:
      labels:
        app: unit-app
    spec:
      containers:
      - name: unit-app
        image: <username>/flask-app:latest
        imagePullPolicy: IfNotPresent
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
        - containerPort: 8080

用于负载均衡创建的服务

apiVersion: v1
kind: Service
metadata:
  name: unit7-app-service
spec:
  selector:
    app: unit7-app-deploy
  ports:
  - port: 8080
    targetPort: 8080
    protocol: TCP
  type: LoadBalancer

现在我正在尝试通过Kubernetes外部IP和8080端口访问我的应用程序,但不起作用。Pod的日志显示我的Flask正在运行。

您哪里出错了?请帮助我。

英文:

i am trying deploy a simple python flask application in digital ocean Kubernetes cluster using below deployment and service configuration. flask app is using 8080 port while running the code and same is used to expose through load balancer.

flask app

	if __name__ == &#39;__main__&#39;:
		app.run(debug=True, host=&#39;0.0.0.0&#39;, port=8080)

deployment

	ubuntu@ubuntu-22lts:~$ cat deployment.yaml
	apiVersion: apps/v1
	kind: Deployment
	metadata:
	  name: unit7-app-deploy
	spec:
	  replicas: 2
	  selector:
		matchLabels:
		  app: unit-app
	  template:
		metadata:
		  labels:
			app: unit-app
		spec:
		  containers:
		  - name: unit-app
			image: &lt;username&gt;/flask-app:latest
			imagePullPolicy: IfNotPresent
			resources:
			  limits:
				memory: &quot;128Mi&quot;
				cpu: &quot;500m&quot;
			ports:
			- containerPort: 8080

and service for load balance creation

	apiVersion: v1
	kind: Service
	metadata:
	  name: unit7-app-service
	spec:
	  selector:
		app: unit7-app-deploy
	  ports:
	  - port: 8080
		targetPort: 8080
		protocol: TCP
	  type: LoadBalancer

Now I am trying to access my app through Kubernetes external IP and 8080 port which is not working. pods logs are showing that my flaks is running.

	ubuntu@ubuntu-22lts:~$ kubectl --kubeconfig=k8s-1-27-2-do-0-blr1-cluster1-kubeconfig.yaml logs -f unit7-app-deploy-6568dss8-ddsds
	 * Serving Flask app &#39;run&#39;
	 * Debug mode: on
	WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
	 * Running on all addresses (0.0.0.0)
	 * Running on http://127.0.0.1:8080
	 * Running on http://10.244.0.214:8080

where am I wrong. Kindly help me.

答案1

得分: 1

以下是翻译好的内容:

"Just in case this is part of your issue, your Service's selector does not match your Deployment's label.
The selector in your service should match the labels in your deployment to establish the correct connection.

In your Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: unit7-app-deploy
spec:
  replicas: 2
  selector:
    matchLabels:
      app: unit-app

The label for the pods being deployed is app: unit-app.

But in your Service:

apiVersion: v1
kind: Service
metadata:
  name: unit7-app-service
spec:
  selector:
    app: unit7-app-deploy

Here, the service selector is looking for pods with label app: unit7-app-deploy.

The selector in the service configuration should match the label you defined in your Deployment configuration for the pods, which is app: unit-app.
For instance, service configuration should look like:

apiVersion: v1
kind: Service
metadata:
  name: unit7-app-service
spec:
  selector:
    app: unit-app # Change from 'unit7-app-deploy' to 'unit-app'
  ports:
  - port: 8080
    targetPort: 8080
    protocol: TCP
  type: LoadBalancer

That mismatch is likely the reason your load balancer is not working correctly, as it is not able to find the correct pods to send traffic to."

英文:

Just in case this is part of your issue, your Service's selector does not match your Deployment's label.
The selector in your service should match the labels in your deployment to establish the correct connection.

In your Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: unit7-app-deploy
spec:
  replicas: 2
  selector:
    matchLabels:
      app: unit-app

The label for the pods being deployed is app: unit-app.

But in your Service:

apiVersion: v1
kind: Service
metadata:
  name: unit7-app-service
spec:
  selector:
    app: unit7-app-deploy

Here, the service selector is looking for pods with label app: unit7-app-deploy.

The selector in the service configuration should match the label you defined in your Deployment configuration for the pods, which is app: unit-app.
For instance, service configuration should look like:

apiVersion: v1
kind: Service
metadata:
  name: unit7-app-service
spec:
  selector:
    app: unit-app # Change from &#39;unit7-app-deploy&#39; to &#39;unit-app&#39;
  ports:
  - port: 8080
    targetPort: 8080
    protocol: TCP
  type: LoadBalancer

That mismatch is likely the reason your load balancer is not working correctly, as it is not able to find the correct pods to send traffic to.

huangapple
  • 本文由 发表于 2023年7月5日 01:25:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614770.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定