# Deploying workloads via API

**URL:** <https://forums.suse.com/t/deploying-workloads-via-api/10727>\
**Category:** SUSE Rancher Prime\
**Created:** [June 12, 2018, 10:09am UTC](https://forums.suse.com/t/deploying-workloads-via-api/10727 "2018-06-12T10:09:49Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![dibi](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/dibi/32/4160_2.png) [@dibi](https://forums.suse.com/u/dibi)\
**Post date:** [June 12, 2018, 10:09am UTC](https://forums.suse.com/t/deploying-workloads-via-api/10727/1 "2018-06-12T10:09:49Z")

</div>

I’ve got a problem regarding deploying workloads via `kubectl`. So basically the way we do deploy our services is a combination of `kubectl create -f deployment.yml` and `kubectl apply -f ...` etc. This also works for our rancher 2 setup but alle those deployments are not appearing inside the rancher project UI. Instead they are listed at `Project -> Namespaces -> Not in a project`.

My question is: Do I need a special rancher/cattle annotation to add a deployment to a project or how do I do this?

---

<div class="post-metadata">

**Author:** ![etlweather](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/etlweather/32/419_2.png) [@etlweather](https://forums.suse.com/u/etlweather)\
**Post date:** [June 12, 2018, 2:02pm UTC](https://forums.suse.com/t/deploying-workloads-via-api/10727/2 "2018-06-12T14:02:22Z")

</div>

I just tried this out - I created a new project in my cluster and was able to deploy via `kubectl apply` by specifying the namespace.

Presumably, the namespace names must be unique across all projects? I don’t know.

But I created a “test-project” with a “test-namespace” - which does not exist in my “default” project. My deployment looked like:

```auto
kind: Deployment
apiVersion: apps/v1
metadata:
  name: test-deploy
  namespace: test-namespace
spec: ...

```

---

<div class="post-metadata">

**Author:** ![dibi](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/dibi/32/4160_2.png) [@dibi](https://forums.suse.com/u/dibi)\
**Post date:** [June 12, 2018, 2:14pm UTC](https://forums.suse.com/t/deploying-workloads-via-api/10727/3 "2018-06-12T14:14:23Z")

</div>

Thats right! But I wanted to do this automatically say: `kubectl create namespace test-namespace` which will automatically be part of a given project or default if none is defined.

---

<div class="post-metadata">

**Author:** ![etlweather](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/etlweather/32/419_2.png) [@etlweather](https://forums.suse.com/u/etlweather)\
**Post date:** [June 12, 2018, 2:28pm UTC](https://forums.suse.com/t/deploying-workloads-via-api/10727/4 "2018-06-12T14:28:14Z")

</div>

You mean, without specifying the namespace in the YAML file?

Then maybe try:

`kubectl --namespace test-namespace apply -f myfile.yml`

---

<div class="post-metadata">

**Author:** ![dibi](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/dibi/32/4160_2.png) [@dibi](https://forums.suse.com/u/dibi)\
**Post date:** [June 12, 2018, 3:30pm UTC](https://forums.suse.com/t/deploying-workloads-via-api/10727/5 "2018-06-12T15:30:48Z")

</div>

Maybe I did not express myself correctly.  
I know how to create a namespace and or apply a yaml file. But when I am doing this remotely just by using `kubectl create namespace foo` the cluster DOES create a namespace but it is NOT actually bound to any rancher project. How do I bind a newly created namespace to a rancher project without accessing the UI. I thought I can bind it via any kind of annotation or something.

[Screenshot](https://dbecker.net/ds/Screen-Shot-2018-06-12-17-27-43-W1SWWWA2O9.png)

---

<div class="post-metadata">

**Author:** ![etlweather](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/etlweather/32/419_2.png) [@etlweather](https://forums.suse.com/u/etlweather)\
**Post date:** [June 12, 2018, 5:00pm UTC](https://forums.suse.com/t/deploying-workloads-via-api/10727/6 "2018-06-12T17:00:54Z")

</div>

Got you! I created the namespace through the UI… it’s not so much deploying the workload in a namespace within a project then the problem, but creating namespaces within a project, through the CLI…

---

<div class="post-metadata">

**Author:** ![etlweather](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/etlweather/32/419_2.png) [@etlweather](https://forums.suse.com/u/etlweather)\
**Post date:** [June 12, 2018, 5:06pm UTC](https://forums.suse.com/t/deploying-workloads-via-api/10727/7 "2018-06-12T17:06:21Z")

</div>

Not with `kubectl`, but this may help:

```auto
λ rancher.exe namespaces help
Operations on namespaces

Usage: rancher namespaces [OPTIONS] COMMAND [arg...]

Version: v2.0.1

Options:
  --quiet, -q Only display IDs
  --help, -h show help

Commands:
  ls List namespaces
  create Create a namespace
  delete, [rm] Delete a namespace by name or ID
  associate Associate a namespace with a project
  help, [h] Shows a list of commands or help for one command

Run 'rancher namespaces COMMAND --help' for more information on a command.

```

---

<div class="post-metadata">

**Author:** ![dibi](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/dibi/32/4160_2.png) [@dibi](https://forums.suse.com/u/dibi)\
**Post date:** [June 12, 2018, 5:19pm UTC](https://forums.suse.com/t/deploying-workloads-via-api/10727/8 "2018-06-12T17:19:08Z")

</div>

Ahh thank you very much thats what I’ve been looking for! I thought it might work by an annotation in conjunction with a rancher auto discovery service or something like that. That would be nice.

---

<div class="post-metadata">

**Author:** ![etlweather](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/etlweather/32/419_2.png) [@etlweather](https://forums.suse.com/u/etlweather)\
**Post date:** [June 12, 2018, 6:07pm UTC](https://forums.suse.com/t/deploying-workloads-via-api/10727/9 "2018-06-12T18:07:30Z")

</div>

I believe it is based on annotations - you could do more tests but the namespace I created inside a project through the UI shows this:

```auto
kubectl.exe describe namespace defaultns
Name: defaultns
Labels: field.cattle.io/projectId=p-r7jvd
Annotations: cattle.io/status={"Conditions":[{"Type":"InitialRolesPopulated","Status":"True","Message":"","LastUpdateTime":"2018-06-12T13:57:33Z"}]}
              field.cattle.io/creatorId=user-5srnj
              field.cattle.io/projectId=c-9n972:p-r7jvd
              lifecycle.cattle.io/create.namespace-auth=true
Status: Active

No resource quota.

No resource limits.

```

In annotations: `field.cattle.io/projectId=c-9n972:p-r7jvd` and in labels: `field.cattle.io/projectId=p-r7jvd`

---

<div class="post-metadata">

**Author:** ![dibi](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/dibi/32/4160_2.png) [@dibi](https://forums.suse.com/u/dibi)\
**Post date:** [June 13, 2018, 9:22am UTC](https://forums.suse.com/t/deploying-workloads-via-api/10727/10 "2018-06-13T09:22:38Z")

</div>

Sadly this doesn’t work… I added the label and the annotations ☹

---

<div class="post-metadata">

**Author:** ![seimic](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/seimic/32/6370_2.png) [@seimic](https://forums.suse.com/u/seimic)\
**Post date:** [June 19, 2018, 1:47pm UTC](https://forums.suse.com/t/deploying-workloads-via-api/10727/11 "2018-06-19T13:47:59Z")

</div>

Take a look at

> rancher context switch

This listes all available clusters and projects in it and lets you choose one as the current one.  
After this

> rancher kubectl create/apply -f whateve.yml

I suppose this creates things in context of the currently selected project.

---

<div class="post-metadata">

**Author:** ![jeremyweber-np](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/jeremyweber-np/32/4190_2.png) [@jeremyweber-np](https://forums.suse.com/u/jeremyweber-np)\
**Post date:** [June 19, 2018, 8:58pm UTC](https://forums.suse.com/t/deploying-workloads-via-api/10727/12 "2018-06-19T20:58:29Z")

</div>

The approach I took was:

Deploy the application in the UI. After its been deployed, export the yml, so you have something to work with. Then execute something like:

./rancher kubectl create --namespace yournamespace -f pathtoyaml

HTH

---

<div class="post-metadata">

**Author:** ![marco](https://avatars.discourse-cdn.com/v4/letter/m/a698b9/32.png) [@marco](https://forums.suse.com/u/marco)\
**Post date:** [January 14, 2019, 8:15pm UTC](https://forums.suse.com/t/deploying-workloads-via-api/10727/13 "2019-01-14T20:15:30Z")

</div>

have a look at [https://github.com/saxix/lazo](https://github.com/saxix/lazo)

---

<div class="post-metadata">

**Author:** ![Shafi\_Rasulov](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/shafi_rasulov/32/6194_2.png) [@Shafi\_Rasulov](https://forums.suse.com/u/Shafi_Rasulov)\
**Post date:** [March 20, 2020, 11:02pm UTC](https://forums.suse.com/t/deploying-workloads-via-api/10727/14 "2020-03-20T23:02:33Z")

</div>

Select the namespace and move it to the corresponding project. Probably the namespace was created by kubectl, that is why rancher does not have mapping of namespace-\>project. So, you should do it manually.
