Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Modify the worker-join process hang when creating a cluster by applying Kilo-network-cni. #121

Merged
merged 1 commit into from
Dec 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 51 additions & 83 deletions src/core/model/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import (
)

const (
VM_USER_ACCOUNT = "cb-user"
VM_USER_ACCOUNT = "cb-user"
REMOTE_TARGET_PATH = "/tmp"
CNI_CANAL_FILE = "addons/canal/canal_v3.20.0.yaml"
CNI_KILO_CRDS_FILE = "addons/kilo/crds_v0.3.0.yaml"
CNI_KILO_KUBEADM_FILE = "addons/kilo/kilo-kubeadm-flannel_v0.3.0.yaml"
CNI_KILO_FLANNEL_FILE = "addons/kilo/kube-flannel_v0.14.0.yaml"
)

type VM struct {
Expand Down Expand Up @@ -59,10 +64,6 @@ type VMDetail struct {
VMSpecName string
}

const (
remoteTargetPath = "/tmp"
)

func (self *VM) SSHRun(format string, a ...interface{}) (string, error) {

address := fmt.Sprintf("%s:22", self.PublicIP)
Expand All @@ -75,9 +76,9 @@ func (self *VM) SSHRun(format string, a ...interface{}) (string, error) {
ServerPort: address,
}, command)
if err != nil {
logger.Errorf("[%s] failed to run SSH command (server=%s, cause=%v) \ncommand=%s", self.Name, address, err, command)
logger.Errorf("[%s] failed to run SSH command (server=%s, cause=%v, command=%s, output=%s)", self.Name, address, err, command, output)
} else {
logger.Infof("[%s] ssh execute is completed (server=%s) \ncommand=%s \noutput=%s", self.Name, address, command, output)
logger.Infof("[%s] ssh execute is completed (server=%s, command=%s)", self.Name, address, command)
}
return output, err
}
Expand Down Expand Up @@ -119,15 +120,6 @@ func (self *VM) checkConnectivity() error {
return errors.New(fmt.Sprintf("Check connectivity failed. (vm=%s, cause=connection is nil)", self.Name))
}

func (self *VM) createAddonsDirectory(networkCni string) error {

addonsPath := fmt.Sprintf("%s/addons/%s", remoteTargetPath, networkCni)
if _, err := self.SSHRun("mkdir -p %s", addonsPath); err != nil {
return errors.New(fmt.Sprintf("Failed to create a addon directory. (node=%s, path=%s)", self.Name, addonsPath))
}
return nil
}

