Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
#set -x
AWS_DEFAULT_REGION=us-east-2
NR_MASTER=1
NR_NODES=2
SERVER_TYPE=t2.small
K8S_VERSION=v1.8.15
KOPS_VERSION=1.12.2
key=devops23
KOPS_HOME=/home/bikram/kops
KUBE_HOME=$KOPS_HOME/kubectl
SUBDOMAIN_NAME=becram.k8s.local
KUBE_CONFIG_HOME=$KOPS_HOME/$SUBDOMAIN_NAME/kubeconfig
HOSTED_ZONE_FILE=$KOPS_HOME/hosted-zone.json
K8_SUB_DOMAIN_DEFAULT=$KOPS_HOME/k8-sub-domain-default.json
K8_SUB_DOMAIN_ENV=$KOPS_HOME/k8-sub-domain.json
SSH_KEY_HOME=$KOPS_HOME/$SUBDOMAIN_NAME/sshkeys
# Log a string via the syslog facility.
log(){
if [ "$1" == "notice" ]; then
# logger -p user.$1 -t " $(date "+%F %T")=> $2"
echo -e " $(date "+%F %T") => \e[32m$2\e[0m"
else
echo -e " $(date "+%F %T") => \e[91m$2\e[0m"
fi
}
getSubDomain() {
read -p "Hello, what is the SUB-DOMAIN ?. : " SUBDOMAIN_NAME
if [[ $SUBDOMAIN_NAME == "" ]]; then
echo "Exiting... SUB-DOMAIN is MANDATORY"
exit
fi
# craete kops home if it does not exist
mkdir -p $KOPS_HOME
}
installKubectl() {
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.7.0/bin/linux/amd64/kubectl
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
}
installKops() {
wget https://github.com/kubernetes/kops/releases/download/"$KOPS_VERSION"/kops-linux-amd64
chmod +x kops-linux-amd64
sudo mv kops-linux-amd64 /usr/local/bin/kops
}
installAWSClient() {
log notice "Installing aws cli"
# update repository
sudo apt-get update
# install python-pip
sudo apt install python-pip
#install AWS client
pip install awscli --upgrade --user
#install jq
sudo apt-get install jq
}
getZONES(){
log notice "Geting zones"
# If Windows, use `'\r'` instead `'\n'`
export ZONES=$(aws ec2 describe-availability-zones \
--region $AWS_DEFAULT_REGION | jq -r \
'.AvailabilityZones[].ZoneName' | tr '\n' ',' | tr -d ' ')
ZONES=${ZONES%?}
echo "$ZONES"
}
createS3Bucket() {
log notice "######### Creating S3 Bucket $SUBDOMAIN_NAME ######"
aws s3 rb s3://$SUBDOMAIN_NAME-kubernetes-state
aws s3 mb s3://$SUBDOMAIN_NAME-kubernetes-state --region "$AWS_DEFAULT_REGION"
log notice "######### export KOPS_STATE_STORE ######"
export KOPS_STATE_STORE=s3://$SUBDOMAIN_NAME-kubernetes-state
log notice "######### S3 Bucket Creation Complete ######"
}
createSSHKeys() {
log notice "######### Creating SSH Keys ######"
#create directory for subdomain
mkdir -p $KOPS_HOME/$SUBDOMAIN_NAME
touch $KUBE_CONFIG_HOME
export KUBECONFIG="$KUBE_CONFIG_HOME"
#kube config home
log notice "KUBECONFIG=$KUBECONFIG"
# create ssh key home
mkdir $SSH_KEY_HOME
# generate ssh key
ssh-keygen -f $SSH_KEY_HOME/id_rsa -t rsa #save the key in the sshkeys
log notice "######### SSH Keys created successfully ######"
}
createComment() {
COMMENT=$1
jq '. | .Comment="'"$COMMENT"'"' $K8_SUB_DOMAIN_DEFAULT >>$K8_SUB_DOMAIN_ENV
log notice "Created $COMMENT"
}
createResourceRecordSet() {
SUBDOMAIN_NAME=$1
jq '. | .Changes[0].ResourceRecordSet.Name="'"$SUBDOMAIN_NAME"'"' $K8_SUB_DOMAIN_ENV >>$KOPS_HOME/k8-sub-domain-updated.json
mv $KOPS_HOME/k8-sub-domain-updated.json $K8_SUB_DOMAIN_ENV
log notice "Created Sub-Domain $SUBDOMAIN_NAME"
createAddress
}
createAddress() {
ADDRESS_1=$(jq '. | .DelegationSet.NameServers[0]' $KOPS_HOME/hosted-zone.json)
ADDRESS_2=$(jq '. | .DelegationSet.NameServers[1]' $KOPS_HOME/hosted-zone.json)
ADDRESS_3=$(jq '. | .DelegationSet.NameServers[2]' $KOPS_HOME/hosted-zone.json)
ADDRESS_4=$(jq '. | .DelegationSet.NameServers[3]' $KOPS_HOME/hosted-zone.json)
log notice "Created Address $SUBDOMAIN_NAME"
jq '. | .Changes[0].ResourceRecordSet.ResourceRecords[0].Value='"$ADDRESS_1"' | .Changes[0].ResourceRecordSet.ResourceRecords[1].Value='"$ADDRESS_2"' | .Changes[0].ResourceRecordSet.ResourceRecords[2].Value='"$ADDRESS_3"' | .Changes[0].ResourceRecordSet.ResourceRecords[3].Value='"$ADDRESS_4"' ' $K8_SUB_DOMAIN_ENV >>$KOPS_HOME/k8-sub-domain-updated.json
mv $KOPS_HOME/k8-sub-domain-updated.json $K8_SUB_DOMAIN_ENV
}
createRecordInParentDomain() {
PARENT_HOSTED_ZONE_ID=$(aws route53 list-hosted-zones | jq --raw-output '. | .HostedZones[0].Id')
CHANGE_ID=$(aws route53 change-resource-record-sets \
--hosted-zone-id $PARENT_HOSTED_ZONE_ID \
--change-batch file://$KOPS_HOME/k8-sub-domain.json | jq --raw-output '. | .ChangeInfo.Id')
log notice "CHANGE CREATED : $CHANGE_ID"
waitForINSYNC
}
createCluster() {
# create SSH KEYS
createSSHKeys
# create S3 Bucket
createS3Bucket
#Execute create cluster
log notice "#### Creating KOPS Cluster ####"
SSH_PUBLIC_KEY=$SSH_KEY_HOME/id_rsa.pub
log notice $SSH_PUBLIC_KEY
kops create cluster --v=0 \
--cloud=aws \
--node-count 2 \
--master-size=t2.medium \
--master-zones=us-east-2a \
--zones us-east-2a,us-east-2b \
--name=$SUBDOMAIN_NAME \
--node-size=t2.micro \
--ssh-public-key=$SSH_PUBLIC_KEY \
2>&1 | tee $KOPS_HOME/create_cluster.txt
log notice "############# UPDATE CLUSTER STARTS ################"
#run update to create the cluster
kops update cluster $SUBDOMAIN_NAME --yes
log notice "############# UPDATE CLUSTER ENDS ################"
log notice "**export KOPS_STATE_STORE=s3://$SUBDOMAIN_NAME-kubernetes-state ***"
}
#kops create secret --name ${NAME} sshpublickey admin -i ~/.ssh/id_rsa.pub
deleteCluster() {
log notice "Delete cluster $SUBDOMAIN_NAME"
export KOPS_STATE_STORE=s3://$SUBDOMAIN_NAME-kubernetes-state
kops delete cluster $SUBDOMAIN_NAME --yes
rm -rfv $KOPS_HOME/$SUBDOMAIN_NAME
rm -rfv create_cluster.txt
}
clean() {
rm -rf $K8_SUB_DOMAIN_ENV
rm -rf $KOPS_HOME/k8-sub-domain-updated.json
}
drawMenu() {
# clear the screen
tput clear
# Move cursor to screen location X,Y (top left is 0,0)
tput cup 3 15
# Set a foreground colour using ANSI escape
tput setaf 3
echo "BecramDhoju"
tput sgr0
tput cup 5 17
# Set reverse video mode
tput rev
echo "M A I N - M E N U"
tput sgr0
tput cup 7 15
echo "1. Clean Install Kubernetes"
tput cup 8 15
echo "2. Install kops"
tput cup 9 15
echo "3. Install Kubectl"
tput cup 10 15
echo "4. Create K8 Cluster"
tput cup 12 15
echo "5. Delete Cluster"
# Set bold mode
tput bold
tput cup 14 15
# The default value for PS3 is set to #?.
# Change it i.e. Set PS3 prompt
read -p "Enter your choice [1-5] " choice
}
drawMenu
tput sgr0
# set deployservice list
case $choice in
1)
echo "#########################"
echo "Starting a clean INSTALL."
getSubDomain
clean
installAWSClient
installKops
installKubectl
createSubDomain
createComment "k8 subdomain $SUBDOMAIN_NAME"
createResourceRecordSet "$SUBDOMAIN_NAME"
createRecordInParentDomain
echo "#########################"
;;
2)
echo "#########################"
echo "Starting a Kops INSTALL."
installKops
echo "#########################"
;;
3)
echo "#########################"
echo "Starting a Kubectl INSTALL."
installKubectl
echo "#########################"
;;
4)
echo "#########################"
echo "Creating Cluster."
# getSubDomain
createCluster
echo "#########################"
;;
5)
echo "################# ########"
echo "Deleteing Cluster $SUBDOMAIN_NAME"
deleteCluster
echo "#########################"
;;
*)
echo "Error: Please try again (select 1..3)!"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.