Search code examples
namespacesyamlcontainersrancherrancher-rke

Rancher - Is it possible to spin-up / recreate an entire namespace available in one environment on a new environment


Rancher: v2.2.4

In Rancher GUI, I see on one of our environment (Dev) and it contains a namespace 'n1'. This namespace under different sections (i.e. Workloads, LoadBalancers, ConfigMaps, Volumes etc) have few entries (containers/settings etc).

I want to create the same namespace on a new environment where Rancher is running. This environment lets say is (Test). After getting all the required docker images (sudo docker image pull <server:port>/<imagename:imageversion>), do I need to download YAMLs of all these entries under each sections and import them to the target environment? (possibly changing volumes-id, container image entries i.e. name: <server:port>/<imagename:imageversion> locations (if any), controller-uid to keep the one on the target (TEST) environement)? My understanding is, if I create a new workload/add anything under a respective section, the label/annotations will generate a fresh controller-id value! so, I'm wondering before importing the YAML, if I should leave the controller-uid entry value blank (not sure if it'll barf).

Is there a simple way to spin up/create an entire namespace 'n1' on TEST environment (i.e. namespace replica of n1 Dev in Test) with auto-generating the necessary Storage bits (volume classes/volumes and persistent volumes - all of these have some Vol ID/name/uid associated with each entity), Deployments bits (uid/controller-uids) etc?

What's an efficient way to do this so that I don't have to manually download YAMLs (from Dev) and import them in Test at each component level (i.e. Volumes YAMLs, Volume Class YAML, Workloads/Deployment YAMLs etc - one by one)?


Solution

  • You can use the following to grab all resources from a namespace and apply them in a new namespace.

    #!/bin/bash
    SourceNamespace="SourceNS"
    TargetNamespace="TargetNS"
    TempDir="./tmp"
    
    echo "Grabbing all resources in $SourceNamespace"
    for APIResource in `kubectl api-resources --verbs=list --namespaced -o name`
    do
      kubectl -n "$SourceNamespace" get "$APIResource" -o yaml > "$TempDir"/"$APIResource".yaml
    done
    
    echo "Deploying all resources in $TargetNamespace"
    for yaml in `ls $TempDir`
    do
      kubectl apply -n "$TargetNamespace" -f "$TempDir"/"$yaml"
    done