-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
365 lines (334 loc) · 10.3 KB
/
build.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/bin/sh
# 接收传入参数
REGION=$1
USERNAME=$2
PASSWD=$3
CLUSTER_NAME=$4
IP=$5
SP=$6
PORT_RAND=$7
UUID=$8
SS_PWD=$9
BBR=$10
DOMAIN=$11
EMAIL=$12
WEBSOCKET_PATH=$13
SS_WEBSOCKET_PATH=$14
KUBECTLVER=$15
IBMCR_DOMAIN=$16
# 安装 IBM Cloud CLI
echo -e '\nDownload IBM Cloud CLI ...'
curl -Lo IBM_Cloud_CLI_amd64.tar.gz https://clis.cloud.ibm.com/download/bluemix-cli/latest/linux64
echo -e '\nInstall IBM Cloud CLI ...'
tar -zxf IBM_Cloud_CLI_amd64.tar.gz
cd Bluemix_CLI
sh ./install_bluemix_cli
ibmcloud config --usage-stats-collect false
#登录到 IBM Cloud CLI
ibmcloud login -a https://cloud.ibm.com -r $REGION -u $USERNAME -p $PASSWD
(echo 1; echo 1) | ibmcloud target --cf #Target Cloud Foundry org/space.
# 安装 IBM Cloud CLI 插件
echo -e '\nInstall IBM Cloud CLI plugins ...'
ibmcloud plugin install container-service -r Bluemix
ibmcloud plugin install container-registry -r Bluemix
ibmcloud ks init
# 安装 kubectl
echo -e '\nDownload kubectl ...'
curl -LO https://storage.googleapis.com/kubernetes-release/release/${KUBECTLVER}/bin/linux/amd64/kubectl
echo -e '\nInstall kubectl ...'
chmod +x ./kubectl
mv ./kubectl /usr/local/bin/kubectl
echo
# 将 IBM Cloud CLI 配置为运行 kubectl
echo -e '\nConfigurate IBM Cloud CLI to run kubectl ...'
$(ibmcloud ks cluster config $CLUSTER_NAME --export -s)
echo -e '\nKubectl version:'
echo
kubectl version --short
# 启动 Docker
echo -e '\nStart Docker ...'
dockerd >/dev/null 2>&1 &
sleep 3
docker -v
# 初始化 IBM 容器(映像)注册表
echo -e '\nInitiate IBM Cloud container registry ...'
ibmcloud cr login
for name in $(ibmcloud cr namespace-list | grep del_); do (echo y) | ibmcloud cr namespace-rm $name; done
NS=del_$(openssl rand -base64 16 | md5sum | head -c16)
ibmcloud cr namespace-add $NS
# 准备 V2RAY 文件,配置 V2RAY 端口
mkdir -p /root/v2ray
cd /root/v2ray/
echo -e '\nDownload V2Ray ...'
curl -Lo v2ray.zip https://github.com/v2ray/dist/raw/master/v2ray-linux-64.zip
unzip -q v2ray.zip
chmod +x ./v2ray ./v2ctl
echo -e "\nConfigure the Inbound PORT of V2Ray server ..."
let V2RAY_PORT=$PORT_RAND+30000
# 构建与注册容器映像 V2RAY
echo -e '\nBuild and register the container image of "V2Ray" for VMess server ...'
echo -e "\nDownload V2Ray's config.json of VMess server ..."
curl -Lo config.json https://raw.githubusercontent.com/jogolor/V2RoIBMCKS/master/config-vmess.json
sed -i "s/{V2RAY_PORT}/$V2RAY_PORT/g" config.json
sed -i "s/{UUID_PWD}/$UUID/g" config.json
sed -i "s/{WEBSOCKET_PATH}/$WEBSOCKET_PATH/g" config.json
cat << _EOF_ > Dockerfile
FROM alpine:latest
RUN apk add --update --no-cache ca-certificates
ADD v2ray /usr/local/bin/
ADD v2ctl /usr/local/bin/
ADD geoip.dat /usr/local/bin/
ADD geosite.dat /usr/local/bin/
RUN mkdir /etc/v2ray
ADD config.json /etc/v2ray/
RUN mkdir /var/log/v2ray
CMD ["v2ray", "-config=/etc/v2ray/config.json"]
_EOF_
echo -e '\nBuild and push the container image of "V2Ray" ...'
ibmcloud cr build -t ${IBMCR_DOMAIN}/${NS}/v2ray .
# 创建容器映像 V2RAY 的部署文件
echo -e '\nCreate the deployment file "v2ray.yaml" for the container image of "V2Ray" ...'
echo 'The image is from IBM Cloud container registry.'
cat << _EOF_ > v2ray.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: v2ray
labels:
app: v2ray
spec:
replicas: 1
selector:
matchLabels:
app: v2ray
template:
metadata:
name: v2ray
labels:
app: v2ray
spec:
containers:
- name: v2ray
image: ${IBMCR_DOMAIN}/${NS}/v2ray
ports:
- containerPort: $V2RAY_PORT
protocol: TCP
- containerPort: $V2RAY_PORT
protocol: UDP
resources:
requests:
memory: "128Mi"
cpu: "150m"
limits:
memory: "256Mi"
cpu: "300m"
restartPolicy: Always
_EOF_
# 部署容器映像 V2RAY 到 IBM Cloud Kubernetes Service,获取 V2RAY_IP
echo -e '\nDeploy the container images of "V2Ray" to IBM Cloud Kubernetes Service ...'
kubectl create -f v2ray.yaml
kubectl expose deployment v2ray --name=v2ray-tcp-for-caddy --port=$V2RAY_PORT --protocol="TCP"
V2RAY_IP=$(kubectl get svc v2ray-tcp-for-caddy -o=custom-columns=IP:.spec.clusterIP | tail -n1)
# 构建与注册容器映像 V2RAY-SS
rm -f ./config.json # 清理容器映像 V2RAY 的 config.json
echo -e '\nBuild and register the container image of "V2Ray-SS" for Shadowsocks server ...'
echo -e "\nDownload V2Ray's config.json of Shadowsocks server ..."
curl -Lo config.json https://raw.githubusercontent.com/jogolor/V2RoIBMCKS/master/config-shadowsocks.json
sed -i "s/{V2RAY_PORT}/$V2RAY_PORT/g" config.json
sed -i "s/{UUID_PWD}/$SS_PWD/g" config.json
sed -i "s/{WEBSOCKET_PATH}/$SS_WEBSOCKET_PATH/g" config.json
echo -e '\nBuild and push the container image of "V2Ray-SS" ...'
ibmcloud cr build -t ${IBMCR_DOMAIN}/${NS}/v2ray-ss . # 复用容器映像 V2RAY 的 Dockerfile
# 创建容器映像 V2RAY-SS 的部署文件
echo -e '\nCreate the deployment file "v2ray-ss.yaml" for the container image of "V2Ray-SS" ...'
echo 'The image is from IBM Cloud container registry.'
cat << _EOF_ > v2ray-ss.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: v2ray-ss
labels:
app: v2ray-ss
spec:
replicas: 1
selector:
matchLabels:
app: v2ray-ss
template:
metadata:
name: v2ray-ss
labels:
app: v2ray-ss
spec:
containers:
- name: v2ray-ss
image: ${IBMCR_DOMAIN}/${NS}/v2ray-ss
ports:
- containerPort: $V2RAY_PORT
protocol: TCP
- containerPort: $V2RAY_PORT
protocol: UDP
resources:
requests:
memory: "128Mi"
cpu: "150m"
limits:
memory: "256Mi"
cpu: "300m"
restartPolicy: Always
_EOF_
# 部署容器映像 V2RAY-SS 到 IBM Cloud Kubernetes Service,获取 V2RAY_SS_IP
echo -e '\nDeploy the container images of "V2Ray-SS" to IBM Cloud Kubernetes Service ...'
kubectl create -f v2ray-ss.yaml
kubectl expose deployment v2ray-ss --name=v2ray-ss-tcp-for-caddy --port=$V2RAY_PORT --protocol="TCP"
V2RAY_SS_IP=$(kubectl get svc v2ray-ss-tcp-for-caddy -o=custom-columns=IP:.spec.clusterIP | tail -n1)
# 构建与注册容器映像 CADDY
echo -e '\nBuild and register the container image of "Caddy" ...'
mkdir -p /root/caddy
cd /root/caddy/
echo -e '\nDownload Caddy ...'
curl -Lo caddy.tar.gz https://caddyserver.com/download/linux/amd64?license=personal
tar -zxf caddy.tar.gz
chmod +x ./caddy
cat << _EOF_ > Caddyfile
https://${DOMAIN} {
log /var/log/caddy/access.log
errors /var/log/caddy/error.log
tls ${EMAIL}
timeouts {
read 60s
write 30s
}
root /var/www
proxy /${WEBSOCKET_PATH} ${V2RAY_IP}:${V2RAY_PORT} {
websocket
header_upstream -Origin
}
proxy /${SS_WEBSOCKET_PATH} ${V2RAY_SS_IP}:${V2RAY_PORT} {
websocket
header_upstream -Origin
}
}
_EOF_
cat << _EOF_ > index.html
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Being maintained</title>
</head>
<body style="background-color: #C2C2C2; color: #000000">
<p style="padding: 15% 0 0 25%; font-size: 20px">
This site is being maintained. It may take a while or a longer time.
</p>
</body>
</html>
_EOF_
cat << _EOF_ > Dockerfile
FROM alpine:latest
RUN apk add --update --no-cache openssh-client ca-certificates
ADD caddy /usr/local/bin/
RUN mkdir /etc/caddy
ADD Caddyfile /etc/caddy/
RUN mkdir /var/www
ADD index.html /var/www/
RUN mkdir /var/log/caddy
CMD ["caddy", "-conf=/etc/caddy/Caddyfile", "-agree"]
_EOF_
echo -e '\nBuild and push the container image of "Caddy" ...'
ibmcloud cr build -t ${IBMCR_DOMAIN}/${NS}/caddy .
# 创建容器映像 CADDY 的部署文件
echo -e '\nCreate the deployment file "caddy.yaml" for the container image of "Caddy" ...'
echo 'The image is from IBM Cloud container registry.'
cat << _EOF_ > caddy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: caddy
labels:
app: caddy
spec:
replicas: 1
selector:
matchLabels:
app: caddy
template:
metadata:
name: caddy
labels:
app: caddy
spec:
containers:
- name: caddy
image: ${IBMCR_DOMAIN}/${NS}/caddy
ports:
- containerPort: $SP
protocol: TCP
- containerPort: $SP
protocol: UDP
resources:
requests:
memory: "256Mi"
cpu: "300m"
limits:
memory: "512Mi"
cpu: "500m"
restartPolicy: Always
_EOF_
# 创建容器映像 BBR 的部署文件
echo -e '\nCreate the deployment file "bbr.yaml" for the container image of "BBR" ...'
echo 'The image is from hub.docker.com.'
cat << _EOF_ > bbr.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: bbr
labels:
app: bbr
spec:
replicas: 1
selector:
matchLabels:
app: bbr
template:
metadata:
name: bbr
labels:
app: bbr
spec:
containers:
- name: bbr
image: wuqz/lkl:latest
env:
- name: TARGET_HOST
value: TARGET_IP
- name: TARGET_PORT
value: "$SP"
- name: BIND_PORT
value: "$SP"
ports:
- containerPort: $SP
protocol: TCP
resources:
requests:
memory: "256Mi"
cpu: "300m"
limits:
memory: "512Mi"
cpu: "500m"
securityContext:
privileged: true
restartPolicy: Always
_EOF_
# 部署容器映像 CADDY & BBR 到 IBM Cloud Kubernetes Service
echo -e '\nDeploy the container image of "Caddy" to IBM Cloud Kubernetes Service ...'
kubectl create -f caddy.yaml
if $BBR; then
kubectl expose deployment caddy --name=caddy-tcp-for-bbr --port=$SP --protocol="TCP"
sed -i "s/TARGET_IP/$(kubectl get svc caddy-tcp-for-bbr -o=custom-columns=IP:.spec.clusterIP | tail -n1)/g" bbr.yaml
echo -e '\nDeploy the container image of "BBR" to IBM Cloud Kubernetes Service ...'
kubectl create -f bbr.yaml
kubectl expose deployment bbr --type=LoadBalancer --name=bbr-tcp --port=$SP --external-ip $IP --protocol="TCP"
else
kubectl expose deployment caddy --type=LoadBalancer --name=caddy-tcp --port=$SP --external-ip $IP --protocol="TCP"
fi
kubectl expose deployment caddy --type=LoadBalancer --name=caddy-udp --port=$SP --external-ip $IP --protocol="UDP"