简体   繁体   English

Kubernetes:如何将 Pod 公开为服务

[英]Kubernetes: How to expose a Pod as a service

I am learning kubernetes and created first pod using below command我正在学习 kubernetes 并使用以下命令创建了第一个 pod

kubectl run helloworld --image=<image-name> --port=8080

The Pod creation was successful. Pod 创建成功。 But since it is neither a ReplicationController or a Deloyment, how could I expose it as a service.但由于它既不是 ReplicationController 也不是 Deloyment,我怎么能将它公开为服务。 Please advise.请指教。

Please refer to the documentation of kubernetes service concept https://kubernetes.io/docs/tutorials/kubernetes-basics/expose/expose-intro/ At the end of the page, there also is an interactive tutorial in minikube请参考文档kubernetes 服务理念https://kubernetes.io/docs/tutorials/kubernetes-basics/expose/expose-intro/页面末尾还有minikube的互动教程

You can create the service with the same set of selector and labels您可以使用相同的选择器和标签集创建服务

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: helloworld
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376

so if selector matching it will route the traffic to POD and you can expose it.因此,如果选择器匹配它会将流量路由到 POD 并且您可以公开它。

ref: https://kubernetes.io/docs/concepts/services-networking/service/参考: https://kubernetes.io/docs/concepts/services-networking/service/

You may simply use --expose while creating the pod您可以在创建 pod 时简单地使用 --expose

$ kubectl run nginx --image=nginx --port=80 --expose
service/nginx created
pod/nginx created

Thanks All I was able to achieve using below command (thanks to comment from Amit kumar):感谢所有我能够使用以下命令实现(感谢 Amit kumar 的评论):

# Create a service for a pod valid-pod, which serves on port 444 with the name "frontend"
kubectl expose pod valid-pod --port=444 --name=frontend

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

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