Skip to content

Instantly share code, notes, and snippets.

@sergiogarciadev
Last active March 8, 2020 10:04
Show Gist options
  • Select an option

  • Save sergiogarciadev/1f95ba8e1cfb566bb4167019c37ba5bd to your computer and use it in GitHub Desktop.

Select an option

Save sergiogarciadev/1f95ba8e1cfb566bb4167019c37ba5bd to your computer and use it in GitHub Desktop.

Revisions

  1. Sergio Garcia revised this gist Sep 22, 2017. 1 changed file with 20 additions and 5 deletions.
    25 changes: 20 additions & 5 deletions create_lxd_swarm.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    #!/bin/sh

    MANAGER_COUNT=3
    WORKER_COUNT=19
    MANAGER_COUNT=1
    WORKER_COUNT=1

    MANAGER_NAME=swarm-manager
    WORKER_NAME=swarm-worker
    @@ -78,9 +78,13 @@ function node_cmd_all_async {
    done
    }

    function node_lxc_launch {
    lxc launch $1 $2 -p default -p docker
    }

    function node_create {
    echo "Creating $1"
    lxc launch docker $1 -c security.nesting=true -c security.privileged=true -p default -p docker
    node_lxc_launch docker $1
    }

    function node_create_all {
    @@ -155,10 +159,17 @@ function swarm_create {
    swarm_join_all
    }

    function create_lxc_docker_profile {
    lxc profile set docker security.nesting true
    lxc profile set docker security.privileged true
    lxc profile set docker linux.kernel_modules ip_tables,ip6_tables,netlink_diag,nf_nat,br_netfilter,overlay
    lxc profile set docker raw.lxc lxc.aa_profile=unconfined
    lxc profile set docker linux.kernel_modules overlay,nf_nat,ip_tables,ip6_tables,netlink_diag,br_netfilter,xt_conntrack,nf_conntrack,ip_vs,vxlan
    }

    function create_lxc_docker_image {
    # https://askubuntu.com/questions/815993/how-to-add-br-netfilter-to-an-lxd-container
    lxc launch ubuntu:16.04 docker -c security.nesting=true -c security.privileged=true
    node_lxc_launch ubuntu:16.04 docker

    sleep 30

    @@ -177,9 +188,13 @@ function create_lxc_docker_image {
    lxc delete docker
    }

    function delete_lxc_docker_image {
    lxc image delete docker
    }

    function setup_lxc_containers {
    node_exec_all touch /.dockerenv
    }


    create_lxc_docker_image && node_create_all && setup_lxc_containers && swarm_create
    # create_lxc_docker_image && node_create_all && setup_lxc_containers && swarm_create
  2. Sergio Garcia revised this gist Sep 22, 2017. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion create_lxd_swarm.sh
    Original file line number Diff line number Diff line change
    @@ -177,5 +177,9 @@ function create_lxc_docker_image {
    lxc delete docker
    }

    function setup_lxc_containers {
    node_exec_all touch /.dockerenv
    }


    create_lxc_docker_image && node_create_all && swarm_create
    create_lxc_docker_image && node_create_all && setup_lxc_containers && swarm_create
  3. Sergio Garcia created this gist Sep 22, 2017.
    181 changes: 181 additions & 0 deletions create_lxd_swarm.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,181 @@
    #!/bin/sh

    MANAGER_COUNT=3
    WORKER_COUNT=19

    MANAGER_NAME=swarm-manager
    WORKER_NAME=swarm-worker

    MANAGER_LIST=""
    for i in `seq 1 $MANAGER_COUNT`;
    do
    MANAGER_LIST="$MANAGER_LIST $MANAGER_NAME-$i"
    done

    WORKER_LIST=""
    for i in `seq 1 $WORKER_COUNT`;
    do
    WORKER_LIST="$WORKER_LIST $WORKER_NAME-$i"
    done

    NODE_LIST="$MANAGER_LIST $WORKER_LIST"

    function contains {
    for item in $2
    do
    if [ "$item" == "$1" ]; then
    echo true
    return
    fi
    done
    echo false
    }

    function is_worker {
    if $(contains $1 "$WORKER_LIST")
    then
    echo true
    return
    fi
    echo false
    }

    function is_manager {
    if $(contains $1 "$MANAGER_LIST")
    then
    echo true
    return
    fi
    echo false
    }

    function node_exec_all {
    for node in $NODE_LIST
    do
    lxc exec $node -- $*
    done
    }


    function node_cmd_all {
    for node in $NODE_LIST
    do
    $1 $node
    done
    }

    function node_cmd_all_async {
    PIDS=""
    for node in $NODE_LIST
    do
    $1 $node &
    PIDS="$PIDS $!"
    done

    for pid in $PIDS
    do
    wait $pid
    done
    }

    function node_create {
    echo "Creating $1"
    lxc launch docker $1 -c security.nesting=true -c security.privileged=true -p default -p docker
    }

    function node_create_all {
    node_cmd_all node_create
    }

    function node_start {
    echo "Starting $1"
    lxc start $1
    }

    function node_start_all {
    node_cmd_all node_start
    }

    function node_stop {
    echo "Stoping $1"
    lxc stop $1
    }

    function node_stop_all {
    node_cmd_all node_stop
    }

    function node_remove {
    echo "Removing $1"
    lxc delete --force $1
    }

    function node_remove_all {
    node_cmd_all node_remove
    }

    function get_ip {
    lxc exec $1 -- hostname -i
    }

    function get_swarm_worker_token {
    lxc exec $MANAGER_NAME-1 -- docker swarm join-token -q worker
    }

    function get_swarm_manager_token {
    lxc exec $MANAGER_NAME-1 -- docker swarm join-token -q manager
    }

    function swarm_join {
    if [ "$1" == "$MANAGER_NAME-1" ]; then
    return
    fi

    ip=$(get_ip $MANAGER_NAME-1)

    if $(is_manager $1)
    then
    token=$(get_swarm_manager_token)
    fi

    if $(is_worker $1)
    then
    token=$(get_swarm_worker_token)
    fi

    lxc exec $1 -- docker swarm join --token $token $ip:2377
    }

    function swarm_join_all {
    node_cmd_all swarm_join
    }

    function swarm_create {
    lxc exec $MANAGER_NAME-1 -- docker swarm init
    swarm_join_all
    }


    function create_lxc_docker_image {
    # https://askubuntu.com/questions/815993/how-to-add-br-netfilter-to-an-lxd-container
    lxc launch ubuntu:16.04 docker -c security.nesting=true -c security.privileged=true

    sleep 30

    lxc exec docker -- apt update -qq
    lxc exec docker -- apt install -y apt-transport-https ca-certificates curl software-properties-common
    lxc exec docker -- sh -c "curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -"
    lxc exec docker -- add-apt-repository \
    "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
    $(lsb_release -cs) \
    stable"
    lxc exec docker -- apt update -qq
    lxc exec docker -- apt install -y docker-ce

    lxc stop docker
    lxc publish docker --alias docker
    lxc delete docker
    }


    create_lxc_docker_image && node_create_all && swarm_create