func (self *VM) CheckConnectSSH() error {
if _, err := self.SSHRun("/bin/hostname"); err != nil {
return errors.New(fmt.Sprintf("Failed to check connect VM. (vm=%s)", self.Name))
Expand Down Expand Up @@ -157,78 +149,54 @@ func (self *VM) ConnectionTest() error {
return nil
}

func (self *VM) CopyScripts(networkCni string) error {
/* bootstrap */
func (self *VM) Bootstrap(networkCni string) error {

logger.Infof("[%s] start the process of 'copy script files'", self.Name)
logger.Infof("[%s] Start copying files. of 'bootstrap'", self.Name)

// 1. copy files
// - list-up copy bootstrap files
sourcePath := fmt.Sprintf("%s/src/scripts", *config.Config.AppRootPath)
sourceFiles := []string{config.BOOTSTRAP_FILE, config.SYSTEMD_SERVICE_FILE}
if self.Role == config.CONTROL_PLANE && self.IsCPLeader {
sourceFiles = append(sourceFiles, config.INIT_FILE)
sourceFiles = append(sourceFiles, config.HA_PROXY_FILE)
sourceFiles := []string{"bootstrap.sh"}

if err := self.createAddonsDirectory(networkCni); err != nil {
return err
// - list-up for leader-node
if self.Role == config.CONTROL_PLANE && self.IsCPLeader {
sourceFiles = append(sourceFiles, "haproxy.sh", "k8s-init.sh")
if _, err := self.SSHRun("mkdir -p %s/addons/%s", REMOTE_TARGET_PATH, networkCni); err != nil {
return errors.New(fmt.Sprintf("Failed to create a addon directory. (node=%s, path=%s)", self.Name, "addons/"+networkCni))
}
if networkCni == config.NETWORKCNI_CANAL {
sourceFiles = append(sourceFiles, CNI_CANAL_FILE)
} else {
sourceFiles = append(sourceFiles, CNI_KILO_CRDS_FILE, CNI_KILO_KUBEADM_FILE, CNI_KILO_FLANNEL_FILE)
}
cniFiles := getCniFiles(networkCni)
sourceFiles = append(sourceFiles, cniFiles...)
}
if networkCni == config.NETWORKCNI_CANAL {
sourceFiles = append(sourceFiles, config.MCKS_BOOTSTRAP_CANAL_FILE)
} else {
sourceFiles = append(sourceFiles, config.MCKS_BOOTSTRAP_KILO_FILE)
}

// - copy list-up files
logger.Infof("[%s] Start copying files. (files=%v)", self.Name, sourceFiles)
for _, f := range sourceFiles {
src := fmt.Sprintf("%s/%s", sourcePath, f)
dest := fmt.Sprintf("%s/%s", remoteTargetPath, f)
dest := fmt.Sprintf("%s/%s", REMOTE_TARGET_PATH, f)
if err := self.SSHCopy(src, dest); err != nil {
return err
logger.Errorf("[%s] Failed to copy bootstrap files (source=%s, destination=%s, cause=%v)", self.Name, src, dest, err)
return errors.New(fmt.Sprintf("Failed to copy bootstrap files. (vm=%s, file=%s)", self.Name, f))
}
}
logger.Infof("[%s] completed the 'copy script files' process", self.Name)
return nil
}

func (self *VM) SetSystemd(networkCni string) error {

logger.Infof("[%s] start the process of 'set systemd'", self.Name)

var bsFile string
if networkCni == config.NETWORKCNI_CANAL {
bsFile = config.MCKS_BOOTSTRAP_CANAL_FILE
} else {
bsFile = config.MCKS_BOOTSTRAP_KILO_FILE
}

if _, err := self.SSHRun("cd %s;./%s %s", remoteTargetPath, bsFile, self.PublicIP); err != nil {
return errors.New(fmt.Sprintf("Failed to execute bootstrap shell. (vm=%s, shell=%s)", self.Name, bsFile))
}

if _, err := self.SSHRun("cd %s;./%s", remoteTargetPath, config.SYSTEMD_SERVICE_FILE); err != nil {
return errors.New(fmt.Sprintf("Failed to execute systemd shell. (vm=%s, shell=%s)", self.Name, bsFile))
}

logger.Infof("[%s] completed the 'set systemd' process", self.Name)
return nil
}

func (self *VM) Bootstrap() error {

logger.Infof("[%s] start the process of 'bootstrap'", self.Name)

if output, err := self.SSHRun("cd %s;./%s %s %s", remoteTargetPath, config.BOOTSTRAP_FILE, self.Name, self.PublicIP); err != nil {
return errors.New(fmt.Sprintf("Failed to execute bootstrap shell. (vm=%s, shell=%s)", self.Name, config.BOOTSTRAP_FILE))
// 2. execute bootstrap.sh
if output, err := self.SSHRun("%s/bootstrap.sh %s %s %s %s %s", REMOTE_TARGET_PATH, self.Csp, self.Region.Region, self.Name, self.PublicIP, networkCni); err != nil {
return errors.New(fmt.Sprintf("Failed to execute bootstrap.sh (vm=%s)", self.Name))
} else if !strings.Contains(output, "kubectl set on hold") {
logger.Errorf("[%s] failed to execute bootstrap shell (cause='kubectl not set on hold')", self.Name)
return errors.New(fmt.Sprintf("Failed to execute bootstrap shell. (vm=%s, cause='kubectl not set on hold', cause=kubectl set on hold)", self.Name))
logger.Errorf("[%s] failed to execute bootstrap.sh (cause='kubectl not set on hold')", self.Name)
return errors.New(fmt.Sprintf("Failed to execute bootstrap.sh shell. (vm=%s, cause='kubectl not set on hold')", self.Name))
}

logger.Infof("[%s] complted the'bootstrap process", self.Name)
logger.Infof("[%s] completed the'bootstrap process", self.Name)
return nil

}

/* setup haproxy */
func (self *VM) InstallHAProxy(IPs []string) error {

logger.Infof("[%s] start the process of 'set up HA'", self.Name)
Expand All @@ -241,8 +209,8 @@ func (self *VM) InstallHAProxy(IPs []string) error {
}
}

if output, err := self.SSHRun("sudo sed 's/^{{SERVERS}}/%s/g' %s/%s", servers, remoteTargetPath, config.HA_PROXY_FILE); err != nil {
return errors.New(fmt.Sprintf("Failed to set up haproxy. (vm=%s, shell=%s)", self.Name, config.HA_PROXY_FILE))
if output, err := self.SSHRun("sudo sed 's/^{{SERVERS}}/%s/g' %s/%s", servers, REMOTE_TARGET_PATH, "haproxy.sh"); err != nil {
return errors.New(fmt.Sprintf("Failed to set up haproxy. (vm=%s, shell=%s)", self.Name, "haproxy.sh"))
} else {
if _, err = self.SSHRun(output); err != nil {
return errors.New(fmt.Sprintf("Failed to set up haproxy. (vm=%s, command='%s')", self.Name, output))
Expand All @@ -253,14 +221,15 @@ func (self *VM) InstallHAProxy(IPs []string) error {
return nil
}

// coantrol-plane init
func (self *VM) ControlPlaneInit(reqKubernetes Kubernetes) ([]string, string, error) {

logger.Infof("[%s] start the process of 'control-plane init.'", self.Name)

var joinCmd []string

if output, err := self.SSHRun("cd %s;./%s %s %s %s %s", remoteTargetPath, config.INIT_FILE, reqKubernetes.PodCidr, reqKubernetes.ServiceCidr, reqKubernetes.ServiceDnsDomain, self.PublicIP); err != nil {
return nil, "", errors.New(fmt.Sprintf("Failed to initialize control-plane. (vm=%s, shell=%s)", self.Name, config.INIT_FILE))
if output, err := self.SSHRun("cd %s;./%s %s %s %s %s", REMOTE_TARGET_PATH, "k8s-init.sh", reqKubernetes.PodCidr, reqKubernetes.ServiceCidr, reqKubernetes.ServiceDnsDomain, self.PublicIP); err != nil {
return nil, "", errors.New(fmt.Sprintf("Failed to initialize control-plane. (vm=%s, shell=%s)", self.Name, "k8s-init.sh"))
} else if strings.Contains(output, "Your Kubernetes control-plane has initialized successfully") {
joinCmd = getJoinCmd(output)
} else {
Expand All @@ -274,22 +243,23 @@ func (self *VM) ControlPlaneInit(reqKubernetes Kubernetes) ([]string, string, er
return joinCmd, ouput, nil
}

/* install network-cni */
func (self *VM) InstallNetworkCNI(networkCni string) error {

logger.Infof("[%s] start the process of 'set up network-cni'", self.Name)
logger.Infof("[%s] start the process of 'install network-cni'", self.Name)

var cmd string
cniFiles := getCniFiles(networkCni)

for _, file := range cniFiles {
cmd += fmt.Sprintf("sudo kubectl apply -f %s/%s --kubeconfig=/etc/kubernetes/admin.conf;\n", remoteTargetPath, file)
cmd += fmt.Sprintf("sudo kubectl apply -f %s/%s --kubeconfig=/etc/kubernetes/admin.conf;\n", REMOTE_TARGET_PATH, file)
}

if _, err := self.SSHRun(cmd); err != nil {
return errors.New(fmt.Sprintf("Failed to set up network-cni plug-in. (node=%s, command='%s')", self.Name, cmd))
return err
}

logger.Infof("[%s] completed the 'set up network-cni' process", self.Name)
logger.Infof("[%s] completed the 'install network-cni' process", self.Name)
return nil
}

Expand Down Expand Up @@ -338,8 +308,6 @@ func (self *VM) WorkerJoin(workerJoinCmd *string) error {

func (self *VM) AddNodeLabels() error {

logger.Infof("[%s] start the process of 'set node label'", self.Name)

configFile := "admin.conf"
if self.Role == config.WORKER {
configFile = "kubelet.conf"
Expand All @@ -362,7 +330,7 @@ func (self *VM) AddNodeLabels() error {
return errors.New(fmt.Sprintf("Failed to set label. (node=%s, label=%s)", self.Name, labels))
}

logger.Infof("[%s] completed the 'set node label' process", self.Name)
logger.Infof("[%s] set node label (label=%s)", self.Name, labels)
return nil
}

Expand All @@ -387,11 +355,11 @@ func getJoinCmd(cpInitResult string) []string {

func getCniFiles(cni string) (cniFiles []string) {
if cni == config.NETWORKCNI_CANAL {
cniFiles = append(cniFiles, config.CNI_CANAL_FILE)
cniFiles = append(cniFiles, CNI_CANAL_FILE)
} else {
cniFiles = append(cniFiles, config.CNI_KILO_CRDS_FILE)
cniFiles = append(cniFiles, config.CNI_KILO_KUBEADM_FILE)
cniFiles = append(cniFiles, config.CNI_KILO_FLANNEL_FILE)
cniFiles = append(cniFiles, CNI_KILO_CRDS_FILE)
cniFiles = append(cniFiles, CNI_KILO_KUBEADM_FILE)
cniFiles = append(cniFiles, CNI_KILO_FLANNEL_FILE)
}
return
}
8 changes: 1 addition & 7 deletions src/core/service/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,7 @@ func CreateCluster(namespace string, req *model.ClusterReq) (*model.Cluster, err
if err := vm.ConnectionTest(); err != nil {
return err
}
if err = vm.CopyScripts(cluster.NetworkCni); err != nil {
return err
}
if err = vm.SetSystemd(cluster.NetworkCni); err != nil {
return err
}
if err = vm.Bootstrap(); err != nil {
if err = vm.Bootstrap(cluster.NetworkCni); err != nil {
return err
}
return nil
Expand Down
8 changes: 1 addition & 7 deletions src/core/service/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,7 @@ func AddNode(namespace string, clusterName string, req *model.NodeReq) (*model.N
if err := vm.ConnectionTest(); err != nil {
return err
}
if err = vm.CopyScripts(networkCni); err != nil {
return err
}
if err = vm.SetSystemd(networkCni); err != nil {
return err
}
if err = vm.Bootstrap(); err != nil {
if err = vm.Bootstrap(networkCni); err != nil {
return err
}
if err = vm.WorkerJoin(&workerJoinCmd); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/addons/kilo/kilo-kubeadm-flannel_v0.3.0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ spec:
hostNetwork: true
containers:
- name: kilo
image: squat/kilo
image: squat/kilo:0.3.1
args:
- --kubeconfig=/etc/kubernetes/kubeconfig
- --hostname=$(NODE_NAME)
Expand Down
85 changes: 80 additions & 5 deletions src/scripts/bootstrap.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
#!/bin/bash
K8S_VERSION="1.18.9-00" # curl https://packages.cloud.google.com/apt/dists/kubernetes-xenial/main/binary-amd64/Packages
# K8S_VERSION="1.17.8-00"
CSP="$1"
REGION="$2"
HOSTNAME="$3"
PUBLIC_IP="$4"
NETWORK_CNI="$5"
LOCATION="${CSP}-${REGION}"

# ---------------------------------------------------------------------------------
# packages

# hostname
sudo hostnamectl set-hostname $1
sudo hostnamectl set-hostname ${HOSTNAME}

# packages
sudo killall apt apt-get > /dev/null 2>&1
Expand Down Expand Up @@ -42,11 +50,78 @@ sudo apt-get update
sudo apt-get install -y kubeadm=$K8S_VERSION kubelet=$K8S_VERSION kubectl=$K8S_VERSION
sudo apt-mark hold kubeadm kubelet kubectl


# ---------------------------------------------------------------------------------
# System deamon (mcks-bootstrap)


if [ "${CSP}" != "openstack" ]; then
PUBLIC_IP='$(dig +short myip.opendns.com @resolver1.opendns.com)'
fi

if [ "${NETWORK_CNI}" == "kilo" ]; then
# install wireguard
sudo add-apt-repository -y ppa:wireguard/wireguard
sudo apt-get update
sudo apt-get install -y wireguard
# mcks-bootstrap
echo -e '#!/bin/sh
IFACE="$(ip route get 8.8.8.8 | awk \047{ print $5; exit }\047)"
PUBLIC_IP="{{PUBLIC_IP}}"
ifconfig ${IFACE}:1 ${PUBLIC_IP} netmask 255.255.255.255 broadcast 0.0.0.0 up
echo "KUBELET_EXTRA_ARGS=-\"-node-ip=${PUBLIC_IP}\"" > /etc/default/kubelet
if [ -f "/etc/kubernetes/kubelet.conf" ]; then
systemctl restart kubelet
kubectl --kubeconfig=/etc/kubernetes/kubelet.conf annotate node {{HOSTNAME}} kilo.squat.ai/location={{LOCATION}} --overwrite
kubectl --kubeconfig=/etc/kubernetes/kubelet.conf annotate node {{HOSTNAME}} kilo.squat.ai/force-endpoint=${PUBLIC_IP}:51820 --overwrite
kubectl --kubeconfig=/etc/kubernetes/kubelet.conf annotate node {{HOSTNAME}} kilo.squat.ai/persistent-keepalive=25 --overwrite
kubectl --kubeconfig=/etc/kubernetes/kubelet.conf annotate node {{HOSTNAME}} flannel.alpha.coreos.com/public-ip-overwrite=${PUBLIC_IP} --overwrite
fi
exit 0
fi' | sed "s/{{HOSTNAME}}/${HOSTNAME}/g" | sed "s/{{PUBLIC_IP}}/${PUBLIC_IP}/g" | sed "s/{{LOCATION}}/${LOCATION}/g" | sudo tee /lib/systemd/system/mcks-bootstrap > /dev/null
sudo chmod +x /lib/systemd/system/mcks-bootstrap
fi

if [ "${NETWORK_CNI}" == "canal" ]; then
# mcks-bootstrap
echo -e '#!/bin/sh
IFACE="$(ip route get 8.8.8.8 | awk \047{ print $5; exit }\047)"
PUBLIC_IP="{{PUBLIC_IP}}"
ifconfig ${IFACE}:1 ${PUBLIC_IP} netmask 255.255.255.255 broadcast 0.0.0.0 up
echo "KUBELET_EXTRA_ARGS=-\"-node-ip=${PUBLIC_IP}\"" > /etc/default/kubelet
if [ -f "/etc/kubernetes/kubelet.conf" ]; then
systemctl restart kubelet
R="$(kubectl --kubeconfig=/etc/kubernetes/kubelet.conf annotate node {{HOSTNAME}} flannel.alpha.coreos.com/public-ip-overwrite=${PUBLIC_IP} --overwrite)"
if echo "$R" | grep "annotated"; then
R=$(kubectl --kubeconfig=/etc/kubernetes/kubelet.conf get nodes --no-headers | awk \047END { print NR }\047)
echo "nodes count = ${R}"
if [ "$R" != "1" ]; then
systemctl restart docker
echo "docker daemon restarted"
fi
exit 0
else
exit 1
fi
fi
exit 0
fi' | sed "s/{{HOSTNAME}}/${HOSTNAME}/g" | sed "s/{{PUBLIC_IP}}/${PUBLIC_IP}/g" | sudo tee /lib/systemd/system/mcks-bootstrap > /dev/null
sudo chmod +x /lib/systemd/system/mcks-bootstrap
fi

# public ip nic up
IFACE="$(ip route get 8.8.8.8 | awk '{ print $5; exit }')"
sudo ifconfig ${IFACE}:1 $2 netmask 255.255.255.255 broadcast 0.0.0.0 up
# setup bootstrap service deamon
sudo bash -c 'cat > /lib/systemd/system/mcks-bootstrap.service <<EOF
[Unit]
Description=MCKS bootstrap script
After=multi-user.target
StartLimitIntervalSec=60
StartLimitBurst=3
[Service]
ExecStart=/lib/systemd/system/mcks-bootstrap
Restart=on-failure
RestartSec=10s
[Install]
WantedBy=kubelet.service
EOF'
sudo systemctl daemon-reload
sudo systemctl enable mcks-bootstrap
Loading