简体   繁体   English

从 pod minikube kubernetes 中的容器公开端口

[英]Expose port from container in a pod minikube kubernetes

I'm new to K8s, I'll try minikube with 2 container running in a pod with this command:我是 K8s 的新手,我将使用以下命令尝试在 pod 中运行 2 个容器的 minikube:

kubectl apply -f deployment.yaml

and this deployment.yml:和这个 deployment.yml:

 apiVersion: v1 kind: Pod metadata: name: site-home spec: restartPolicy: Never volumes: - name: v-site-home emptyDir: {} containers: - name: site-web image: site-home:1.0.0 ports: - containerPort: 80 volumeMounts: - name: v-site-home mountPath: /usr/share/nginx/html/assets/quotaLago - name: site-cron image: site-home-cron:1.0.0 volumeMounts: - name: v-site-home mountPath: /app/quotaLago

I've a shared volume so if I understand I cannot use deployment but only pods (maybe stateful set?)我有一个共享卷,所以如果我明白我不能使用部署而只能使用 Pod(也许是有状态集?)

In any case I want to expose the port 80 from the container site-web in the pod site-home.无论如何,我想从 pod site-home 中的容器 site-web 公开端口 80。 In the official docs I see this for deployments:在官方文档中,我看到了这个部署:

kubectl expose deployment hello-node --type=LoadBalancer --port=8080

but I cannot use for example:但我不能使用例如:

kubectl expose pod site-web --type=LoadBalancer --port=8080

any idea?任何的想法?

but I cannot use for example:但我不能使用例如:

 kubectl expose pod site-web --type=LoadBalancer --port=8080

Of course you can, however exposing a single Pod via LoadBalancer Service doesn't make much sense.当然可以,但是通过LoadBalancer Service公开单个Pod没有多大意义。 If you have a Deployment which typically manages a set of Pods between which the real load can be balanced, LoadBalancer does its job.如果您的Deployment通常管理一组 Pod,在这些Pods之间可以平衡实际负载,则LoadBalancer会完成它的工作。 However you can still use it just for exposing a single Pod .但是,您仍然可以仅将其用于公开单个Pod

Note that your container exposes port 80 , not 8080 ( containerPort: 80 in your container specification) so you need to specify it as target-port in your Service .请注意,您的容器公开端口80 ,而不是8080container规范中的containerPort: 80 ),因此您需要在Service中将其指定为target-port Your kubectl expose command may look like this:您的kubectl expose命令可能如下所示:

kubectl expose pod site-web --type=LoadBalancer --port=8080 --target-port=80

If you provide only --port=8080 flag to your kubectl expose command it assumes that the target-port 's value is the same as value of --port .如果您仅向kubectl expose命令提供--port=8080标志,它会假定target-port的值与--port的值相同。 You can easily check it by yourself looking at the service you've just created:您可以自己查看刚刚创建的服务轻松地检查它:

kubectl get svc site-web -o yaml

and you'll see something like this in spec.ports section:你会在spec.ports部分看到类似这样的内容:

- nodePort: 32576
    port: 8080
    protocol: TCP
    targetPort: 8080

After exposing your Pod (or Deployment ) properly ie using:正确公开您的Pod (或Deployment )后,即使用:

kubectl expose pod site-web --type=LoadBalancer --port=8080 --target-port=80

you'll see something similar:你会看到类似的东西:

  - nodePort: 31181
    port: 8080
    protocol: TCP
    targetPort: 80

After issuing kubectl get services you should see similar output:发出kubectl get services后,您应该会看到类似的输出:

NAME                            TYPE           CLUSTER-IP    EXTERNAL-IP      PORT(S)          AGE
site-web                              ClusterIP      <Cluster IP>   <External IP>           8080:31188/TCP         4m42s

If then you go to http://<External IP>:8080 in your browser or run curl http://<External IP>:8080 you should see your website's frontend.然后,如果您在浏览器中转到http://<External IP>:8080或运行curl http://<External IP>:8080 ,您应该会看到网站的前端。

