Deploying workloads via API

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?

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:

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

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.

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

Then maybe try:

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

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

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…

Not with kubectl, but this may help:

λ 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.

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.

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:

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

1 Like

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

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.

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

have a look at https://github.com/saxix/lazo

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.