-
Notifications
You must be signed in to change notification settings - Fork 327
/
keepalived.go
254 lines (223 loc) · 8.31 KB
/
keepalived.go
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
/*
* Tencent is pleased to support the open source community by making TKEStack
* available.
*
* Copyright (C) 2012-2019 Tencent. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* https://opensource.org/licenses/Apache-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package keepalived
import (
"bytes"
"fmt"
"net"
"strings"
"github.com/pkg/errors"
"tkestack.io/tke/pkg/platform/provider/baremetal/constants"
"tkestack.io/tke/pkg/platform/provider/baremetal/images"
"tkestack.io/tke/pkg/util/log"
"tkestack.io/tke/pkg/util/ssh"
"tkestack.io/tke/pkg/util/template"
)
type Option struct {
IP string
VIP string
VRID *int32
LoadBalance bool
IPVS bool
KubernetesSvcIP string
}
var svcPortChain = "KUBE-SVC-NPX46M4PTMTKRN6Y"
// vrid gets vrid from last byte of vip and plus 1 to prevent from vip ends with zero,
// bcs vrid ranges from 1 to 255. if vip ends with 255, then throw error.
func vrid(vip string, vrid *int32) (int, error) {
if vrid != nil {
if *vrid > 0 && *vrid < 256 {
return int(*vrid), nil
}
}
var lastbyte = net.ParseIP(vip).To16()[net.IPv6len-1]
if lastbyte >= 255 {
return 1, fmt.Errorf("unusual vip :%s", vip)
}
return int(net.ParseIP(vip).To16()[net.IPv6len-1]) + 1, nil
}
func Install(s ssh.Interface, option *Option) error {
networkInterface := ssh.GetNetworkInterface(s, option.IP)
if networkInterface == "" {
return fmt.Errorf("can't get network interface by %s", option.IP)
}
vrid, err := vrid(option.VIP, option.VRID)
if err != nil {
return err
}
data, err := template.ParseFile(constants.ManifestsDir+"keepalived/keepalived.conf", map[string]interface{}{
"Interface": networkInterface,
"VRID": vrid,
"VIP": option.VIP,
})
if err != nil {
return errors.Wrap(err, option.IP)
}
err = s.WriteFile(bytes.NewReader(data), constants.KeepavliedConfigFile)
if err != nil {
return errors.Wrap(err, option.IP)
}
data, err = template.ParseFile(constants.ManifestsDir+"keepalived/keepalived.yaml", map[string]interface{}{
"Image": images.Get().Keepalived.FullName(),
})
if err != nil {
return errors.Wrap(err, option.IP)
}
err = s.WriteFile(bytes.NewReader(data), constants.KeepavlivedManifestFile)
if err != nil {
return errors.Wrap(err, option.IP)
}
if option.LoadBalance {
err := installLoadBalanceIfKubeProxyRunning(s, option)
if err != nil {
return errors.Wrap(err, option.IP)
}
}
return nil
}
// clearLoadbalanceRuleInIpvsMode delete all ipvs mode relative iptables rules
func clearLoadbalanceRuleInIpvsMode(s ssh.Interface, vip string, chain string, kubernetesSvcIP string) {
for {
cmd := fmt.Sprintf("iptables -w 30 -t nat -D %s -d %s -p tcp --dport 6443 -j DNAT --to-destination %s:443", chain, vip, kubernetesSvcIP)
_, err := s.CombinedOutput(cmd)
log.Info(fmt.Sprintf("delete iptables %s err:%s", cmd, err))
if err != nil {
break
}
}
for {
cmd := fmt.Sprintf("iptables -w 30 -t nat -D %s -d %s -p tcp --dport 6443 -j KUBE-MARK-MASQ", chain, vip)
_, err := s.CombinedOutput(cmd)
log.Info(fmt.Sprintf("delete iptables %s err:%s", cmd, err))
if err != nil {
break
}
}
}
// clearLoadbalanceRuleInIptablesMode delete all iptables mode relative iptables rules
func clearLoadbalanceRuleInIptablesMode(s ssh.Interface, vip string, chain string) {
for {
cmd := fmt.Sprintf("iptables -w 30 -t nat -D %s -d %s -p tcp --dport 6443 -j %s", chain, vip, svcPortChain)
_, err := s.CombinedOutput(cmd)
log.Info(fmt.Sprintf("delete iptables %s err:%s", cmd, err))
if err != nil {
break
}
}
for {
cmd := fmt.Sprintf("iptables -w 30 -t nat -D %s -d %s -p tcp --dport 6443 -j KUBE-MARK-MASQ", chain, vip)
_, err := s.CombinedOutput(cmd)
log.Info(fmt.Sprintf("delete iptables %s err:%s", cmd, err))
if err != nil {
break
}
}
}
func ClearLoadBalance(s ssh.Interface, vip string, kubernetesSvcIP string) {
chains := []string{"PREROUTING", "OUTPUT"}
for _, chain := range chains {
clearLoadbalanceRuleInIpvsMode(s, vip, chain, kubernetesSvcIP)
clearLoadbalanceRuleInIptablesMode(s, vip, chain)
}
}
// installLoadBalanceInIpvsModeIfKubeProxyRunning dnat vip:vport to kubernetes svc cluster ip:port and mark the request.
// For more information, see the proposal: https://github.com/tkestack/tke/blob/master/docs/design-proposals/controlplane-ha-loadbalance.md
func installLoadBalanceInIpvsModeIfKubeProxyRunning(s ssh.Interface, option *Option, chain string) error {
cmd := fmt.Sprintf("ip a | grep %s", option.KubernetesSvcIP)
stdout, _, exit, err := s.Exec(cmd)
if err != nil || exit != 0 {
return fmt.Errorf("exec %q failed:exit %d:error %s:stdout %s", cmd, exit, err, stdout)
}
if !strings.Contains(stdout, option.KubernetesSvcIP) {
return nil
}
cmd = fmt.Sprintf("iptables -w 30 -t nat -C %s -d %s -p tcp --dport 6443 -j DNAT --to-destination %s:443",
chain, option.VIP, option.KubernetesSvcIP)
_, err = s.CombinedOutput(cmd)
if err != nil && strings.Contains(err.Error(), "rule exist") {
cmd := fmt.Sprintf("iptables -w 30 -t nat -I %s -d %s -p tcp --dport 6443 -j DNAT --to-destination %s:443",
chain, option.VIP, option.KubernetesSvcIP)
_, err = s.CombinedOutput(cmd)
if err != nil {
return fmt.Errorf("run cmd(%s) error:%s", cmd, err)
}
}
cmd = fmt.Sprintf("iptables -w 30 -t nat -C %s -d %s -p tcp --dport 6443 -j KUBE-MARK-MASQ",
chain, option.VIP)
_, err = s.CombinedOutput(cmd)
if err != nil && strings.Contains(err.Error(), "rule exist") {
cmd = fmt.Sprintf("iptables -w 30 -t nat -I %s -d %s -p tcp --dport 6443 -j KUBE-MARK-MASQ",
chain, option.VIP)
_, err = s.CombinedOutput(cmd)
if err != nil {
return fmt.Errorf("run cmd(%s) error:%s", cmd, err)
}
}
log.Info("loadblance on ipvs mode created success.", log.String("node", option.IP), log.String("chain", chain))
return nil
}
// installLoadBalanceInIptableModeIfKubeProxyRunning dnat vip:vport to kubernetes service chain and mark the request.
// kubernetes service chain generated by service name: kubernetes and port: 443 using hash alg
// For more information, see the proposal: https://github.com/tkestack/tke/blob/master/docs/design-proposals/controlplane-ha-loadbalance.md
func installLoadBalanceInIptableModeIfKubeProxyRunning(s ssh.Interface, option *Option, chain string) error {
cmd := fmt.Sprintf("iptables -w 30 -t nat -nxL %s", svcPortChain)
stdout, _, exit, err := s.Exec(cmd)
if err != nil || exit != 0 {
return fmt.Errorf("exec %q failed:exit %d:error %s:stdout %s", cmd, exit, err, stdout)
}
if !strings.Contains(stdout, svcPortChain) {
return nil
}
cmd = fmt.Sprintf("iptables -w 30 -t nat -C %s -d %s -p tcp --dport 6443 -j %s", chain, option.VIP, svcPortChain)
_, err = s.CombinedOutput(cmd)
if err != nil && strings.Contains(err.Error(), "rule exist") {
cmd = fmt.Sprintf("iptables -t nat -I %s -d %s -p tcp --dport 6443 -j %s", chain, option.VIP, svcPortChain)
_, err = s.CombinedOutput(cmd)
if err != nil {
return fmt.Errorf("run cmd(%s) error:%s", cmd, err)
}
}
cmd = fmt.Sprintf("iptables -w 30 -t nat -C %s -d %s -p tcp --dport 6443 -j KUBE-MARK-MASQ", chain, option.VIP)
_, err = s.CombinedOutput(cmd)
if err != nil && strings.Contains(err.Error(), "rule exist") {
cmd = fmt.Sprintf("iptables -w 30 -t nat -I %s -d %s -p tcp --dport 6443 -j KUBE-MARK-MASQ", chain, option.VIP)
_, err = s.CombinedOutput(cmd)
if err != nil {
return fmt.Errorf("run cmd(%s) error:%s", cmd, err)
}
}
log.Info("loadblance on iptable mode created success.", log.String("node", option.IP), log.String("chain", chain))
return nil
}
func installLoadBalanceIfKubeProxyRunning(s ssh.Interface, option *Option) error {
chains := []string{"PREROUTING", "OUTPUT"}
for _, chain := range chains {
if option.IPVS {
err := installLoadBalanceInIpvsModeIfKubeProxyRunning(s, option, chain)
if err != nil {
return err
}
} else {
err := installLoadBalanceInIptableModeIfKubeProxyRunning(s, option, chain)
if err != nil {
return err
}
}
}
return nil
}