Skip to content

Instantly share code, notes, and snippets.

@chrisxaustin
Created June 8, 2020 18:52
Show Gist options
  • Save chrisxaustin/7beb8814601d2d09c9acd63ffb3a894f to your computer and use it in GitHub Desktop.
Save chrisxaustin/7beb8814601d2d09c9acd63ffb3a894f to your computer and use it in GitHub Desktop.
userdata script to mount i3, i3en, and d2 instance storage on eks elasticsearch workers
#!/bin/bash
set -o xtrace
echo '* - nofile 65536' >> /etc/security/limits.conf
echo 'root - nofile 65536' >> /etc/security/limits.conf
echo "session required pam_limits.so" >> /etc/pam.d/common-session
echo 'vm.max_map_count=262144' >> /etc/sysctl.conf
# Identify the ephemeral volumes using either the nvme command for i3 disks or lsblk and the AWS API to query block device mappings
# https://aws.amazon.com/premiumsupport/knowledge-center/ec2-linux-instance-store-volumes/
if [[ -e /dev/nvme0n1 ]]; then
yum install nvme-cli -y
instance_stores=$(nvme list | awk '/Instance Storage/ {print $1}')
echo $instance_stores
else
OSDEVICE=$(sudo lsblk -o NAME -n | grep -v '[[:digit:]]' | sed "s/^sd/xvd/g")
BDMURL="http://169.254.169.254/latest/meta-data/block-device-mapping/"
instance_stores=$(
for bd in $(curl -s $BDMURL); do
MAPDEVICE=$(curl -s $BDMURL/$bd/ | sed "s/^sd/xvd/g");
if grep -wq $MAPDEVICE <<< "$OSDEVICE"; then
echo $MAPDEVICE
fi
done
)
echo $instance_stores
fi
# If one volume is found, mount it at /mnt/data
# If multple, create a raid0 array as /dev/md0 and mount it as /mnt/data
# A local-storage-provisioner using /mnt as the hostPath will pick up either of these
if [[ -n "$instance_stores" ]]; then
count=$(echo $instance_stores | wc -w)
if [[ $count -eq 1 ]]; then
mkdir -p /mnt/data
mkfs.ext4 $instance_stores
echo $instance_stores /mnt/data ext4 defaults,noatime 0 2 >> /etc/fstab
mount -a
elif [[ $count -gt 1 ]]; then
yum install mdadm -y
mkdir -p /mnt/data
mdadm --create --verbose --level=0 /dev/md0 --name=DATA --raid-devices=$count $instance_stores
mdadm --wait /dev/md0
mkfs.ext4 /dev/md0
mdadm --detail --scan >> /etc/mdadm.conf
echo /dev/md0 /mnt/data ext4 defaults,noatime 0 2 >> /etc/fstab
mount -a
fi
fi
/etc/eks/bootstrap.sh --apiserver-endpoint '${var.eks_endpoint}' --b64-cluster-ca '${var.eks_ca_data}' --kubelet-extra-args '--node-labels=${var.node_labels}' '${var.cluster_name}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment