简体   繁体   English

Kubernetes - 如何在 pod 中公开 2 个不同的容器?

[英]Kubernetes - How to expose 2 different containers in a pod?

I am trying to create a pod with 2 containers each having different images!我正在尝试创建一个包含 2 个容器的 pod,每个容器都有不同的图像! Am not sure how to expose the two different containers to the client.我不确定如何向客户端公开两个不同的容器。 Following is my yaml file for deployment.以下是我用于部署的 yaml 文件。

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 am currently doing is after I have the deployment up.目前正在做的是在我完成部署之后。 I run the following command to expose the service :我运行以下命令来公开服务:

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

This works for a single container and am able to hit the service from a rest client.这适用于单个容器,并且能够从其余客户端访问服务。 But when I use 2 containers, i am only able to hit one container.但是当我使用 2 个容器时,我只能击中一个容器。 How should I proceed to hit both the containers?我应该如何继续击中两个容器? Also, if someone can please guide on what are the advantages and disadvantages of using one pod having one image vs one pod having multiple images!另外,如果有人可以请指导使用具有一张图像的一个 pod 与具有多个图像的一个 pod 的优缺点是什么!

You have multiple Options:您有多个选项:

  1. Create multiple services exposing one port each, on the same deployment.在同一部署中创建多个服务,每个服务公开一个端口。

  2. Create single service exposing 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
  3. Using kubectl expose:使用 kubectl 暴露:

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

Note that if no port is specified via –port and the exposed resource has multiple ports, all will be re-used by the new service.请注意,如果没有通过 –port 指定端口并且暴露的资源有多个端口,则所有端口都将被新服务重新使用。 Also if no labels are specified, the new service will re-use the labels from the resource it exposes.此外,如果未指定标签,新服务将重新使用它公开的资源中的标签。

Source 来源

When to use multi container pods: A pod is a group of one or more containers, the shared storage for those containers, and options about how to run the containers.何时使用多容器 Pod:Pod 是一组一个或多个容器、这些容器的共享存储以及有关如何运行容器的选项。 Pods are always co-located and co-scheduled , and run in a shared context. Pod 始终位于同一地点和共同调度,并在共享上下文中运行。 A pod models an application-specific “logical host” - it contains one or more application containers which are relatively tightly coupled — in a pre-container world, they would have executed on the same physical or virtual machine. Pod 模拟特定于应用程序的“逻辑主机”——它包含一个或多个相对紧密耦合的应用程序容器——在预容器世界中,它们将在相同的物理或虚拟机上执行。

在此处输入图片说明

You can actually do this in the command line:您实际上可以在命令行中执行此操作:

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

Set the ports just by a comma只用逗号设置端口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM