forked from charlesqing/LinuxShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaliyun_sealos_k8s.sh
111 lines (92 loc) · 3.67 KB
/
aliyun_sealos_k8s.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
#!/bin/bash
# sh aliyun_sealos_k8s.sh deploy 1.18.2 https://sealyun.oss-cn-beijing.aliyuncs.com/9a8299ea8016abe32e1564a44d5162e4-1.18.2/kube1.18.2.tar.gz
RegionId=cn-hongkong
ImageId=centos_7_7_x64_20G_alibase_20191225.vhd
InstanceType=ecs.c5.large
Password=Aliyun#123
SecurityGroupId=sg-j6c8mr535xgiug4hlfpz
VSwitchId=vsw-j6cwuu2i3u9nb5d20i4sz
ZoneId= cn-hongkong-b
command_exists() {
command -v "$@" > /dev/null 2>&1
}
if ! command_exists jq; then
yum install -y jq
fi
create_instance() {
aliyun ecs RunInstances --Amount 4 \
--ImageId centos_7_7_x64_20G_alibase_20191225.vhd \
--InstanceType ecs.c5.large \
--InternetChargeType PayByTraffic \
--InternetMaxBandwidthIn 50 \
--InternetMaxBandwidthOut 50 \
--Password Aliyun#123 \
--InstanceChargeType PostPaid \
--SpotStrategy SpotAsPriceGo \
--RegionId cn-hongkong \
--SecurityGroupId sg-j6c8mr535xgiug4hlfpz \
--VSwitchId vsw-j6cwuu2i3u9nb5d20i4sz \
--ZoneId cn-hongkong-b > InstanceId.json
ID0=$(jq -r ".InstanceIdSets.InstanceIdSet[0]" < InstanceId.json)
ID1=$(jq -r ".InstanceIdSets.InstanceIdSet[1]" < InstanceId.json)
ID2=$(jq -r ".InstanceIdSets.InstanceIdSet[2]" < InstanceId.json)
ID3=$(jq -r ".InstanceIdSets.InstanceIdSet[3]" < InstanceId.json)
echo "sleep 40s wait for IP and FIP"
sleep 40 # wait for IP
aliyun ecs DescribeInstanceAttribute --InstanceId $ID0 > info.json
master0=$(jq -r ".VpcAttributes.PrivateIpAddress.IpAddress[0]" < info.json)
master0FIP=$(jq -r ".PublicIpAddress.IpAddress[0]" < info.json)
aliyun ecs DescribeInstanceAttribute --InstanceId $ID1 > info.json
master1=$(jq -r ".VpcAttributes.PrivateIpAddress.IpAddress[0]" < info.json)
aliyun ecs DescribeInstanceAttribute --InstanceId $ID2 > info.json
master2=$(jq -r ".VpcAttributes.PrivateIpAddress.IpAddress[0]" < info.json)
aliyun ecs DescribeInstanceAttribute --InstanceId $ID3 > info.json
node=$(jq -r ".VpcAttributes.PrivateIpAddress.IpAddress[0]" < info.json)
echo "all nodes IP: $master0 $master1 $master2 $node"
echo "wait for sshd start"
sleep 100 # wait for sshd
}
deploy_k8s(){
# $2 is sealos clientip
alias remotecmd="aliyun ecs RunCommand --InstanceId.1 $ID0 --RegionId cn-hongkong --Type RunShellScript --CommandContent"
echo "install sealos command"
remotecmd "wget -c https://sealyun.oss-cn-beijing.aliyuncs.com/latest/sealos && chmod +x sealos && mv sealos /usr/bin"
echo "sleep 100s wait for sealos command"
sleep 100
version=$2
pkgurl=$3
echo "run sealos init command"
remotecmd "sealos init --master $master0 --master $master1 --master $master2 --node $node --passwd Aliyun#123 --version v$version --pkg-url $pkgurl | tee sealos_init.log"
echo "wait for everything ok"
sleep 40
remotecmd "kubectl get node && kubectl get pod --all-namespaces && kubectl get cs"
#./sshcmd --passwd Aliyun#123 --host $master0FIP --cmd "kubectl get node && kubectl get pod --all-namespaces && kubectl get cs"
}
release_instance() {
echo "release instance"
sleep 20
aliyun ecs DeleteInstances --InstanceId.1 $ID0 --RegionId cn-hongkong --Force true
aliyun ecs DeleteInstances --InstanceId.1 $ID1 --RegionId cn-hongkong --Force true
aliyun ecs DeleteInstances --InstanceId.1 $ID2 --RegionId cn-hongkong --Force true
aliyun ecs DeleteInstances --InstanceId.1 $ID3 --RegionId cn-hongkong --Force true
}
shift "$((OPTIND -1))"
subcommand=$1
case "$subcommand" in
create)
create_instance
exit 0
shift
;;
delete)
release_instance
exit 0
shift
;;
deploy)
create_instance
deploy_k8s
exit 0
shift
;;
esac