Kubernetes: How to expose a Pod as a service

Viewed 72

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.

4 Answers

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/

You may simply use --expose while creating the pod

$ 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):

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