When we have a large project with a kubernetes cluster, it’s sometimes difficult to debug and test certain parts of the application. Mirrord allows us to bind a local port to a remote server.

flowchart TB
    request --> mirrord
    subgraph Pod
        mirrord -.-x|intercepted| live_server:3000
    end
    mirrord -->|db connection| db[(Database db:5000)]
    mirrord -->|forwarded| local_machiene(local:3000)
    local_machiene -.-|connects db:5000 via mirrord| db

You can run a local server for example on port 3000 and bind it to a remote server. And any request coming to live_server:3000 will be intercepted by mirrord and forwarded to your local machine. We can debug the incoming request, connect to other services etc… Like our local machiene is a part of the kubernetes cluster.

How to use Mirrord

Pre-requisites:

  • mirrod cli
  • kubectl

With kubectl pointed to our target cluster, we can use the following command to create a mirrord pod in the target cluster.

mirrord exec --target deployment/app-name/container/main <command-to-run-server>

or you can use a config file

{
  "target": {
    "path": "deployment/app-name/container/main"
  },
  "accept_invalid_certificates": false,
  "feature": {
    "network": {
      "incoming": "steal",
      "outgoing": true
    },
    "fs": "read",
    "env": true
  },
  "kubeconfig": "~/.kube/KUBECONFIG",
}

and run the following command

mirrord exec -f config.json

the target can be pod/pod-name or any name that can be resolved by kubectl.

Once we start mirrord, A tunnel will be created between the local machine and the remote server. Any request coming to the remote server will be intercepted by mirrord and forwarded to the local machine. And our local machiene will be a part of the kubernetes cluster network.

This means you can use the cluster dns to connect to other services in the cluster. For example, if you have a database running in the cluster, you can connect to it using http://db-service:5000 from your local machine.