-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk8scluster.sh
156 lines (145 loc) · 5.06 KB
/
k8scluster.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
azurecli() {
systype=$(uname -s)
if [[ $systype == "Linux" ]]; then
echo "Installing Azure CLI for Azure Authentication"
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
elif [[ $systype == "Darwin" ]]; then
echo "Installing Azure CLI for Azure Authentication"
brew uninstall azure-cli --force
brew update && brew install azure-cli
else
echo "System should be either Mac or Linux"
exit 0
fi
}
googlecloudsdk() {
systype=$(uname -s)
if [[ $systype == "Linux" ]]; then
echo "Installing Google Cloud SDK for Google Cloud Authentication"
curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-321.0.0-linux-x86.tar.gz
tar -xzf google-cloud-sdk-321.0.0-linux-x86.tar.gz
#./google-cloud-sdk/install.sh
#rm -rf google-cloud-sdk-321.0.0-linux-x86.tar.gz google-cloud-sdk
elif [[ $systype == "Darwin" ]]; then
echo "Installing Google cloud SDK for Google Cloud Authentication"
curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-321.0.0-darwin-x86_64.tar.gz
tar -xzf google-cloud-sdk-321.0.0-darwin-x86_64.tar.gz
#./google-cloud-sdk/install.sh
# rm -rf google-cloud-sdk-321.0.0-darwin-x86_64.tar.gz google-cloud-sdk
else
echo "System should be either Mac or Linux"
exit 0
fi
}
terraforminstall() {
systype=$(uname -s)
if [[ $systype == "Linux" ]]; then
echo "Installing Terraform"
curl -sL https://raw.github.com/robertpeteuil/terraform-installer/master/terraform-install.sh > terraform-install.sh
chmod +x terraform-install.sh
./terraform-install.sh
rm -rf terraform-install.sh
elif [[ $systype == "Darwin" ]]; then
echo "Installing Terraform"
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
else
echo "System should be either Mac or Linux"
exit 0
fi
}
installansible() {
systype=$(uname -s)
if [[ $systype == "Linux" ]]; then
echo "Installing Ansible"
os=$(cat /etc/os-release | grep -iw ID | awk -F'=' '{print $2}')
version=$(cat /etc/os-release | grep -i VERSION_CODENAME | awk -F'=' '{print $2}')
if [[ $os == "ubuntu" && $version != "focal" ]]; then
echo "Installing Ansible"
sudo apt-add-repository ppa:ansible/ansible -y
sudo apt update
sudo apt install ansible -y
elif [[ $os == "ubuntu" && $version == "focal" ]]; then
echo "Installing Ansible"
sudo apt update
sudo apt install ansible -y
elif [ $os == "rhel*" ]; then
version=$(cat /etc/os-release | grep VERSION_ID | awk -F'=' '{print $2}')
if [ $version == "*7.*" ]; then
sudo subscription-manager repos --enable rhel-7-server-ansible-2.9-rpms
sudo yum install ansible -y
elif [ $version == "*8.*" ]; then
sudo subscription-manager repos --enable ansible-2.9-for-rhel-8-x86_64-rpms
sudo yum install ansible -y
fi
fi
elif [[ $systype == "Darwin" ]]; then
echo "Installing Ansible"
brew install ansible
else
echo "System should be either Mac or Linux"
exit 0
fi
}
if [[ $1 == "azure" ]]; then
installansible
azurecli
echo "Login to Azure Account using your browser"
az login
echo "Login Successfull"
terraforminstall
cd terraform/azure
terraform init
terraform plan
terraform apply -auto-approve
terraform output inventory > ../../ansible/inventory
rm -rf azure.pem
terraform ouput tls_private_key > azure.pem
chmod 400 azure.pem
echo "Please wait for a while to bring azure vm's are up"
sleep 60
cd ../../ansible
ansible -m ping -i inventory all
ansible-playbook -i inventory prerequisites.yaml
ansible-playbook -i inventory k8s.yaml
elif [[ $1 == "aws" ]]; then
installansible
terraforminstall
cd terraform/aws
terraform init
terraform plan
terraform apply -auto-approve
terraform output inventory > ../../ansible/inventory
echo "Please wait for a while to bring aws instances up"
sleep 60
cd ../../ansible
ansible -m ping -i inventory all
ansible-playbook -i inventory prerequisites.yaml
ansible-playbook -i inventory k8s.yaml
elif [[ $1 == "gcp" ]]; then
installansible
googlecloudsdk
rm -rf ~/.config/gcloud ~/.ssh/google*
echo "Login to Google cloud Account using your browser"
# gcloud auth login
./google-cloud-sdk/bin/gcloud init
echo "Login Successfull"
./google-cloud-sdk/bin/gcloud beta compute config-ssh
./google-cloud-sdk/bin/gcloud auth application-default login
terraforminstall
cd terraform/gcp
terraform init
terraform plan
terraform apply -auto-approve
terraform output inventory > ../../ansible/inventory
echo "Please wait for a while to bring GCP vm's are up"
sleep 30
cd ../../ansible
ansible -m ping -i inventory all
ansible-playbook -i inventory prerequisites.yaml
ansible-playbook -i inventory k8s.yaml
cd ../
rm -rf google-cloud-sdk*
else
echo -e "Usage\n\nAvailable Options:\n\n aws: To Provision Kubernetes Cluster on AWS\n azure: To Provision Kubernetes Cluster on Azure\n google: To Provision Kubernetes Cluster on Google Cloud\n"
fi