swaran
swaran

Reputation: 115

Kubernetes - How to access deployments in minikube?

I have used the following configurations to deploy an app on minikube.

Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: angular-app
spec:
  replicas: 2
  selector:
    matchLabels:
      run: angular-app
  template:
    metadata:
      labels:
        run: angular-app
    spec:
      containers:
      - name: angular-app
        image: nheidloff/angular-app
        ports:
        - containerPort: 80
        - containerPort: 443
    

Service:

apiVersion: v1
kind: Service
metadata:
  name: angular-app
  labels:
    run: angular-app
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
    name: http

Service description:

Name:                     angular-app    
Namespace:                default        
Labels:                   run=angular-app
Annotations:              <none>
Selector:                 <none>
Type:                     NodePort       
IP Family Policy:         SingleStack    
IP Families:              IPv4
IP:                       10.102.174.98  
IPs:                      10.102.174.98
Port:                     http  80/TCP
TargetPort:               80/TCP
NodePort:                 http  31503/TCP
Endpoints:                172.17.0.3:80,172.17.0.4:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

When i try to access the endpoints, the links are not responding. However after using minikube service angular-app. Following showed up:

|-----------|-------------|-------------|---------------------------|
| NAMESPACE |    NAME     | TARGET PORT |            URL            |
|-----------|-------------|-------------|---------------------------|
| default   | angular-app | http/80     | http://192.168.49.2:31503 |
|-----------|-------------|-------------|---------------------------|
šŸƒ  Starting tunnel for service angular-app.
|-----------|-------------|-------------|------------------------|
| NAMESPACE |    NAME     | TARGET PORT |          URL           |
|-----------|-------------|-------------|------------------------|
| default   | angular-app |             | http://127.0.0.1:60611 |
|-----------|-------------|-------------|------------------------|

With this ip http://127.0.0.1:60611 im able to access the app. What is the use of the endpoints given in the service description? How to access each replica? Say if i have 4 replicas, how do i access each one of them?

Upvotes: 2

Views: 552

Answers (2)

GeertPt
GeertPt

Reputation: 17864

The answer from ~al-waleed-shihadeh is correct, but I want to give some additional info.

  • You should be able to access the service via the NodePort, too, without needing the minikube service command: http://192.168.49.2:31503 . The port is assigned at random, but you can choose a fixed one in the range 30000-32767

    spec:
      type: NodePort
      ports:
      - port: 80
        targetPort: 80
        protocol: TCP
        name: http
        nodePort: 30080
    
  • If you want a 'normal' URL to access the service, you must add an Ingress that allow access to the service via a reverse proxy. It will route to one of your services' pods using load-balancing.

  • If you want fixed URL's to each of your pods separately, you could use a StatefulSet instead of a Deployment, and create 4 different services with selectors for angular-app-0 to angular-app-3, and then have 4 different ingresses as well.

Upvotes: 2

Al-waleed Shihadeh
Al-waleed Shihadeh

Reputation: 2855

  • The endpoints provided in the service description are the endpoints for each of the pods 172.17.0.3:80,172.17.0.4:80, when you deploy more replicas you will have more endpoints.

  • The angular-app service is bonded to port number 31503 and you can access your service on this port from cluster nodes (not your host machine).

  • minikube service angular-app will create a tunnel between your host machine and the cluster nodes on port 60611. This means anything that comes on 127.0.0.1:60611 will be redirected to 192.168.49.2:31503 and then one of the available endpoints.

  • The service will take care of balancing the load between all replicas automatically and you don't need to worry about it.

  • if you would like to access a specific pod you can use the below command:

kubectl port-forward ${POD_NAME} 80:80

you need to replace the pod name from the above command and the command assumes that port 80 is available on your machine.

Upvotes: 2

Related Questions