Coder Social home page Coder Social logo

yokawasa / kubectl-plugin-ssh-jump Goto Github PK

View Code? Open in Web Editor NEW
171.0 6.0 18.0 464 KB

A kubectl plugin to access nodes or remote services using a SSH jump Pod

License: Apache License 2.0

Shell 100.00%
kubectl kubectl-plugins kubernetes ssh ssh-agent kubectl-plugin

kubectl-plugin-ssh-jump's Introduction

kubectl-plugin-ssh-jump

kubectl plugin

A kubectl plugin to access Kubernetes nodes or remote services using a SSH jump Pod

A jump host Pod is an intermediary Pod or an SSH gateway to Kubernetes node machines, through which a connection can be made to the node machines or remote services.

Here is an scenario where you want to connect to Kubernetes nodes or remote services, but you have to go through a jump host Pod, because of firewalling, access privileges. etc. There is a number of valid reasons why the jump hosts are needed...

CASE 1: SSH into Kubernetes nodes via SSH jump Pod

CASE 2: Connect to remote serivces via SSH local port forwarding. SSH local port forwarding allows to forward the traffic form local machine to SSH jump then SSH jump will forward the traffic to remote services (host:port)s.

[NOTE]

  • Kubectl versions >= 1.12.0 (Preferred)
    • As of Kubernetes 1.12, kubectl now allows adding external executables as subcommands. For more detail, see Extend kubectl with plugins
    • You can run the plugin with kubectl ssh-jump ...
  • Kubectl versions < 1.12.0
    • You still can run the plugin directly with kubectl-ssh-jump ...

Table of Content

Pre-requistes

This plugin needs the following programs:

  • ssh(1)
  • ssh-agent(1)
  • ssh-keygen(1)

Installation

Install through krew

This is a way to install kubectl-ssh-jump through krew. After installing krew by following this, you can install kubectl-ssh-jump like this:

$ kubectl krew install ssh-jump

Expected output would be like this:

Updated the local copy of plugin index.
Installing plugin: ssh-jump
CAVEATS:
\
 |  This plugin needs the following programs:
 |  * ssh(1)
 |  * ssh-agent(1)
 |
 |  Please follow the documentation: https://github.com/yokawasa/kubectl-plugin-ssh-jump
/
Installed plugin: ssh-jump

Once it's installed, run:

$ kubectl plugin list

The following kubectl-compatible plugins are available:

/Users/yoichi.kawasaki/.krew/bin/kubectl-krew
/Users/yoichi.kawasaki/.krew/bin/kubectl-ssh_jump

$ kubectl ssh-jump

Manual Installation

Install the plugin by copying the script in the $PATH of your shell.

# Get source
$ git clone https://github.com/yokawasa/kubectl-plugin-ssh-jump.git
$ cd kubectl-plugin-ssh-jump
$ chmod +x kubectl-ssh-jump
# Add kubeclt-ssh-jump to the install path.
$ sudo cp -p kubectl-ssh-jump /usr/local/bin

Once in the $PATH, run:

$ kubectl plugin list

The following kubectl-compatible plugins are available:
/usr/local/bin/kubectl-ssh-jump

$ kubectl ssh-jump

How to use

Usage

Usage: 
  kubectl ssh-jump <dest_node> [options]

Options:
  <dest_node>                     Destination node name or IP address
                                  dest_node must start from the following letters:
                                  ASCII letters 'a' through 'z' or 'A' through 'Z',
                                  the digits '0' through '9', or hyphen ('-').
                                  NOTE: Setting dest_node as 'jumphost' allows to
                                  ssh into SSH jump Pod as 'root' user
  -u, --user <sshuser>            SSH User name
  -i, --identity <identity_file>  Identity key file, or PEM(Privacy Enhanced Mail)
  -p, --pubkey <pub_key_file>     Public key file
  -P, --port <port>               SSH port for target node SSH server
                                  Defaults to 22
  -a, --args <args>               Args to exec in ssh session
  -n, --namespace <ns>            Namespace for jump pod
  --context <context>             Kubernetes context
  --pod-template <file>           Path to custom sshjump pod definition
  -l, --labels <key>=<val>[,...]  Find a pre-existing sshjump pod using labels
  --skip-agent                    Skip automatically starting SSH agent and adding
                                  SSH Identity key into the agent before SSH login
                                  (=> You need to manage SSH agent by yourself)
  --cleanup-agent                 Clearning up SSH agent at the end
                                  The agent is NOT cleaned up in case that
                                  --skip-agent option is given
  --cleanup-jump                  Clearning up sshjump pod at the end
                                  Defaults to skip cleaning up sshjump pod
  -v, --verbose                   Run ssh in verbose mode (=ssh -vvv)
  -h, --help                      Show this message

Example:
  Scenario1 - You have private & public SSH key on your side
  $ kubectl ssh-jump -u myuser -i ~/.ssh/id_rsa -p ~/.ssh/id_rsa.pub hostname

  Scenario2 - You have .pem file but you don't have public key on your side
  $ kubectl ssh-jump -u ec2-user -i ~/.ssh/mykey.pem hostname

Option parameters Cache

username, identity, pubkey, port options are cached, therefore you can omit these options afterward. The options are stored in a file named $HOME/.kube/kubectlssh/options

$ cat $HOME/.kube/kubectlssh/options
sshuser=myuser
identity=/Users/yoichi.kawasaki/.ssh/id_rsa_k8s
pubkey=/Users/yoichi.kawasaki/.ssh/id_rsa_k8s.pub
port=22

SSH Agent (ssh-agent)

The plugin automatically check if there are any ssh-agents started running by the plugin, and starts ssh-agentif it doesn't find any ssh-agent running and adds SSH Identity key into the agent before SSH login. If the command find that ssh-agent is already running, it doesn't start a new agent, and re-use the agent. Add --cleanup-agent option if you want to kill the created agent at the end of command.

In addtion, add --skip-agent option if you want to skip automatic starting ssh-agent. This is actually a case where you already have ssh-agent managed or you want to manually start the agent.

Customize SSH jump pod

You can customize the sshjump pod created by kubectl ssh-jump by setting the --pod-template flag to the path to a pod template on disk. However, customized sshjump pods must be named sshjump and run in the current namespace or kubectl ssh-jump won't be able to find them without the required flags. If you change the pod name, you must give the pod a unique set of labels and provide them on the command line by setting the --labels flag.

You can also specify the namespace and context used by kubectl ssh-jump by setting the --namespace and --context flags respectively.

Examples

Show all node list. Simply executing kubectl ssh-jump gives you the list of destination nodes as well as command usage

$ kubectl ssh-jump

Usage: 
  kubectl ssh-jump <dest_node> [options]

...snip...

List of destination node...
Hostname                    Internal-IP
aks-nodepool1-18558189-0    10.240.0.4
...snip...

CASE 1: SSH into Kubernetes nodes via SSH jump Pod

1-1 - You have private & public SSH key on your side

Suppose you have private & public SSH key on your side and you want to SSH to a node named aks-nodepool1-18558189-0, execute the plugin with options like this:

  • usernaem: azureuser
  • identity:~/.ssh/id_rsa_k8s
  • pubkey:~/.ssh/id_rsa_k8s.pub)
$ kubectl ssh-jump aks-nodepool1-18558189-0 \
  -u azureuser -i ~/.ssh/id_rsa_k8s -p ~/.ssh/id_rsa_k8s.pub

[NOTE] you can try SSH into a node using node IP address (Internal-IP) instead of Hostname

As explained in usage secion, username, identity, pubkey options will be cached, therefore you can omit these options afterward.

$ kubectl ssh-jump aks-nodepool1-18558189-0

You can pass the commands to run in the destination node like this (Suppose that username, identity, pubkey options are cached):

echo "uname -a" | kubectl ssh-jump aks-nodepool1-18558189-0

(Output)
Linux aks-nodepool1-18558189-0 4.15.0-1035-azure #36~16.04.1-Ubuntu SMP Fri Nov 30 15:25:49 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

You can pass commands with --args or -a option

kubectl ssh-jump aks-nodepool1-18558189-0 --args "uname -a"

(Output)
Linux aks-nodepool1-18558189-0 4.15.0-1035-azure #36~16.04.1-Ubuntu SMP Fri Nov 30 15:25:49 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

You can clean up sshjump pod at the end of the command with --cleanup-jump option, otherwise, the sshjump pod stay running by default.

$ kubectl ssh-jump aks-nodepool1-18558189-0 \
  -u azureuser -i ~/.ssh/id_rsa_k8s -p ~/.ssh/id_rsa_k8s.pub \
  --cleanup-jump

