Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes - Deploying Multiple Images into a single Pod

I'm having an issue where because an application was originally configured to execute on docker-compose. I managed to port and rewrite the .yaml deployment files to Kubernetes, however, the issue lies within the communication of the pods.

The frontend communicates with the backend to access the services, and I assume as it should be in the same network, the frontend calls the services from the localhost. I don't have access to the code, as it is an proprietary application that was developed by a company and it does not support Kubernetes, so modifying the code is out of question.

I believe the main reason is because the frontend and backend are runnning on different pods, with different IPs.

When the frontend tries to call the APIs, it does not find the service, and returns an error. Therefore, I'm trying to deploy both the frontend image and backend image into the same pod, so they share the same Cluster IP.

Unfortunately, I do not know how to make a yaml file to create both containers within a single pod.

Is it possible to have both frontend and backend containers running on the same pod, or would there be another way to make the containers communicate (maybe a proxy)?

like image 572
user3653379 Avatar asked Sep 08 '18 03:09

user3653379


People also ask

Can a single container have multiple images?

Can we have a single container with multiple image like nginx + Redis + alpine ? You can have multiple processes inside a container but it wouldn't really be a best practice. Having NGINX and Redis in separate containers but inside the same pod would be better.

How do I make multiple containers in one pod?

Creating a pod. Multi-container pods must be created with the create command. Properties are passed to the command as a YAML- or JSON-formatted configuration file. The create command can be used to create a pod directly, or it can create a pod or pods through a Deployment .

Can a pod contains multiple containers?

At the same time, a Pod can contain more than one container, usually because these containers are relatively tightly coupled.

Can a Kubernetes deployment have multiple pods?

A Deployment is meant to represent a single group of PODs fulfilling a single purpose together. You can have many Deployments work together in the virtual network of the cluster. For accessing a Deployment that may consist of many PODs running on different nodes you have to create a Service.

How do I create a multi-container pod in Kubernetes?

As with every file definition in Kubernetes, we define our multi-container pod in a YAML file. So, the first step is to create a new file with the command: In the above YAML file, you will see that we have deployed a container based on the Nginx image, as our web server.

How to deploy Docker image to Kubernetes with kubectl?

Deploy Docker Image to Kubernetes Quickly with – Kubectl run command ( Deprecated) You make the choice. In this step, we are instantiating our Docker Image as Container. As you know the basic and the core element of Kubernetes is POD and that’s a logical group of one or more containers.

What is ifnotpresent in Kubernetes deployment?

When you first create a Deployment , StatefulSet, Pod, or other object that includes a Pod template, then by default the pull policy of all containers in that pod will be set to IfNotPresent if it is not explicitly specified. This policy causes the kubelet to skip pulling an image if it already exists.

What does imagepullbackoff mean in Kubernetes?

The status ImagePullBackOff means that a container could not start because Kubernetes could not pull a container image (for reasons such as invalid image name, or pulling from a private registry without imagePullSecret ). The BackOff part indicates that Kubernetes will keep trying to pull the image, with an increasing back-off delay.


2 Answers

Therefore, I'm trying to deploy both the frontend image and backend image into the same pod, so they share the same Cluster IP.

Although you have the accepted answer already in place that is tackling example of running more containers in the same pod I'd like to point out few details:

  • Containers should be in the same pod only if they scale together (not if you want to communicate over clusterIP amongst them). Your scenario of frontend/backend division doesn't really look like good candidate to cram them together.

  • If you opt for containers to be in the same pod they can communicate over localhost (they see each other as if two processes are running on the same host (except the part their file systems are different) and can use localhost for direct communication and because of that can't allocate both the same port. Using cluster IP is as if on same host two processes are communicating over external ip).

  • More kubernetes philosophy approach here would be to:

    • Create deployment for backend
    • Create service for backend (exposing necessary ports)
    • Create deployment for frontend
    • Communicate from frontend to backend using backend service name (kube-dns resolves this to cluster ip of backend service) and designated backend ports.
    • Optionally (for this example) create service for frontend for external access or whatever goes outside. Note that here you can allocate same port as for backend service since they are not living on the same pod (host)...

Some of the benefits of this approach include: you can isolate backend better (backend-frontend communication is within cluster only, not exposed to outside world), you can schedule them independently on nodes, you can scale them independently (say you need more backend power but fronted is handling traffic ok or viceversa), you can replace any of them independently etc...

like image 177
Const Avatar answered Nov 13 '22 00:11

Const


Yes, you just add entries to the containers section in your yaml file, example:

apiVersion: v1
kind: Pod
metadata:
name: two-containers
spec:
    restartPolicy: Never
containers:
    - name: nginx-container
      image: nginx
    - name: debian-container
      image: debian
like image 27
Chris Johnson Avatar answered Nov 13 '22 00:11

Chris Johnson