Home > Software engineering >  Kubernetes: How to expose a Pod as a service
Kubernetes: How to expose a Pod as a service

Time:08-11

I am learning kubernetes and created first pod using below command

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

The Pod creation was successful. But since it is neither a ReplicationController or a Deloyment, how could I expose it as a service. Please advise.

CodePudding user response:

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

CodePudding user response:

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.

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

CodePudding user response:

You may simply use --expose while creating the pod

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