linux系统准备 - Centos7 Jupyter Hub安装

centos7容器创建,直接使用物理机当然也是可以的

 
docker run -itd --privileged --name hub -h hub --net=host -v /media/xt/source/soft:/soft -v /tmp:/tmp  cent7  bash

挂载软件这一步,也可以先建docker,然后挂载windows上的共享目录
或者直接在docker中下载软件,总之,能得到相应软件即可 

docker run -itd --privileged --name hub -h hub --net=host -v /tmp:/tmp  cent7  bash

docker run -itd --privileged --name hh -h hh --net=host -v /tmp:/tmp  cent7  bash


hub安装概述 - Centos7 Jupyter Hub安装

用户是系统用户

 
Hub多用户添加用户时,这个用户使用的是系统OS的用户,其原因个人猜测如下:
多用户使用,每个用户下有自己的目录空间,互不影响,或者说每个用户都没有权限访问其他用户的目录
这一点,与访问页面不同,无法通过session限定
而OS用户,本身就具有这个能力,所以就直接使用OS的用户了

另外,python 窗口可直接调用OS shell命令,一个rm命令指哪删哪,也需要OS限定其权限

使用root用户

 
使用root用户安装   
安装过程可以使用其他用户,但最后Hub的启动要使用root用户,还要修改系统上的配置文件,
所以,安装直接使用root用户进行安装

使用root用户启动服务
如果使用普通用户启动服务,那么登录时,只有该普通用户可以登录,其他普通用户无法登录 
如果使用root用户启动,所有普通用户都可以登录  

这只是安装,登录时,使用的是普通用户

目录规划

 
本次所有软件安装于/opt/hub目录
mkdir /opt/hub 

软件本身不到2G,
重点是用户使用的目录,预计用多大的空间,就要分配多大的目录

安装nodejs - Centos7 Jupyter Hub安装

安装nodejs

 
wget https://nodejs.org/dist/v16.14.2/node-v16.14.2-linux-x64.tar.xz
这是一个二进制安装包,编辑好的linux上的二进制文件,解压就能用的可执行文件

选择一个目录进行解压,解压过程并不会创建关于OS的软链接,故此步也可以别处解压再移动到需要的目录中
这里就解压在用户home的app目录下
cp soft/jupyter_hub/node-v16.14.2-linux-x64.tar.xz ./hub/
cd hub
tar -xvf node-v16.14.2-linux-x64.tar.xz

添加到环境变量,非必须,启动hub服务时,临时执行也可以 
export NODE_BASE=/opt/hub/node-v16.14.2-linux-x64
export PATH=$NODE_BASE/bin:$PATH

$ npm -v
8.5.0

$ node -v
v16.14.2

这一步有可能报错,多执行一次就可以了
npm install -g configurable-http-proxy@4.2.3

在不提前设置全局目录的情况下,-g会将生成的bin文件安装到nodejs安装目录的bin目录下,
前面已经将之添加到系统PATH中了
configurable-http-proxy -h

# which configurable-http-proxy   
/opt/hub/node-v16.14.2-linux-x64/bin/configurable-http-proxy

miniconda3安装 - Centos7 Jupyter Hub安装

miniconda的安装

 
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
cp ../soft/jupyter_hub/Miniconda3-latest-Linux-x86_64.sh ./
sh Miniconda3-latest-Linux-x86_64.sh
Do you accept the license terms? [yes|no]
[no] >>> yes

Miniconda3 will now be installed into this location:
  - Press ENTER to confirm the location
  - Press CTRL-C to abort the installation
  - Or specify a different location below

>>> /opt/hub/miniconda3

这一步需要注意一下,输入yes后每次进入terminl就默认是miniconda的环境,
如果用户下有其他业务在运行,这个环境会对其造成影响
by running conda init? [yes|no]
[no] >>> yes 

退出当前窗口,重新进入,就有一个base环境


创建虚拟环境

 
临时执行
export PATH=/opt/hub/miniconda3/bin:$PATH

conda env list
conda create -n py39 python=3.9

conda activate py39

# which python
/opt/hub/miniconda3/envs/py39/bin/python

conda deactivate

因为这里是使用root用户安装的,选择yes会给root加一个默认的base环境,
这改变了root下的原来的python环境,会影响OS上的其他软件的使用
因此,创建完单独的虚拟化环境后,就执行下面的命令取消base环境
conda config --set auto_activate_base false

依赖包安装

 
# 安装jupyter相关的库,从左至右依次是:hub,lab,用户管理,远程调试包
pip install jupyterhub jupyterlab jupyterhub-idle-culler ptvsd

jupyterhub -h
configurable-http-proxy -h

生成配置文件

 
以下两个位置,或者其他位置也行,关键是配置文件中的参数路径要修改成自己的
# 在/etc下创建一个jupyterhub文件夹,用于存储jupyterhub的配置文件
mkdir /etc/jupyterhub
    
# 进入这个文件夹并生成配置文件
mkdir /opt/hub/miniconda3/etc/jupyterhub
cd /opt/hub/miniconda3/etc/jupyterhub
jupyterhub --generate-config


下面这两个命令的路径修改成自己的
# which jupyterhub-singleuser
/opt/hub/miniconda3/envs/py39/bin/jupyterhub-singleuser

export NODE_BASE=/opt/hub/node-v16.14.2-linux-x64
export PATH=$NODE_BASE/bin:$PATH
# which configurable-http-proxy                    
/opt/hub/node-v16.14.2-linux-x64/bin/configurable-http-proxy

vim /opt/hub/miniconda3/etc/jupyterhub/jupyterhub_config.py

import sys
 
c.Authenticator.admin_users = {'root'}  # 管理员用户
# 管理员是否有权在各自计算机上以其他用户身份登录,以进行调试,此选项通常用于 JupyterHub 的托管部署,以避免在启动服务之前手动创建所有用户
c.JupyterHub.admin_access = True
c.JupyterHub.authenticator_class = 'jupyterhub.auth.PAMAuthenticator' #解决Centos下用户认证问题
c.PAMAuthenticator.open_sessions = False # 解决多用户同时登录问题
c.Spawner.args = ['--allow-root']  # 允许root用户使用
c.LocalAuthenticator.create_system_users = True  # 允许创建其他用户
c.Spawner.notebook_dir = '~'  # 设置工作目录,不同的用户就是不同的home目录
c.Spawner.default_url = '/lab'
c.JupyterHub.base_url = '/'
c.ConfigurableHTTPProxy.command = '/opt/hub/node-v16.14.2-linux-x64/bin/configurable-http-proxy'
c.Spawner.cmd = ['/opt/hub/miniconda3/envs/py39/bin/jupyterhub-singleuser']
c.JupyterHub.ip = '0.0.0.0'
c.JupyterHub.port = 8080
c.PAMAuthenticator.open_sessions = False
c.Authenticator.delete_invalid_users = True

# 设置用户一小时内无使用则关闭jupyterlab服务
c.JupyterHub.services = [
    {
        'name': 'idle-culler',
        'command': [sys.executable, '-m', 'jupyterhub_idle_culler', '--timeout=3600'],
    }
]
 
c.JupyterHub.load_roles = [
    {
        "name": "list-and-cull",  # name the role
        "services": [
            "idle-culler",  # assign the service to this role
        ],
        "scopes": [
            # declare what permissions the service should have
            "list:users",  # list users
            "read:users:activity",  # read user last-activity
            "admin:servers",  # start/stop servers
        ],
    }
]

PAM系统配置

 
vim /etc/pam.d/login
注释掉两行
#session required pam_loginuid.so
# session required pam_selinux.so open

添加用户

 
/home/wks/wks3 目录本不存在,在创建用户时会新建这个目录
useradd test3 -d /home/wks/wks3
passwd test3 

这样使用Jupyterhub时,就可以使用这组用户名与密码,
至于权限,完全依赖于操作系统,
在操作系统上test3能访问哪些目录,读写哪些文件,在jupyterhub中是等效的
不只过一个是通常Python访问,一个是在命令行中访问 

添加用户不需要重启hub 

hub启动

启动服务:root用户启动

 

# 启动服务,
/opt/hub/miniconda3/bin/jupyterhub -f=/opt/hub/miniconda3/etc/jupyterhub/jupyterhub_config.py --no-ssl

# 服务挂载在后台
nohup jupyterhub  -f=/opt/hub/miniconda3/etc/jupyterhub/jupyterhub_config.py --no-ssl >> jupyterhub_running.log 2>&1 &

重启脚本启动

 
[root@hub hub]# cat restart_hub.sh 
#!/bin/bash
export PATH=/opt/hub/miniconda3/bin:$PATH
export NODE_BASE=/opt/hub/node-v16.14.2-linux-x64
export PATH=$NODE_BASE/bin:$PATH

pid=`ps -ef | grep jupyterhub_config | grep -v grep |awk '{print $2}'`;if [ -n "$pid" ] ;then kill $pid;else echo ''; fi
sleep 2s

nohup /opt/hub/miniconda3/envs/py39/bin/jupyterhub  -f=/opt/hub/miniconda3/etc/jupyterhub/jupyterhub_config.py --no-ssl >> /tmp/jupyterhub_running.log 2>&1 &
sleep 7s

pid=`ps -ef | grep jupyterhub_config | grep -v grep |awk '{print $2}'`;if [ -n "$pid" ] ;then echo '';else nohup /opt/hub/miniconda3/envs/py39/bin/jupyterhub  -f=/opt/hub/miniconda3/etc/jupyterhub/jupyterhub_config.py --no-ssl >> /tmp/jupyterhub_running.log 2>&1 & fi;sleep 11s 
echo "----restart over-------"

