devops-admin
devops-admin

Reputation: 1993

can we create a service for a kubernetes deployment which does not have port or expose a port?

can we create a service for a Kubernetes deployment that does not have a port or exposes a port?

I have k8s deployment which does not have a port and doesn't expose a port to connect from outside. It's a rabbitmq producer/consumer application.

Below this the deployment spec of it.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: reports-bot
spec:
  replicas: 2
  selector:
    matchLabels:
     app: reports-bot
  template:
    metadata:
     labels:
       app: reports-bot
    spec:
      containers:
      - name: reports-bot
        image: devops/reports-bot:latest
        imagePullPolicy: Always
        envFrom:
        - configMapRef:
            name: dependents-config
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: dependents-config
  labels:
    app: rabbitmq
data:
  DEV_HOST: dev-rabbitmq
  DEV_PORT: "5672"
  DEV_USERNAME: admin
  DEV_PASSWORD: admin_1265
  DEV_GPASSWORD: devops

Upvotes: -1

Views: 775

Answers (1)

Exposing a port in the deployment manifest gives the system additional information about the network connections a container uses, but is primarily informational. Not specifying a port here DOES NOT prevent that port from being exposed. Any port which is listening on the default "0.0.0.0" address inside a container will be accessible from the network.

Declaring containerPort in Pod specification is optional. Even without it your Service will know where to direct the request based on the info it has declared in its targetPort.

When creating a service, it is necessary to define the port that the service will serve on. This port is mapped to a target port inside the pod that the service targets. Incoming requests to the service in port are forwarded to the target port in the pod. If no target port is provided, then the port value is used.

Upvotes: 1

Related Questions