You can clean up ssh-agent at the end of the command with --cleanup-agent option, otherwise, the ssh-agent process stay running once it's started.

$ kubectl ssh-jump aks-nodepool1-18558189-0 \
  -u azureuser -i ~/.ssh/id_rsa_k8s -p ~/.ssh/id_rsa_k8s.pub \
  --cleanup-agent

You can skip starting ssh-agent by giving --skip-agent. This is actually a case where you already have ssh-agent managed. Or you can start new ssh-agent and add an identity key to the ssh-agent like this:

# Start ssh-agent manually
$ eval `ssh-agent`
# Add an arbitrary private key, give the path of the key file as an argument to ssh-add
$ ssh-add ~/.ssh/id_rsa_k8s
# Then, run the plugin with --skip-agent
$ kubectl ssh-jump aks-nodepool1-18558189-0 \
  -u azureuser -i ~/.ssh/id_rsa_k8s -p ~/.ssh/id_rsa_k8s.pub \
  --skip-agent

# At the end, run this if you want to kill the current agent
$ ssh-agent -k
1-2 - You have .pem file but you don't have public key on your side

From v0.4.0, the plugin supports PEM (Privacy Enhanced Mail) scenario where you create key-pair but you only have .pem / private key (downloaded from AWS, for example) and you don't have the public key on your side.

Suppose you've already downloaded a pem file and you want to ssh to your EKS worker node (EC2) named ip-10-173-62-96.ap-northeast-1.compute.internal using the pem, execute the plugin with options like this:

  • usernaem: ec2-user
  • identity: ~/.ssh/mykey.pem
$ kubectl ssh-jump -u ec2-user -i ~/.ssh/mykey.pem ip-10-173-62-96.ap-northeast-1.compute.internal

CASE 2: Access remote serivces via SSH local port forwarding

SSH local port forwarding allows to forward the traffic form local machine to SSH jump then SSH jump will forward the traffic to remote services (host:port)s.

2-1 - Configuring SSH local port forwarding with --args or -a option

Suppose you have private & public SSH key on your side and you want to access a remote server (IP: 10.100.10.8) using 3389/TCP port which is not accessible directly but accessible via SSH jump, execute the plugin with options like this, at first:

  • identity:~/.ssh/id_rsa_k8s
  • pubkey:~/.ssh/id_rsa_k8s.pub)

The command below allows to forward the traffic form local machine (localhost:13200) to SSH jump then SSH jump will forward the traffic to the remote server (10.100.10.8:3389).

$ kubectl ssh-jump sshjump \
  -i ~/.ssh/id_rsa_k8s -p ~/.ssh/id_rsa_k8s.pub \
  -a "-L 13200:10.100.10.8:3389"
  • sshjump is the hostname for SSH jump Pod
  • The value for --arg or -a should be in this format: "-L local_port:remote_address:remote_port"

Now, you're ready to access to the remote server at port 13200 at local machine.

Useful Links

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/yokawasa/kubectl-plugin-ssh-jump

kubectl-plugin-ssh-jump's People

Contributors

benjaminherbert avatar partcyborg avatar yokawasa avatar zbioe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

kubectl-plugin-ssh-jump's Issues

Bad configuration option: pubkeyacceptedalgorithms

description

ssh-jump fails with the following error message when local openssh version is less than equal to 8.5

kubectl ssh-jump -u azureuser -i ~/.ssh/id_rsa -p ~/.ssh/id_rsa.pub aks-nodepool1-20050870-vmss000001
using: port=22
Agent pid 31442
ssh-agent is already running
Forwarding from 127.0.0.1:2222 -> 22
Forwarding from [::1]:2222 -> 22
command-line: line 0: Bad configuration option: pubkeyacceptedalgorithms

My ssh-jump version is v0.7.1.

I tried with older ssh-jump ( version is less than v0.7.1) but I did NOT reproduce the issue, thus it may be caused by the change made in v0.7.1

environment info

kubectl ssh-jump: v0.7.1
OS: macOS BigSur ver 11.4

OpenSSH

$ ssh -V
OpenSSH_8.1p1, LibreSSL 2.7.3

kex_exchange_identification: Connection closed by remote host connection closed by unknown port 65535

Open a new issue to discuss the issue reported by @nippyin

#13 (comment)

it works from macOS however while trying on WSL2 i get same error

