single service with multiple exposed ports on a pod with multiple containers

微笑、不失礼 提交于 2019-12-23 22:10:15

问题


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

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/

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?

Thank you.

APPEND:

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

calling

http://192.168.99.100:30784/myrestmethod

does not work

and calling

http://192.168.99.100:31751

or

http://192.168.99.100:31751/index.html

does not work

Anyone see what I'm missing.

APPEND (working now)

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

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

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:


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.



来源:https://stackoverflow.com/questions/55935077/single-service-with-multiple-exposed-ports-on-a-pod-with-multiple-containers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!