Copy a Kubernetes pod and run commands in its environment

Related tags

DevOps Toolscopypod
Overview

copypod

Utility for copying a running Kubernetes pod so you can run commands in a copy of its environment, without worrying about it the pod potentially being removed due to a deploy.

copypod can work in two different modes, depending on if the --interactive flag is provided:

  • If the flag is left out, copypod will copy the specified pod and start it. When the pod reaches the "Running" state the name of the pod will be outputted as the only output. This is intended for use in automation scenarios.
  • If a command is provided with the --interactive flag, then the pod will be copied and started as before, but when the pod is running kubectl will be called and connect to the pod where the provided command is then run interactively. When the kubectl program exits the pod will be removed. This is intended for running ad-hoc tasks and processes.

Install

You can either install copypod into a virtual environment directly with:

pip install git+ssh://[email protected]/Memrise/copypod.git

then the program will be available as copypod inside the virtual environment, or you can install it by cloning this repository and then use pipenv to set up a virtual environment where it will get installed into:

git clone [email protected]:Memrise/copypod.git
cd copypod/
pipenv install

Then you can run the program with pipenv run copypod.

Usage

$ copypod --help
usage: copypod [-h] [--context CONTEXT] [-n NAMESPACE] (-l SELECTOR | -p POD) [--container CONTAINER] [-c COMMAND] [-i INTERACTIVE]

Copy a Kubernetes pod and run commands in its environment.

optional arguments:
  -h, --help            show this help message and exit
  --context CONTEXT     Kubectl context to use for configuration (default: None)
  -n NAMESPACE, --namespace NAMESPACE
                        Namespace for where the source pod is located (default: default)
  -l SELECTOR, --selector SELECTOR
                        Label selector of pod to copy (default: None)
  -p POD, --pod POD     Name of the pod to copy (default: None)
  --container CONTAINER
                        Name of container to copy, only needed if the pod has more than one container (default: None)
  -c COMMAND, --command COMMAND
                        Initial command to run in the copied pod (default: sleep infinity)
  -i INTERACTIVE, --interactive INTERACTIVE
                        Command to run in an interactive console (default: None)
  --image IMAGE         Copy the entire environment of the pod but use this Docker image instead (default: None)

If the `--interactive` flag is provided, the copied pod will be removed immediately after the command exits, otherwise the name of the pod will be printed.

Examples

Say you wanted to copy the pod named my-great-pod and have the copied pod run until you specifically remove it, you could run:

$ copypod -p my-great-pod
pod-copy-girwak

pod-copy-girwak is then the name of the new pod created for you, and it will by default run sleep infinity as the starting command, meaning it will keep running forever until it's deleted.

At this point you can enter the pod and run commands as you'd like, for instance start a shell inside the pod with:

$ kubectl exec -it pod-copy-girwak -- bash
[email protected]:/#

When you are done you can remove the copied pod again with kubectl:

$ kubectl delete pod pod-copy-girwak
pod "pod-copy-girwak" deleted

Say you instead would like to copy a pod, start a shell in the copied pod and have the pod be deleted when you exit the shell, you can do that by supplying the --interactive flag like this:

$ copypod -p my-great-pod -i bash
[email protected]:/# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 10:43 ?        00:00:00 sleep infinity
root         7     0  0 10:43 ?        00:00:00 bash
root        13     7  0 10:43 ?        00:00:00 ps -ef

When you are done doing what you needed the pod for, you can exit the shell and the pod will be removed immediately.

The value for the --interactive flag is the command you'd like to start inside the pod.


Instead of having to look up the name of a pod before running copypod, you can also specify labels which match one or more pods that you'd like to copy. copypod will then pick the first pod matching the lables and copy that for you. This can be done with the --selector flag. It works the same way as for the kubectl command.

If we for example have one or more pods with the label app: my-great-service we can copy any of those pods without having to know the exact pod name by running:

$ copypod -l app=my-great-service -i bash
[email protected]:/#

Note regarding Alpine Linux

The sleep command in images based on Alpine Linux does not support "infinity" as an argument unless the "coreutils" package is installed. As a work around you can instead specify --command "sleep 1d" as an argument to copypod to change the command run in the new pod.

Owner
Memrise
Learn a language. Meet the world.
Memrise
A collection of beginner-friendly DevOps content

mansion Mansion is just a testing repo for learners to commit into open source project. These are the steps you need to learn: Please do not edit thes

Bryan Lim 62 Nov 30, 2022
DC/OS - The Datacenter Operating System

DC/OS - The Datacenter Operating System The easiest way to run microservices, big data, and containers in production. What is DC/OS? Like traditional