restart脚本有时不好用,但拆分成的start_hub.sh与stop_hub.sh脚本好用,次次生效

# cat start_hub.sh   
#!/bin/bash
export PATH=/opt/hub/miniconda3/bin:$PATH
export NODE_BASE=/opt/hub/node-v16.14.2-linux-x64
export PATH=$NODE_BASE/bin:$PATH


nohup /opt/hub/miniconda3/envs/py39/bin/jupyterhub  -f=/opt/hub/miniconda3/etc/jupyterhub/jupyterhub_config.py --no-ssl >> /tmp/jupyterhub_running.log 2>&1 &
sleep 11s
echo "----start over-------"


# cat stop_hub.sh 
#!/bin/bash
export PATH=/opt/hub/miniconda3/bin:$PATH
export NODE_BASE=/opt/hub/node-v16.14.2-linux-x64
export PATH=$NODE_BASE/bin:$PATH

pid=`ps -ef | grep jupyterhub_config | grep -v grep |awk '{print $2}'`;if [ -n "$pid" ] ;then kill $pid;else echo ''; fi
sleep 2s


echo "----stop over-------"

pycurl模型安装

pycurl模型安装

 
不安装也能正常启动Jupyter hub,就是会报错 
[W 230731 22:28:44 __init__:568] Could not load pycurl: No module named 'pycurl'
    pycurl is recommended if you have a large number of users.

从错误上可以看出, 如果用户量大,推荐安装,
那意思是不安装也可以

sudo yum -y install gcc gcc-c++ kernel-devel

由于没有使用虚拟环境,所以要临时加载一下环境变量 
这种方式只在当前命令窗口生效,不影响新开窗口
export PATH=/opt/hub/miniconda3/bin:$PATH

pip install pycurl

数据分析相关包

 
pip install pandas
pip install chinese_calendar
pip install matplotlib
pip install pydotplus

pip install requests
pip install Flask
pip install gevent 
pip install Flask-APScheduler

pip install cx_Oracle 
pip install pymysql==1.0.2
pip install sqlalchemy

镜像保存

 
docker commit -m "jupyter hub" -a "tanpf" 247cad425efb hub:1
docker save -o ./hub_1.tar hub:1

删除现有的容器镜像,
docker rm hub
docker rmi hub:1

然后导入刚导出的镜像,再新建镜像
docker load -i ./hub_1.tar
docker run -itd --privileged --name hub -h hub --net=host -v /tmp:/tmp  hub:1  bash

以root用户启动服务
source /etc/profile
/opt/app/hub/miniconda3/bin/jupyterhub -f=/opt/app/hub/miniconda3/etc/jupyterhub/jupyterhub_config.py --no-ssl

文件保存

文件保存

 
cd /opt 
tar hub_20230731_1.tar.gz hub/ 

下次将这个文件解压到/opt下,配置一下环境变量,就可以直接用了

环境变量

 
export NODE_BASE=/opt/hub/node-v16.14.2-linux-x64
export PATH=$NODE_BASE/bin:$PATH
export PATH=/opt/hub/miniconda3/bin:$PATH

文件使用

使用文件进行安装

 
前面安装后压缩的文件,解压使用

新建一个dokcer环境,软件自己挂载 
cd /opt 

修改系统配置

 
jupyter hub的配置在其etc目录下的配置文件中,但还有系统的配置要改 

PAM系统配置,这一步必须使用root
vim /etc/pam.d/login
注释掉两行
#session required pam_loginuid.so
# session required pam_selinux.so open

配置环境变量

 
主要是nodejs的环境变量,因为Jupyter自己的环境变量可以自己关联,
比如,只要指定了jupyterhub的全路径,其相关的Python在哪里,它就能自己找到
但运行过程中还要去找Nodejs命令的环境变量,
可将nodejs的环境加入系统PATH,永远或临时都可以  

export NODE_BASE=/opt/hub/node-v16.14.2-linux-x64
export PATH=$NODE_BASE/bin:$PATH

不将python的一系列变量加入root,是因为有一些系统级服务依赖root环境下的Python2,
尽量不改变root的环境变量 

启动服务

 
# 启动服务,
/opt/hub/miniconda3/bin/jupyterhub -f=/opt/hub/miniconda3/etc/jupyterhub/jupyterhub_config.py --no-ssl

# 服务挂载在后台
nohup /opt/hub/miniconda3/bin/jupyterhub  -f=/opt/hub/miniconda3/etc/jupyterhub/jupyterhub_config.py --no-ssl >> /tmp/jupyterhub_running.log 2>&1 &

文件保存

 

参考文章
    centos7.9下从0配置jupyterhub实现多用户使用全过程
    CentOS7安装配置jupyterhub
    Centos搭建jupyterhub环境