为什么我的 Flask 应用只在 AKS 上的一个副本上正常工作?

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

Why does my Flask application only work properly on AKS with one replica?

问题

当我在AKS上部署一个简单的Flask应用程序,只有一个副本时,该应用程序按预期运行。但是当我部署具有两个副本的应用程序时,它并不按预期工作。用户在成功登录后无法重定向到主页,有时它会工作。

我们如何管理具有两个副本的Flask应用程序?

以下是我的Kubernetes清单供您参考。

谢谢

Deployment.yaml

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: frontpage
  5. namespace: default
  6. spec:
  7. replicas: 1
  8. selector:
  9. matchLabels:
  10. app: frontpage
  11. template:
  12. metadata:
  13. labels:
  14. app: frontpage
  15. spec:
  16. containers:
  17. - name: frontpage
  18. image: ***.azurecr.io/frontpage:latest
  19. limits:
  20. ports:
  21. - containerPort: 5000

Service.yaml

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: frontpage
  5. namespace: default
  6. spec:
  7. selector:
  8. app: frontpage
  9. ports:
  10. - name: http
  11. port: 80
  12. targetPort: 5000
  13. type: ClusterIP

Ingress.yaml

  1. apiVersion: networking.k8s.io/v1
  2. kind: Ingress
  3. metadata:
  4. name: frontpage
  5. namespace: default
  6. annotations:
  7. nginx.ingress.kubernetes.io/rewrite-target: /
  8. spec:
  9. ingressClassName: nginx
  10. tls:
  11. - hosts:
  12. - www.***.com
  13. secretName: ingress-tls-csi
  14. rules:
  15. - host: www.***.com
  16. http:
  17. paths:
  18. - path: /
  19. pathType: Prefix
  20. backend:
  21. service:
  22. name: frontpage
  23. port:
  24. number: 80
英文:

When I deploy a simple flask application on AKS with one replica, the application is running as expected. But when I deploy the application with two replicas, it was not working as expected. User not able to redirect home page post sign in was successful, sometimes it works.

How do we manage the flask application with two replicas?

Here are my kubernetes manifests for your reference.

Thanks

Deployment.yaml

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: frontpage
  5. namespace: default
  6. spec:
  7. replicas: 1
  8. selector:
  9. matchLabels:
  10. app: frontpage
  11. template:
  12. metadata:
  13. labels:
  14. app: frontpage
  15. spec:
  16. containers:
  17. - name: frontpage
  18. image: ***.azurecr.io/frontpage:latest
  19. limits:
  20. ports:
  21. - containerPort: 5000

Service.yaml

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: frontpage
  5. namespace: default
  6. spec:
  7. selector:
  8. app: frontpage
  9. ports:
  10. - name: http
  11. port: 80
  12. targetPort: 5000
  13. type: ClusterIP

Ingress.yaml

  1. apiVersion: networking.k8s.io/v1
  2. kind: Ingress
  3. metadata:
  4. name: frontpage
  5. namespace: default
  6. annotations:
  7. nginx.ingress.kubernetes.io/rewrite-target: /
  8. spec:
  9. ingressClassName: nginx
  10. tls:
  11. - hosts:
  12. - www.***.com
  13. secretName: ingress-tls-csi
  14. rules:
  15. - host: www.***.com
  16. http:
  17. paths:
  18. - path: /
  19. pathType: Prefix
  20. backend:
  21. service:
  22. name: frontpage
  23. port:
  24. number: 80

答案1

得分: 0

尝试删除注释 nginx.ingress.kubernetes.io/rewrite-target 或添加 捕获组

更新

Your application 使用了 文件系统会话亲和性,因此包含一个状态(参见 SESSION_TYPE)。这个状态不会在副本之间共享。

您能否更改 会话后端?您还可以考虑在副本之间用卷共享后端以进行测试。

英文:

Try to delete the annotation nginx.ingress.kubernetes.io/rewrite-target or to add the captured group.


UPDATE

Your application uses a file system session affinity and therefore contains a state (see SESSION_TYPE). This state is not shared between your replicas.

