具有多个容器的pod上具有多个暴露端口的单个服务

问题描述 投票:2回答:1

我有多个容器在同一个pod中工作。

 kubectl apply  -f myymlpod.yml

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

然后我能够测试“暴露”

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上运行。

下面的文章〜暗示〜我需要做什么。

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

在Pod中暴露多个容器

虽然这个例子展示了如何使用单个容器来访问pod中的其他容器,但是Pod中的几个容器监听不同的端口是很常见的 - 所有这些容器都需要暴露。要实现这一点,您可以创建具有多个公开端口的单个服务,也可以为您尝试公开的每个poirt创建单个服务。

“使用多个暴露端口创建单个服务”

我找不到任何关于如何实际执行此操作,暴露多个端口。

如何在单个服务上公开多个端口?

谢谢。

附加:

K8Containers.yml(下)\ t

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(下)

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>

....

不幸的是,当我尝试调用“暴露”的项目时,它仍然无法正常工作。

调用

http://192.168.99.100:30784/myrestmethod

不起作用

并打电话

http://192.168.99.100:31751

要么

http://192.168.99.100:31751/index.html

不起作用

任何人都能看到我所缺少的东西。

APPEND(现在工作)

选择器与“名称”不匹配,它在标签上匹配。

k8containers.yml(部分在顶部)

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

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
docker kubernetes kubernetes-pod
1个回答
2
投票

是的,您可以创建一个单个服务,其中多个端口打开或服务端口连接指向容器端口。

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

目标端口正在灌输到您的容器端口。

© www.soinside.com 2019 - 2024. All rights reserved.