โ•š $ k ssh-jump XX.XX.XX.XXX --clean-agent --clean-jump -i ~/.ssh/my_rsa -p ~/.ssh/my_rsa.pub -u azureuser
using: port=22
Agent pid 6022
ssh-agent is already running
Creating SSH jump host (Pod)...
pod/sshjump created
Forwarding from 127.0.0.1:2222 -> 22
Forwarding from [::1]:2222 -> 22
Handling connection for 2222
[email protected]: Permission denied (publickey).
kex_exchange_identification: Connection closed by remote host
Connection closed by UNKNOWN port 65535

More than one connection (Connection to machine closed by remote host.)

Hey ๐Ÿ‘‹ great plugin! :)

It looks very promising, but I see that there are some problems if there is a one user, but with two connections.

Example:

Terminal 1:

โžœ kubectl ssh-jump -i my-key.pem -u admin machine-a
using: port=22
Agent pid 31693
ssh-agent is already running
Forwarding from 127.0.0.1:2222 -> 22
Forwarding from [::1]:2222 -> 22
Handling connection for 2222
Linux machine-a 4.9.0-12-amd64 #1 SMP Debian 4.9.210-1+deb9u1 (2020-06-07) x86_64
...

Terminal 2:

โžœ kubectl ssh-jump -i my-another-key.pem -u admin machine-b
using: port=22
Agent pid 31693
ssh-agent is already running
Linux machine-b 4.19.0-10-cloud-amd64 #1 SMP Debian 4.19.132-1 (2020-07-24) x86_64
...

Looks great so far, but if I log out from the machine-a:

admin@machine-a:~$ logout
Connection to machine-a closed.

It also closes a connection to machine-b

admin@machine-b:~$ Connection to 127.0.0.1 closed by remote host.
Connection to machine-b closed by remote host.
Connection to machine-b closed.

Do you have any suggestion how to deal with that problem? I can create a pod for every connection for example... But I need to edit the plugin then: https://github.com/yokawasa/kubectl-plugin-ssh-jump/blob/master/kubectl-ssh-jump#L131

I also wonder how it behaves if my whole team would want to ssh via this one pod :D I must run more tests...

Again, this plugin looks great!

identity-file not applied to forwarded connection

first of all, thx for publishing this plugin

I do have troubles on getting the connection to work. While debugging I found that this is the "final" ssh connection string that is used:

ssh -i /home/pn/.ssh/id_MYKEY -J [email protected]:2222 MYUSER@MYNODE

ssh extracts an ssh jump-host command from the '-J' option that results in:

debug1: Setting implicit ProxyCommand from ProxyJump: ssh -l root -p 2222 -v -W '[%h]:%p' 127.0.0.1

In this command the "-i /home/pn/.ssh/id_MYKEY" thingy get's lost. :-<

ssh then seems to attempt a bunch of my ssh keys that are lying around but doesn't pick up the correct (but NON standard named) key.

I could fix this by applying a general ssh-config like so:

Host 127.0.0.1
    Port 2222
    IdentityFile ~/.ssh/id_MYKEY

While I can get ssh-jump working this way it's not really nice as I don't want to tie 127.0.0.1 to this port/key in general.

Do you have an idea how this could be handled inside ssh-jump?

Thx for your time and feedback,
greets,
Peter

Ubuntu 18.04: nc: getaddrinfo: Name or service not known

Hi,

For Kubernetes on Ubuntu 18.04 hosts I get the following error:

jeroen@jri-linux-dell:~(acc-jri-ot)$ kubectl ssh-jump --skip-agent --cleanup-jump acc-jri-ot-k8s003
using: sshuser=j.rijken
using: identity=/home/jeroen/.ssh/XXXXX
using: pubkey=/home/jeroen/.ssh/XXXXX
using: port=22
Creating SSH jump host (Pod)...
pod/sshjump created
Forwarding from 127.0.0.1:2222 -> 22
Forwarding from [::1]:2222 -> 22
Handling connection for 2222
nc: getaddrinfo: Name or service not known
ssh_exchange_identification: Connection closed by remote host
Clearning up SSH Jump host (Pod)...
pod "sshjump" deleted
jeroen@jri-linux-dell:~(acc-jri-ot)$ kubectl get pod --watch
NAME      READY   STATUS    RESTARTS   AGE
sshjump   0/1     Pending   0          0s
sshjump   0/1     Pending   0          0s
sshjump   0/1     ContainerCreating   0          0s
sshjump   1/1     Running             0          5s
sshjump   1/1     Terminating         0          8s
sshjump   0/1     Terminating         0          38s
sshjump   0/1     Terminating         0          41s
sshjump   0/1     Terminating         0          41s

