Kubernetes Service not able to get External IP

4/10/2021

The service is external-ip is <pending> and is unable to bind with a external port. I have run the command to try to find the port.

minikube service mongodb-express-service

There is no error shown on the terminal. But the browser says that it is unable to connect to that url.

Enviroment

  • Minikube
  • Oracle Virtual Box
  • Ubuntu 20.04

mongo-express.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongo-express
  labels:
    app: mongo-express
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongo-express
  template:
    metadata:
      labels:
        app: mongo-express
    spec:
      containers:
        - name: mongo-express
          image: mongo-express
          ports:
            - containerPort: 8081
          env:
            - name: ME_CONFIG_MONGODB_ADMINUSERNAME
              valueFrom:
                secretKeyRef:
                  name: mongodb-secret
                  key: username
            - name: ME_CONFIG_MONGODB_ADMINPASSWORD
              valueFrom:
                secretKeyRef:
                  name: mongodb-secret
                  key: password
            - name: ME_CONFIG_MONGODB_SERVER
              valueFrom:
                configMapKeyRef:
                  name: mongodb-config-map
                  key: database_url
---
apiVersion: v1
kind: Service
metadata:
  name: mongodb-express-service
spec:
  selector:
    app: mongodb-express-service
  type: LoadBalancer
  ports:
    - protocol: TCP
      port: 8081
      targetPort: 8081
      nodePort: 30001

Update Kubectl get service output

NAME                      TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes                ClusterIP      10.96.0.1        <none>        443/TCP          14d
mongodb-express-service   LoadBalancer   10.106.190.5     <pending>     8081:30001/TCP   2d3h
mongodb-service           ClusterIP      10.110.241.194   <none>        27017/TCP        3d23h

Kubectl get pod output

mongo-express-85bdd6688f-mhv2x        1/1     Running   2          2d3h
mongodb-deployment-7f79c5f88f-h9dvf   1/1     Running   2          2d3h
-- tristan lim
kubernetes

3 Answers

4/10/2021

There is a couple of resource missing in your application. If you have created it already then please verify with mine. else create it.

1: create Configmap resource

apiVersion: v1
kind: ConfigMap
metadata:
  name: mongodb-config-map
data:
  database_url: mongodb-service

2: create Secret resource

apiVersion: v1
kind: Secret
metadata:
  name: mongodb-secret
type: Opaque
data:
  username: dXNlcm5hbWU=
  password: cGFzc3dvcmQ=

3: On a terminal

minikube service mongodb-express-service

Below is the output of my terminal you may vary.

|-----------|-------------------------|-------------|---------------------------|
| NAMESPACE |          NAME           | TARGET PORT |            URL            |
|-----------|-------------------------|-------------|---------------------------|
| default   | mongodb-express-service |        8081 | http://192.168.49.2:30001 |
|-----------|-------------------------|-------------|---------------------------|
🏃  Starting tunnel for service mongodb-express-service.
|-----------|-------------------------|-------------|------------------------|
| NAMESPACE |          NAME           | TARGET PORT |          URL           |
|-----------|-------------------------|-------------|------------------------|
| default   | mongodb-express-service |             | http://127.0.0.1:53898 |
|-----------|-------------------------|-------------|------------------------|
🎉  Opening service default/mongodb-express-service in default browser...

Edit Updated:

I've verify your manifests and noticed that the Service selector doesn't match the Deployment selector. Change the Service: spec.selector to app: mongo-express instead of: app: mongodb-express-service.

-- Gupta
Source: StackOverflow

10/10/2021

You didn't connect your LoadBalancer to your mongo-express deployment. Change selector -> app to mongo-express

###... previous config here
---
apiVersion: v1
kind: Service
metadata:
  name: mongodb-express-service
spec:
  selector:
    app: mongo-express
  type: LoadBalancer
  ports:
    - protocol: TCP
      port: 8081
      targetPort: 8081
      nodePort: 30001
-- Anatolii Vasilev
Source: StackOverflow

4/19/2021

Posting this answer to give more reference on 2 topics:

  • Kubernetes Service not able to get External IP
  • Issue that was present in this question

Kubernetes Service not able to get External IP

Service of type LoadBalancer:

Exposes the Service externally using a cloud provider's load balancer. NodePort and ClusterIP Services, to which the external load balancer routes, are automatically created.

In a Kubernetes solution like minikube you won't get out of the box External IP that is associated with Service of type LoadBalancer. You don't need the External IP to connect to your Services. You can connect to your Services by either:

  • Using a NodePort ($ minikube service xyz is using it)
  • Using a $ minikube tunnel (this would "assign" External IP's)
  • Virtualbox specific:

Issue that was present in this question

The issue that was present in the question was connected with the mismatch between the Service .spec.selector and Deployment .spec.selector.matchLabels.

The easiest way to check if your selector is configured correctly (apart from looking at the YAML manifest), is by:

  • $ kubectl get endpoints
NAME         ENDPOINTS                AGE
kubernetes   A.B.C.D:443              28m
nginx    --> 10.32.0.13:80 <--        5s 

Disclaimer!

The endpoint will be present when Pod is in Running state and Ready.

If the nginx (Service name) endpoints was empty it could mean a mismatched selector or issue with the Pod (CrashLoopBackOff, Pending, etc.).

You can also check this by:

  • $ kubectl describe svc SERVICE_NAME <-- Endpoints field
  • $ kubectl describe endpoints SERVICE_NAME <-- Subsets.addresses field

Additional resources:

-- Dawid Kruk
Source: StackOverflow