当前位置:网站首页>Deploy netron services through kubernetes and specify model files at startup

Deploy netron services through kubernetes and specify model files at startup

2022-06-11 00:34:00 xujingyiss

netron Basic service deployment

Linux Deployed on the server netron The service is very simple . I am using python The way :

# install netron
pip3 install netron
# start-up netron, The default port is 8080, Can pass -p Parameters change
netron

Then you can pass ip:8080 Visited .

When you use it , Select the model file locally to import .

If it is docker Mode deployment , When starting, you need to add --host Parameters , Can be accessed outside the container :

netron --host 0.0.0.0 -p 8080 -b

scene

Because the platform I made , The model file is not local to the user , But on the company's cloud storage . Users can only use various model files in the platform . So in this case , The above deployment method can not meet my requirements , Because the model file cannot be imported !

But there are ways to solve , It's starting up netron Specify the model file path (netron The model file path can be specified in the startup command of ).

in other words , Whenever a user wants to pass netron When viewing the model , I create one through the platform docker Containers , And specify the path of the model ! Because other functions have been used in the project kubernetes 了 , So it is also directly used here kubernetes To create containers , Finally through ingress External exposure services !

Make some customization

By default netron Realization , After opening the model , In the upper left corner , It provides the function of model export , And in my scene , This operation is obviously not allowed , So we need to make some customization .

The way is to github Download source code , Modify the code , For example, delete the export function , Then REPACK , Then deploy in the same way as above .

adopt k8s Create the container and specify the model file at startup

To prevent too many containers from being created , I set that each user can only create one netron Containers , In other words, only one model file can be opened at the same time .

Prepare the image in advance , It's installed inside python Environmental Science , as well as netron.

The startup script

Because I haven't found it yet kubernetes Automatically execute when creating a container netron How to start the command , I tried several ways, but they didn't work , So write the startup command to a shell Script , Before each container creation , Write the startup command in the background code first ( It mainly replaces the model file name ), Then designate kubernetes Execute the script when you start the container .

netron --host 0.0.0.0 -p 8080 -b /netron/test.caffemodel

deployment

When it finally happens pod The name needs to be modified according to the model .

For the sake of simplicity , Posted is yml, Actual background implementation , It's using fabric8, Create... Programmatically deployment,service, as well as ingress. therefore deploymentName,serviceName,ingressName When creating resources in the background , Specified according to different models .

Model files and scripts are placed on the host  /data/netron Big catalog , Mount as... In the container when creating the container  /netron Catalog .

Specify the script to execute when the container starts start.sh.

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: netron-dep
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: netron-dep
  template:
    metadata:
      labels:
        app: netron-dep
    spec:
      containers:
      - image: netron_python:v1.0
        imagePullPolicy: IfNotPresent
        name: netron-container
        command:
        - "/bin/bash"
        args:
        - "/netron/script/user1/start.sh"
        ports:
        - containerPort: 8080
        volumeMounts:
        - mountPath: /netron
          name: model-path
      volumes:
      - name: model-path
        hostPath:
          path: /data/netron

service

Used to access... Within the cluster pod.

apiVersion: v1
kind: Service
metadata:
  name: netron-svc
  namespace: default
spec:
  ports:
  - port: 8070
    targetPort: 8080
  selector:
    app: netron-dep

Multiple service You can specify the same port

service After creation ,netron The service can be accessed inside the cluster , But it can not be accessed outside the cluster , For example, browser . Need to create ingress To expose services to the outside .

Of course , Use service Of NodePort Patterns can also expose services , But you have to specify an extra one 30000 Ports above , And this port cannot be duplicated . That is to say, each container must specify a host port to expose services , This is obviously troublesome , So we decided to use ingress The way .

ingress

External exposure netron service , With someone service binding .

The effect I want is , Of different models netron service , The difference is made through the access path :

  • Model A, adopt xx.xx.xx.xx/path1/ To visit
  • Model B, adopt xx.xx.xx.xx/path2/ To visit
  • ......

The final yml The contents are as follows , The most important thing is path and  annotations Configuration of .

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: netron-igr
  namespace: default
  annotations:
    nginx.ingress.kubernetes.io/configuration-snippet: 'rewrite ^(/test)$ $1/ permanent;'
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
  rules:
  - http:
      paths:
      - path: /test/
        backend:
          serviceName: netron-svc
          servicePort: 8070

According to the above yml, When the browser accesses xx.xx.xx.xx/test/ when , You can access service Corresponding services .

path & rewrite-target

ingress Internally configured - path: /test/ , Do not represent netron Must be provided in the container /test Interface , stay  annotations Added in rewrite-target Parameters , It is used to rewrite the request path . It's configured here /test/ It is only used to distinguish different services . So , When you create a container ,path、ingressName、serviceName、deploymentName To correspond , In this way, we can pass path To find the appropriate service .

Here I put path Rewrite to "/", Because inside the container , netron The request path for service opening is "/", Access in a container is through localhost:8080 Visiting .

Be careful : At the time of the visit, the last "/" Is a must , Take the above request as an example , Must be xx.xx.xx.xx/test/, And can't be  xx.xx.xx.xx/test. Of course , It should be possible to solve this problem with regular expressions , But I'm too lazy to delve into it .

configuration-snippet

Path for rewriting static files .

above path and rewrite-target, It only solves the problem of rewriting the request path . and netron There's an interface , So many static files are involved , such as js, Pictures and the like . This  configuration-snippet Can be used to rewrite the path of a static file . Note that the path of each service is also different .

As for this rewrite grammar , I didn't study it carefully , But what is provided above is available .

ssl-redirect

Set to false, Prevent redirection . The default is true Of , So you need to reset . I don't need it ssl, It is also used directly when accessing ip, So this is set to false, Otherwise, an error will be reported :

ingress After deployment , You can access the corresponding on the browser according to different request paths netron Yes .

xx.xx.xx.xx/test/ 

xx.xx.xx.xx/test2/ 

原网站

版权声明
本文为[xujingyiss]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020628160612.html