Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using lxc_conf in start #79

Closed
ureyes84 opened this issue Nov 6, 2013 · 7 comments
Closed

using lxc_conf in start #79

ureyes84 opened this issue Nov 6, 2013 · 7 comments

Comments

@ureyes84
Copy link
Contributor

ureyes84 commented Nov 6, 2013

How do I use lxc_conf in start?

I'd like to set cpu and cpu shares, something like this:

docker run -i -t -lxc-conf="lxc.cgroup.cpu.shares=1" -lxc-conf="lxc.cgroup.cpuset.cpus=0" my_image /bin/bash stress_cpu.sh

According to the documentation I should be able to pass a dictionary:

lxc_conf allows to pass LXC configuration options in dict form.

So my code looks like this:

self.client.start(container, lxc_conf = self.get_lxc_conf())
def get_lxc_conf(self):        
  return {
  #Restrict container to a core
  'lxc.cgroup.cpuset.cpus' : str(self.cpu_number), 
  #All containers get the same share
  'lxc.cgroup.cpu.cpu.shares' : str(1) 
}

However, I can't get it work, I keep getting the following error message:

500 Server Error: Internal Server Error ("json: cannot unmarshal object into Go value of type []docker.KeyValuePair")

I get the same error even with the dictionary example in the API docs:

def get_lxc_conf(self):
    return {"lxc.utsname":"docker"}

I've tried passing lists and strings

Thanks!

@ureyes84
Copy link
Contributor Author

ureyes84 commented Nov 6, 2013

This doesn't seem to error out but I it doesn't work either (takes all CPUs):

self.client.start(container, lxc_conf = self.get_lxc_conf())
def get_lxc_conf(self):
return[
    #All containers get the same share
    {'lxc.cgroup.cpu.shares':1},
    #Restrict container to a core
    {'lxc.cgroup.cpuset.cpus':self.cpu_number}
]

The CLI counterpart is able to limit this CPU-intensive script to a single CPU:

docker run -i -t -lxc-conf="lxc.cgroup.cpu.shares=1" -lxc-conf="lxc.cgroup.cpuset.cpus=0" my_image /bin/bash stress_cpu.sh

@ureyes84
Copy link
Contributor Author

ureyes84 commented Nov 6, 2013

Interestingly, after running

docker run -i -t -lxc-conf="lxc.cgroup.cpu.shares=1" -lxc-conf="lxc.cgroup.cpuset.cpus=0" my_image /bin/bash stress_cpu.sh

I can just do

docker start 104059c2e613

And the previous resource limits are applied. However if I do afterwards the equivalent (I think) with docker-py, the resource limits are ignored:

self.docker_client.start('104059c2e613')

@denibertovic
Copy link
Contributor

@ureyes84 When using the docker CLI the run command has a -c flag that's used for setting CPU shares.

As for docker-py. the lxc_conf param in the Client.start method expects a list of dicts like so:

[{"Key":"lxc.cgroup.cpu.shares","Value":"1"}, {"Key": ..., "Value": }]

And, both methods (CLI and docker-py) set that correctly in the lxc config (you can verify this in /var/lib/docker/containers/{container_id}/config.

You can test with:

client.create_container('ubuntu', '/bin/bash -c "dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null"')

and then:

c.start('04095d4fc948', lxc_conf=[{'Key': 'lxc.cgroup.cpuset.cpus', 'Value': '1'}])

Note that the restriction we use to restrict the number of cores to be used is 'lxc.cgroup.cpuset.cpus'
and not lxc.cgroup.cpu.shares (this one does something different and depends on the current usage of the CPU, ie.
if nobody is using their shares the one container will get all the cores to it's disposal until some other container
needs it's shares.).

I hope this helps.

@ureyes84
Copy link
Contributor Author

ureyes84 commented Nov 6, 2013

Thank you very much! They Key Value syntax helped. I didn't see that coming though, do you think the docs should be updated?

Also, thank you for explaining the cpu shares. My intent is to limit resources, I think restricting the number of cores is the way to go.

Quick question regarding this, say I have a dedicated server that creates ALL containers with:

[{"Key":"lxc.cgroup.cpu.shares","Value":"1"}]

Does it really make a difference adding this parameter? I was wondering whether this is useful only when you need to give more "weight" to a specific container and set the Value parameter to something higher.

@denibertovic
Copy link
Contributor

You are right about the docs. And tnx for the pull request, I've commented there.

Regarding cpu.shared, you are right, it's suited for giving priority to certain containers by setting a higher value.

@denibertovic
Copy link
Contributor

I think we can close this.

@ureyes84
Copy link
Contributor Author

ureyes84 commented Nov 7, 2013

Thanks again for your help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants