|
| 1 | +#!/bin/bash -e |
| 2 | + |
| 3 | +if [ $# -lt 9 ]; then |
| 4 | + echo "Usage: /bin/bash $0 BINARIES_DIR CLUSTER_JOIN_IP CLUSTER_TOKEN CERTIFICATE_KEY REGISTRY_URL REGISTRY_URL_ENDPOINT REGISTRY_USERNAME REGISTRY_PASSWORD REGISTRY_TOKEN" >&2 |
| 5 | + exit 1 |
| 6 | +fi |
| 7 | + |
| 8 | +BINARIES_DIR="$1" |
| 9 | +CLUSTER_JOIN_IP="$2" |
| 10 | +CLUSTER_TOKEN="$3" |
| 11 | +CERTIFICATE_KEY="$4" |
| 12 | +REGISTRY_URL="$5" |
| 13 | +REGISTRY_URL_ENDPOINT="$6" |
| 14 | +REGISTRY_USERNAME="$7" |
| 15 | +REGISTRY_PASSWORD="$8" |
| 16 | +REGISTRY_TOKEN="$9" |
| 17 | + |
| 18 | +ATTEMPT_ONLINE_INSTALL=false |
| 19 | +setup_complete=false |
| 20 | + |
| 21 | +MAX_SETUP_CRUCIAL_CMD_ATTEMPTS=3 |
| 22 | +crucial_cmd_attempts=1 |
| 23 | + |
| 24 | +if [[ "$PATH" != *:/opt/bin && "$PATH" != *:/opt/bin:* ]]; then |
| 25 | + export PATH=$PATH:/opt/bin |
| 26 | +fi |
| 27 | + |
| 28 | +if [ -f ${BINARIES_DIR}/scripts/control-node-add/deploy-kube-system.tmpl ]; then |
| 29 | + sed -e "s/{{ k8s_control_node.cluster.token }}/${CLUSTER_TOKEN}/g" -e "s/{{ k8s_control_node.cluster.ha.certificate.key }}/${CERTIFICATE_KEY}/g" -e "s/{{ k8s_control_node.join_ip }}/${CLUSTER_JOIN_IP}/g" ${BINARIES_DIR}/scripts/control-node-add/deploy-kube-system.tmpl > /opt/bin/deploy-kube-system |
| 30 | +fi |
| 31 | + |
| 32 | +if [ -f ${BINARIES_DIR}/scripts/control-node-add/setup-containerd.tmpl ]; then |
| 33 | + sed -e "s/{{registry.url}}/${REGISTRY_URL}/g" -e "s/{{registry.url.endpoint}}/${REGISTRY_URL_ENDPOINT}/g" -e "s/{{registry.username}}/${REGISTRY_USERNAME}/g" -e "s/{{registry.password}}/${REGISTRY_PASSWORD}/g" -e "s/{{registry.token}}/${REGISTRY_TOKEN}/g" ${BINARIES_DIR}/scripts/control-node-add/setup-containerd.tmpl > /opt/bin/setup-containerd |
| 34 | +fi |
| 35 | + |
| 36 | +if [ -d "$BINARIES_DIR" ]; then |
| 37 | + ### Binaries available offline ### |
| 38 | + echo "Installing binaries from ${BINARIES_DIR}" |
| 39 | + mkdir -p /opt/cni/bin |
| 40 | + tar -f "${BINARIES_DIR}/cni/cni-plugins-"*64.tgz -C /opt/cni/bin -xz |
| 41 | + |
| 42 | + mkdir -p /opt/bin |
| 43 | + tar -f "${BINARIES_DIR}/cri-tools/crictl-linux-"*64.tar.gz -C /opt/bin -xz |
| 44 | + |
| 45 | + mkdir -p /opt/bin |
| 46 | + cd /opt/bin |
| 47 | + cp -a ${BINARIES_DIR}/k8s/{kubeadm,kubelet,kubectl} . |
| 48 | + chmod +x {kubeadm,kubelet,kubectl} |
| 49 | + |
| 50 | + sed "s:/usr/bin:/opt/bin:g" ${BINARIES_DIR}/kubelet.service > /etc/systemd/system/kubelet.service |
| 51 | + mkdir -p /etc/systemd/system/kubelet.service.d |
| 52 | + sed "s:/usr/bin:/opt/bin:g" ${BINARIES_DIR}/10-kubeadm.conf > /etc/systemd/system/kubelet.service.d/10-kubeadm.conf |
| 53 | + |
| 54 | + echo "KUBELET_EXTRA_ARGS=--cgroup-driver=systemd" > /etc/default/kubelet |
| 55 | + |
| 56 | + output=`ls ${BINARIES_DIR}/docker/` |
| 57 | + if [ "$output" != "" ]; then |
| 58 | + while read -r line; do |
| 59 | + crucial_cmd_attempts=1 |
| 60 | + while true; do |
| 61 | + if (( "$crucial_cmd_attempts" > "$MAX_SETUP_CRUCIAL_CMD_ATTEMPTS" )); then |
| 62 | + echo "Loading docker image ${BINARIES_DIR}/docker/$line failed!" |
| 63 | + break; |
| 64 | + fi |
| 65 | + retval=0 |
| 66 | + set +e |
| 67 | + ctr -n k8s.io image import "${BINARIES_DIR}/docker/$line" |
| 68 | + retval=$? |
| 69 | + set -e |
| 70 | + if [ $retval -eq 0 ]; then |
| 71 | + break; |
| 72 | + fi |
| 73 | + crucial_cmd_attempts=$[$crucial_cmd_attempts + 1] |
| 74 | + done |
| 75 | + done <<< "$output" |
| 76 | + setup_complete=true |
| 77 | + fi |
| 78 | + if [ -e "${BINARIES_DIR}/autoscaler.yaml" ]; then |
| 79 | + mkdir -p /opt/autoscaler |
| 80 | + cp "${BINARIES_DIR}/autoscaler.yaml" /opt/autoscaler/autoscaler_tmpl.yaml |
| 81 | + fi |
| 82 | + if [ -e "${BINARIES_DIR}/provider.yaml" ]; then |
| 83 | + mkdir -p /opt/provider |
| 84 | + cp "${BINARIES_DIR}/provider.yaml" /opt/provider/provider.yaml |
| 85 | + fi |
| 86 | + |
| 87 | + PAUSE_IMAGE=`ctr -n k8s.io images ls -q | grep "pause" | sort | tail -n 1` |
| 88 | + echo $PAUSE_IMAGE |
| 89 | + if [ -n "$PAUSE_IMAGE" ]; then |
| 90 | + sed -i "s|sandbox_image = .*|sandbox_image = \"$PAUSE_IMAGE\"|g" /etc/containerd/config.toml |
| 91 | + fi |
| 92 | + systemctl daemon-reload |
| 93 | + systemctl restart containerd |
| 94 | + |
| 95 | +fi |
| 96 | +if [ "$setup_complete" = false ] && [ "$ATTEMPT_ONLINE_INSTALL" = true ]; then |
| 97 | + ### Binaries not available offline ### |
| 98 | + RELEASE="v1.16.3" |
| 99 | + CNI_VERSION="v0.7.5" |
| 100 | + CRICTL_VERSION="v1.16.0" |
| 101 | + echo "Warning: ${BINARIES_DIR} not found. Will get binaries and docker images from Internet." |
| 102 | + mkdir -p /opt/cni/bin |
| 103 | + curl -L "https://github.com/containernetworking/plugins/releases/download/${CNI_VERSION}/cni-plugins-amd64-${CNI_VERSION}.tgz" | tar -C /opt/cni/bin -xz |
| 104 | + |
| 105 | + mkdir -p /opt/bin |
| 106 | + curl -L "https://github.com/kubernetes-incubator/cri-tools/releases/download/${CRICTL_VERSION}/crictl-${CRICTL_VERSION}-linux-amd64.tar.gz" | tar -C /opt/bin -xz |
| 107 | + |
| 108 | + mkdir -p /opt/bin |
| 109 | + cd /opt/bin |
| 110 | + curl -L --remote-name-all https://storage.googleapis.com/kubernetes-release/release/${RELEASE}/bin/linux/amd64/{kubeadm,kubelet,kubectl} |
| 111 | + chmod +x {kubeadm,kubelet,kubectl} |
| 112 | + |
| 113 | + curl -sSL "https://raw.githubusercontent.com/kubernetes/kubernetes/${RELEASE}/build/debs/kubelet.service" | sed "s:/usr/bin:/opt/bin:g" > /etc/systemd/system/kubelet.service |
| 114 | + mkdir -p /etc/systemd/system/kubelet.service.d |
| 115 | + curl -sSL "https://raw.githubusercontent.com/kubernetes/kubernetes/${RELEASE}/build/debs/10-kubeadm.conf" | sed "s:/usr/bin:/opt/bin:g" > /etc/systemd/system/kubelet.service.d/10-kubeadm.conf |
| 116 | +fi |
| 117 | + |
| 118 | +systemctl enable kubelet && systemctl start kubelet |
| 119 | +modprobe overlay && modprobe br_netfilter && sysctl net.bridge.bridge-nf-call-iptables=1 |
| 120 | + |
| 121 | +if [ -d "$BINARIES_DIR" ] && [ "$ATTEMPT_ONLINE_INSTALL" = true ]; then |
| 122 | + crucial_cmd_attempts=1 |
| 123 | + while true; do |
| 124 | + if (( "$crucial_cmd_attempts" > "$MAX_SETUP_CRUCIAL_CMD_ATTEMPTS" )); then |
| 125 | + echo "Warning: kubeadm pull images failed after multiple tries!" |
| 126 | + break; |
| 127 | + fi |
| 128 | + retval=0 |
| 129 | + set +e |
| 130 | + kubeadm config images pull --cri-socket /run/containerd/containerd.sock |
| 131 | + retval=$? |
| 132 | + set -e |
| 133 | + if [ $retval -eq 0 ]; then |
| 134 | + break; |
| 135 | + fi |
| 136 | + crucial_cmd_attempts=$[$crucial_cmd_attempts + 1] |
| 137 | + done |
| 138 | +fi |
0 commit comments