I am new to Kubernetes and I'm trying to deploy an application to kubernetes via microk8s. The application contains python flask backend, angular frontend, redis and mysql database. I deployed the images in multiple pods and the status is showing "running" but the pods are not communicating with each other.
Then app is completely dockerized and its functioning in the docker level.
Before deploying into kubernetes my flask host was 0.0.0.0 and mysql host was "service name" in the docker-compose.yaml but currently I replaced it with service names of kubernetes yml file.
Also, in angular frontend I have changed the url to connect to backed as http://localhost:5000 to http://backend-service, where backend-service is the name(dns) given in the backend-service.yml file. But this also didn't make any change. Can someone tell me how can I make these pods communicate?
I am able to access only the frontend after deploying rest is not connected.
Listing down the service and deployment files of angular, backend.
apiVersion: v1
kind: Service
metadata:
name: angular-service
spec:
type: NodePort
selector:
name: angular
ports:
- protocol: TCP
nodePort: 30042
targetPort: 4200
port: 4200
apiVersion: v1
kind: Service
metadata:
name: backend-service
spec:
type: ClusterIP
selector:
name: backend
ports:
- protocol: TCP
targetPort: 5000
port: 5000
Thanks in advance!
(Modified service files)
解决方案
For internal communication between different microservices in Kubernetes you should use Service of a type ClusterIP. It is actually the default type so even if you don't specify it in your Service yaml definition file, Kubernetes assumes you want to create ClusterIP.
It creates virtual internal IP (accessible within your Kubernetes cluster) and exposes your cluster component (microservice) as a single entry point even if it is backed up by many pods.
Let's assume you have front-end app which needs to communicate with back-end component which is run in 3 different pods. ClusterIP service provides single entry point and handles load-balancing between different pods, distributing requests evenly among them.
You can access your ClusterIP service by providing its IP address and port that your application component is exposed on. Note that you may define a different port (referred to as port in Service definition) for the Service to listen on than the actual port used by your application (referred to as targetPort in your Service definition). Although it is possible to access the Service using its ClusterIP address, all components that communicate with pods internally exposed by it should use its DNS name. It is simply a Service name that you created if all application components are placed in the same namespace. If some components are in a different namespaces you need to use fully qualified domain name so they can communicate across the namespaces.
Your Service definition files may look like this:
apiVersion: v1
kind: Service
metadata:
name: angular-service
spec:
type: ClusterIP ### may be ommited as it is a default type
selector:
name: angular ### should match your labels defined for your angular pods
ports:
- protocol: TCP
targetPort: 4200 ### port your angular app listens on
port: 4200 ### port on which you want to expose it within your cluster
apiVersion: v1
kind: Service
metadata:
name: backend-service
spec:
type: ClusterIP ### may be ommited as it is a default type
selector:
name: backend ### should match your labels defined for your backend pods
ports:
- protocol: TCP
targetPort: 5000 ### port your backend app listens on
port: 5000 ### port on which you want to expose it within your cluster
You can find a detailed description of this topic in official Kubernetes documentation.
NodePort has totally different function. It may be used e.g. to expose your front-end app on a specific port on your node's IP. Note that if you have Kubernetes cluster consisting of many nodes and your front-end pods are placed on different nodes, in order to access your application you need to use 3 different IP addresses. In such case you need an additional load balancer. If you use some cloud platform solution and you want to expose front-end part of your application to external world, Service type LoadBalancer is the way to go (instead of using NodePort).