How to expose pods in Minikube?

by wayne.swaniawski , in category: General Help , 9 months ago

How to expose pods in Minikube?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by ari.olson , 9 months ago

@wayne.swaniawski 

To expose pods in Minikube, you can use the kubectl expose command or define a service manifest file.

  1. Using kubectl expose command: a. Open a terminal window and start the Minikube cluster by running: minikube start b. Verify that the cluster is running by executing: kubectl cluster-info c. Deploy your application by creating a deployment using: kubectl create deployment
  2. Using the service manifest file: a. Create a YAML file with the following contents:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
apiVersion: v1
kind: Service
metadata:
  name: <service-name>
spec:
  type: NodePort
  selector:
    app: <deployment-name>
  ports:
    - protocol: TCP
      port: <port-number>
      targetPort: <target-port>


  • : Provide a name for your service.
  • : The name of your deployment.
  • : The port number on which your application should listen.
  • : The port number on which your application is actually running.


b. Save the file as service.yaml or any preferred name. c. Apply the service manifest by running: kubectl apply -f service.yaml


After exposing the pods as a service, you can access them using the Minikube IP and the NodePort assigned. To obtain the IP and port, run: minikube service <service-name> --url

Member

by tina , 4 months ago

@wayne.swaniawski 

In addition to the provided steps, it is important to note that when exposing pods in Minikube, you have different options for the service type, such as NodePort, ClusterIP, and LoadBalancer. The choice of service type depends on the network requirements of your application.


For example, the NodePort type exposes the service on a specific port on each Node in the cluster, making the service accessible from outside the cluster using the Node's IP address and the NodePort.


It's also good practice to ensure that your pod deployment labels match the selector defined in your service manifest file to correctly expose the desired pods.


Remember to set up port forwarding rules on your machine if you need to access the exposed services directly from your local browser or tools, as Minikube runs in a VM or Docker container depending on the configuration.