Have you ever wanted to shuck away the hard, rough exterior of an ECS cluster and get to the soft, chewy innards? Sounds like you need KnuckleCluster! This tool provides scripts, invoked via cli or rakefile, to list, connect to and/or run commands on ecs agents and containers via ssh. This makes it very easy to interrogate ECS agents and containers without having to go digging for IP addresses and things. Primarily created as a tool to connect to instances in an ECS cluster and see what is running on them, it has evolved slightly to include the ability to list instances in spot requests and auto-scaling groups.
- See what agents in your ECS cluster are doing
- Easily connect to running agents
- Easily connect and get a console inside running containers
- Create shortcuts to oft-used commands and run them easily
- Optionally integrates with aws-vault for AWS authentication
Is being used in production for various projects and is considered stable. Any new features/bug fixes etc are most welcome!
KnuckleCluster can be used in two ways:
- As a system-wide executable with a config file (preferred)
- As a rake task in your project
Install the executable with:
$ gem install knuckle_cluster
Add this line to your application's Gemfile:
source 'https://rubygems.envato.com/' do gem 'knuckle_cluster' end
And then execute:
Knuckle Cluster can be used either as a part of an application via a rakefile, or at a system level.
You'll need to execute knuckle_cluster with appropriate AWS permissions on the cluster in question, stored in your ENV. I like to use
aws-vault to handle this for me.
Create a file:
~/.ssh/knuckle_cluster. This is the config file that will be used to make connections from the command line. It is a yaml file. The connection name is the key, and all parameters are below it. EG:
platform: cluster_name: platform-ecs-cluster-ABC123 region: us-east-1 bastion: platform_bastion rsa_key_location: ~/.ssh/platform_rsa_key ssh_username: ubuntu sudo: true aws_vault_profile: platform_super_user shortcuts: console: container: web command: bundle exec rails console db: container: worker command: script/db_console tunnels: db: local_port: 54321 remote_host: postgres-db.yourcompany.com remote_port: 5432 hide: container: some_container_regex_i_dont_care_about task: some_task_regex_i_dont_care_about
You can also use inheritance to simplify the inclusion of multiple similar targets:
super_platform: <<: *default_platform cluster_name: super-platform-ecs-cluster-ABC123 ultra_platform: <<: *default_platform cluster_name: ultra-platform-ecs-cluster-DEF987 sudo: false default_platform: &default_platform region: us-east-1 bastion: platform_bastion rsa_key_location: ~/.ssh/platform_rsa_key ssh_username: ubuntu sudo: true aws_vault_profile: platform_super_user other_platform: &other_platform region: us-east-1 bastion: username: ubuntu host: bastion.endpoint.example.com rsa_key_location: ~/.ssh/bastion_rsa_key rsa_key_location: ~/.ssh/platform_rsa_key ssh_username: ubuntu sudo: true aws_vault_profile: platform_super_user
See Options for Knuckle Cluster below for a list of what each option does.
Command line options:
knuckle_cluster list - list all available clusters knuckle_cluster CLUSTER_PROFILE agents - list all agents and select one to start a shell knuckle_cluster CLUSTER_PROFILE containers - list all containers and select one to start a shell knuckle_cluster CLUSTER_PROFILE logs CONTAINER_NAME - tail the logs for a container knuckle_cluster CLUSTER_PROFILE CONTAINER_NAME [OPTIONAL COMMANDS] - connect to a container and start a shell or run a command knuckle_cluster CLUSTER_PROFILE SHORTCUT_NAME - run a shortcut defined in your knuckle_cluster configuration knuckle_cluster CLUSTER_PROFILE tunnel TUNNEL_NAME - open a tunnel defined in your knuckle_cluster configuration knuckle_cluster CLUSTER_PROFILE scp source destination - copied a file via scp to or from a container or agent. Use container:<location> or agent:<location>
It takes one argument at minimum:
cluster_name . A region is likely also required as it will default to
kc = .( cluster_name: 'platform-ecs-cluster-ABC123', region: 'us-east-1', bastion: 'platform_bastion', rsa_key_location: "~/.ssh/platform_rsa_key", ssh_username: "ubuntu", sudo: true ) task :agents do kc.connect_to_agents end task :containers do kc.connect_to_containers end
rake agents or
Once you have an instance of KnuckleCluster, you can now do things!
$ knuckle_cluster super_platform containers
Which will give you the output and run bash for you on the actual docker container:
Listing Containers TASK | AGENT | INDEX | CONTAINER ------------------|---------------------|-------|-------------------- task-one | i-123abc123abc123ab | 1 | t1-container-one task-two | i-123abc123abc123ab | 2 | t2-container-one | | 3 | t2-container-two task-three | i-456def456def456de | 4 | t3-container-one | | 5 | t3-container-two | | 6 | t3-container-three Connect to which container?
Same with connecting directly to agents
$ knuckle_cluster super_platform agents
Listing Agents INDEX | INSTANCE_ID | TASK | CONTAINER ------|---------------------|------------|-------------------- 1 | i-123abc123abc123ab | task-one | t1-container-one | | task-two | t2-container-one | | | t2-container-two 2 | i-456def456def456de | task-three | t3-container-one | | | t3-container-two | | | t3-container-three Connect to which agent?
connect_to_agents can have the following optional arguments:
|command||Runs a command on the specified container/agent. When connecting to containers, this defaults to
|auto||Automatically connects to the first container/agent it can find. Handy when used in conjunction with
Eg: To connect to a container, echo something and then immediately disconnect you could use:
kc.connect_to_containers(auto: true, command: "echo I love KnuckleCluster!")
Options for Knuckle Cluster
Possible options are below. If left blank, they will be ignored and defaults used where available.:
|cluster_name||The name of the cluster (not the ARN). eg 'my-super-cluster'. One of
|spot_request_id||The spot request ID you are connecting to. eg 'sfr-abcdef'. One of
|asg_name||The auto-scaling group name you are connecting to. eg 'very-scaly-group'. One of
|region||The AWS region you would like to use. Defaults to
|bastion||if you have a bastion to proxy to your ecs cluster via ssh, put the name of it here as defined in your
|rsa_key_location||The RSA key needed to connect to an ecs agent eg
|ssh_username||The username to conncet. Will default to
|sudo||true or false - will sudo the
|aws_vault_profile||If you use the
|profile||Another profile to inherit settings from. Settings from lower profiles can be overridden in higher ones.|
|hide||allows you to specify a regex for either
If you wish to see what instances are running within a spot fleet, KnuckleCluster can do that too!. In your config, use
spot_request_id instead of
cluster_name. Note that the
containers command will not work when invoking (use
If you wish to see what instances are running within an ASG, KnuckleCluster can do that too!. In your config, use
asg_name instead of
cluster_name. Note that the
containers command will not work when invoking (use
You can use Knuckle Cluster to copy files in and out of agents or containers. Note that this will only work where one of the source or destination is your local machine, copying between containers is not yet supported. Use a syntax similar to existing
scp syntax when specifying a source or destination, but use the keyword
agent for the remote. When using one of these keywords, you will be prompted as to which agent/container you wish to use.
#Copy some_file.txt into a container at /app/some_file.txt knuckle_cluster super_platform scp ./some_file.txt container:/app/some_file.txt #Copy /app/some_file.txt from a remote container to ./some_file.txt on your local machine knuckle_cluster super_platform scp container:/app/some_file.txt ./some_file.txt #Copy some_file.txt into an agent at ~/some_file.txt knuckle_cluster super_platform scp ./some_file.txt agent:~/some_file.txt #Copy /app/some_file.txt from a remote container to ./some_file.txt on your local machine knuckle_cluster super_platform scp agent:~/some_file.txt ./some_file.txt
KnuckleCluster uses MIT license. See
Code of conduct
We welcome contribution from everyone. Read more about it in
For bug fixes, documentation changes, and small features:
- Fork it ( https://github.com/[my-github-username]/knuckle_cluster/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
For larger new features: Do everything as above, but first also make contact with the project maintainers to be sure your change fits with the project direction and you won't be wasting effort going in the wrong direction.