etcd集群安装卸载.md

集群安装脚本

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
# 函数:创建必要的目录结构
create_directories() {
mkdir -p "$@"
}

# 创建etcd配置文件
create_etcd_config() {
local config_file=$1
local node_name=$2
local data_dir=$3
local wal_dir=$4
local client_url=$5
local peer_url=$6
local cluster_token=$7
local cluster=$8
local new_opt=${9}

cat << EOF > "$config_file"
ETCD_OPTS="--name $node_name \\
--max-request-bytes 10485760 \\
--data-dir $data_dir \\
--wal-dir $wal_dir \\
--advertise-client-urls $client_url \\
--listen-client-urls $client_url \\
--listen-peer-urls $peer_url \\
--initial-advertise-peer-urls $peer_url \\
--initial-cluster-token ${cluster_token} \\
--initial-cluster ${cluster} \\
--initial-cluster-state ${new_opt}"
EOF
}

reload_and_start_etcd() {
systemctl daemon-reload
systemctl start etcd
}

# 函数:配置并启动etcd服务
create_etcd_service() {
local service_file=$1
local config_path=$2
local etcd_executable=$3

cat << EOF > "$service_file"
[Unit]
Description=Etcd For Kubernetes
Wants=network-online.target
After=network.target network-online.target

[Service]
EnvironmentFile=$config_path
ExecStart=$etcd_executable \$ETCD_OPTS
Restart=always

[Install]
WantedBy=multi-user.target
EOF
}

get_etcd_from_local() {
local source_path=$1
local target_dir=$2

if [ ! -f "$source_path" ]; then
echo "Source etcd binary does not exist at ${source_path}."
exit 1
fi

echo "Copying etcd binary from ${source_path} to ${target_dir}..."
if cp "$source_path" "${target_dir}/etcd"; then
chmod +x "${target_dir}/etcd"
echo "Successfully copied and prepared etcd binary."
else
echo "Failed to copy etcd binary from ${source_path}."
exit 1
fi
}

get_etcd_from_remote() {
local bin_dir=$1
local ftpserver=$2
local http_user=$3
local http_passwd=$4

echo "Downloading etcd binary from ${ftpserver}..."
if wget -P "$bin_dir" --http-user="$http_user" --http-passwd="$http_passwd" "${ftpserver}/repo/kubernetes/etcd"; then
chmod +x "${bin_dir}/etcd"
echo "Successfully downloaded and prepared etcd binary."
else
echo "Failed to download etcd binary from ${ftpserver}."
exit 1
fi
}

get_etcd() {
local option=$1
local target_dir=$2
local source_path_or_url=$3
local http_user=$4 # 仅 remote 时需要
local http_passwd=$5 # 仅 remote 时需要

case "$option" in
"local")
get_etcd_from_local "$source_path_or_url" "$target_dir"
;;
"remote")
get_etcd_from_remote "$target_dir" "$source_path_or_url" "$http_user" "$http_passwd"
;;
*)
echo "Invalid option: $option. Please choose 'local' or 'remote'."
exit 1
;;
esac
}

list_etcd_members() {
local server_url=$1

# todo --user --cert --key
curl -X POST "${server_url}/v3/cluster/member/list" \
-H "Content-Type: application/json" \
-d '{}'
}

add_etcd_member_by_curl() {
local server_url=$1
local peer_url=$2

# 构造请求体
local data=$(cat <<EOF
{
"peerURLs": ["$peer_url"]
}
EOF
)

# 发送请求
curl -X POST "${server_url}/v3/cluster/member/add" \
-H "Content-Type: application/json" \
-d "$data"
}


