An open API service indexing awesome lists of open source software.

https://github.com/epolevov/k8s-migrate

Migrate Kubernetes namespaces and resources between clusters using kubectl in one command.
https://github.com/epolevov/k8s-migrate

devops devops-tools k8s kubernetes transfer-data

Last synced: about 1 month ago
JSON representation

Migrate Kubernetes namespaces and resources between clusters using kubectl in one command.

Awesome Lists containing this project

README

          

# ๐Ÿ› ๏ธ k8s-migrate

[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](./LICENSE)
[![Shell Script](https://img.shields.io/badge/Language-Bash-blue)](https://www.gnu.org/software/bash/)
[![Kubernetes](https://img.shields.io/badge/Kubernetes-namespace--migrator-blue)](https://kubernetes.io/)

A lightweight Bash CLI utility to **migrate a namespace's resources between Kubernetes clusters** (contexts) using `kubectl`.

---

## ๐Ÿ“† What This Tool Does

`k8s-migrate.sh` interactively exports and applies Kubernetes resources from a specified namespace between two kube-contexts โ€” allowing you to:

- Migrate applications from **dev โž” prod**, **test โž” staging**, etc.
- Sync environments quickly
- Prepare for cluster upgrades or provider migrations

It works using native `kubectl` commands and supports all common namespaced resources.

---

## ๐Ÿš€ Quick Start

### โœ… Prerequisites

- `kubectl` installed and configured
- Both clusters defined in your `~/.kube/config`
- Access rights to export and apply resources

> Optional:
>
> - [`yq`](https://github.com/mikefarah/yq) for sanitizing exported YAML
> - `kubectl neat` for cleaning unnecessary fields

---

### ๐Ÿงช Demo

```bash
chmod +x k8s-migrate.sh
./k8s-migrate.sh [--dryRun]
```

```text
Enter namespace to migrate: my-app
Enter source K8S context (from-cluster): dev-cluster
Enter target K8S context (to-cluster): prod-cluster

Select resources to migrate (space-separated or 'all'):
Available: deployments services configmaps secrets ingresses pvc all
> all

๐Ÿ” Migration Summary:
Namespace: my-app
Source cluster: dev-cluster
Target cluster: prod-cluster
Resources: deployments services configmaps secrets ingresses pvc
Dry Run Mode: true

Proceed with migration? (y/n): y

๐Ÿ“ค Exporting...
๐Ÿ“ฅ Importing...
โœ… Migration complete (dry-run only).
```

---

## ๐Ÿ”ง Supported Resources

- `deployments`
- `services`
- `configmaps`
- `secrets` โš ๏ธ
- `ingresses`
- `pvc` (PersistentVolumeClaims)

You can select specific types or use `all` to migrate everything listed above.

---

## ๐Ÿ” Secret Handling

Secrets are exported and applied as-is. Be careful:

- **Do not** migrate `service account tokens` or `cloud provider credentials` unless necessary.
- Consider sanitizing secrets before applying them to another cluster.

---

## ๐Ÿ“‚ Temporary Files

All exported resources are saved to a temporary working directory (via `mktemp -d`) and applied from there. This folder is **not deleted automatically**, so you can inspect or archive it if needed.

---

## ๐Ÿงฑ Advanced Ideas

Want to improve or extend this?

- โœ… Add `--dryRun` support to preview changes before applying โœ…
- Sanitize YAML via `yq` or `kubectl neat`
- Support Helm releases or CRDs
- Use labels or selectors to filter resources
- Migrate RBAC (`RoleBinding`, `ServiceAccount`, etc.)

---

## ๐Ÿง‘โ€๐Ÿ’ป Author

Created by [@epolevov](https://github.com/epolevov)\
Maintained by the engineering team at [EMD Labs](https://emd.one)

---

## ๐Ÿ“œ License

This project is licensed under the [MIT License](./LICENSE).