{
  "slug": "Running-multi-Port-forward-with-tmux-ed5a23bc063a",
  "title": "Running multi Port-forward with tmux",
  "subtitle": "This guide demonstrates how to set up and use tmux for multi-port forwarding in Kubernetes environments. Tmux is a terminal multiplexer…",
  "excerpt": "This guide demonstrates how to set up and use tmux for multi-port forwarding in Kubernetes environments. Tmux is a terminal multiplexer…",
  "date": "2024-10-10",
  "tags": [
    "Kubernetes"
  ],
  "readingTime": "2 min",
  "url": "https://medium.com/@mobinshaterian/running-multi-port-forward-with-tmux-ed5a23bc063a",
  "hero": "https://cdn-images-1.medium.com/max/800/1*dYkPvKzIQzPwtbf3LFPqJQ.jpeg",
  "content": [
    {
      "type": "heading",
      "level": 2,
      "text": "Running multi Port-forward with tmux"
    },
    {
      "type": "paragraph",
      "html": "This guide demonstrates how to set up and use tmux for multi-port forwarding in Kubernetes environments. Tmux is a terminal multiplexer that allows users to manage multiple terminal sessions within a single window. By combining tmux with Kubernetes port-forwarding, you can efficiently manage multiple port-forward connections simultaneously, which is particularly useful for complex microservices architectures or when working with multiple pods across different namespaces."
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*dYkPvKzIQzPwtbf3LFPqJQ.jpeg",
      "alt": "Running multi Port-forward with tmux",
      "caption": "",
      "width": 1024,
      "height": 1024
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Summary:"
    },
    {
      "type": "paragraph",
      "html": "1. Install tmux using the package manager (e.g., `sudo apt install tmux`).<br>2. Create a Bash script to automate the process:<br> — Set up variables for the tmux session name, Kubernetes namespaces, pod name patterns, and port mappings.<br> — Create a new tmux session.<br> — Iterate through the configured namespaces and pods, creating new windows for each.<br> — Use kubectl commands to find the appropriate pods and set up port-forwarding.<br> — Attach to the tmux session to view and manage all port-forward connections.<br>3. Make the script executable using `chmod +x script_name.sh`.<br>4. Run the script to start the tmux session with multiple port-forward connections."
    },
    {
      "type": "paragraph",
      "html": "This approach streamlines the process of managing multiple port-forward connections, making it easier to work with complex Kubernetes deployments and access various services simultaneously."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Install tmux"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo\napt install tmux"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Make bash file"
    },
    {
      "type": "paragraph",
      "html": "Creating and executing a Bash script is a straightforward process. Here’s a step-by-step guide to help you get started:"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "1. Create a Bash Script"
    },
    {
      "type": "list",
      "ordered": true,
      "items": [
        "<strong>Open a terminal</strong>.",
        "<strong>Create a new file</strong> using a text editor like <code>nano</code>, <code>vim</code>, or <code>gedit</code>. For example, using <code>nano</code>:<code>nano myscript.sh</code>",
        "<strong>Write your script</strong>. For a simple example, you can add the following lines to print “Hello, World!”<code>#!/bin/bash echo \"Hello, World!\"</code>",
        "<strong>Save and exit</strong> the editor. In <code>nano</code>, you can do this by pressing <code>Ctrl + X</code>, then <code>Y</code>, and <code>Enter</code>."
      ]
    },
    {
      "type": "heading",
      "level": 2,
      "text": "2. Make the Script Executable"
    },
    {
      "type": "paragraph",
      "html": "<strong>Change the file permissions</strong> to make it executable<code>chmod +x myscript.sh</code>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "3. Execute the Script"
    },
    {
      "type": "paragraph",
      "html": "<strong>Run the script</strong> by typing:<code>./myscript.sh</code>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "4. Script"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "#!/bin/bashSESSION=\"port_forward\"NAMESPACE=(\"name-space\" \"name-space\" )PATTERN=(\"podname-\"\n\"podname-\" )PORT_MAPPINGS=(\"8081:9000\" \"8082:9000\")# Start a new tmux sessiontmux new-session -d -s\n$SESSION# Create three windows (tabs)for i in 0 1 2 3 4 5; do  tmux new-window -t $SESSION:$((i+1))\ntmux send-keys -t $SESSION:$((i+1)) \"\nkubectl get pods -n ${NAMESPACE[$i]} | grep ${PATTERN[$i]}\" C-m\nPOD_NAME=$(kubectl get pods -n ${NAMESPACE[$i]} | grep ${PATTERN[$i]} | awk '{print $1}')  tmux\nsend-keys -t $SESSION:$((i+1))\n\"\nkubectl port-forward $POD_NAME ${PORT_MAPPINGS[$i]} -n ${NAMESPACE[$i]}\" C-mdone# Attach to the\ntmux sessiontmux attach-session -t $SESSION"
    }
  ]
}