# 主函数:安装etcd集群节点
install_etcd_cluster_node() {
local node_name=$1
local node_ip=$2
local cluster_ips=$3
local new_opt=$4

local data_dir="/opt/k8s/data"
local wal_dir="/opt/k8s/wal"
local bin_dir="/opt/k8s/bin"
local work_dir="/opt/k8s/work"
local ftpserver="http://119.91.145.27:12800"
local http_user="team"
local http_passwd="xdmybl"
local config_file="${work_dir}/etcd.conf"
local service_file="/lib/systemd/system/etcd.service"
local etcd_executable="${bin_dir}/etcd"
local etcd_src_path="/opt/etcd"
local cluster_gs="cluster_token1"

local client_url="http://${node_ip}:2379"
local listen_peer_url="http://${node_ip}:2380"

# 创建所需目录
create_directories "$data_dir" "$wal_dir" "$bin_dir" "$work_dir"

create_etcd_config "$config_file" "$node_name" "$data_dir" "$wal_dir" "$client_url" "${listen_peer_url}" "${cluster_gs}" "${cluster_ips}" ${new_opt}

create_etcd_service "$service_file" "$config_file" "$etcd_executable"
# 下载etcd二进制文件
get_etcd "local" "$bin_dir" "$etcd_src_path"
reload_and_start_etcd
# 配置并启动etcd服务
}

extract_hosts_to_cluster_info() {
local hosts_file=$1
local cluster_info=$(awk '/^\s*[^#]/ && $2 ~ /.srhino.svc.local$/ && $2 !~ /^s/ && $2 !~ /^localhost/ {sub(/\.srhino\.svc\.local$/, "", $2); print $2"=http://"$1":2380"}' ${hosts_file} | paste -sd,)
echo $cluster_info
}

安装三节点(建立三节点),适用于创建集群时,直接声明集群三节点

时序图

sequenceDiagram
    participant Manager as Manager
    participant Node1 as node1
    participant Node2 as node2
    participant Node3 as node3

    par 建立新集群
        Manager->>Node1: new cluster: node1, node2, node3
        Manager->>Node2: new cluster: node1, node2, node3
        Manager->>Node3: new cluster: node1, node2, node3
    and 相应节点响应
        Node1-->>Manager: 响应
        Node2-->>Manager: 响应
        Node3-->>Manager: 响应
    end
    Note over Manager,Node3: etcd 初始集群建立完成


1
2
3
4
5
6
7
8
#!/bin/bash

# 并发执行三节点安装
ssh -n root@192.168.122.26 "cd /opt && ./install_cluster.sh" &
ssh -n root@192.168.122.35 "cd /opt && ./install_cluster.sh" &
ssh -n root@192.168.122.238 "cd /opt && ./install_cluster.sh" &

wait
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash

source ./install_cluster_lib.sh
# first node param
#local_ip="ouryun.node2.com"
local_ip="192.168.122.26"
local_host="node1"
# 三节点都需要是 new
new_opt="new"
# 这里包含集群信息
cluster_info="node1=http://192.168.122.26:2380,node2=http://192.168.122.35:2380,node3=http://192.168.122.237:2380"

# 使用示例
# install_etcd_cluster_node "node1" "192.168.1.1" "node1=http://192.168.1.1:2380,node2=http://192.168.1.2:2380,node3=http://192.168.1.3:2380"
install_etcd_cluster_node $local_host $local_ip "${cluster_info}" ${new_opt}

安装三节点(先创建一个节点的集群,再加入 node2 ,再加入 node3),适用于往已有集群加入新节点

时序图

sequenceDiagram
	participant manager as Manager
    participant Node1 as node1
    participant Node2 as node2
    participant Node3 as node3


	manager->>Node1: 建立集群
	activate Node1
    Node1->>Node1: new cluster node1
    Node1-->>manager: 
    deactivate Node1
    
    manager->>Node2: 新增节点2
    activate Node2
    Note over Node1,Node2: 环境变量中 cluster: node1,node2
    Node2->>Node1: member add node2
    Node1-->>Node2: 
    Node2->>Node2: node2 start (existing cluster, cluster: node1,node2)
    Node2-->>manager: 
    deactivate Node2
    
    manager->>Node3: 新增节点3
    activate Node3
    Note over Node1,Node3: 环境变量中 cluster: node1,node2,node3
    alt request endpoints
    	Node3->>Node1: member add node3
    	Node1-->>Node3: 
    else
    	Node3->>Node2: member add node3
    	Node2-->>Node3: 
    end
    Node3->>Node3: node3 start(existing cluster, cluster: node1,node2,node3)
    Node3-->>manager: 响应
	deactivate Node3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# install_etcd_first.sh
