traefik 域名路由及 jupyter 远程访问

  Posted by Mr.Zhang on 12 Jun, 2019

   DOCKER   

traefik真是一个好东西啊,动态监听和管理微服务。可以方便的路由二级域名,并支持多个域名的SSL证书,只需要添加证书位置即可,如下:

defaultEntryPoints = ["http", "https"]
[entryPoints]
    [entryPoints.http]
        address = ":80"
        [entryPoints.http.redirect]
            entryPoint = "https"
    [entryPoints.https]
        address = ":443"
        compress = true
        [entryPoints.https.tls]
        [[entryPoints.https.tls.certificates]]
            CertFile = "/etc/traefik/certs/zhangyong.name.pem"
            KeyFile = "/etc/traefik/certs/zhangyong.name.key"
        [[entryPoints.https.tls.certificates]]
            CertFile = "/etc/traefik/certs/api.zhangyong.name.pem"
            KeyFile = "/etc/traefik/certs/api.zhangyong.name.key"

习惯jupyter notebook写代码,又怕环境缺包,又想支持服务器远程访问,可以使用spellrun/mxnet-cpu-jupyter的docker镜像,也可以自己配置。不过该镜像需要自己启动jupyter,可以用docker-compose配置入口脚本。首先新建一个entrypoint.sh文件,

#!/bin/bash
jupyter notebook --no-browser --allow-root --ip=0.0.0.0 --port=80

然后修改此文件权限,

chmod +x entrypoint.sh

编写docker-compose.yaml文件:

version: '3'
networks:
    web:
        external: true
services:
    api_test:
        image: emilevauge/whoami
        labels:
            - traefik.frontend.rule=Host:api.zhangyong.name
            - traefik.port=80
        networks:
            - web
    mx_test:
        image: spellrun/mxnet-cpu-jupyter:latest
        expose:
            - "80"
        labels:
            - traefik.frontend.rule=Host:mx.zhangyong.name
            - traefik.port=80
        networks:
            - web
        entrypoint:
            - /entrypoint.sh
        volumes:
            - ~/notebook/entrypoint.sh:/entrypoint.sh

编排运行后,traefik通过监听docker自动路由二级域名。远程访问jupyter需要输入token,可以使用docker logs命令获取对应容器记录,不用的时候,可以暂停对应容器。