-
Notifications
You must be signed in to change notification settings - Fork 6
/
install.sh
254 lines (217 loc) · 7.14 KB
/
install.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/bin/bash
set -e
# --- helper functions for logs ---
info()
{
echo '[INFO] ' "$@"
}
warn()
{
RED='\033[0;31m'
NC='\033[0m' # No Color
echo -e "${RED}[WARN] ${NC}" "${RED}$@${NC}" >&2
}
fatal()
{
echo '[ERROR] ' "$@" >&2
exit 1
}
# --- add quotes to command arguments ---
quote() {
for arg in "$@"; do
printf '%s\n' "$arg" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/'/"
done
}
# --- add indentation and trailing slash to quoted args ---
quote_indent() {
printf ' \\\n'
for arg in "$@"; do
printf '\t%s \\\n' "$(quote "$arg")"
done
}
# --- escape most punctuation characters, except quotes, forward slash, and space ---
escape() {
printf '%s' "$@" | sed -e 's/\([][!#$%&()*;<=>?\_`{|}]\)/\\\1/g;'
}
# --- escape double quotes ---
escape_dq() {
printf '%s' "$@" | sed -e 's/"/\\"/g'
}
eval set -- $(escape "$@") $(quote "$@")
download_files() {
if [ "$#" -ne 1 ]; then
fatal "Usage: download_files <URL>"
return 1
fi
local url=$1
local path=".$(echo "$url" | sed -E 's|^https?://[^/]+||')"
local save_path=$(dirname "$path")
if [ ! -d "$save_path" ]; then
mkdir -p "$save_path"
fi
if [ -f "$path" ]; then
info "File already exists, skipping download: ${path}"
return 0
fi
curl -L --insecure --output "${path}" "$url" >/dev/null 2>&1
if [ $? -eq 0 ]; then
info "download success ${path}"
else
info "download failed"
return 2
fi
}
downloadResource() {
info "start download w7panel resource!"
# images cilium
download_files 'https://cdn.w7.cc/w7panel/images/cilium.cilium-v1.16.4.tar'
download_files 'https://cdn.w7.cc/w7panel/images/cilium.operator-generic-v1.16.4.tar'
# images cert-manager
download_files 'https://cdn.w7.cc/w7panel/images/jetstack.cert-manager-cainjector-v1.16.2.tar'
download_files 'https://cdn.w7.cc/w7panel/images/jetstack.cert-manager-controller-v1.16.2.tar'
download_files 'https://cdn.w7.cc/w7panel/images/jetstack.cert-manager-webhook-v1.16.2.tar'
download_files 'https://cdn.w7.cc/w7panel/images/jetstack.cert-manager-startupapicheck-v1.16.2.tar'
# images longhorn
download_files 'https://cdn.w7.cc/w7panel/images/longhornio.csi-attacher-v4.7.0.tar'
download_files 'https://cdn.w7.cc/w7panel/images/longhornio.csi-node-driver-registrar-v2.12.0.tar'
download_files 'https://cdn.w7.cc/w7panel/images/longhornio.csi-provisioner-v4.0.1-20241007.tar'
download_files 'https://cdn.w7.cc/w7panel/images/longhornio.csi-resizer-v1.12.0.tar'
download_files 'https://cdn.w7.cc/w7panel/images/longhornio.csi-snapshotter-v7.0.2-20241007.tar'
download_files 'https://cdn.w7.cc/w7panel/images/longhornio.livenessprobe-v2.14.0.tar'
download_files 'https://cdn.w7.cc/w7panel/images/longhornio.longhorn-engine-v1.7.2.tar'
download_files 'https://cdn.w7.cc/w7panel/images/longhornio.longhorn-instance-manager-v1.7.2.tar'
download_files 'https://cdn.w7.cc/w7panel/images/longhornio.longhorn-manager-v1.7.2.tar'
download_files 'https://cdn.w7.cc/w7panel/images/longhornio.longhorn-share-manager-v1.7.2.tar'
# manifests
download_files 'https://cdn.w7.cc/w7panel/manifests/cert-manager.yaml'
download_files 'https://cdn.w7.cc/w7panel/manifests/cilium.yaml'
download_files 'https://cdn.w7.cc/w7panel/manifests/higress.yaml'
download_files 'https://cdn.w7.cc/w7panel/manifests/longhorn.yaml'
download_files 'https://cdn.w7.cc/w7panel/manifests/w7panel-offline.yaml'
# etc
download_files 'https://cdn.w7.cc/w7panel/etc/registries.yaml'
download_files 'https://cdn.w7.cc/w7panel/etc/sysctl.d/k3s.conf'
download_files 'https://cdn.w7.cc/w7panel/etc/k3s.service.d/override.conf'
}
# PUBLIC_IP=
publicNetworkIp() {
#publicIp 为空,则重新获取publicIp
if [ -z "$PUBLIC_IP" ]; then
PUBLIC_IP=$(curl -s ifconfig.me);
echo $PUBLIC_IP
else
echo $PUBLIC_IP
fi
}
internalIP() {
if [ -z "$INTERNAL_IP" ]; then
INTERNAL=$(ip addr show eth0 | grep 'inet ' | grep -v '127.0.0.1' | awk '{ print $2 }' | cut -d/ -f1);
echo $INTERNAL
else
echo $INTERNAL
fi
}
etcSysctl() {
if command -v sysctl &> /dev/null; then
ETC_PATH="/etc/sysctl.d"
mkdir -p $ETC_PATH
chmod -R 755 $ETC_PATH
cp "./w7panel/etc/sysctl.d/k3s.conf" $ETC_PATH
sysctl -p >/dev/null 2>&1
fi
}
etcPrivaterRegistry(){
ETC_PATH="/etc/rancher/k3s/"
mkdir -p $ETC_PATH
cp "./w7panel/etc/registries.yaml" $ETC_PATH
}
etcSystemd(){
ETC_PATH="/etc/systemd/system/k3s.service.d/"
mkdir -p $ETC_PATH
cp "./w7panel/etc/k3s.service.d/override.conf" $ETC_PATH
}
checkK3SInstalled() {
info 'start check server is installed 检测k3s是否已安装'
if [ -x /usr/local/bin/k3s ]; then
warn "K3s has been installed , Please execute /usr/local/bin/k3s-uninstall.sh to uninstall k3s "
warn "K3s 已安装 , 请先执行 /usr/local/bin/k3s-uninstall.sh 命令卸载 "
exit
fi
}
checkW7panelInstalled() {
info '微擎面板正在安装中,请耐心等待'
max_attempts=300
attempt=0
while [ $attempt -lt $max_attempts ]; do
response=$(echo $(curl -s --max-time 5 -I "http://$(internalIP):9090"))
if [ $? -eq 0 ]; then
if echo "$response" | grep -q "HTTP/"; then
break
fi
fi
echo -n "."
sleep 3
attempt=$((attempt + 1))
done
}
importImages() {
info "开始导入核心组件镜像"
IMAGES_DIR="./w7panel/images"
if [ ! -d "$IMAGES_DIR" ]; then
return 0
fi
for IMAGE_FILE in "$IMAGES_DIR"/*.tar; do
if [ -f "$IMAGE_FILE" ]; then
k3s ctr -n=k8s.io images import "$IMAGE_FILE" >/dev/null 2>&1
if [ $? -eq 0 ]; then
info "镜像导入成功: $IMAGE_FILE"
else
info "镜像导入失败: $IMAGE_FILE"
fi
else
info "不是文件: $IMAGE_FILE"
fi
done
}
installHelmCharts() {
info 'start install helm charts'
M_PATH="/var/lib/rancher/k3s/server/manifests/"
mkdir -p $M_PATH $C_PATH
cp -r "./w7panel/manifests/." $M_PATH
}
# Install k3s
k3sInstall() {
info "current server's public network ip: $(publicNetworkIp)"
curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | K3S_KUBECONFIG_MODE='644' INSTALL_K3S_SKIP_SELINUX_RPM=true INSTALL_K3S_SELINUX_WARN=true INSTALL_K3S_MIRROR=cn INSTALL_K3S_MIRROR_URL=rancher-mirror.rancher.cn \
sh -s - --write-kubeconfig-mode 644 \
--tls-san "$(internalIP)" \
--system-default-registry "registry.cn-hangzhou.aliyuncs.com" \
--kubelet-arg="image-gc-high-threshold=70" \
--kubelet-arg="image-gc-low-threshold=60" \
--node-label "w7.public-ip=$(publicNetworkIp)" \
--embedded-registry \
--flannel-backend "none" \
--disable-network-policy \
--disable-kube-proxy \
--disable "local-storage,traefik"
}
{
checkK3SInstalled
downloadResource
etcSysctl
etcSystemd
etcPrivaterRegistry
k3sInstall
importImages
installHelmCharts
checkW7panelInstalled
echo -e "\n=================================================================="
echo -e "\033[32m内网地址: http://$(internalIP):9090\033[0m"
echo -e "\033[32m公网地址: http://$(publicNetworkIp):9090\033[0m"
echo -e "\033[32m微擎面板安装成功,请访问后台设置登录密码!\033[0m"
echo -e ""
echo -e "\033[31mwarning:\033[0m"
echo -e "\033[33m如果您的面板无访问,\033[0m"
echo -e "\033[33m请确认服务器安全组是否放通 (80|443|6443|9090) 端口\033[0m"
echo -e "=================================================================="
}