Skip to content

Instantly share code, notes, and snippets.

@cloudbow
Last active March 20, 2020 07:53
Show Gist options
  • Save cloudbow/73a361aee1c10da29248bc06fc74c780 to your computer and use it in GitHub Desktop.
Save cloudbow/73a361aee1c10da29248bc06fc74c780 to your computer and use it in GitHub Desktop.
Single line kubeflow
function first_names() {
### Pass the paramters before you execute
export CLUSTER_NAME=${CLUSTER_NAME}
export AWS_REGION=${REGION}
export AWS_DEFAULT_REGION=${AWS_REGION}
}
## Set account id settings
function aws_account_id_settings(){
rm -vf ${HOME}/.aws/credentials
export ACCOUNT_ID=$(aws sts get-caller-identity --output text --query Account)
export AWS_REGION=$(curl -s 169.254.169.254/latest/dynamic/instance-identity/document | jq -r '.region')
test -n "$AWS_REGION" && echo AWS_REGION is "$AWS_REGION" || echo AWS_REGION is not set
echo "export ACCOUNT_ID=${ACCOUNT_ID}" | tee -a ~/.bash_profile
echo "export AWS_REGION=${AWS_REGION}" | tee -a ~/.bash_profile
aws configure set default.region ${AWS_REGION}
aws configure get default.region
aws sts get-caller-identity
}
## Generate and upload keys
function generate_and_upload_key(){
test -n "$(aws ec2 describe-key-pairs --key-name $1)" && return
ssh-keygen
aws ec2 import-key-pair --key-name $1 --public-key-material file://~/.ssh/id_rsa.pub
}
## Install Kubectl
function install_kubectl() {
test -e /usr/local/bin/kubectl && return
sudo curl --silent --location -o /usr/local/bin/kubectl https://amazon-eks.s3-us-west-2.amazonaws.com/1.14.6/2019-08-22/bin/linux/amd64/kubectl
sudo chmod +x /usr/local/bin/kubectl
sudo yum -y install jq gettext bash-completion
for command in kubectl jq envsubst
do
which $command &>/dev/null && echo "$command in path" || echo "$command NOT FOUND"
done
kubectl completion bash >> ~/.bash_completion
. /etc/profile.d/bash_completion.sh
}
## Install eksctl
function install_eksctl {
test -e /usr/local/bin/eksctl && return
curl --silent --location "https://github.com/weaveworks/eksctl/releases/download/latest_release/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp
sudo mv -v /tmp/eksctl /usr/local/bin
eksctl version
eksctl completion bash >> ~/.bash_completion
. /etc/profile.d/bash_completion.sh
. ~/.bash_completion
}
## Install eks cluster
function create_eks_cluster(){
test -n "$(eksctl get cluster --name $CLUSTER_NAME)" && return
eksctl create cluster --name=$CLUSTER_NAME --nodes=$1 --managed --alb-ingress-access --region=${AWS_REGION}
}
## List stacks . required since aws cloudformation stack does not get loaded sometimes
function list_stacks() {
aws cloudformation list-stacks
}
## export required names
function export_names() {
list_stacks
#export CONFIG_URI=/home/ec2-user/environment/$CLUSTER_NAME/kfctl_aws.0.7.0.yaml
#export CONFIG_URI="https://raw.githubusercontent.com/kubeflow/manifests/v0.7-branch/kfdef/kfctl_aws.0.7.1.yaml"
#export CONFIG_URI="https://raw.githubusercontent.com/kubeflow/manifests/v1.0-branch/kfdef/kfctl_aws_cognito.v1.0.0.yaml"
export CONFIG_URI="https://raw.githubusercontent.com/kubeflow/manifests/v1.0-branch/kfdef/kfctl_aws.v1.0.0.yaml"
export NODEGROUP_NAME=$(eksctl get nodegroups --cluster $CLUSTER_NAME -o json | jq -r '.[0].Name')
export AWS_CLUSTER_NAME=$CLUSTER_NAME
export KF_NAME=${AWS_CLUSTER_NAME}
export BASE_DIR=~/environment
export KF_DIR=${BASE_DIR}/${KF_NAME}
export AWS_REGION=$AWS_DEFAULT_REGION
}
# autoscale
function auto_scale() {
export_names
COUNT=$(eksctl get nodegroup --cluster ml-eks-v8 | awk 'NR==2' | awk '{print $6}')
test $COUNT -eq $1 && return
eksctl scale nodegroup --cluster $CLUSTER_NAME --name $NODEGROUP_NAME --nodes $1
}
## Install kfctl
function install_kfctl() {
test -e /usr/local/bin/kfctl && return
curl --silent --location "$1" | tar xz -C /tmp
sudo mv -v /tmp/kfctl /usr/local/bin
}
## download iam-auth
function download_iam_auth() {
test -e /usr/local/bin/aws-iam-authenticator && return
curl -o aws-iam-authenticator https://amazon-eks.s3-us-west-2.amazonaws.com/1.13.7/2019-06-11/bin/linux/amd64/aws-iam-authenticator
chmod +x aws-iam-authenticator
sudo mv aws-iam-authenticator /usr/local/bin
}
## Add roles
function role_name() {
list_stacks
STACK_NAME=$(eksctl get nodegroup --cluster $CLUSTER_NAME -o json | jq -r '.[].StackName')
ROLE_NAME=$(aws cloudformation describe-stack-resources --stack-name $STACK_NAME | jq -r '.StackResources[] | select(.ResourceType=="AWS::IAM::Role") | .PhysicalResourceId')
export ROLE_NAME=${ROLE_NAME}
echo $ROLE_NAME
}
## Build kubeflow
function build_kf() {
export_names
install_kfctl "https://github.com/kubeflow/kfctl/releases/download/v1.0-rc.4/kfctl_v1.0-rc.3-1-g24b60e8_linux.tar.gz"
download_iam_auth
echo ${KF_DIR}
mkdir -p ${KF_DIR}
cd ${KF_DIR}
echo `pwd`
rm -rf *
kfctl build -V -f ${CONFIG_URI}
}
## Replace config
function repalce_kube_config() {
export_names
download_iam_auth
role_name
TMP_FILE=/tmp/config.$(date +%s)
cp ${CONFIG_FILE} $TMP_FILE
echo copied config is "$TMP_FILE"
export CONFIG_FILE=${KF_DIR}/$1
sed -i -e 's/kubeflow-aws/'"$AWS_CLUSTER_NAME"'/' ${CONFIG_FILE}
sed -i "s@us-west-2@$AWS_REGION@" ${CONFIG_FILE}
test -n "$ROLE_NAME" && echo ROLE_NAME is "$ROLE_NAME" || echo ROLE_NAME is not set
sed -i "s@eksctl-$CLUSTER_NAME-nodegroup-ng-a2-NodeInstanceRole-xxxxxxx@$ROLE_NAME@" ${CONFIG_FILE}
diff $TMP_FILE ${CONFIG_FILE}
}
## Create kubeflow
function create_kubeflow() {
rm -rf kustomize
kfctl apply -V -f ${CONFIG_FILE}
}
## Create instion endpoint
function get_istio_endpoint() {
kubectl get ingress -n istio-system -o jsonpath='{.items[0].status.loadBalancer.ingress[0].hostname}'
}
CLUSTER_NAME=ml-eks-v8 AWS_REGION=us-east-2 AWS_DEFAULT_REGION=us-east-2 first_names
install_kubectl
aws_account_id_settings
generate_and_upload_key "eksworkshop"
install_eksctl
create_eks_cluster 3
auto_scale 6
build_kf
repalce_kube_config "kfctl_aws.v1.0.0.yaml"
create_kubeflow
get_istio_endpoint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment