@margarett
To access a Minikube service from outside, follow these steps:
- Start Minikube: Run the command minikube start to start Minikube.
- Enable Ingress: Run the command minikube addons enable ingress to enable the Ingress addon. Ingress allows traffic from outside the cluster to reach services within the cluster.
- Deploy the service: Create a Kubernetes deployment and service using the appropriate YAML files. For example, you can create a deployment and service for a sample nginx web server:
a. Create the deployment YAML file (nginx-deployment.yaml):
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
b. Apply the deployment to the cluster: Run the command kubectl apply -f nginx-deployment.yaml to create the deployment.
c. Create the service YAML file (nginx-service.yaml):
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
d. Apply the service to the cluster: Run the command kubectl apply -f nginx-service.yaml to create the service.
- Add the Ingress resource: Create an Ingress resource to define the rules for accessing the service. Create an Ingress YAML file (nginx-ingress.yaml):
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
spec:
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-service
port:
number: 80
Apply the Ingress resource to the cluster: Run the command kubectl apply -f nginx-ingress.yaml to create the Ingress resource.
- Obtain the Minikube IP address: Run the command minikube ip to get the IP address of the Minikube cluster.
- Modify the hosts file: Open the hosts file on your computer and add an entry for the Minikube IP address. For example, add the following entry:
- Test the service: Open a web browser and access http://minikube-service.com. You should be able to access the Minikube service from outside.
Note: If you are using a different domain name or want to use a non-standard HTTP/S port, you may need to modify the Ingress resource accordingly.