Fix Python App Deployed on Kubernetes Cluster
This article details the successful troubleshooting and resolution of a misconfiguration issue preventing a Python Flask application from being accessible on a Kubernetes cluster via the required NodePort. The deployment, named python-deployment-nautilus
, is now fully operational and accessible as specified.
Overview of the Problem
Initial attempts to access the application failed, and terminal output confirmed the service was not configured correctly.
The specific requirements for the fix were:
- Deployment Name:
python-deployment-nautilus
- Service Type:
NodePort
- Target Port: The default Flask application port (
5000
). - NodePort:
32345
.
Three Critical Issues Identified and Resolved
The resolution required correcting errors across both the Deployment and the associated Service objects.
1. Correcting the Deployment Image (Initial Fix)
The primary reason the application was failing to start was an incorrect image name used in the Deployment manifest.
Initial Status: The Deployment template was using the wrong image (
poroko/flask-app-demo
).-
Resolution: I used the
kubectl edit
command to update the Deployment and specify the correct image:poroko/flask-demo-appimage
.
thor@jumphost ~$ kubectl edit deployment python-deployment-nautilus
(Inside the editor, the image was changed from the incorrect value to
poroko/flask-demo-appimage
)
2. Fixing the Service's Target Port
The Service, named python-service-nautilus
, was correctly set to use NodePort: 32345
, but was routing internal traffic to the wrong container port.
-
Initial Status: The service object confirmed the port mapping issue:
thor@jumphost ~$ kubectl get svc python-service-nautilus NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE python-service-nautilus NodePort 10.96.182.199 <none> 8080:32345/TCP 5m27s
The
targetPort
was implicitly or explicitly set to8080
. -
Resolution: The Service object was edited to explicitly change the
targetPort
from8080
to5000
, the standard listening port for a Flask application.
thor@jumphost ~$ kubectl edit service python-service-nautilus
(Inside the editor, the following change was made in the
spec.ports
section:)
ports: - nodePort: 32345 port: 8080 protocol: TCP targetPort: 5000 # CHANGED FROM 8080 TO 5000
3. Validating Service Selector Matching
A check was performed to ensure the Service could locate the Pods. This involved inspecting the labels used by the Deployment and confirming they matched the Service selector.
-
Validation Command:
thor@jumphost ~$ kubectl get deployment python-deployment-nautilus -o yaml | grep 'selector:' -A 3
Result: The output confirmed the selector label was
app: python_app
for the Pods, which successfully matched the selector already present in the Service manifest, ensuring proper traffic routing.
Conclusion of the Task
With the corrections made to the Deployment image and the Service's targetPort
, the Kubernetes application is now fully functional and compliant with all project requirements.
The application is successfully deployed as python-deployment-nautilus
and is externally accessible via the Service python-service-nautilus
on the specified NodePort 32345
.
Top comments (0)