-
Notifications
You must be signed in to change notification settings - Fork 22
/
kube.sh
executable file
·231 lines (209 loc) · 6.86 KB
/
kube.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/bin/sh
# justin lim <[email protected]>
# $ curl -fsSL https://raw.githubusercontent.com/jlim0930/scripts/master/kube.sh -o kube.sh
# This script will have options to start up minikube or clean it up.
# options: start|stop|delete
# if minikube & kubectl is not installed it will automatically install it.
# will enable metallb and add IP pool with minikube ip network and pool of 150-175
# sudo access is required for some instances
# tested on linux and macOS
#
#################
## User configurable variables
CPU="" # Please change this to the # of cores you want minikube to use. If not set it will use half of your total core count
MEM="" # Please change this to the amount of memory to give to minikube. If not set it will use half of your memory up to 16GB max
# HDD="" # Please change this to the amount of hdd space to give for minikube. default is 20,000MB
VERSION="v1.33.0"
SHELL=`env | grep SHELL | awk -F"/" '{ print $NF }'`
## vars
# colors
red=`tput setaf 1`
green=`tput setaf 2`
blue=`tput setaf 4`
reset=`tput sgr0`
# Get OS & set CPU & MEM
OS=`uname -s`
case ${OS} in
"Linux")
OS="linux"
if [ -z ${CPU} ]; then
temp=`nproc`
CPU=`echo $((${temp}/2))`
fi
if [ -z ${MEM} ]; then
temp=`free -m | grep Mem | awk {' print $2 '}`
value=`echo $((${temp}/2))`
if [ ${value} -gt "16384" ]; then
MEM="16384"
else
MEM="${value}"
fi
fi
;;
"Darwin")
if [ `uname -m` == "x86_64" ]; then
OS="macos-x86_64"
elif [ `uname -m` == "arm64" ]; then
OS="macos-arm64"
fi
if [ -z ${CPU} ]; then
temp=`sysctl -n hw.ncpu`
CPU=`echo "${temp}/2" | bc`
fi
if [ -z ${MEM} ]; then
temp=`sysctl -n hw.memsize`
value=`docker system info --format '{{.MemTotal}}' | grep -o '[0-9]*' | awk '{printf "%.0f\n", $1/1024/1024}'`
if [ ${value} -gt "16384" ]; then
MEM="16384"
else
MEM="${value}"
fi
fi
;;
*)
echo "${red}[DEBUG]${reset} This script only supports macOS and linux"
exit
;;
esac
## functions
# function help
function help() {
echo -e "${green}Usage:{$reset} ./`basename $0` [start|stop|delete]"
exit
}
# function check and install minikube
function checkminikube() {
if ! { [ -x "$(command -v minikube)" ] && [ `minikube version | grep version | awk {' print $3 '}` = "${VERSION}" ]; } then
echo "${red}[DEBUG]${reset} minikube not found or wrong version. Installing."
if [ $OS == "linux" ]; then
echo "${green}[DEBUG]${reset} Linux found."
curl -LO -s https://storage.googleapis.com/minikube/releases/${VERSION}/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
rm -rf minikube-linux-amd64 >/dev/null 2>&1
elif [ ${OS} == "macos-x86_64" ]; then
echo "${gree}[DEBUG]${reset} macOS x86_64 found."
curl -LO -s https://storage.googleapis.com/minikube/releases/${VERSION}/minikube-darwin-amd64
sudo install minikube-darwin-amd64 /usr/local/bin/minikube
rm -rf minikube-darwin-amd64 >/dev/null 2>&1
elif [ ${OS} == "macos-arm64" ]; then
echo "${green}[DEBUG]${reset} macOS arm64 found."
curl -LO -s https://storage.googleapis.com/minikube/releases/${VERSION}/minikube-darwin-arm64
sudo install minikube-darwin-arm64 /usr/local/bin/minikube
rm -rf minikube-darwin-arm64 >/dev/null 2>&1
fi
else
echo "${green}[DEBUG]${reset} minikube found."
fi
}
# function check and install kubectl
function checkkubectl() {
if ! [ -x "$(command -v kubectl)" ]; then
echo "${red}[DEBUG]${reset} kubectl not found. Installing."
if [ $OS == "linux" ]; then
echo "${green}[DEBUG]${reset} Linux found."
curl -LO -s "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
sudo install kubectl /usr/local/bin/kubectl
rm -rf kubectl >/dev/null 2>&1
elif [ ${OS} == "macos-x86_64" ]; then
echo "${gree}[DEBUG]${reset} macOS x86_64 found."
curl -LO -s "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/darwin/amd64/kubectl"
sudo install kubectl /usr/local/bin/kubectl
rm -rf kubectl >/dev/null 2>&1
elif [ ${OS} == "macos-arm64" ]; then
echo "${gree}[DEBUG]${reset} macOS arm64 found."
curl -LO -s "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/darwin/arm64/kubectl"
sudo install kubectl /usr/local/bin/kubectl
rm -rf kubectl >/dev/null 2>&1
fi
else
echo "${green}[DEBUG]${reset} kubectl found."
fi
}
# function delete the local minikube and kubectl in /usr/local/bin
function deletemk() {
# if [ -x "$(command -v kubectl)" ]; then
# rm -rf $(command -v kubectl)
# echo "${green}[DEBUG]${reset} Deleted kubectl"
# fi
if [ -x "$(command -v minikube)" ]; then
rm -rf $(command -v minikube)
echo "${green}[DEBUG]${reset} Deleted minikube"
fi
}
# function check docker
function checkdocker() {
if [ ${OS} == "macos-x86_64" ] || [ ${OS} == "macos-arm64" ]; then
echo "${green}[DEBUG]${reset} Docker found"
else
docker info >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "${red}[DEBUG]${reset} Docker is not running or installed or your not part of the docker group. Please fix"
exit
fi
fi
}
# function start
function build() {
echo "${green}[DEBUG]${reset} CPU will be set to ${CPU} cores"
minikube config set cpus ${CPU}
echo "${green}[DEBUG]${reset} MEM will be set to ${MEM}mb"
minikube config set memory ${MEM}
# minikube config set disk-size ${HDD}
if [ ${OS} == "linux" ]; then
minikube start --driver=docker
else
minikube start
fi
minikube addons enable metallb
baseip=`minikube ip | cut -d"." -f1-3`
startip="${baseip}.150"
endip="${baseip}.175"
cat > /tmp/metallb-config.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
namespace: metallb-system
name: config
data:
config: |
address-pools:
- name: default
protocol: layer2
addresses:
- ${startip}-${endip}
EOF
kubectl apply -f /tmp/metallb-config.yaml >/dev/null 2>&1
rm -rf /tmp/metallb-config.yaml >/dev/null 2>&1
echo "${green}[DEBUG]${reset} minikube IP is: `minikube ip`"
echo "${green}[DEBUG]${reset} LoadBalancer Pool: ${startip} - ${endip}"
source <(kubectl completion ${SHELL})
}
## script
case ${1} in
build|start)
checkminikube
checkkubectl
checkdocker
echo "${green}[DEBUG]${reset} build minikube"
build
;;
stop)
echo "${green}[DEBUG]${reset} Stopping minikube"
minikube stop
;;
upgrade)
deletemk
checkminikube
checkkubectl
echo "${green}[DEBUG]${reset} minikube and kubectl upgraded"
;;
delete|cleanup)
echo "${green}[DEBUG]${reset} Deleting minikube"
minikube delete
rm -rf ${HOME}/.minikube >/dev/null 2>&1
rm -rf ${HOME}/.kube >/dev/null 2>&1
;;
*)
help
;;
esac