How to expose nginx in minikube to outside?

Member

by lonzo , in category: General Help , 8 months ago

How to expose nginx in minikube to outside?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by gaston , 8 months ago

@lonzo 

To expose Nginx in Minikube to the outside, you need to follow these steps:

  1. Start Minikube by running the command: minikube start
  2. Create a Nginx deployment by applying the following YAML configuration:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80


Save the above configuration in a file called nginx-deployment.yaml and apply it using the command: kubectl apply -f nginx-deployment.yaml

  1. Expose the Nginx deployment by creating a service. Create a file called nginx-service.yaml with the following content:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: NodePort
  selector:
    app: nginx
  ports:
    - port: 80
      targetPort: 80
      protocol: TCP


Apply the service using the command: kubectl apply -f nginx-service.yaml

  1. Get the NodePort of the Nginx service by running: kubectl get service nginx-service
  2. Access the Nginx service from outside the Minikube cluster by using the IP of Minikube and the NodePort obtained in the previous step. The URL format will be:


By following these steps, you should be able to expose Nginx running in Minikube to the outside.

by buster.gaylord , 3 months ago

@lonzo 

To access the Nginx service from outside the Minikube cluster, use the minikube service command instead of directly accessing the IP and NodePort. This command helps in automatic port forwarding. Here's how you can do that:

  1. Create the Nginx deployment as mentioned in the previous steps.
  2. Expose the Nginx deployment using a service similarly to the previous steps.
  3. Run the following command to open the Nginx service in the default browser:
1
minikube service nginx-service


This command will automatically open the Nginx service in the default browser on your host machine.


By using minikube service, Minikube will set up the necessary port forwarding and mapping for you, making it easier to access the Nginx service running within Minikube from outside.