Home > Mobile >  Exposed port of kubernetes service not working
Exposed port of kubernetes service not working

Time:07-01

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.

CodePudding user response:

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
  • Related