在具有多个容器的容器上具有多个裸露端口的单一服务

时间:2019-05-01 10:52:47

标签: docker kubernetes kubernetes-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中的多个容器在聆听不同内容时很常见   端口-所有这些都需要暴露。为此,您可以   创建具有多个公开端口的单一服务,或者您可以   为您要揭露的每个污点创建一项服务。

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

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

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

谢谢。

APPEND:

K8Containers.yml(如下)

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

1 个答案:

答案 0 :(得分: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

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