Skip to content

Instantly share code, notes, and snippets.

@jmbarbier
Last active October 14, 2015 00:34
Show Gist options
  • Save jmbarbier/ab06cf23735845a0167a to your computer and use it in GitHub Desktop.
Save jmbarbier/ab06cf23735845a0167a to your computer and use it in GitHub Desktop.
"Automatisation" de la formation d'un cloud de CoreOS chez #Gandi...

Lancement "auto" d'un cloud CoreOS chez Gandi

  • gandi cli configuré et prêt à lancer des machines
  • git clone ce gist
  • préparer un discoveryToken sur https://discovery.etcd.io/new?size=1
  • éditer gandi_coreos_test.sh selon besoins (nb instances, noms, ...)

TODO

  • être + efficace (cloner un modèle au lieu de recréer, etc..)
  • bug sur discoveryToken
#cloud-config
# needs
# - discoveryToken
# - private_ipv4
# - public-ipv4
# - public_ipv4_gw
# - dns_addresses
# - ssh_key
# - hostname
coreos:
etcd2:
discovery: "https://discovery.etcd.io/{{discoveryToken}}"
advertise-client-urls: "http://{{private_ipv4}}:2379"
initial-advertise-peer-urls: "http://{{private_ipv4}}:2380"
listen-client-urls: "http://0.0.0.0:2379"
listen-peer-urls: "http://{{private_ipv4}}:2380"
fleet:
public-ip: "{{private_ipv4}}"
metadata: "provider=gandi"
flannel:
etcd_prefix: "/coreos.com/network"
locksmith:
endpoint: "http://{{private_ipv4}}:2379"
update:
reboot-strategy: "best-effort"
units:
- name: systemd-networkd.service
command: stop
- name: 00-eth0.network
runtime: true
content: |
[Match]
Name=eth0
[Network]
DNS={{dns_addresse}}
DNS=8.8.8.8
DNS=8.8.4.4
Address={{public_ipv4}}
Gateway={{public_ipv4_gw}}
- name: 00-eth1.network
runtime: true
content: |
[Match]
Name=eth1
[Network]
Address={{private_ipv4}}
- name: systemd-networkd.service
command: start
- name: "etcd2.service"
command: "start"
- name: flanneld.service
drop-ins:
- name: 50-network-config.conf
content: |
[Service]
ExecStartPre=/usr/bin/etcdctl set /coreos.com/network/config '{ "Network": "10.1.0.0/16" }'
command: start
- name: systemd-networkd.service
command: start
- name: "fleet.service"
command: "start"
ssh_authorized_keys:
- "{{ssh_key}}"
hostname: "{{hostname}}"
users: []
write_files: []
#!/bin/sh
# Tentative pour lancer un cluster de coreos "à la laborieuse"
# BUG (je crois) : le clusterId n'est pas mis -> en ssh (core -> sudo su = root)
#, le mettre dans le fichier /var/lib/coreos-install/user_data et rebooter
# la machine coreos
# Arguments : clusterId, [basehostname]
set -xv
BASEHOSTNAME=$2
INSTALLDIR=gdicos
function init {
# Création d'un VLAN et d'une ip sur ce vlan
gandi vlan create --datacenter FR --subnet 10.0.0.0/16 \
--gateway 10.0.255.254 --name ${BASEHOSTNAME}net
echo $1 > /tmp/clusterId
}
function prepare {
HOSTNAME=$BASEHOSTNAME-$1
# Create disk, create ip on vlan
gandi disk create --name cor-$HOSTNAME --size 10G --datacenter FR
# Créer une vm pour installer coreOS sur ce disque
gandi vm create --datacenter FR --memory 256 --cores 1 --ip-version 4 \
--hostname $HOSTNAME --image "Debian 8 64 bits (HVM)" --size 3G
sleep 10 # ?? utile ?
gandi ip create --vlan ${BASEHOSTNAME}net --ip 10.0.0.$1 --attach $HOSTNAME
sleep 10 # ?? utile ?
gandi disk attach -f cor-$HOSTNAME $HOSTNAME
sleep 10 # ?? utile ?
# Préparation de l'installation de coreOs, méthode "disque"
gandi vm ssh --wipe-key $HOSTNAME "umount /dev/sdc;\
apt-get update &&\
apt-get install -y wget git &&\
git clone https://gist.github.com/ab06cf23735845a0167a.git $INSTALLDIR &&\
cd $INSTALLDIR &&\
wget https://raw.github.com/coreos/init/master/bin/coreos-install &&\
chmod +x coreos-install prepare_gandi_cloudinit.sh"
# Get server network address
IP=`gandi vm info $HOSTNAME | grep ip4 | grep -v 10.0. | sed 's/ip4 *: //g'`
# Préparation du cloud-init
scp ~/.ssh/id_rsa.pub root@$IP:/root/$INSTALLDIR/
scp /tmp/clusterId root@$IP:/root/$INSTALLDIR/
ssh root@$IP "./$INSTALLDIR/prepare_gandi_cloudinit.sh $INSTALLDIR > /root/$INSTALLDIR/cloud-config.yml"
# Installation coreOS
ssh root@$IP ./$INSTALLDIR/coreos-install -d /dev/sdc -C alpha\
-c /root/$INSTALLDIR/cloud-config.yml
sleep 10
# # Remise en l'état de la future machine
gandi vm stop $HOSTNAME
# Démontage des disques, et définition en disque de boot
gandi disk detach -f cor-$HOSTNAME
gandi disk detach -f sys_$HOSTNAME
gandi disk delete -f sys_$HOSTNAME
gandi disk update --kernel raw cor-$HOSTNAME
# p 0 => premier disque (/dev/sda) ... ?? le p=0 fait foirer la commande de
# temps en temps ?.?. Je pense qu'on peut le virer ?
gandi disk attach -f -p 0 cor-$HOSTNAME $HOSTNAME
# Redémarrage
gandi vm start $HOSTNAME
# Suppression de l'empreinte du serveur "temporaire"
ssh-keygen -f ~/.ssh/known_hosts -R $IP
}
init
prepare 1
prepare 2
prepare 3
# Pas oublier de faire le ménage après avoir joué
# gandi vm delete -f ...
#!/bin/bash
discoveryToken=`cat /root/$1/clusterId`
ssh_key=`cat /root/$1/id_rsa.pub`
public_ipv4=`ip -f inet -o addr show dev eth0 | awk '{split($4,a,"/"); print a[1]}'`
public_ipv4_gw=`route --inet | grep default | sed 's/^default *\([^ ]*\).*/\1/g'`
private_ipv4=`ip -f inet -o addr show dev eth1 | awk '{split($4,a,"/"); print a[1]}'`
dns_addresse=`cat /etc/resolv.conf | grep nameserver | grep -v : -m 1 | sed "s/nameserver \([^ :]*\)/\1/"`
hostname=`hostname`
echo "s/{{discoveryToken}}/${discoveryToken}/g
s%{{ssh_key}}%${ssh_key}%g
s/{{public_ipv4}}/${public_ipv4}/g
s/{{public_ipv4_gw}}/${public_ipv4_gw}/g
s/{{private_ipv4}}/${private_ipv4}/g
s/{{dns_addresse}}/${dns_addresse}/g
s/{{hostname}}/${hostname}/g" > /tmp/replace.sed
sed -f /tmp/replace.sed /root/$1/cloud-init.tpl.yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment