|
| 1 | +--- |
| 2 | +title: 'Singularity Compose: Orchestration for Singularity Instances' |
| 3 | +tags: |
| 4 | + - containers |
| 5 | + - singularity |
| 6 | + - linux |
| 7 | + - orchestration |
| 8 | +authors: |
| 9 | + - name: Vanessa Sochat |
| 10 | + orcid: 0000-0002-4387-3819 |
| 11 | + affiliation: 1 |
| 12 | +affiliations: |
| 13 | + - name: Stanford University Research Computing |
| 14 | + index: 1 |
| 15 | +date: 24 June 2019 |
| 16 | +bibliography: paper.bib |
| 17 | +--- |
| 18 | + |
| 19 | +# Summary |
| 20 | + |
| 21 | +Singularity Compose is an orchestration tool for Singularity container instances. |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +The Singularity container technology started to become popular in 2016, |
| 26 | +as it offered a more secure option to run encapsulated environments [@Kurtzer2017-xj]. |
| 27 | +Traditionally, this meant that Singularity users could run a script built into the container |
| 28 | +(called a runscript), execute a custom command, or shell into a container. |
| 29 | +Unlike Docker [@Merkel2014-da], these basic interactions simply interacted with processes in the |
| 30 | +foreground (e.g., running a script and exiting) and were not appropriate to run |
| 31 | +background services. This was a task for container instances [@SingularityInstances]. |
| 32 | + |
| 33 | +A container instance [@SingularityInstances] equates to running a container in a detached or |
| 34 | +daemon mode. Instances allow for running persistent services in the background, |
| 35 | +and then interaction with these services from the host and other containers. |
| 36 | +Examples of services include databases, web servers, and associated applications |
| 37 | +that interact with them. While a container technology can provide command line |
| 38 | +and other programmatic interfaces for interaction with instances, what is also needed |
| 39 | +is a configuration file for orchestration and customization of several instances. |
| 40 | +For sibling container technology Docker, Docker Compose [@DockerCompose] was developed |
| 41 | +for this purpose. For local and production usage, the user could create a `docker-compose.yml` |
| 42 | +file to define services, volumes, ports exposed, and other customizations to networking and environment |
| 43 | +[@DockerCompose]. Notably, there was strong incentive for the development of such a tool, |
| 44 | +because Docker Compose existed before Kubernetes was available in the middle of 2015 [@Wikipedia_contributors2019-bw]. |
| 45 | + |
| 46 | +No equivalent orchestration tool was created for Singularity container |
| 47 | +instances. While Singularity has empowered enterprise users to run |
| 48 | +services via platforms such as Kubernetes [@Meyer2019-sd], these platforms come |
| 49 | +with privilege. It is often the case that a production Kubernetes cluster is not |
| 50 | +readily available to a user via his or her institution, or that the user |
| 51 | +cannot pay a cloud provider to deploy one. However, this does not imply that |
| 52 | +a non enterprise user (e.g., an open source developer |
| 53 | +or academic) would not benefit from such an orchestration tool. Unfortunately, |
| 54 | +since the current trend and strongest potential for making profits is centered |
| 55 | +around encouraging usage of enterprise tools like Kubernetes [@Wikipedia_contributors2019-bw], |
| 56 | +there is not any urgent incentive on part of the provider companies to |
| 57 | +invest in a non-enterprise orchestration tool. It is logical, rational, and |
| 58 | +understandable that companies exist to make profit, and must make profit |
| 59 | +to exist. As the need is unfulfilled, it is the responsibility of the open source community to step up. |
| 60 | + |
| 61 | + |
| 62 | +## Singularity Compose |
| 63 | + |
| 64 | +The solution for orchestration of container instances from the open source |
| 65 | +community is Singularity Compose [@SingularityCompose]. Singularity Compose |
| 66 | +is software for non enterprise users to easily create a configuration file to |
| 67 | +control creation and interaction of Singularity container instances. |
| 68 | +It allows for the creation of a `singularity-compose.yml` file, in which |
| 69 | +the user can define one or more container services, optionally with exposed ports |
| 70 | +and volumes on the host. The user can easily define a container binary |
| 71 | +to build or pull from a remote resource, along with custom scripts to |
| 72 | +run after creation of the instances. Singularity Compose handles designation |
| 73 | +of addresses on a local bridge network for each container, and creation of |
| 74 | +resource files to bind to the containers to "see" one another. |
| 75 | +Importantly, by way of adding a Singularity Compose to a repository, |
| 76 | +a user is ensuring not just reproducibility of a container recipe, but also |
| 77 | +reproducibility of it's build and creation of services. For example, a simplified |
| 78 | +version of a sequence of steps to build two containers and bring them up |
| 79 | +as instances might look like this: |
| 80 | + |
| 81 | +```bash |
| 82 | +$ sudo singularity build app/app.sif app/Singularity |
| 83 | +$ sudo singularity build nginx/nginx.sif nginx/Singularity.nginx |
| 84 | + |
| 85 | +$ singularity instance start \ |
| 86 | + --bind nginx.conf:/etc/nginx/conf.d/default.conf \ |
| 87 | + --bind nginx/uwsgi_params.par:/etc/nginx/uwsgi_params.par \ |
| 88 | + --bind nginx/cache:/var/cache/nginx \ |
| 89 | + --bind nginx/run:/var/run \ |
| 90 | + --bind app:/code \ |
| 91 | + --bind static:/var/www/static \ |
| 92 | + --bind images:/var/www/images \ |
| 93 | + --bind etc.hosts:/etc/hosts \ |
| 94 | + --net --network-args "portmap=80:80/tcp" --network-args "IP=10.22.0.2" \ |
| 95 | + --hostname nginx --writable-tmpfs nginx/nginx.sif nginx |
| 96 | + |
| 97 | +$ singularity instance start \ |
| 98 | + --bind app:/code \ |
| 99 | + --bind static:/var/www/static \ |
| 100 | + --bind images:/var/www/images \ |
| 101 | + --bind etc.hosts:/etc/hosts \ |
| 102 | + --net --network-args "portmap=8000:8000/tcp" --network-args "IP=10.22.0.3" \ |
| 103 | + --hostname app --writable-tmpfs app/app.sif app |
| 104 | + |
| 105 | +$ singularity instance list |
| 106 | +``` |
| 107 | + |
| 108 | +This is a complicated set of commands. In the above, we |
| 109 | +first build the two containers. There are no checks here if the recipes |
| 110 | +exist, or if the containers themselves already exist. |
| 111 | +We then start instances for them. If we save these commands in a file, |
| 112 | +we need to consistently hard code the paths to the container binaries, |
| 113 | +along with the ip addresses, hostnames, and volumes. There are no checks |
| 114 | +done before attempting the creation if the volumes meant to be bound |
| 115 | +actually exist. We also take for granted that we've already generated an |
| 116 | +`etc.hosts` file to bind to the container at `/etc/hosts`, which will |
| 117 | +define the container instances to have the same names supplied with `--hostname`. |
| 118 | +For the networking, we have to be mindful of the default bridge provided by Singularity, |
| 119 | +along with how to specify networking arguments under different conditions. |
| 120 | +This entire practice is clearly tedious. For a user to constantly need to generate and then |
| 121 | +re-issue these commands, it's not a comfortable workflow. However, |
| 122 | +with Singularity Compose, the user writes a `singularity-compose.yml` |
| 123 | +file once: |
| 124 | + |
| 125 | +```yaml |
| 126 | +version: "1.0" |
| 127 | +instances: |
| 128 | + |
| 129 | + nginx: |
| 130 | + build: |
| 131 | + context: ./nginx |
| 132 | + recipe: Singularity.nginx |
| 133 | + volumes: |
| 134 | + - ./nginx.conf:/etc/nginx/conf.d/default.conf |
| 135 | + - ./uwsgi_params.par:/etc/nginx/uwsgi_params.par |
| 136 | + - ./nginx/cache:/var/cache/nginx |
| 137 | + - ./nginx/run:/var/run |
| 138 | + ports: |
| 139 | + - 80:80 |
| 140 | + depends_on: |
| 141 | + - app |
| 142 | + volumes_from: |
| 143 | + - app |
| 144 | + |
| 145 | + app: |
| 146 | + build: |
| 147 | + context: ./app |
| 148 | + volumes: |
| 149 | + - ./app:/code |
| 150 | + - ./static:/var/www/static |
| 151 | + - ./images:/var/www/images |
| 152 | + ports: |
| 153 | + - 8000:8000 |
| 154 | +``` |
| 155 | +
|
| 156 | +And then can much more readily see and reproduce generation of the same services. |
| 157 | +The user can easily build all non-existing containers, and bring up all services |
| 158 | +with one command: |
| 159 | +
|
| 160 | +```bash |
| 161 | +$ singularity-compose up |
| 162 | +``` |
| 163 | + |
| 164 | +And then easily bring services down, restart, shell into a container, execute |
| 165 | +a command to a container, or run a container's internal runscript. |
| 166 | + |
| 167 | +```bash |
| 168 | +$ singularity-compose down # stop services |
| 169 | +$ singularity-compose restart # stop and start services |
| 170 | +$ singularity-compose shell app # shell into an instance |
| 171 | +$ singularity-compose exec app "Hello!" # execute a command |
| 172 | +$ singularity-compose run app # run internal runscript |
| 173 | +``` |
| 174 | + |
| 175 | +These interactions greatly improve both reproducibility and running of |
| 176 | +any development workflow that is not appropriate for an enterprise cluster but |
| 177 | +relies on orchestration of container instances. |
| 178 | + |
| 179 | +For the interested reader, the complete documentation for Singularity Compose [@SingularityCompose] |
| 180 | +is provided, along with the code on GitHub [@SingularityComposeGithub]. For |
| 181 | +additional walkthroughs and complete examples, we direct the reader to the examples |
| 182 | +repository, also on GitHub [@SingularityComposeExamples]. Contribution by way |
| 183 | +of additional examples, questions, or requests for development of a new example |
| 184 | +are appreciated and welcome. |
| 185 | + |
| 186 | + |
| 187 | +# References |
0 commit comments