# The following is information about using LXD to build and maintain containers # snap is the preferred method for LXD installation becaues the apt channels are not maintained # Check snap capabilities on your host computer snap info lxd # This should show all the channel information for the LXD package # Snap is usually installed on Ubuntu, if it is not use your package manager to install an # up to date version of snap # This will install an LXC container system # which is like Docker. You can pick from all types of different # base distros and the snapshot and export an entire stack setup # # set snap config for proxy sudo snap set system proxy.http="http://172.16.0.129:3128" sudo snap set system proxy.https="http://172.16.0.129:3128" # it is important to install lxd from snap, the LXC and LXD packages from standard apt # do not seem to work as well and are very outdated # we want to be using LXD version 5.15+ sudo snap install lxd --channel=latest/stable # go through with the defaults of the init script, unless the defaults are obviously wrong lxd init # make the NAT prerouting connections needed to map the host to container sudo ufw allow in on lxdbr0 comment 'lxdbr0 for LXD' sudo ufw route allow in on lxdbr0 comment 'lxdbr0 for LXD' sudo ufw route allow out on lxdbr0 comment 'lxdbr0 for LXD' # allow normal services, there could be more depending on the resposibilities of the instance sudo ufw allow ssh sudo ufw allow http sudo ufw allow https sudo ufw enable # set proxy for lxc commands lxc config set core.proxy_http http://172.16.0.129:3128 lxc config set core.proxy_https http://172.16.0.129:3128 # these are steps needed if you want to create your own container lxc launch images:ubuntu/jammy/amd64 ubuntu-jammy # list the containers, you will need to know the IP of the container # to map it to the host lxc list # map the host and container network so you can use the browser for testing # this is just an example, the ports and names of things can change depending # on what you are trying to do lxc config device add ubuntu-jammy http8080 proxy listen=tcp:127.0.0.1:8080 connect=tcp:10.80.77.38:8080 # map a file path in the container to a path on the host # again this is an example of mapping /home/webstack/www in the container to a path in the host homespace lxc config device add ubuntu-jammy www_test disk source=/home/ccampbell/lxc_root/webstack/www/ path=/home/webstack/www/ # to enter a shell on the container lxc exec ubuntu-jammy bash # create a snapshot of a container lxc snapshot ubuntu-jammy ubuntu-jammy_snap0 # create a backup of a container lxc export ubuntu-jammy lxd_snapshots/schema_webstack_container.08042023.tar.xz --optimized-storage # push a file from the host to the container lxc file push schema_webstack_08042023.sql.gz ubuntu-jammy/root/schema-webstack/ # set up a proxy to the containers version of Directus lxc config device add ubuntu-jammy directus8055 proxy listen=tcp:127.0.0.1:8055 connect=tcp:10.80.77.38:8055 # show the LXD network lxc network list # show the LXD storage lxc storage list # show all the configuration information for a container lxc config device list ubuntu-jammy --debug # mount dev repo into the container lxc config device add ubuntu-jammy schema_webstack disk source=/home/ccampbell/node.js/schema-webstack/ path=/home/webstack/schema-webstack/ # updated export example lxc export schema-webstack-8080 lxd_snapshots/schema_webstack_container.08242023.tar.xz --optimized-storage ## Container organization and maintenance # Using the projects concept in LXD, it is possible to organize and maintain containers # that follow the development cycle functionality dev, stage, live # each project will be based on the site, for example denamarin.com and have as many # container within the project to achieve the devops capabilities that are required for the project # The following are example commands for creating and organizing projects lxc project create denamarin.com --config features.images=false --config features.profiles=false # stop the existing container, they cannot be moved while live lxc stop stage-schema-webstack # Move and rename an existing container in the default project, into the new project lxc move stage-schema-webstack stage --project default --target-project denamarin.com # LXD will remain in the default project "namespace" until you switch into a different project lxc project switch denamarin.com # Now commands will automatically assume the denamarin.com project namespace # to switch back to the default project or another project lxc project switch default ## Creating EBS storage on AWS and setting up the filesystem # show the current attached devices lsblk # show information about the new attached device ( note currently lightsail console does not tell you the correct device name ) # if this is the new device and empty it will say: data if it does not be careful, this might not be the correct name for the new device sudo file -s /dev/nvme1n1 ## the following is how you would normally create a btrfs filesystem and mount it. ## This is not how the disk will be formatted and mounted using LXD # create the btrfs filesystem on the new device sudo mkfs.btrfs /dev/nvme1n1 # create a mount point for the new filesystem and change permissions if needed sudo mkdir -p /lxd/disk0 sudo chown -R ubuntu:root /lxd sudo chmod -R 0775 /lxd # add line to /etc/fstab so new filesystem will mount on reboot /dev/nvme1n1 /lxd/disk0 btrfs defaults 0 0 ## This is how to format and mount a fresh disk device using LXD # lxc storage create pool0 btrfs source=/dev/nvme1n1 ## was able to create a project denamarin.com on ohio-web and copy the existing container # to the new project and the new storage pool in 2 commands lxc project create denamarin.com --config features.images=false --config features.profiles=false lxc copy schema-webstack-8080 dev --project default --target-project denamarin.com -s pool0 ## Setting up redis on the host so that the containers can use # modify /etc/redis/redis.conf set the bind address to all, set protected-mode to no bind * -::* protected-mode no ## Inside the containers the redis host will have to be changed to the gateway of the container bridge # this will work for the containers connecting to any services on the host ## testing on ohio-web the host and container configurations, redis on the host # changing the directus_latest/.env the REDIS configs about host need to be changed REDIS_HOST="10.134.132.1" CACHE_REDIS="redis://@10.134.132.1:6379" ## sending new git keys to ohio-web and inserting into the new denamrin.com/dev container # install git package in container # this container was still using apt proxy.conf, had to remove and run aptitude clean, update upgrade ## mount local dev codebase into container dev for development # lxc config device add dev localdev disk source=/home/ccampbell/node.js/schema-webstack/ path=/home/webstack/schema-webstack/ ## Create a new instance from a "gold" instance # in this example the gold instance is dev in project denamarin.com # this example if for local dev environment lxc copy dev stage --project denamarin.com --target-project denamarin.com -s default # once the instance is cloned, the network device proxies need to be set to something different than dev or other running instances lxc config show stage lxc config device remove stage directus8055 lxc config device remove stage node9001 # start and find IP of stage lxc list # ip is 10.80.77.243 # set the config lxc config device add stage directus8056 proxy listen=tcp:127.0.0.1:8056 connect=tcp:10.80.77.243:8055 lxc config device add stage node9002 proxy listen=tcp:127.0.0.1:9002 connect=tcp:10.80.77.243:9001 ## creating a new instance from an import file # in this example a new instance is being created that will represent stage and live denamarin # this instance was setup and tested on localhost and is capable of running dev,stage and live denamarin.com # lxc import denamarin.com_12062023.tar.xz stage --project denamarin.com -s pool0 # remove the previous config devices that bind the previous interface, these must be removed and recreated before the # instance will start cleanly # example, remove all the devices except for the root disk lxc config device remove stage denamarin_live ... # Add the config device entries for the new instance # example lxc config device add stage directus_stage proxy listen=tcp:127.0.0.1:8155 connect=tcp:10.134.132.161:8155 lxc config device add stage directus_live proxy listen=tcp:127.0.0.1:8255 connect=tcp:10.134.132.161:8255 lxc config device add stage stack_stage proxy listen=tcp:127.0.0.1:9002 connect=tcp:10.134.132.161:9002 lxc config device add stage stack_live proxy listen=tcp:127.0.0.1:9003 connect=tcp:10.134.132.161:9003 # Directus stage .env PUBLIC_URL="https://ohio-api.nutramax.com/denamarin/stage" CACHE_ENABLED=true CACHE_STORE="redis" REDIS_HOST="10.134.132.1" REDIS_USERNAME="default" REDIS_PASSWORD="804309921d514c7a" CACHE_REDIS="redis://@10.134.132.1:6379" CACHE_REDIS_DB=1 CACHE_TTL="480m" CACHE_AUTO_PURGE=true CACHE_NAMESPACE="directus.stage.denamarin" # Stack stage .env DIRECTUS_API_TOKEN="u0rx21K4weO3mzEqXgtbLD5tHwJjeL0M" IO_REDIS_HOST="10.134.132.1" SESSION_REDIS_PREFIX="stage.denamarin.sess:" lxc config device remove stage localdev lxc config device add stage localdev disk source=/home/ccampbell/node.js/denamarin-sw/ path=/home/webstack/schema-webstack/