Skip to content

Expose a Microservice

By default, Microservices are not exposed in, or out of the cluster where your application is running. You can expose a microservice internally, so other microservices may communicate with it. And you can expose a microservice externally, so that it can be accessed publicly.

If you are familiar with Docker, this will be familiar. The expose property allows communication between containers, while the ports property exposes a service on the host.

Internally

When microservices should communicate internally, ensure their deploymentData correctly exposes them. Other Microservices running in the cluster can raech them using their host or service name. See examples below:

Use the expose property

version: '3.9'
services:
    mymicros:
        image: digitbrain/codeigniter-php5
        expose: 8080

Success

This example exposes the service running on port 8080 inside this container.
Other Microservices in the cluster can reach it at mymicros:8080

In addition to your Pod, Deployment or other workload, include a Service.
Use --- to separate the definitions.

---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: mymicros
spec:
  <truncated>
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: mymicros
  name: mymicros-svc
spec:
  ports:
  - name: http
    port: 8080
    targetPort: 8080
  selector:
    app: mymicros    

Success

This example exposes the service running on port 8080 inside this container.
Other Microservices in the cluster can reach it at mymicros-svc:8080

Externally

When microservices should be available publicly, ensure their deploymentData correctly exposes them. You can access them via the IP of the node they are running on. See examples below:

Warning

Don't forget to add the published or hostPort to opened ports on the node where your Microservice will run!

Use the ports property.

services:
  mymicros:
    image: digitbrain/codeigniter-php5
    ports:
    - "80:8080"

Success

This example exposes a service running on port 8080 inside the container.
It can be reached publicly at 192.168.10.20:80, where 192.168.10.20 is the public IP address of the node hosting the Microservice.

Use the ports property with long-syntax and host if you need to specify the protocol.

services:
  mymicros:
    image: digitbrain/codeigniter-php5
    ports:
    - target: 8080
      published: 80
      protocol: tcp
      mode: host

Success

This example exposes a service running on port 8080 inside the container.
It can be reached publicly at 192.168.10.20:80, where 192.168.10.20 is the public IP address of the node hosting the Microservice.

In your Pod, Deployment or other workload, include hostPort.

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: mymicros
  name: mymicros
spec:
  template:
    spec:
      containers:
      - image: digitbrain/codeigniter-php5
        name: codeigniter
        ports:
        - containerPort: 8080
          hostPort: 80
    <truncated>

Success

This example exposes a service running on port 8080 inside the container.
It can be reached publicly at 192.168.10.20:80, where 192.168.10.20 is the public IP address of the node hosting the Microservice.