Kubernetes-如何在Pod中暴露2个不同的容器? [英] Kubernetes - How to expose 2 different containers in a pod?

查看:658
本文介绍了Kubernetes-如何在Pod中暴露2个不同的容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个包含2个容器的容器,每个容器具有不同的图像!不知道如何将两个不同的容器公开给客户端.以下是我要部署的yaml文件.

I am trying to create a pod with 2 containers each having different images! Am not sure how to expose the two different containers to the client. Following 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 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

这适用于单个容器,并且能够从其他客户端访问该服务.但是,当我使用2个容器时,我只能击中一个容器.我应该如何打两个容器?另外,如果有人可以指导使用一个带有一个图像的容器与一个带有多个图像的容器有什么优点和缺点呢?

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. 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!

推荐答案

您有多个选项:

  1. 在同一部署上创建多个服务,每个服务公开一个端口.

  1. Create multiple services exposing one port each, on the same deployment.

创建公开多个端口的单一服务:

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

  • 使用kubectl公开:

  • Using kubectl expose:

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

  • 请注意,如果没有通过–port指定端口,并且公开的资源具有多个端口,则新服务将重新使用所有端口.同样,如果未指定标签,则新服务将重新使用其公开的资源中的标签.

    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. Also if no labels are specified, the new service will re-use the labels from the resource it exposes.

    来源

    何时使用多容器吊舱: 吊舱是一个或多个容器,这些容器的共享存储以及有关如何运行这些容器的选项的一组. Pod始终位于同一位置共同安排,并在共享上下文中运行. Pod为特定于应用程序的逻辑主机"建模-它包含一个或多个应用程序容器,这些容器相对紧密耦合-在容器之前的世界中,它们将在同一台物理或虚拟机上执行

    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. Pods are always co-located and co-scheduled, and run in a shared context. 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.

    这篇关于Kubernetes-如何在Pod中暴露2个不同的容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