我将一些应用程序部署到了我可以通过Flask API访问的azure容器实例中,现在我正在创建一个处理程序来启动/停止容器,例如
app1位于container1中,如果我收到对app1的请求,则我的工作流程为:
1) Start the contianer1 from a python subprocess using azure CLI comands
2) Make the API call to app1
3) Get the result and stop the container
由于我一次可以处理一个以上的请求,因此将该过程添加到函数add中将其馈入redis队列,如下所示:
def make_call(arg1,arg2,arg3):
start_container = 'az container start --name mycontainer --resource-group mygroup'
subprocess.call([start_container],shell=True)
data = dict()
data['query'] = {'query':"query"}
####
url = "http://api_url"
resp = requests.post(url,data=json.dumps(data), headers=headers )
print (resp)
stop_container = 'az container stop --name mycontainer --resource-group mygroup'
subprocess.call([stop_container],shell=True)
return resp
因此,每次我收到请求时,都会将一个任务添加到队列中:
r = redis.Redis()
q = Queue(connection=r,default_timeout=3600)
task = q.enqueue(make_call,args = (arg1,arg2,arg3),timeout=500)
print (q.jobs)
如果我只打一个电话,它就可以正常工作,而且我可以从天蓝中看到如何打开和关闭容器:
但是,那当然不是队列的目的, 当我将几个作业添加到队列中时,第一个作业可以正常工作,但其余的则失败:
Traceback (most recent call last):
File "/home/luis/anaconda3/lib/python3.7/site-packages/urllib3/connection.py", line 159, in _new_conn
(self._dns_host, self.port), self.timeout, **extra_kw)
File "/home/luis/anaconda3/lib/python3.7/site-packages/urllib3/util/connection.py", line 80, in create_connection
raise err
File "/home/luis/anaconda3/lib/python3.7/site-packages/urllib3/util/connection.py", line 70, in create_connection
sock.connect(sa)
TimeoutError: [Errno 110] Connection timed out
此外,在这种情况下,我不使用kubernetes,因为我只有少数几个或服务,一周将使用一两次。
**编辑:**
我已找到错误,但尚未解决。 如果我删除了用于启动和停止容器的行,那么我的工作就很好了,所以问题似乎与开始发出请求时容器尚未准备就绪有关,是确保容器已启动并正在运行的任何方法在进行API调用之前?
Edit2
当前,我正在使用CLI来创建容器,例如:
az container create -g MyResourceGroup --name myapp --image myimage:latest --cpu 1 --memory 1
是否可以从那里进行更改,还是必须将其全部更改为YML文件?
答案 0 :(得分:0)
您可以在创建容器时为此配置liveness probe,以便在启动容器时,它会等待活动性探针返回成功,然后再将流量转发到容器。
apiVersion: 2018-06-01
location: eastus
name: livenesstest
properties:
containers:
- name: mycontainer
properties:
image: nginx
ports: []
resources:
requests:
cpu: 1.0
memoryInGB: 1.5
livenessProbe:
exec:
command:
- "curl"
- "http://localhost"
periodSeconds: 5
osType: Linux
restartPolicy: Always
type: Microsoft.ContainerInstance/containerGroups
符合这些原则