具有多个容器的容器上具有多个裸露端口的单一服务 [英] single service with multiple exposed ports on a pod with multiple containers

查看:83
本文介绍了具有多个容器的容器上具有多个裸露端口的单一服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个容器可以在同一个吊舱中工作.

I have gotten multiple containers to work in the same pod.

 kubectl apply  -f myymlpod.yml

kubectl expose pod mypod --name=myname-pod --port 8855 --type=NodePort

然后我就可以测试曝光"

then I was able to test the "expose"

minikube service list

..

|-------------|-------------------------|-----------------------------|
|  NAMESPACE  |          NAME           |             URL             |
|-------------|-------------------------|-----------------------------|
| default     | kubernetes              | No node port                |
| default     | myname-pod              | http://192.168.99.100:30036 |
| kube-system | kube-dns                | No node port                |
| kube-system | kubernetes-dashboard    | No node port                |
|-------------|-------------------------|-----------------------------|

现在,myymlpod.yml中包含多个容器. 一个容器的服务可以在8855上运行,而另一个容器则可以在8877上运行.

Now, my myymlpod.yml has multiple containers in it. One container has a service running on 8855, and one on 8877.

下面的文章〜提示〜我需要做什么.

The below article ~hints~ at what I need to do .

https://www. mirantis.com/blog/multi-container-pods-and-container-communication-in-kubernetes/

在Pod中暴露多个容器

尽管此示例显示了如何 使用单个容器访问广告连播中的其他容器,它是 Pod中的多个容器在聆听不同的声音时很常见 端口-所有这些都需要暴露.为此,您可以 创建具有多个公开端口的单一服务,或者您可以 为您要暴露的每一个污点创建一个单一的服务.

Exposing multiple containers in a Pod

While this example shows how to use a single container to access other containers in the pod, it’s quite common for several containers in a Pod to listen on different ports — all of which need to be exposed. To make this happen, you can either create a single service with multiple exposed ports, or you can create a single service for every poirt you’re trying to expose.

创建具有多个公开端口的单个服务"

"create a single service with multiple exposed ports"

我找不到任何有关如何实际执行此操作的信息,公开了多个端口.

I cannot find anything on how to actually do this, expose multiple ports.

如何在一项服务中公开多个端口?

How does one expose multiple ports on a single service?

谢谢.

APPEND:

K8Containers.yml(如下)

K8Containers.yml (below)

apiVersion: v1
kind: Pod
metadata:
  name: mypodkindmetadataname
  labels:
    example: mylabelname

spec:

  containers:

  - name: containername-springbootfrontend
    image: mydocker.com/webfrontendspringboot:latest 
    resources:
      limits:
        memory: "800Mi"
        cpu: "800m" 
      requests:
        memory: "612Mi"
        cpu: "400m"
    ports:
      - containerPort: 8877


  - name: containername-businessservicesspringboot
    image: mydocker.com/businessservicesspringboot:latest
    resources:
      limits:
        memory: "800Mi"
        cpu: "800m" 
      requests:
        memory: "613Mi"
        cpu: "400m" 
    ports:
      - containerPort: 8855


kubectl apply  -f K8containers.yml
pod "mypodkindmetadataname" created

kubectl get pods
NAME                    READY     STATUS    RESTARTS   AGE
mypodkindmetadataname   2/2       Running   0          11s


k8services.yml(如下)


k8services.yml (below)

apiVersion: v1
kind: Service
metadata: 
  name: myymlservice
  labels: 
    name: myservicemetadatalabel
spec: 
  type: NodePort
  ports:
  - name: myrestservice-servicekind-port-name
    port: 8857
    targetPort: 8855
  - name: myfrontend-servicekind-port-name
    port: 8879
    targetPort: 8877
  selector: 
    name: mypodkindmetadataname

........

kubectl apply  -f K8services.yml
service "myymlservice" created

........

 minikube service myymlservice --url
http://192.168.99.100:30784
http://192.168.99.100:31751

........

 kubectl describe service myymlservice


Name:                     myymlservice
Namespace:                default
Labels:                   name=myservicemetadatalabel
Annotations:              kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"name":"myservicemetadatalabel"},"name":"myymlservice","namespace":"default"...
Selector:                 name=mypodkindmetadataname
Type:                     NodePort
IP:                       10.107.75.205
Port:                     myrestservice-servicekind-port-name  8857/TCP
TargetPort:               8855/TCP
NodePort:                 myrestservice-servicekind-port-name  30784/TCP
Endpoints:                <none>
Port:                     myfrontend-servicekind-port-name  8879/TCP
TargetPort:               8877/TCP
NodePort:                 myfrontend-servicekind-port-name  31751/TCP
Endpoints:                <none>
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

....

不幸的是,当我尝试调用公开的"项目时,它仍然无法正常工作.

Unfortunately, it is still not working when I try to invoke the "exposed" items.

通话

http://192.168.99.100:30784/myrestmethod

不起作用

并致电

http://192.168.99.100:31751

http://192.168.99.100:31751/index.html

不起作用

任何人都知道我在想什么.

Anyone see what I'm missing.

APPEND(正在运行)

APPEND (working now)

选择器在名称"上不匹配,在标签上匹配.

The selector does not match on "name", it matches on label(s).

k8containers.yml(部分在顶部)

k8containers.yml (partial at the top)

apiVersion: v1
kind: Pod
metadata:
  name: mypodkindmetadataname
  labels:
    myexamplelabelone: mylabelonevalue
    myexamplelabeltwo: mylabeltwovalue

spec:

  containers:

  # Main application container
  - name: containername-springbootfrontend
    image: mydocker.com/webfrontendspringboot:latest 
    resources:
      limits:
        memory: "800Mi"
        cpu: "800m" 
      requests:
        memory: "612Mi"
        cpu: "400m"
    ports:
      - containerPort: 8877


  - name: containername-businessservicesspringboot
    image: mydocker.com/businessservicesspringboot:latest
    resources:
      limits:
        memory: "800Mi"
        cpu: "800m" 
      requests:
        memory: "613Mi"
        cpu: "400m" 
    ports:
      - containerPort: 8855

k8services.yml

k8services.yml

apiVersion: v1
kind: Service
metadata: 
  name: myymlservice
  labels: 
    name: myservicemetadatalabel
spec: 
  type: NodePort
  ports:
  - name: myrestservice-servicekind-port-name
    port: 8857
    targetPort: 8855
  - name: myfrontend-servicekind-port-name
    port: 8879
    targetPort: 8877
  selector: 
    myexamplelabelone: mylabelonevalue
    myexamplelabeltwo: mylabeltwovalue

推荐答案

是的,您可以创建一个具有多个开放端口或服务端口连接指向容器端口的单一服务.

Yes you can create one single service with multiple ports open or service port connect pointing to container ports.

kind: Service
apiVersion: v1
metadata:
  name: mymlservice
spec:
  selector:
    app: mymlapp
  ports:
  - name: servicename-1
    port: 4444
    targetPort: 8855
  - name: servicename-2
    port: 80
    targetPort: 8877

目标端口正在填充到您的容器端口的地方.

Where target ports are poting out to your container ports.

这篇关于具有多个容器的容器上具有多个裸露端口的单一服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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