Keep in mind that this solution makes sense and will be fully functional in cloud environment which is able to provide you with a real load balancer.请记住,此解决方案是有意义的,并且可以在能够为您提供真正的负载平衡器的云环境中完全发挥作用。 Note that if you declare such Service type in Minikukbe in fact it creates NodePort service as it is unable to provide you with a real load balancer.请注意,如果您在Minikukbe中声明此类Service类型,实际上它会创建NodePort服务,因为它无法为您提供真正的负载均衡器。 So your application will be available on your Node's ( your Minikube VM's ) IP address on randomly selected port in range 30000-32767 (in my example it's port 31181 ).因此,您的应用程序将在您的节点(您的 Minikube 虚拟机)的 IP 地址上在随机选择的端口范围30000-32767上可用(在我的示例中是端口31181 )。

As to your question about the volume:关于您关于音量的问题:

I've a shared volume so if I understand I cannot use deployment but only pods (maybe stateful set?)我有一个共享卷,所以如果我明白我不能使用部署而只能使用 Pod(也许是有状态集?)

yes, if you want to use specifically EmptyDir volume, it cannot be shared between different Pods (even if they were scheduled on the same node), it is shared only between containers within the same Pod .是的,如果你想专门使用EmptyDir卷,它不能在不同的Pods之间共享(即使它们被安排在同一个节点上),它只能在同一个Pod内的容器之间共享。 If you want to use Deployment you'll need to think about another storage solution such asPersistenetVolume .如果您想使用Deployment ,您需要考虑另一种存储解决方案,例如PersistenetVolume


EDIT:编辑:

In the first moment I didn't notice the error in your command:一开始我没有注意到您的命令中的错误:

kubectl expose pod site-web --type=LoadBalancer --port=8080

You're trying to expose non-existing Pod as your Pod 's name is site-home , not site-web .您正在尝试公开不存在的Pod ,因为您的Pod的名称是site-home ,而不是site-web site-web is a name of one of your containers (within your site-home Pod ). site-web是您的其中一个容器的名称(在您的site-home Pod中)。 Remember: we're exposing Pod , not containers via Service .请记住:我们公开的是Pod ,而不是通过Service公开的containers

I change 80->8080 but I always come to error:kubectl expose pod site-home --type=LoadBalancer --port=8080 return:error: couldn't retrieve selectors via --selector flag or introspection: the pod has no labels and cannot be exposed See 'kubectl expose -h' for help and examples.我更改 80->8080 但我总是会error:kubectl expose pod site-home --type=LoadBalancer --port=8080 return:error: couldn't retrieve selectors via --selector flag or introspection: the pod has no labels and cannot be exposed请参阅'kubectl expose -h'以获取帮助和示例。

The key point here is: the pod has no labels and cannot be exposed这里的重点是: the pod has no labels and cannot be exposed

It looks like your Pod doesn't have any labels defined which are required so that the Service can select this particular Pod (or set of similar Pods which have the same label) from among other Pods in your cluster.看起来您的Pod没有定义任何必需的labels ,以便Service可以从集群中的其他Pods中选择这个特定的Pod (或一组具有相同标签的类似Pods )。 You need at least one label in your Pod definition.您的Pod定义中至少需要一个标签。 Adding simple label name: site-web under Pod 's metadata section should help.Podmetadata部分下添加简单的标签name: site-web应该会有所帮助。 It may look like this in your Pod definition:在您的Pod定义中它可能看起来像这样:

apiVersion: v1
kind: Pod
metadata:
  name: site-home
  labels:
    name: site-web
spec:
...

Now you may even provide this label as selector in your service however it should be handled automatically if you omit --selector flag:现在您甚至可以在您的服务中提供此标签作为选择器,但是如果您省略--selector标志,它应该会自动处理:

kubectl expose pod site-home --type=LoadBalancer --port=8080 --target-port=80 --selector=name=site-web

Remember: in Minikube real load balancer cannot be created and instead of LoadBalancer NodePort type will be created.请记住:Minikube中无法创建真正的负载均衡器,将创建NodePort类型而不是LoadBalancer Command kubectl get svc will tell you on which port (in range 30000-32767 ) your application will be available.命令kubectl get svc将告诉您您的应用程序将在哪个端口(在30000-32767范围内)可用。

and `kubectl expose pod site-web --type=LoadBalancer --port=8080 return: Error from server (NotFound): pods "site-web" not found.和`kubectl expose pod site-web --type=LoadBalancer --port=8080 return: Error from server (NotFound): pods "site-web" not found. Site-home is the pod, site-web is the container with the port exposed, what's the issue? site-home是pod,site-web是暴露端口的容器,有什么问题吗?

if you don't have a Pod with name "site-web" you can expect such message.如果您没有名为"site-web"Pod ,您会收到这样的消息。 Here you are simply trying to expose non-existing Pod .在这里,您只是想公开不存在的Pod

If I exposed a port from a container the port is automatically exposed also for the pod?如果我从容器中公开一个端口,该端口是否也会自动公开给 pod?

Yes, you have the port defined in Container definition.是的,您在容器定义中定义了端口。 Your Pods automatically expose all ports that are exposed by the Containers within them.您的Pods会自动公开由其中的Containers公开的所有端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM