Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
huataihuang committed Sep 8, 2023
1 parent 8568fbe commit 7f97141
Show file tree
Hide file tree
Showing 19 changed files with 232 additions and 11 deletions.
89 changes: 89 additions & 0 deletions source/django/startup/django_env_linux.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
.. _django_env_linux:

=======================
Django开发环境(linux)
=======================

:ref:`centos` 环境上部署Django开发运行环境,基本和 :ref:`django_env_macos` 相同,细节差异:

- 生产环境采用了古老的CentOS 7.2环境,实际我为了能够追平最新的运行开发环境,采用了 :ref:`build_python3_in_centos7`
- :ref:`virtualenv` 环境采用 :ref:`rebuild_virtualenv`
- 由于数据库后端采用 :ref:`mysql` ,所以需要安装 ``mysqlclient`` 模块

构建步骤
===========

- 完成 :ref:`build_python3_in_centos7` 准备工作

- 创建 :ref:`virtualenv` (这里项目名为 ``onesre`` ):

.. literalinclude:: django_mysql/virtualenv_onesre
:caption: 构建一个 ``onesre`` 项目运行环境

- 需要安装 mysql 开发包,例如 ``mariadb-devel`` ,安装以后系统会有 ``mysql_config`` 工具:

.. literalinclude:: django_mysql/install_mysql-devel
:caption: 需要提前安装 ``mysql-devel`` 或 ``mariadb-devel`` 才能执行 ``pip install mysqlclient``

- 在项目目录下重新恢复

.. literalinclude:: ../../python/startup/rebuild_virtualenv/pip_install_requirements
:language: bash
:caption: 根据requirements.txt恢复virtualenv依赖模块包

解决 ``mysqlclient`` 模块安装
------------------------------

这次遇到一个报错,和之前 :ref:`django_mysql` 不同:

.. literalinclude:: django_env_linux/mysqlclient_error
:caption: ``pip`` 安装 ``mysqlclient`` 报错
:emphasize-lines: 10-13

这里可以看到 ``pkg-config --exists mysqlclient`` 和 ``pkg-config --exists mariadb`` 都是返回 ``1`` (失败)。我手工执行了一下,确实 ``echo $?`` 显示 ``1``

仔细一看,原来这个 ``pkg-config`` 是操作系统默认的 ``/usr/bin/pkg-config`` 。这里通过 ``rpm -qf /usr/bin/pkg-config`` 可以看出是属于 ``pkgconfig-0.27.1-4.1.alios7.x86_64`` ,显然不会获得正确的包信息

`PyMySQL / mysqlclient / README.md <https://github.com/PyMySQL/mysqlclient/blob/main/README.md>`_ 提供了一个线索:

.. literalinclude:: django_env_linux/pkg-config_mysqlclient
:caption: 定制编译时指定环境变量

既然由于操作系统的 ``pkg-config`` 无法正常工作,那么该如何设置环境变量呢?

我找了自己部署的一台 :ref:`fedora` 开发环境(吐槽一下公司魔改的CentOS),可以看到:

.. literalinclude:: django_env_linux/pkg-config_mariadb
:caption: 在正确的环境中执行 ``pkg-config`` 获取环境变量
:emphasize-lines: 2,4

- 所以修正安装方法:

.. literalinclude:: django_env_linux/pkg-config_mariadb_env
:caption: 设置正确 ``mysqlclient`` 环境变量

- 然后再次执行就可以完成:

.. literalinclude:: ../../python/startup/rebuild_virtualenv/pip_install_requirements
:language: bash
:caption: 根据requirements.txt恢复virtualenv依赖模块包

解决 ``pip`` 下载失败(手工下载安装)
-------------------------------------

墙内的杯具就是,在线安装的灵活方便往往会被GFW干成生不如死: ``pip install`` 过程中,遇到 ``Babel-2.12.1-py3-none-any.whl`` 下载始终中断的问题。解决的方法: :ref:`pip_offline`