Can you change the session backend? You can also probably share the backend with a volume between replicas for testing.

答案2

得分: 0

谢谢你的更新。我已经配置了 app_config.py,如下所示:

SESSION_TYPE = "filesystem"

SESSION_FILE_DIR = "/mnt/blob"

并更新了我的 kubernetes manifests,包括卷的详细信息,

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: frontpage
  5. spec:
  6. replicas: 2
  7. selector:
  8. matchLabels:
  9. app: frontpage
  10. template:
  11. metadata:
  12. labels:
  13. app: frontpage
  14. spec:
  15. containers:
  16. - name: frontpage
  17. image: ****.azurecr.io/frontpage:latest
  18. resources:
  19. limits:
  20. cpu: 250m
  21. memory: 1000Gi
  22. requests:
  23. cpu: 100m
  24. memory: 128Mi
  25. volumeMounts:
  26. - name: flask-session-volume
  27. mountPath: "/mnt/blob"
  28. volumes:
  29. - name: flask-session-volume
  30. persistentVolumeClaim:
  31. claimName: pvc-blob
  32. ---
  33. apiVersion: v1
  34. kind: Service
  35. metadata:
  36. name: frontpage
  37. spec:
  38. selector:
  39. app: frontpage
  40. ports:
  41. - name: http
  42. port: 80
  43. targetPort: 5000
  44. type: ClusterIP
  45. ---
  46. apiVersion: networking.k8s.io/v1
  47. kind: Ingress
  48. metadata:
  49. name: frontpage
  50. annotations:
  51. nginx.ingress.kubernetes.io/use-regex: "true"
  52. spec:
  53. ingressClassName: nginx
  54. tls:
  55. - hosts:
  56. - ***.host.com
  57. secretName: ingress-tls-csi
  58. rules:
  59. - host: ***.host.com
  60. http:
  61. paths:
  62. - path: /
  63. pathType: Prefix
  64. backend:
  65. service:
  66. name: frontpage
  67. port:
  68. number: 80
英文:

Thank you for your update. I have configured app_config.py, like

SESSION_TYPE = "filesystem"

SESSION_FILE_DIR = "/mnt/blob"

And updated my kubernetes manifests with volume details,

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: frontpage
  5. spec:
  6. replicas: 2
  7. selector:
  8. matchLabels:
  9. app: frontpage
  10. template:
  11. metadata:
  12. labels:
  13. app: frontpage
  14. spec:
  15. containers:
  16. - name: frontpage
  17. image: ****.azurecr.io/frontpage:latest
  18. resources:
  19. limits:
  20. cpu: 250m
  21. memory: 1000Gi
  22. requests:
  23. cpu: 100m
  24. memory: 128Mi
  25. volumeMounts:
  26. - name: flask-session-volume
  27. mountPath: "/mnt/blob"
  28. volumes:
  29. - name: flask-session-volume
  30. persistentVolumeClaim:
  31. claimName: pvc-blob
  32. ---
  33. apiVersion: v1
  34. kind: Service
  35. metadata:
  36. name: frontpage
  37. spec:
  38. selector:
  39. app: frontpage
  40. ports:
  41. - name: http
  42. port: 80
  43. targetPort: 5000
  44. type: ClusterIP
  45. ---
  46. apiVersion: networking.k8s.io/v1
  47. kind: Ingress
  48. metadata:
  49. name: frontpage
  50. annotations:
  51. nginx.ingress.kubernetes.io/use-regex: "true"
  52. spec:
  53. ingressClassName: nginx
  54. tls:
  55. - hosts:
  56. - ***.host.com
  57. secretName: ingress-tls-csi
  58. rules:
  59. - host: ***.host.com
  60. http:
  61. paths:
  62. - path: /
  63. pathType: Prefix
  64. backend:
  65. service:
  66. name: frontpage
  67. port:
  68. number: 80

huangapple
  • 本文由 发表于 2023年5月24日 21:02:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323854.html
匿名

发表评论

匿名网友

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

确定