Kubernetes - How to set 2 containers in a container?

I am trying to create a container with two containers, each of which has different images! Not sure how to expose two different containers to the client. Below is my yaml file for deployment.

apiVersion: extensions/v1beta1 kind: Deployment metadata: name: checkdifimage spec: replicas: 1 template: metadata: labels: app: checkdifimagelab spec: containers: - name: checkallcont1 image: <dockerimage> ports: - containerPort: 32030 - name: checkall1cont2 image: <dockerimage2> ports: - containerPort: 32031 

What I'm doing now is after my deployment has unfolded. To call the service, run the following command:

 kubectl expose pod checkdifimage --port=8080 --type=NodePort --name=diffimage 

This works for a single container and can be detrimental to service from a rest customer. But when I use 2 containers, I can hit only one container. How do I switch to both containers? In addition, if someone can, please, trace the advantages and disadvantages of using one unit having one image against one container having several images!

+5
source share
2 answers

You have several options:

  • Create several services that expose on one port in one deployment.

  • Create a single service providing multiple ports:

     --- kind: Service apiVersion: v1 metadata: name: my-service spec: selector: app: MyApp ports: - name: http protocol: TCP port: 80 targetPort: 9376 - name: https protocol: TCP port: 443 targetPort: 9377 
  • Using kubectl expose:

     kubectl expose service nginx --port=443 --target-port=8443 --name=nginx-https 

Please note that if the port is not specified through the -port, and the open resource has several ports, all will be reused by the new service. In addition, if no tags are specified, the new service will reuse the tags from the resource that it provides.

A source

When to use several container containers: A container is a group of one or more containers, a common storage for these containers and options for launching containers. Stands are always placed and co-planned and work in a common context. The package simulates a “logical host” for a specific application - it contains one or more application containers that are relatively tightly connected - in the world in front of the container, they would run on the same physical or virtual machine.

enter image description here

+9
source

You can do this on the command line:

 kubectl expose deployment xxx --port=8080,18000 --type=NodePort 

Specify ports with a comma only

0
source

Source: https://habr.com/ru/post/1264293/


All Articles