Skip to content

Instantly share code, notes, and snippets.

@michel-zimmer
Last active April 27, 2021 11:36
Show Gist options
  • Save michel-zimmer/c8e2692d45c41cce083764dfe3e13795 to your computer and use it in GitHub Desktop.
Save michel-zimmer/c8e2692d45c41cce083764dfe3e13795 to your computer and use it in GitHub Desktop.
Docker Swarm inside LXC 2.0

Docker Swarm inside LXC 2.0

sudo lxd init

./swarm-manager-create.sh
./swarm-agent-create.sh swarm-agent-{0..9}

lxc exec swarm-manager -- docker -H localhost:3375 info
#!/bin/bash
set -eu
SWARM_MANAGER_IP="$(lxc exec swarm-manager -- ifconfig eth0 | grep -Po '(?<=inet addr:)\S+')"
for LXC_NAME in "$@"
do
lxc launch ubuntu-daily:16.04 "${LXC_NAME}" -p default -p docker
SWARM_AGENT_IP=''
while [[ "x${SWARM_AGENT_IP}" == 'x' ]]; do
sleep .5
SWARM_AGENT_IP="$(lxc exec "${LXC_NAME}" -- ifconfig eth0 | grep -Po '(?<=inet addr:)\S+')" || true
done
lxc exec "${LXC_NAME}" -- apt update
lxc exec "${LXC_NAME}" -- apt dist-upgrade -y
lxc exec "${LXC_NAME}" -- apt install docker.io -y
lxc exec "${LXC_NAME}" -- sed -i -e 's/ -H fd:\/\/ / -H 0.0.0.0:2375 /' /lib/systemd/system/docker.service
lxc exec "${LXC_NAME}" -- systemctl daemon-reload
lxc exec "${LXC_NAME}" -- systemctl restart docker
lxc exec "${LXC_NAME}" -- docker run -d --restart=always --name=swarm \
swarm join --addr="${SWARM_AGENT_IP}:2375" "etcd://${SWARM_MANAGER_IP}:2379"
done
#!/bin/bash
set -eu
lxc launch ubuntu-daily:16.04 swarm-manager -p default -p docker
SWARM_MANAGER_IP=''
while [[ "x${SWARM_MANAGER_IP}" == 'x' ]]; do
sleep .5
SWARM_MANAGER_IP="$(lxc exec swarm-manager -- ifconfig eth0 | grep -Po '(?<=inet addr:)\S+')" || true
done
lxc exec swarm-manager -- apt update
lxc exec swarm-manager -- apt dist-upgrade -y
lxc exec swarm-manager -- apt install docker.io -y
lxc exec swarm-manager -- docker run -d --restart=always --name=etcd \
-p 4001:4001 -p 2380:2380 -p 2379:2379 \
quay.io/coreos/etcd \
/usr/local/bin/etcd \
--data-dir=data.etcd --name etcd-node-0 \
--initial-advertise-peer-urls "http://${SWARM_MANAGER_IP}:2380" --listen-peer-urls http://0.0.0.0:2380 \
--advertise-client-urls "http://${SWARM_MANAGER_IP}:2379,http://${SWARM_MANAGER_IP}:4001" --listen-client-urls http://0.0.0.0:2379,http://0.0.0.0:4001 \
--initial-cluster etcd-node-0="http://${SWARM_MANAGER_IP}:2380" \
--initial-cluster-state new --initial-cluster-token etcd-cluster-1
lxc exec swarm-manager -- docker run -d --restart=always --name=swarm \
-p 3375:3375 \
swarm manage -H 0.0.0.0:3375 "etcd://${SWARM_MANAGER_IP}:2379"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment