user2746466
user2746466

Reputation: 381

Exposed port of kubernetes service not working

Following is my application deployment yaml file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: sharemarket-crud-deployment
spec:
  selector:
    matchLabels:
      app: sharemarket-k8s-sb-service
  replicas: 2
  template:
    metadata:
      labels:
        app: sharemarket-k8s-sb-service
    spec:
      containers:
        - name: sharemarket-k8s-sb-service-container
          image: joy999/shareserviceproj:release06
          ports:
            - containerPort: 8080
          env:   # Setting Enviornmental Variables
            - name: DB_HOST   # Setting Database host address from configMap
              valueFrom :
                configMapKeyRef :
                  name : db-config
                  key :  host

            - name: DB_NAME  # Setting Database name from configMap
              valueFrom :
                configMapKeyRef :
                  name : db-config
                  key :  dbName

            - name: DB_USERNAME  # Setting Database username from Secret
              valueFrom :
                secretKeyRef :
                  name : mysql-secrets
                  key :  username

            - name: DB_PASSWORD # Setting Database password from Secret
              valueFrom :
                secretKeyRef :
                  name : mysql-secrets
                  key :  password

---

apiVersion: v1 # Kubernetes API version
kind: Service # Kubernetes resource kind we are creating
metadata: # Metadata of the resource kind we are creating
  name: springboot-sb-service-svc
spec:
  selector:
    app: springboot-k8s-sb-service
  ports:
    - protocol: "TCP"
      port: 8080 # The port that the service is running on in the cluster
      targetPort: 8080 # The port exposed by the service
  type: NodePort # type of the service.

I can see pods are created successfully, services are also good. Database is also good with table created.

enter image description here

The exposed port service shows as 30119 but if I POST or GET the request from postman, I am getting fallowing error all the time:

POST http://192.168.99.100:30119/stock Error: connect ETIMEDOUT 192.168.99.100:30119

GET http://192.168.99.100:30119/stock/1 Error: connect ETIMEDOUT 192.168.99.100:30119

Can anyone please help to troubleshoot the issue.

Upvotes: 0

Views: 1913

Answers (2)

Solomon Sunday
Solomon Sunday

Reputation: 265

you need the External IP to access the cluster from the outside the cluster. what you need is external-Ip:port to access the pods in the cluster.

To get the external IP, change the type in your service.yaml from NodePort to LoadBalancer then run "kubectl get service" You should see the external Ip

LoadBalancer is what directs traffic from the internet to your pods in the cluster and also load balances the pods.

Upvotes: 0

Shachar297
Shachar297

Reputation: 832

You need to expose the NodePort PORT as well, so you can reach this service from outside the cluster. Your GET request tries to reach port 30119, but you only exposed 8080, make sure to expose 30119

type: NodePort
ports:
 port: 8080
 TargetPort: 8080
 nodePort: 30119

Another way for you is a load balancer, and use the end point.

Kubectl get endpoints -A

Upvotes: 2

Related Questions