#!/bin/bash

source ./install_cluster_lib.sh
# first node param
local_ip="192.168.122.26"
local_host="node1"
new_opt="new"
# 这里包含集群信息
existing_node_info=""

# 使用示例
# install_etcd_cluster_node "node1" "192.168.1.1" "node1=http://192.168.1.1:2380,node2=http://192.168.1.2:2380,node3=http://192.168.1.3:2380"

install_etcd_cluster_node $local_host $local_ip "${local_host}=http://${local_ip}:2380" ${new_opt}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# install_etcd_other.sh

# other node param

source ./install_cluster_lib.sh

local_ip="192.168.122.35"
local_host="node2"
new_opt="existing"
existing_node_info="node1=http://192.168.122.26:2380"

# 使用示例
# install_etcd_cluster_node "node1" "192.168.1.1" "node1=http://192.168.1.1:2380,node2=http://192.168.1.2:2380,node3=http://192.168.1.3:2380"
install_etcd_cluster_node $local_host $local_ip "${existing_node_info},${local_host}=http://${local_ip}:2380" ${new_opt}

新增集群节点

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
# member_add_by_etcdctl.sh
# 新增集群
member_add_by_etcdctl() {
local new_member_name=$1
local new_member_peer_url=$2
local existing_member_client_urls=$3

# 设置ETCDCTL_API版本,确保使用v3 API
export ETCDCTL_API=3

# 添加新成员到集群
etcdctl --endpoints="${existing_member_client_urls}" \
member add "${new_member_name}" \
--peer-urls="${new_member_peer_url}"

# 检查命令执行的退出状态
if [ $? -eq 0 ]; then
echo "Member ${new_member_name} successfully added to the cluster."
else
echo "Failed to add member ${new_member_name} to the cluster."
exit 1
fi
}

# member_id="node2"
# member_url="http://192.168.122.35:2380"
# access_endpoint_url="http://192.168.122.26:2379"

# # 新增集群成员
# member_add "node2" "http://192.168.122.35:2380" "http://192.168.122.26:2379"
member_id="node3"
member_url="http://192.168.122.237:2380"
access_endpoint_url="http://192.168.122.26:2379"

# 新增集群成员
member_add_by_etcdctl ${member_id} ${member_url} ${access_endpoint_url}

卸载 etcd

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
# uninstall_etcd.sh

#!/bin/bash

# 停止etcd服务
stop_etcd_service() {
echo "Stopping etcd service..."
systemctl stop etcd
}

# 禁用etcd服务
disable_etcd_service() {
echo "Disabling etcd service..."
systemctl disable etcd
}

# 删除etcd服务文件
remove_etcd_service_file() {
echo "Removing etcd service file..."
rm -f /lib/systemd/system/etcd.service
}

# 重新加载systemd守护进程
reload_systemd() {
echo "Reloading systemd daemon..."
systemctl daemon-reload
}

# 删除etcd二进制文件和数据目录
cleanup_etcd_files() {
local bin_dir="/opt/k8s/bin"
local data_dir="/opt/k8s/data"
local wal_dir="/opt/k8s/wal"
local work_dir="/opt/k8s/work"

echo "Cleaning up etcd binary and data directories..."
rm -f "${bin_dir}/etcd"
rm -rf "$data_dir"
rm -rf "$wal_dir"
rm -rf "$work_dir"
}

# 卸载etcd的主函数
uninstall_etcd() {
stop_etcd_service
disable_etcd_service
remove_etcd_service_file
reload_systemd
cleanup_etcd_files

echo "etcd has been successfully uninstalled."
}

# 执行卸载
uninstall_etcd


etcd集群安装卸载.md
https://abrance.github.io/2024/03/27/domain/k8s/etcd集群安装卸载/
Author
xiaoy
Posted on
March 27, 2024
Licensed under