- 通过 ``pip downlaod`` 命令下载指定软件包(版本),这里举例 ``Babel-2.12.1-py3-none-any.whl`` :

.. literalinclude:: ../../python/startup/pip_offline/pip_download
:caption: ``pip download`` 可以下载指定版本python包

- 将下载好的 ``.whl`` python包复制到目标主机,然后就可以直接离线安装:

.. literalinclude:: ../../python/startup/pip_offline/pip_install_whl
:caption: ``pip install`` 可以安装下载好的 ``.whl`` python包

参考
=====

- `PyMySQL / mysqlclient / README.md <https://github.com/PyMySQL/mysqlclient/blob/main/README.md>`_
44 changes: 44 additions & 0 deletions source/django/startup/django_env_linux/mysqlclient_error
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Collecting mysqlclient (from -r requirements.txt (line 9))
Using cached mysqlclient-2.2.0.tar.gz (89 kB)
Installing build dependencies ... done
Getting requirements to build wheel ... error
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> [25 lines of output]
Trying pkg-config --exists mysqlclient
Command 'pkg-config --exists mysqlclient' returned non-zero exit status 1.
Trying pkg-config --exists mariadb
Command 'pkg-config --exists mariadb' returned non-zero exit status 1.
Traceback (most recent call last):
File "/home/admin/onesre_venv3/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
main()
File "/home/admin/onesre_venv3/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
json_out['return_val'] = hook(**hook_input['kwargs'])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/admin/onesre_venv3/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel
return hook(config_settings)
^^^^^^^^^^^^^^^^^^^^^
File "/tmp/pip-build-env-92qfmlql/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 355, in get_requires_for_build_wheel
return self._get_build_requires(config_settings, requirements=['wheel'])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/pip-build-env-92qfmlql/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 325, in _get_build_requires
self.run_setup()
File "/tmp/pip-build-env-92qfmlql/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 341, in run_setup
exec(code, locals())
File "<string>", line 154, in <module>
File "<string>", line 48, in get_config_posix
File "<string>", line 27, in find_package_name
Exception: Can not find valid pkg-config name.
Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.
4 changes: 4 additions & 0 deletions source/django/startup/django_env_linux/pkg-config_mariadb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$ pkg-config mariadb --cflags
-I/usr/include/mysql
$ pkg-config mariadb --libs
-lmariadb -ldl -lm -lssl -lcrypto -lz
2 changes: 2 additions & 0 deletions source/django/startup/django_env_linux/pkg-config_mariadb_env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export MYSQLCLIENT_CFLAGS="-I/usr/include/mysql"
export MYSQLCLIENT_LDFLAGS="-lmariadb -ldl -lm -lssl -lcrypto -lz"
3 changes: 3 additions & 0 deletions source/django/startup/django_env_linux/pkg-config_mysqlclient
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export MYSQLCLIENT_CFLAGS=`pkg-config mysqlclient --cflags`
export MYSQLCLIENT_LDFLAGS=`pkg-config mysqlclient --libs`
pip install mysqlclient
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.. _django_env:
.. _django_env_macos:

================
Django开发环境
================
=======================
Django开发环境(macOS)
=======================

作为Python Web框架,Django需要Python支持才能运行。目前Python 2.x已经终止开发,如果你的项目刚刚开始,请从Python 3开始(Python 3不兼容Python 2)。请首先安装Python 3。

Expand Down
7 changes: 5 additions & 2 deletions source/django/startup/django_mysql.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Django配置MySQL数据库
==========================

在部署 :ref:`django_env` 后,我们可以开发Django。但是,默认使用的数据库是sqlite,和生产环境往往不同。所以,我们通常会把Django数据库后端替换成MySQL。
在部署 :ref:`django_env_linux` 后,我们可以开发Django。但是,默认使用的数据库是sqlite,和生产环境往往不同。所以,我们通常会把Django数据库后端替换成MySQL。

但是,需要注意的是,我们不能把数据库访问账号密码直接配置在 ``manage.py`` 中,也不能把包含账号信息提交到git仓库中,所以需要有一定的配置方法来实现数据库连接。

Expand Down Expand Up @@ -38,7 +38,10 @@ Django配置MySQL数据库
mariadb_config --version
mysql_config --libs

则需要安装 mysql 开发包,例如 ``mariadb-devel`` ,安装以后系统会有 ``mysql_config`` 工具
则需要安装 mysql 开发包,例如 ``mariadb-devel`` ,安装以后系统会有 ``mysql_config`` 工具:

.. literalinclude:: django_mysql/install_mysql-devel
:caption: 需要提前安装 ``mysql-devel`` 或 ``mariadb-devel`` 才能执行 ``pip install mysqlclient``

配置Django连接数据库
=====================
Expand Down
2 changes: 2 additions & 0 deletions source/django/startup/django_mysql/install_mysql-devel
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# yum install mysql-devel
yum install mariadb-devel
11 changes: 11 additions & 0 deletions source/django/startup/django_mysql/virtualenv_onesre
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/usr/local/bin/python3.11 -m venv onesre_venv3

# 这里是因为CentOS 7.2新老python环境混合,清理了旧python版本
cd ~/onesre_venv3/bin
unlink python
unlink python3
ln -s /usr/local/bin/python3.11 python3
ln -s python3 python

# 激活
. onesre_venv3/bin/activate
3 changes: 2 additions & 1 deletion source/django/startup/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Django 起步
.. toctree::
:maxdepth: 1

django_env.rst
django_env_macos.rst
django_env_linux.rst
django_mysql.rst
run_django.rst
django_migrate.rst
Expand Down
8 changes: 8 additions & 0 deletions source/kernel/process/utils/ps.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ Linux/Unix 通常采用 System V ( ``ps -elf`` ) 或 BSD ( ``ps alx`` ) 风格 `
.. literalinclude:: ps/ps_lstart_output
:caption: 检查进程启动时间,例如 ``qemu-system-x86`` 可以看到详细的启动时间

.. note::

在 ``ps`` 检查进程启动时间的 ``field`` 格式参数可以有3种:

- ``start`` 启动时间类似 ``09:46:10`` ,但是超过24小时的进程会显示为日期,类似 ``Aug 31``
- ``stime`` 启动时间类似 ``09:10`` ,但是超过24小时的进程也是显示为日期,类似 ``Aug31``
- ``lstart`` 启动时间最详尽和标准化,非常方便对比检查,类似 ``Thu Aug 31 23:58:01 2023`` 强烈推荐

``ps`` 检查线程
=================

Expand Down
2 changes: 1 addition & 1 deletion source/kernel/process/utils/ps/ps_lstart
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ps -eo pid,lstart,cmd | grep qemu-system-x86_64
ps -eo pid,ppid,lstart,cmd | grep qemu-system-x86_64
22 changes: 21 additions & 1 deletion source/kubernetes/storage/k8s_csi/k8s_csi_arch.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,30 @@ CSI插件机制
CSI插件包括:

- ``Controller Plugin`` : 以 ``Deployment`` 或 ``StatefulSet`` 形式部署,实现CSI Controller service。Controller负责与Kubernetes API, 外部存储服务的控制面交互,但并不实际处理存储卷在宿主机上的挂载等工作
- ``Node Plugin`` : Node插件需要在所有node节点上运行,所以通常以 ``Daemonset`` 形式部署
- ``Node Plugin`` : Node插件需要在所有node节点上运行,所以通常以 ``Daemonset`` 形式部署。Node插件实现CSI Node service,会暴露出一个 ``uds`` 文件出来,从而让 ``kubelet`` 在进行存储卷操作时,通过这个 ``uds`` 文件调用它的 ``gRPC`` 接口

CSI插件需要实现 **3个** ``gRPC`` 接口服务:

- ``Identity Service`` : Node插件和Controller插件都需要实现Identity Service RPC
- ``Controller Service`` : 控制插件实现
- ``Node Service`` : Node插件实现

CSI插件部署通常采用官方提供的一系列sidecar来完成:

- ``external-provisioner``
- ``external-attacher``
- ``external-snapshotter``
- ``external-resizer``
- ``node-driver-registrar``
- ``livenessprobe``

.. note::

我先实践 :ref:`ceph-csi` ,然后再回来补充理论知识

参考
=======

- `Kubernetes CSI Specification <https://cctoctofx.netlify.app/post/cloud-computing/k8s-csi-interprete/>`_ 这篇文章较为全面清晰,提供了不少索引信息,可以作为学习起点
- `Kubernetes Container Storage Interface (CSI) Documentation <https://kubernetes-csi.github.io/docs/introduction.html>`_ 详细全面的开发、部署、测试文档,作为主要参考资料
- `CSI Volume Plugins in Kubernetes Design Doc <https://github.com/kubernetes/design-proposals-archive/blob/main/storage/container-storage-interface.md>`_ CSI插件设计文档,非常详细,值得深入学习
1 change: 1 addition & 0 deletions source/python/startup/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Python起步
python_doc.rst
pep_8.rst
run_python_in_browser.rst
pip_offline.rst