DC/OS 2.3k Jan 06, 2023
A Simple script to hunt unused Kubernetes resources.

K8SPurger A Simple script to hunt unused Kubernetes resources. Release History Release 0.3 Added Ingress Added Services Account Adding RoleBindding Re

Yogesh Kunjir 202 Nov 19, 2022
Simple ssh overlay for easy, remote server management written in Python GTK with paramiko

Simple "ssh" overlay for easy, remote server management written in Python GTK with paramiko

kłapouch 3 May 01, 2022
Visual disk-usage analyser for docker images

whaler What? A command-line tool for visually investigating the disk usage of docker images Why? Large images are slow to move and expensive to store.

Treebeard Technologies 194 Sep 01, 2022
A tool to clone efficiently all the repos in an organization

cloner A tool to clone efficiently all the repos in an organization Installation MacOS (not yet tested) python3 -m venv .venv pip3 install virtualenv

Ramon 6 Apr 15, 2022
A charmed operator for running PGbouncer on kubernetes.

operator-template Description TODO: Describe your charm in a few paragraphs of Markdown Usage TODO: Provide high-level usage, such as required config

Canonical 1 Dec 01, 2022
Chef-like functionality for Fabric

/ / ___ ___ ___ ___ | | )| |___ | | )|___) |__ |__/ | __/ | | / |__ -- Chef-like functionality for Fabric About Fabric i

Sébastien Pierre 1.3k Dec 21, 2022
A tool to convert AWS EC2 instances back and forth between On-Demand and Spot billing models.

ec2-spot-converter This tool converts existing AWS EC2 instances back and forth between On-Demand and 'persistent' Spot billing models while preservin

jcjorel 152 Dec 29, 2022
Linux, Jenkins, AWS, SRE, Prometheus, Docker, Python, Ansible, Git, Kubernetes, Terraform, OpenStack, SQL, NoSQL, Azure, GCP, DNS, Elastic, Network, Virtualization. DevOps Interview Questions

Linux, Jenkins, AWS, SRE, Prometheus, Docker, Python, Ansible, Git, Kubernetes, Terraform, OpenStack, SQL, NoSQL, Azure, GCP, DNS, Elastic, Network, Virtualization. DevOps Interview Questions

Arie Bregman 35.1k Jan 02, 2023
Jenkins-AWS-CICD - Implement Jenkins CI/CD with AWS CodeBuild and AWS CodeDeploy, build a python flask web application.

Jenkins-AWS-CICD - Implement Jenkins CI/CD with AWS CodeBuild and AWS CodeDeploy, build a python flask web application.

Ning 1 Jan 01, 2022
Hw-ci - Hardware CD/CI and Development Container

Hardware CI & Dev Containter These containers were created for my personal hardware development projects and courses duing my undergraduate degree. Pl

Matthew Dwyer 6 Dec 25, 2022
Oracle Cloud Infrastructure Object Storage fsspec implementation

Oracle Cloud Infrastructure Object Storage fsspec implementation The Oracle Cloud Infrastructure Object Storage service is an internet-scale, high-per

Oracle 9 Dec 18, 2022
Spinnaker is an open source, multi-cloud continuous delivery platform for releasing software changes with high velocity and confidence.

Welcome to the Spinnaker Project Spinnaker is an open-source continuous delivery platform for releasing software changes with high velocity and confid

8.8k Jan 07, 2023
A Habitica Integration with Github Workflows.

Habitica-Workflow A Habitica Integration with Github Workflows. How To Use? Fork (and Star) this repository. Set environment variable in Settings - S

Priate 2 Dec 20, 2021
CDK Template of Table Definition AWS Lambda for RDB

CDK Template of Table Definition AWS Lambda for RDB Overview This sample deploys Amazon Aurora of PostgreSQL or MySQL with AWS Lambda that can define

AWS Samples 5 May 16, 2022
More than 130 check plugins for Icinga and other Nagios-compatible monitoring applications. Each plugin is a standalone command line tool (written in Python) that provides a specific type of check.

Python-based Monitoring Check Plugins Collection This Enterprise Class Check Plugin Collection offers a package of more than 130 Python-based, Nagios-

Linuxfabrik 119 Dec 27, 2022
Asynchronous parallel SSH client library.

parallel-ssh Asynchronous parallel SSH client library. Run SSH commands over many - hundreds/hundreds of thousands - number of servers asynchronously

1.1k Dec 31, 2022
Let's learn how to build, release and operate your containerized applications to Amazon ECS and AWS Fargate using AWS Copilot.

🚀 Welcome to AWS Copilot Workshop In this workshop, you'll learn how to build, release and operate your containerised applications to Amazon ECS and

Donnie Prakoso 15 Jul 14, 2022