blob: 670de0b6ba3c86587b55069e775ef3961c313d96 [file] [log] [blame]
garciadeblasb3797412024-06-06 14:26:24 +02001#!/bin/bash
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14#
15
16set +eux
17
18# K3s releases: https://github.com/k3s-io/k3s/releases/
garciadeblas72a25d52024-09-19 12:38:36 +020019K8S_CLIENT_VERSION="v1.29.3"
garciadeblasb3797412024-06-06 14:26:24 +020020K8S_VERSION="v1.29.3+k3s1"
21
garciadeblas117fd4a2024-08-21 18:18:14 +020022# configure registry
23function configure_registry() {
24 if [ -n "${DOCKER_PROXY_URL}" ]; then
25 echo "Configuring docker proxy URL in /etc/rancher/k3s/registries.yaml"
garciadeblas89cee5e2024-11-18 12:58:29 +010026 sudo mkdir -p /etc/rancher/k3s/
garciadeblas117fd4a2024-08-21 18:18:14 +020027 cat << EOF | sudo tee /etc/rancher/k3s/registries.yaml > /dev/null
28mirrors:
garciadeblas918c61a2025-11-21 10:16:25 +010029 ghcr.io:
30 endpoint:
31 - "${DOCKER_PROXY_URL}"
garciadeblas117fd4a2024-08-21 18:18:14 +020032 docker.io:
33 endpoint:
34 - "${DOCKER_PROXY_URL}"
35EOF
36 fi
garciadeblas2efb98b2024-08-21 21:19:36 +020037 if [ -n "${DOCKER_REGISTRY_URL}" ]; then
38 echo "Configuring docker private registry in /etc/rancher/k3s/registries.yaml"
39 cat << EOF | sudo tee -a /etc/rancher/k3s/registries.yaml > /dev/null
40configs:
41 ${DOCKER_REGISTRY_URL}:
42 auth:
43 username: ${DOCKER_REGISTRY_USER}
44 password: ${DOCKER_REGISTRY_PASSWORD}
45EOF
46 fi
garciadeblas117fd4a2024-08-21 18:18:14 +020047}
48
garciadeblasb3797412024-06-06 14:26:24 +020049# installs k3s
50function install_k3s() {
51 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
52 export INSTALL_K3S_EXEC="--disable traefik"
garciadeblase3a84a52024-09-27 11:34:55 +020053
54 # Regular installation of K3s
garciadeblasb3797412024-06-06 14:26:24 +020055 curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=${K8S_VERSION} sh -s -
garciadeblase3a84a52024-09-27 11:34:55 +020056
57 # If specified a public IP, K3s service is updated accordingly and restarted
58 if [ -n "${K3S_PUBLIC_IP}" ]; then
59 # Back-up service config file to home
60 cp /etc/systemd/system/k3s.service ~/BASE-k3s.service
61
62 # Generate new service config file with additions for using a public IP
63 (
64 cat ~/BASE-k3s.service | sed '${/^$/d}'
65 echo -e "\t'--node-external-ip' \\"
66 echo -e "\t'${K3S_PUBLIC_IP}' \\"
67 echo
68 )| \
69 tee ~/PUBLIC-k3s.service
70
71 # Replace service config and apply
72 sudo cp ~/PUBLIC-k3s.service /etc/systemd/system/k3s.service
73 sudo systemctl daemon-reload
74 sudo systemctl restart k3s
75
76 # Cleanup
77 rm ~/BASE-k3s.service ~/PUBLIC-k3s.service
78 fi
79
80 # Make kubeconfig permissions less restrictive
garciadeblasb3797412024-06-06 14:26:24 +020081 sudo chmod 644 /etc/rancher/k3s/k3s.yaml
82 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
83}
84
85# updates service nodeport range
86function update_service_nodeport_range() {
87 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
88 sudo k3s server --kube-apiserver-arg=service-node-port-range=80-32767
89 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
90}
91
92# checks cluster readiness
93function check_for_readiness() {
94 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
95 # Check for Ready node, takes ~30 seconds
96 echo "Waiting for K8s nodes to be ready"
97 local time_for_failure=60 # seconds broken
98 local sampling_period=5 # seconds
99 local counter=0
100 local cluster_ready=""
101 while (( counter < time_for_failure ))
102 do
103 kubectl get nodes |grep master |grep -v none | grep Ready
104 if [ $? -eq 0 ] ; then
105 echo "K8s cluster is ready"
106 cluster_ready="y"
107 break
108 else
109 echo "K8s cluster is not ready yet"
110 counter=$((counter + sampling_period))
111 sleep ${sampling_period}
112 fi
113 done
114 [ -n "$cluster_ready" ] || FATAL_TRACK k8scluster "K3s cluster nodes not ready after $time_for_failure seconds."
115
116 echo "Waiting for pods to be ready"
117 local time_for_readiness=20 # seconds ready
118 local time_for_failure=100 # seconds broken
119
120 # Equivalent number of samples
121 oks_threshold=$((time_for_readiness/${sampling_period})) # No. ok samples to declare the system ready
122 failures_threshold=$((time_for_failure/${sampling_period})) # No. nok samples to declare the system broken
123 failures_in_a_row=0
124 oks_in_a_row=0
125 ####################################################################################
126 # Loop to check system readiness
127 ####################################################################################
128 K3S_NAMESPACE=kube-system
129 while [[ (${failures_in_a_row} -lt ${failures_threshold}) && (${oks_in_a_row} -lt ${oks_threshold}) ]]
130 do
131 # State of pods rather than completed jobs
132 K3S_PODS_STATE=$(kubectl get pod -n ${K3S_NAMESPACE} --no-headers |grep -v Completed 2>&1)
133 K3S_PODS_READY=$(echo "${K3S_PODS_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
134 K3S_PODS_NOT_READY=$(echo "${K3S_PODS_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
135 COUNT_K3S_PODS_READY=$(echo "${K3S_PODS_READY}"| grep -v -e '^$' | wc -l)
136 COUNT_K3S_PODS_NOT_READY=$(echo "${K3S_PODS_NOT_READY}" | grep -v -e '^$' | wc -l)
137
138 # OK sample
139 if [[ ${COUNT_K3S_PODS_NOT_READY} -eq 0 ]]
140 then
141 ((++oks_in_a_row))
142 failures_in_a_row=0
143 echo -ne ===\> Successful checks: "${oks_in_a_row}"/${oks_threshold}\\r
144 # NOK sample
145 else
146 ((++failures_in_a_row))
147 oks_in_a_row=0
148 echo
149 echo Bootstraping... "${failures_in_a_row}" checks of ${failures_threshold}
150
151 # Reports failed pods in K3S
152 if [[ "${COUNT_K3S_PODS_NOT_READY}" -ne 0 ]]
153 then
154 echo "K3S kube-system: Waiting for ${COUNT_K3S_PODS_NOT_READY} of $((${COUNT_K3S_PODS_NOT_READY}+${COUNT_K3S_PODS_READY})) pods to be ready:"
155 echo "${K3S_PODS_NOT_READY}"
156 echo
157 fi
158 fi
159
160 #------------ NEXT SAMPLE
161 sleep ${sampling_period}
162 done
163
164 ####################################################################################
165 # OUTCOME
166 ####################################################################################
167 if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]]
168 then
169 echo
170 FATAL_TRACK k8scluster "K8S CLUSTER IS BROKEN"
171 else
172 echo
173 echo "K8S CLUSTER IS READY"
174 fi
175 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
176}
177
garciadeblas1f338482024-07-04 19:26:54 +0200178# Initializes kubeconfig file
179function save_kubeconfig() {
garciadeblasb3797412024-06-06 14:26:24 +0200180 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
181 KUBEDIR="${HOME}/.kube"
182 KUBEFILE="$KUBEDIR/config"
183 mkdir -p "${KUBEDIR}"
garciadeblas1f338482024-07-04 19:26:54 +0200184 K3S_KUBECONFIG="/etc/rancher/k3s/k3s.yaml"
185 sudo cp "${K3S_KUBECONFIG}" "${KUBEFILE}"
garciadeblasb3797412024-06-06 14:26:24 +0200186 sudo chown $(id -u):$(id -g) "${KUBEFILE}"
garciadeblas1f338482024-07-04 19:26:54 +0200187 sed -i "s#server: https://127.0.0.1#server: https://${DEFAULT_IP}#g" "${KUBEFILE}"
garciadeblasb3797412024-06-06 14:26:24 +0200188 chmod 700 "${KUBEFILE}"
189 echo
190 echo "Credentials saved at ${KUBEFILE}"
191 echo
192 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
193}
194
garciadeblas82981162024-07-23 15:24:00 +0200195DEBUG_INSTALL=${DEBUG_INSTALL:-}
196DEFAULT_IP=${DEFAULT_IP:-"127.0.0.1"}
garciadeblasdb368c32024-11-20 12:55:45 +0100197DOCKER_PROXY_URL=${DOCKER_PROXY_URL:-}
198DOCKER_REGISTRY_URL=${DOCKER_REGISTRY_URL:-}
199DOCKER_REGISTRY_USER=${DOCKER_REGISTRY_USER:-}
200DOCKER_REGISTRY_PASSWORD=${DOCKER_REGISTRY_PASSWORD:-}
garciadeblase3a84a52024-09-27 11:34:55 +0200201K3S_PUBLIC_IP=${K3S_PUBLIC_IP:-}
garciadeblas1f338482024-07-04 19:26:54 +0200202echo "DEBUG_INSTALL=${DEBUG_INSTALL}"
203echo "DEFAULT_IP=${DEFAULT_IP}"
garciadeblas117fd4a2024-08-21 18:18:14 +0200204echo "DOCKER_PROXY_URL=${DOCKER_PROXY_URL}"
garciadeblas2efb98b2024-08-21 21:19:36 +0200205echo "DOCKER_REGISTRY_URL=${DOCKER_REGISTRY_URL}"
206echo "DOCKER_REGISTRY_USER=${DOCKER_REGISTRY_USER}"
garciadeblase3a84a52024-09-27 11:34:55 +0200207echo "K3S_PUBLIC_IP=${K3S_PUBLIC_IP}"
garciadeblasb3797412024-06-06 14:26:24 +0200208
garciadeblascf603f52025-06-04 11:57:28 +0200209export HERE=$(dirname "$(readlink --canonicalize "$BASH_SOURCE")")
210source "${HERE}/../../../library/logging"
211source "${HERE}/../../../library/track"
garciadeblas82981162024-07-23 15:24:00 +0200212
garciadeblas117fd4a2024-08-21 18:18:14 +0200213configure_registry
garciadeblasb3797412024-06-06 14:26:24 +0200214install_k3s
garciadeblascf603f52025-06-04 11:57:28 +0200215save_kubeconfig
garciadeblasb3797412024-06-06 14:26:24 +0200216track k8scluster k3s_install_ok
217check_for_readiness
218track k8scluster k3s_node_ready_ok
219# update_service_nodeport_range
220# check_for_readiness
garciadeblasb3797412024-06-06 14:26:24 +0200221track k8scluster k3s_creds_ok