2017-09-19 26 views
0

我正在爲CPU密集型計算應用程序使用Docker羣。每個容器都運行相同的單線程應用程序。當然,可用容器越多,整體計算速度越快,但只能達到可用CPU內核的數量。所以,在一個有8個內核的節點上,應該有8個正在運行的容器。具有可用CPU內核數量的擴展塢服務器

但是,節點會動態地加入和離開羣體,因此羣體中可用CPU核心的總數會有所不同。如何自動調整服務以反映這一點?

備註:我的應用程序依賴覆蓋網絡,據我所知,使用服務而不是手動處理容器是首選方法。

回答

0

我想到的一個選擇是定期檢查連接的節點,併爲每個節點創建一個具有適當數量副本的服務。它可能不是一個非常優雅的解決方案,但至少可以與以下Python腳本一起使用。

#!/usr/bin/env python3 

import docker 

def rescale(service_prefix,service_arguments): 
    client = docker.from_env() 
    service_names = [s.name for s in client.services.list()] 

    for node in client.nodes.list(): 
     # Only handle ready nodes 
     if node.attrs['Status']['State'] != 'ready': 
      continue 

     service_name = service_prefix+node.id 
     if service_name not in service_names: 
      # Service does not exist -> Create it 
      cpus = node.attrs['Description']['Resources']['NanoCPUs']/1000000000 
      print("Creating service %s with %i replicas"%(service_name,cpus)) 
      mode={'Replicated':{'Replicas':int(cpus)}} 
      constraints=['node.id==%s'%node.id] 
      client.services.create(name=service_name, mode=mode, constraints=constraints, **service_arguments) 
     else: 
      # Continue running this service 
      service_names.remove(service_name) 

    # Remove services of no longer existing nodes 
    for service in service_names: 
     print("Removing service %s"%service) 
     client.services.get(service).remove() 

rescale(service_prefix="evaluation_", 
     service_arguments={'image':'imagename','networks':['swarmnet']})