.. only:: subproject and html

Expand Down
26 changes: 26 additions & 0 deletions source/python/startup/pip_offline.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.. _pip_offline:

=====================
pip离线安装Python包
=====================

GFW真是墙内IT工作者的梦魇,倘若需要部署 :ref:`kubernetes` 这样大型基础设施,恐怕一半的精力都要浪费在翻墙、搬运上。即使像 :ref:`django_env_linux` 这样简单的日常开发环境,也不得不祭出"愚公移山"的苦力:

- 首先找一台网络通畅的主机,最好是同样的Python环境,例如,相同的 :ref:`virtualenv`

- 通过 ``pip downlaod`` 命令下载指定软件包(版本),这里举例 ``Babel-2.12.1-py3-none-any.whl`` :

.. literalinclude:: pip_offline/pip_download
:caption: ``pip download`` 可以下载指定版本python包

- 将下载好的 ``.whl`` python包复制到目标主机,然后就可以直接离线安装:

.. literalinclude:: pip_offline/pip_install_whl
:caption: ``pip install`` 可以安装下载好的 ``.whl`` python包


参考
======

- `Installing Python packages (Offline mode) <https://www.ibm.com/docs/en/siffs/2.0.3?topic=python-installing-packages-offline-mode>`_
- `How do I install a Python package with a .whl file? <https://stackoverflow.com/questions/27885397/how-do-i-install-a-python-package-with-a-whl-file>`_
2 changes: 2 additions & 0 deletions source/python/startup/pip_offline/pip_download
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mkdir -p ~/onesre_venv3/whl/
pip3.11 download -d ~/onesre_venv3/whl/ Babel==2.12.1
2 changes: 2 additions & 0 deletions source/python/startup/pip_offline/pip_install_whl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# 在目标主机上下载好 .whl 文件,然后执行本地安装
pip install Babel-2.12.1-py3-none-any.whl
6 changes: 4 additions & 2 deletions source/python/startup/rebuild_virtualenv.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
python3 -m venv venv3
. ~/venv3/bin/activate

- 重新安装软件包::
- 重新安装软件包:

pip install -r source/requirements.txt
.. literalinclude:: rebuild_virtualenv/pip_install_requirements
:language: bash
:caption: 根据requirements.txt恢复virtualenv依赖模块包

参考
=======
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pip install -r source/requirements.txt

0 comments on commit 7f97141

Please sign in to comment.