lxcdev.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. .. _lxcdev:
  2. ==============================
  3. Developing in Linux Containers
  4. ==============================
  5. .. _LXC: https://linuxcontainers.org/lxc/introduction/
  6. In this article we will show, how you can make use of Linux Containers (LXC_) in
  7. *distributed and heterogeneous development cycles* (TL;DR; jump to the
  8. :ref:`lxcdev summary`).
  9. .. sidebar:: Audience
  10. This blog post is written for experienced admins and developers. Readers
  11. should have a serious meaning about the terms: *distributed*, *merge* and
  12. *linux container*.
  13. .. contents:: Contents
  14. :depth: 2
  15. :local:
  16. :backlinks: entry
  17. Motivation
  18. ==========
  19. Usually in our development cycle, we edit the sources and run some test and/or
  20. builds by using ``make`` :ref:`[ref] <makefile>` before we commit. This cycle
  21. is simple and perfect but might fail in some aspects we should not overlook.
  22. **The environment in which we run all our development processes matters!**
  23. The :ref:`makefile` and the :ref:`make install` encapsulate a lot for us, but
  24. they do not have access to all prerequisites. For example, there may have
  25. dependencies on packages that are installed on the developer's desktop, but
  26. usually are not preinstalled on a server or client system. Another example is;
  27. settings have been made to the software on developer's desktop that would never
  28. be set on a *production* system.
  29. **Linux Containers are isolate environments and not to mix up all the
  30. prerequisites from various projects on developer's desktop is always a good
  31. choice.**
  32. The scripts from :ref:`searx_utils` can divide in those to install and maintain
  33. software:
  34. - :ref:`searxng.sh`
  35. and the script :ref:`lxc.sh`, with we can scale our installation, maintenance or
  36. even development tasks over a stack of isolated containers / what we call the:
  37. **SearXNG LXC suite**
  38. .. hint::
  39. If you see any problems with the internet connectivity of your
  40. containers read section :ref:`internet connectivity docker`.
  41. Gentlemen, start your engines!
  42. ==============================
  43. .. _LXD: https://linuxcontainers.org/lxd/introduction/
  44. .. _archlinux: https://www.archlinux.org/
  45. Before you can start with containers, you need to install and initiate LXD_
  46. once:
  47. .. tabs::
  48. .. group-tab:: desktop
  49. .. code:: bash
  50. $ snap install lxd
  51. $ lxd init --auto
  52. And you need to clone from origin or if you have your own fork, clone from your
  53. fork:
  54. .. tabs::
  55. .. group-tab:: desktop
  56. .. code:: bash
  57. $ cd ~/Downloads
  58. $ git clone https://github.com/searxng/searxng.git searxng
  59. $ cd searxng
  60. The :ref:`lxc-searxng.env` consists of several images, see ``export
  61. LXC_SUITE=(...`` near by :origin:`utils/lxc-searxng.env#L19`. For this blog post
  62. we exercise on a archlinux_ image. The container of this image is named
  63. ``searxng-archlinux``. Lets build the container, but be sure that this container
  64. does not already exists, so first lets remove possible old one:
  65. .. tabs::
  66. .. group-tab:: desktop
  67. .. code:: bash
  68. $ sudo -H ./utils/lxc.sh remove searxng-archlinux
  69. $ sudo -H ./utils/lxc.sh build searxng-archlinux
  70. .. sidebar:: The ``searxng-archlinux`` container
  71. is the base of all our exercises here.
  72. In this container we install all services :ref:`including searx, morty & filtron
  73. <lxc.sh install suite>` in once:
  74. .. tabs::
  75. .. group-tab:: desktop
  76. .. code:: bash
  77. $ sudo -H ./utils/lxc.sh install suite searxng-archlinux
  78. To proxy HTTP from filtron and morty in the container to the outside of the
  79. container, install nginx into the container. Once for the bot blocker filtron:
  80. .. tabs::
  81. .. group-tab:: desktop
  82. .. code:: bash
  83. $ sudo -H ./utils/lxc.sh cmd searxng-archlinux \
  84. ./utils/filtron.sh nginx install
  85. ...
  86. INFO: got 429 from http://10.174.184.156/searx
  87. and once for the content sanitizer (content proxy morty):
  88. .. tabs::
  89. .. group-tab:: desktop
  90. .. code:: bash
  91. $ sudo -H ./utils/lxc.sh cmd searxng-archlinux \
  92. ./utils/morty.sh nginx install
  93. ...
  94. INFO: got 200 from http://10.174.184.156/morty/
  95. .. sidebar:: Fully functional SearXNG suite
  96. From here on you have a fully functional SearXNG suite running with bot
  97. blocker (filtron) and WEB content sanitizer (content proxy morty), both are
  98. needed for a *privacy protecting* search engine.
  99. On your system, the IP of your ``searxng-archlinux`` container differs from
  100. http://10.174.184.156/searx, just open the URL reported in your installation
  101. protocol in your WEB browser from the desktop to test the instance from outside
  102. of the container.
  103. In such a earXNG suite admins can maintain and access the debug log of the
  104. different services quite easy.
  105. .. _working in containers:
  106. In containers, work as usual
  107. ============================
  108. Usually you open a root-bash using ``sudo -H bash``. In case of LXC containers
  109. open the root-bash in the container using ``./utils/lxc.sh cmd
  110. searxng-archlinux``:
  111. .. tabs::
  112. .. group-tab:: desktop
  113. .. code:: bash
  114. $ sudo -H ./utils/lxc.sh cmd searxng-archlinux bash
  115. INFO: [searxng-archlinux] bash
  116. [root@searxng-archlinux searx]# pwd
  117. /share/searxng
  118. The prompt ``[root@searxng-archlinux ...]`` signals, that you are the root user in
  119. the searxng-container. To debug the running SearXNG instance use:
  120. .. tabs::
  121. .. group-tab:: root@searxng-archlinux
  122. .. code:: bash
  123. $ ./utils/searx.sh inspect service
  124. ...
  125. use [CTRL-C] to stop monitoring the log
  126. ...
  127. Back in the browser on your desktop open the service http://10.174.184.156/searx
  128. and run your application tests while the debug log is shown in the terminal from
  129. above. You can stop monitoring using ``CTRL-C``, this also disables the *"debug
  130. option"* in SearXNG's settings file and restarts the SearXNG uwsgi application.
  131. To debug services from filtron and morty analogous use:
  132. Another point we have to notice is that the service (:ref:`SearXNG <searxng.sh>`
  133. runs under dedicated system user account with the same name (compare
  134. :ref:`create searxng user`). To get a shell from these accounts, simply call:
  135. .. tabs::
  136. .. group-tab:: root@searxng-archlinux
  137. .. code:: bash
  138. $ ./utils/searxng.sh instance cmd bash
  139. To get in touch, open a shell from the service user (searxng@searxng-archlinux):
  140. .. tabs::
  141. .. group-tab:: desktop
  142. .. code:: bash
  143. $ sudo -H ./utils/lxc.sh cmd searxng-archlinux ./utils/searxng.sh instance cmd bash
  144. INFO: [searxng-archlinux] ./utils/searxng.sh instance cmd bash
  145. [searxng@searxng-archlinux ~]$
  146. The prompt ``[searxng@searxng-archlinux]`` signals that you are logged in as system
  147. user ``searx`` in the ``searxng-archlinux`` container and the python *virtualenv*
  148. ``(searxng-pyenv)`` environment is activated.
  149. .. tabs::
  150. .. group-tab:: searxng@searxng-archlinux
  151. .. code:: bash
  152. (searxng-pyenv) [searxng@searxng-archlinux ~]$ pwd
  153. /usr/local/searxng
  154. Wrap production into developer suite
  155. ====================================
  156. In this section we will see how to change the *"Fully functional SearXNG suite"*
  157. from a LXC container (which is quite ready for production) into a developer
  158. suite. For this, we have to keep an eye on the :ref:`installation basic`:
  159. - SearXNG setup in: ``/etc/searxng/settings.yml``
  160. - SearXNG user's home: ``/usr/local/searxng``
  161. - virtualenv in: ``/usr/local/searxng/searxng-pyenv``
  162. - SearXNG software in: ``/usr/local/searxng/searxng-src``
  163. With the use of the :ref:`searxng.sh` the SearXNG service was installed as
  164. :ref:`uWSGI application <searxng uwsgi>`. To maintain this service, we can use
  165. ``systemctl`` (compare :ref:`uWSGI maintenance`).
  166. .. tabs::
  167. .. group-tab:: desktop
  168. .. code:: bash
  169. $ sudo -H ./utils/lxc.sh cmd searxng-archlinux \
  170. systemctl stop uwsgi@searxng
  171. With the command above, we stopped the SearXNG uWSGI-App in the archlinux
  172. container.
  173. The uWSGI-App for the archlinux dsitros is configured in
  174. :origin:`utils/templates/etc/uwsgi/apps-archlinux/searxng.ini`, from where at
  175. least you should attend the settings of ``uid``, ``chdir``, ``env`` and
  176. ``http``::
  177. env = SEARXNG_SETTINGS_PATH=/etc/searxng/settings.yml
  178. http = 127.0.0.1:8888
  179. chdir = /usr/local/searxng/searxng-src/searx
  180. virtualenv = /usr/local/searxng/searxng-pyenv
  181. pythonpath = /usr/local/searxng/searxng-src
  182. If you have read the :ref:`"Good to know section" <lxc.sh>` you remember, that
  183. each container shares the root folder of the repository and the command
  184. ``utils/lxc.sh cmd`` handles relative path names **transparent**. To wrap the
  185. SearXNG installation into a developer one, we simple have to create a smylink to
  186. the **transparent** reposetory from the desktop. Now lets replace the
  187. repository at ``searxng-src`` in the container with the working tree from outside
  188. of the container:
  189. .. tabs::
  190. .. group-tab:: container becomes a developer suite
  191. .. code:: bash
  192. $ sudo -H ./utils/lxc.sh cmd searxng-archlinux \
  193. mv /usr/local/searxng/searxng-src /usr/local/searxng/searxng-src.old
  194. $ sudo -H ./utils/lxc.sh cmd searxng-archlinux \
  195. ln -s /share/searx/ /usr/local/searxng/searxng-src
  196. Now we can develop as usual in the working tree of our desktop system. Every
  197. time the software was changed, you have to restart the SearXNG service (in the
  198. container):
  199. .. tabs::
  200. .. group-tab:: desktop
  201. .. code:: bash
  202. $ sudo -H ./utils/lxc.sh cmd searxng-archlinux \
  203. systemctl restart uwsgi@searx
  204. Remember: :ref:`working in containers` .. here are just some examples from my
  205. daily usage:
  206. .. tabs::
  207. .. group-tab:: desktop
  208. To *inspect* the SearXNG instance (already described above):
  209. .. code:: bash
  210. $ sudo -H ./utils/lxc.sh cmd searxng-archlinux \
  211. ./utils/searx.sh inspect service
  212. Run :ref:`makefile`, e.g. to test inside the container:
  213. .. code:: bash
  214. $ sudo -H ./utils/lxc.sh cmd searxng-archlinux \
  215. make test
  216. To install all prerequisites needed for a :ref:`buildhosts`:
  217. .. code:: bash
  218. $ sudo -H ./utils/lxc.sh cmd searxng-archlinux \
  219. ./utils/searxng.sh install buildhost
  220. To build the docs on a buildhost :ref:`buildhosts`:
  221. .. code:: bash
  222. $ sudo -H ./utils/lxc.sh cmd searxng-archlinux \
  223. make docs.html
  224. .. _lxcdev summary:
  225. Summary
  226. =======
  227. We build up a fully functional SearXNG suite in a archlinux container:
  228. .. code:: bash
  229. $ sudo -H ./utils/lxc.sh install suite searxng-archlinux
  230. To access HTTP from the desktop we installed nginx for the services inside the
  231. container:
  232. .. tabs::
  233. .. group-tab:: [root@searxng-archlinux]
  234. .. code:: bash
  235. $ ./utils/filtron.sh nginx install
  236. $ ./utils/morty.sh nginx install
  237. To wrap the suite into a developer one, we created a symbolic link to the
  238. repository which is shared **transparent** from the desktop's file system into
  239. the container :
  240. .. tabs::
  241. .. group-tab:: [root@searxng-archlinux]
  242. .. code:: bash
  243. $ mv /usr/local/searxng/searxng-src /usr/local/searxng/searxng-src.old
  244. $ ln -s /share/searx/ /usr/local/searxng/searxng-src
  245. $ systemctl restart uwsgi@searx
  246. To get information about the searxNG suite in the archlinux container we can
  247. use:
  248. .. tabs::
  249. .. group-tab:: desktop
  250. .. code:: bash
  251. $ sudo -H ./utils/lxc.sh show suite searxng-archlinux
  252. ...
  253. [searxng-archlinux] INFO: (eth0) filtron: http://10.174.184.156:4004/ http://10.174.184.156/searx
  254. [searxng-archlinux] INFO: (eth0) morty: http://10.174.184.156:3000/
  255. [searxng-archlinux] INFO: (eth0) docs.live: http://10.174.184.156:8080/
  256. [searxng-archlinux] INFO: (eth0) IPv6: http://[fd42:573b:e0b3:e97e:216:3eff:fea5:9b65]
  257. ...