Custer info:

kubectl get nodes -o wide
NAME                 STATUS   ROLES    AGE   VERSION   INTERNAL-IP      EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION      CONTAINER-RUNTIME
acc-jri-ot-k8s001    Ready    <none>   14d   v1.17.5   172.18.113.163   <none>        Ubuntu 18.04.4 LTS   4.15.0-91-generic   docker://18.9.7
acc-jri-ot-k8s002    Ready    <none>   14d   v1.17.5   172.18.113.164   <none>        Ubuntu 18.04.4 LTS   4.15.0-91-generic   docker://18.9.7
acc-jri-ot-k8s003    Ready    <none>   14d   v1.17.5   172.18.113.165   <none>        Ubuntu 18.04.4 LTS   4.15.0-91-generic   docker://18.9.7
acc-jri-ot-k8sm001   Ready    master   14d   v1.17.5   172.18.113.161   <none>        Ubuntu 18.04.4 LTS   4.15.0-91-generic   docker://18.9.7
acc-jri-ot-k8sm002   Ready    master   14d   v1.17.5   172.18.113.162   <none>        Ubuntu 18.04.4 LTS   4.15.0-91-generic   docker://18.9.7

Workstation info:

$ cat /etc/lsb-release 
DISTRIB_ID=neon
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="KDE neon User Edition 5.18"
$ uname -a
Linux jri-linux-dell 4.15.0-99-generic #100-Ubuntu SMP Wed Apr 22 20:32:56 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

Adding the hostnames to my /etc/hosts file has no effect.

Let me know if you need more info.

Regards,
Jeroen Rijken

user switches to root

Hi,

I followed the instructions on https://github.com/yokawasa/kubectl-plugin-ssh-jump#case-2-access-remote-serivces-via-ssh-local-port-forwarding but receive the following error:

Setting destination name as 'jumphost' allows to ssh into SSH jump Pod as 'root' user
using: port=22
using: args=-L 5432:someserver:5432
Agent pid 350109
ssh-agent is already running
Creating SSH jump host (Pod)...
pod/sshjump created
Forwarding from 127.0.0.1:2222 -> 22
Forwarding from [::1]:2222 -> 22
Handling connection for 2222
[email protected]: Permission denied (publickey).

Pod nodeSelector

We run a mix of arch types, I think the ssh-jump pod is supposed to work on x86/amd64, as I can't get it to work on arm64, would you add an additional nodeSelector type:

nodeSelector:
"kubernetes.io/os": linux
"kubernetes.io/arch": "amd64"

what is the default password?

  1. Clean all the things.
$ kubectl ssh-jump minikube -u myuser -i ~/.ssh/id_rsa -p ~/.ssh/id_rsa.pub   --cleanup-jump  --cleanup-agent
using: port=22
Agent pid 1968168
ssh-agent is already running
Forwarding from 127.0.0.1:2222 -> 22
Forwarding from [::1]:2222 -> 22
Handling connection for 2222
Enter passphrase for key '/home/xiaojie/.ssh/id_rsa': 
[email protected]: Permission denied (publickey).
kex_exchange_identification: Connection closed by remote host
Clearning up SSH Jump host (Pod)...
pod "sshjump" deleted


Killing ssh-agent...
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;

  1. reset keys
# all is 'enter'
$ ssh-keygen -t rsa
  1. re-deploy
$ kubectl ssh-jump minikube -u myuser -i ~/.ssh/id_rsa -p ~/.ssh/id_rsa.pub
using: port=22
Started ssh-agent: pid=1998827
Agent pid 1998827
Identity added: /home/xiaojie/.ssh/id_rsa (xiaojie@xiaojie-ubuntu)
Creating SSH jump host (Pod)...
pod/sshjump created
Forwarding from 127.0.0.1:2222 -> 22
Forwarding from [::1]:2222 -> 22
Handling connection for 2222
myuser@minikube's password: 
Permission denied, please try again.
myuser@minikube's password: 
Permission denied, please try again.
myuser@minikube's password: 
myuser@minikube: Permission denied (publickey,password).

what is the password of myuser?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.