Skip to content

Instantly share code, notes, and snippets.

@ric03uec
Last active January 2, 2023 11:39
Show Gist options
  • Save ric03uec/50221c3009a46199cf3c to your computer and use it in GitHub Desktop.
Save ric03uec/50221c3009a46199cf3c to your computer and use it in GitHub Desktop.
flannel setup
description "Docker daemon"
start on started flanneld
stop on stopping flanneld
limit nofile 524288 1048576
limit nproc 524288 1048576
respawn
pre-start script
# see also https://github.com/tianon/cgroupfs-mount/blob/master/cgroupfs-mount
if grep -v '^#' /etc/fstab | grep -q cgroup \
|| [ ! -e /proc/cgroups ] \
|| [ ! -d /sys/fs/cgroup ]; then
exit 0
fi
if ! mountpoint -q /sys/fs/cgroup; then
mount -t tmpfs -o uid=0,gid=0,mode=0755 cgroup /sys/fs/cgroup
fi
(
cd /sys/fs/cgroup
for sys in $(awk '!/^#/ { if ($4 == 1) print $1 }' /proc/cgroups); do
mkdir -p $sys
if ! mountpoint -q $sys; then
if ! mount -n -t cgroup -o $sys cgroup $sys; then
rmdir $sys || true
fi
fi
done
)
end script
script
# modify these in /etc/default/$UPSTART_JOB (/etc/default/docker)
DOCKER=/usr/bin/$UPSTART_JOB
DOCKER_OPTS=
if [ -f /etc/default/$UPSTART_JOB ]; then
. /etc/default/$UPSTART_JOB
fi
if [ -f /var/run/flannel/subnet.env ]; then
## if flannel subnet env is present, then use it to define
## the subnet and MTU values
. /var/run/flannel/subnet.env
DOCKER_OPTS="$DOCKER_OPTS --bip=${FLANNEL_SUBNET} --mtu=${FLANNEL_MTU}"
else
echo "Flannel subnet not found, exiting..."
exit 1
fi
exec "$DOCKER" -d $DOCKER_OPTS
end script
# Don't emit "started" event until docker.sock is ready.
# See https://github.com/docker/docker/issues/6647
post-start script
DOCKER_OPTS=
if [ -f /etc/default/$UPSTART_JOB ]; then
. /etc/default/$UPSTART_JOB
fi
if ! printf "%s" "$DOCKER_OPTS" | grep -qE -e '-H|--host'; then
while ! [ -e /var/run/docker.sock ]; do
initctl status $UPSTART_JOB | grep -q "stop/" && exit 1
echo "Waiting for /var/run/docker.sock"
sleep 0.1
done
echo "/var/run/docker.sock is up"
fi
end script
description "Etcd service"
author "@jainvipin"
start on filesystem or runlevel [2345]
stop on runlevel [!2345]
respawn
pre-start script
# see also https://github.com/jainvipin/kubernetes-ubuntu-start
ETCD=/opt/bin/$UPSTART_JOB
if [ -f /etc/default/$UPSTART_JOB ]; then
. /etc/default/$UPSTART_JOB
fi
if [ -f $ETCD ]; then
exit 0
fi
echo "$ETCD binary not found, exiting"
exit 22
end script
script
# modify these in /etc/default/$UPSTART_JOB (/etc/default/docker)
ETCD=/opt/bin/$UPSTART_JOB
ETCD_OPTS=""
if [ -f /etc/default/$UPSTART_JOB ]; then
. /etc/default/$UPSTART_JOB
fi
exec "$ETCD" $ETCD_OPTS
end script
description "Flanneld service"
author "@ric03uec"
start on filesystem or runlevel [2345]
stop on runlevel [!2345]
respawn
pre-start script
FLANNELD=/usr/bin/$UPSTART_JOB
if [ -f /etc/default/$UPSTART_JOB ]; then
. /etc/default/$UPSTART_JOB
fi
if [ -f $FLANNELD ]; then
exit 0
fi
echo "$FLANNELD binary not found, exiting"
exit 22
end script
script
# modify these in /etc/default/$UPSTART_JOB (/etc/default/flanneld)
FLANNELD=/usr/bin/$UPSTART_JOB
FLANNELD_OPTS=""
if [ -f /etc/default/$UPSTART_JOB ]; then
. /etc/default/$UPSTART_JOB
fi
exec "$FLANNELD" $FLANNELD_OPTS
end script
[terminal-1 (host-one)] $ ./setup.sh master
[terminal-1 (host-two)] $ ./setup.sh slave
#!/bin/bash -e
#
# Copyright 2015 Shippable Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
if [[ $# > 0 ]]; then
if [[ "$1" == "slave" ]]; then
export INSTALLER_TYPE=slave
else
export INSTALLER_TYPE=master
fi
else
export INSTALLER_TYPE=master
fi
export NODE_IP=192.168.33.10
export MASTER_IP=192.168.33.10
export FLANNEL_SUBNET=10.100.0.0/16
export DOWNLOAD_PATH=/tmp
export FLANNEL_VERSION=0.3.0
export FLANNEL_EXECUTABLE_LOCATION=/usr/bin
export ETCD_VERSION=v2.0.5
export ETCD_EXECUTABLE_LOCATION=/usr/bin
export ETCD_PORT=4001
install_prereqs() {
sudo apt-get install -yy bridge-utils
}
install_etcd() {
## download, extract and update etcd binaries ##
echo 'Installing etcd on master...'
cd $DOWNLOAD_PATH;
sudo rm -r etcd-$ETCD_VERSION-linux-amd54 || true;
etcd_download_url="https://github.com/coreos/etcd/releases/download/$ETCD_VERSION/etcd-$ETCD_VERSION-linux-amd64.tar.gz";
sudo curl -L $etcd_download_url -o etcd.tar.gz;
sudo tar xzvf etcd.tar.gz && cd etcd-$ETCD_VERSION-linux-amd64;
sudo mv -v etcd $ETCD_EXECUTABLE_LOCATION/etcd;
sudo mv -v etcdctl $ETCD_EXECUTABLE_LOCATION/etcdctl;
echo `which etcd`
echo 'Etcd installed correctly'
}
update_etcd_config() {
echo "ETCD=$ETCD_EXECUTABLE_LOCATION/etcd" | sudo tee -a /etc/default/etcd
echo "ETCD_OPTS=-listen-client-urls=http://0.0.0.0:$ETCD_PORT" | sudo tee -a /etc/default/etcd
echo "etcd config updated successfully"
}
download_flannel_release() {
## download and extract flannel archive ##
echo "Downloading flannel release version: $FLANNEL_VERSION"
cd $DOWNLOAD_PATH
flannel_download_url="https://github.com/coreos/flannel/releases/download/v$FLANNEL_VERSION/flannel-$FLANNEL_VERSION-linux-amd64.tar.gz";
sudo curl -L $flannel_download_url -o flannel.tar.gz;
sudo tar xzvf flannel.tar.gz && cd flannel-$FLANNEL_VERSION;
sudo mv -v flanneld $FLANNEL_EXECUTABLE_LOCATION/flanneld
}
update_flanneld_config() {
echo "FLANNELD_OPTS='-etcd-endpoints=http://$MASTER_IP:$ETCD_PORT -iface=$NODE_IP'" | sudo tee -a /etc/default/flanneld
}
start_etcd() {
sudo service etcd restart || true
sleep 5
}
update_flanneld_subnet() {
## update the key in etcd which determines the subnet that flannel uses
$ETCD_EXECUTABLE_LOCATION/etcdctl --peers=http://$MASTER_IP:$ETCD_PORT set coreos.com/network/config '{"Network":"'"$FLANNEL_SUBNET"'"}'
}
clear_network_entities() {
## remove the docker0 bridge created by docker daemon
echo "stopping docker"
sudo service docker stop || true
echo "removing docker0 bridge"
sudo ip link set dev docker0 down || true
sudo brctl delbr docker0 || true
}
start_services() {
## need to restart docker to reload the config
## after this docker starts/stops with flanneld service
echo 'Starting services...'
sudo service flanneld restart || true
sudo service docker restart || true
is_success=true
}
before_exit() {
if [ "$is_success" == true ]; then
echo "Script Completed Successfully";
else
echo "Script executing failed";
fi
}
trap before_exit EXIT
install_prereqs
if [[ $INSTALLER_TYPE == 'master' ]]; then
# Only install etcd on master.
trap before_exit EXIT
install_etcd
trap before_exit EXIT
update_etcd_config
fi
trap before_exit EXIT
download_flannel_release
trap before_exit EXIT
update_flanneld_config
if [[ $INSTALLER_TYPE == 'master' ]]; then
trap before_exit EXIT
start_etcd
trap before_exit EXIT
update_flanneld_subnet
fi
trap before_exit EXIT
clear_network_entities
trap before_exit EXIT
start_services
[terminal-1 (host-one)] $ container_id=$(docker run -d -p 5555:5555 ubuntu:14.04 nc -l 5555)
[terminal-1 (host-one)] $ docker inspect $container_id | grep IPA //gives the IP Address of the container
[terminal-1 (host-one)] $ docker logs -f $container_id
[terminal-2 (host-two)] $ docker run -it ubuntu:14.04 /bin/bash
[terminal-2 (host-two)]<container_id> $ echo 'Hello, it works !!' > source_file
[terminal-2 (host-two)]<container_id> $ nc <container1_ip> 5555 < source_file
[terminal-1 (host-one)] $ Hello, it works!!
[terminal 1] $ cd /home/kube
[terminal-1] $ vagrant box add https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box --name trusty64
[terminal-1] $ vagrant up host-one
[terminal-1] $ vagrant ssh host-one
[terminal-2] $ vagrant up host-two
[terminal-2] $ vagrant ssh host-two
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Create a private network, which allows host-only access to the machine
# using a specific IP.
config.vm.define "host1" do |hostone|
hostone.vm.box = "trusty64"
hostone.vm.network "private_network", ip: "192.168.33.10"
hostone.vm.hostname = "host-one"
end
config.vm.define "host2" do |hosttwo|
hosttwo.vm.box = "trusty64"
hosttwo.vm.network "private_network", ip: "192.168.33.11"
hosttwo.vm.hostname = "host-two"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment