Skip to content

Commit c35616d

Browse files
authored
Fix k3s-start.sh error when recreating instances (#37)
Signed-off-by: Alexandre Peixoto Ferreira <[email protected]>
1 parent b46472d commit c35616d

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

scripts/k3s-start.sh

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ HOSTPORT=${HOSTPORT:-6443}
88
# IP that the clients will be used to connect (If on the cloud it will probably be the external IP of the server)
99
# HOSTIP
1010
# Which version of k3s or k8s to use
11-
DOCKERIMAGE=${DOCKERIMAGE:-rancher/k3s:v1.25.2-k3s1}
11+
DOCKERIMAGE=${DOCKERIMAGE:-rancher/k3s:v1.31.3-k3s1}
1212

1313
# Do not set this variables, they are used below
1414
CLOUD_INSTANCE=No
@@ -72,6 +72,24 @@ function check_main_ip() {
7272

7373
}
7474

75+
function check_if_k3s_running() {
76+
CURRENT_INSTANCE=$(docker inspect k3s_${SERVERNAME} 2>/dev/null)
77+
if [ $? -eq 0 ]
78+
then
79+
NAMEFOUND=$(echo "${CURRENT_INSTANCE}" | grep "\"/k3s_${SERVERNAME}\"")
80+
if [ ! -z "${NAMEFOUND}" ]
81+
then
82+
echo "This instance \"${SERVERNAME}\" is already running."
83+
echo "Either create a new one with a different name or stop the current one before recreating"
84+
echo "To stop and remove the database please execute the following commands:"
85+
echo "docker stop k3s_${SERVERNAME}"
86+
echo "sudo rm -rf ${SERVERNAME}"
87+
exit 1
88+
fi
89+
fi
90+
}
91+
92+
7593
[ -z "${HOSTIP}" ] && check_running_cloud
7694
[ -z "${HOSTIP}" ] && check_main_ip
7795

@@ -83,9 +101,10 @@ KUBECTL_FILE=kubectl-${SERVERNAME}.sh
83101
KUBECTL_EDGE_INSTALL_FILE=kube_edge_install-${SERVERNAME}.sh
84102
LOCALSERVERDIR=$(pwd)/${SERVERNAME}
85103

86-
if [ ! -e "${START_FILE}" ]
87-
then
88-
cat<<EOF >> "${START_FILE}"
104+
check_if_k3s_running
105+
106+
rm -f kube.${SERVERNAME}.config token.${SERVERNAME}
107+
cat<<EOF > "${START_FILE}"
89108
#!/bin/bash
90109
#
91110
export SERVERNAME=${SERVERNAME}
@@ -125,10 +144,11 @@ echo "k3s_${SERVERNAME} is running, access this instance using the credentials a
125144
126145
exit 0
127146
EOF
128-
chmod u+x "${START_FILE}"
129-
fi
147+
chmod u+x "${START_FILE}"
130148

149+
echo "Starting k3s server instance \"${SERVERNAME}\" in docker, it will take a minute or so....."
131150
./${START_FILE}
151+
echo "K3s started"
132152

133153
if [ $? -gt 0 ]
134154
then
@@ -149,42 +169,32 @@ case $(uname -m) in
149169
exit 1;;
150170
esac
151171

172+
rm -f "${FILE_DOWNLOAD}"
173+
wget --quiet "https://github.com/k3s-io/k3s/releases/download/${K3S_VERSION}/${FILE_DOWNLOAD}"
174+
152175
if [ ! -e "${FILE_DOWNLOAD}" ]
153176
then
154-
wget "https://github.com/k3s-io/k3s/releases/download/${K3S_VERSION}/${FILE_DOWNLOAD}"
155-
156-
if [ ! -e "${FILE_DOWNLOAD}" ]
157-
then
158-
echo "Download failed....... sorry, but check to see what was wrong"
159-
exit 1
160-
fi
161-
chmod u+x "${FILE_DOWNLOAD}"
177+
echo "Download failed....... sorry, but check to see what was wrong"
178+
exit 1
162179
fi
180+
chmod u+x "${FILE_DOWNLOAD}"
163181

164-
if [ ! -e "${KUBECTL_FILE}" ]
165-
then
166-
cat<<EOF > "${KUBECTL_FILE}"
182+
cat<<EOF > "${KUBECTL_FILE}"
167183
#!/bin/bash
168184
169185
export KUBECONFIG=$(pwd)/kube.default_smarter.config
170186
171187
exec $(pwd)/${FILE_DOWNLOAD} kubectl \$*
172188
EOF
173-
chmod u+x "${KUBECTL_FILE}"
174-
fi
189+
chmod u+x "${KUBECTL_FILE}"
175190

176-
if [ ! -e "${ENV_FILE}" ]
177-
then
178-
cat<<EOF > "${ENV_FILE}"
191+
cat<<EOF > "${ENV_FILE}"
179192
export KUBECONFIG=$(pwd)/kube.default_smarter.config
180193
alias kubectl="$(pwd)/${FILE_DOWNLOAD} kubectl"
181194
EOF
182-
chmod u+x "${ENV_FILE}"
183-
fi
195+
chmod u+x "${ENV_FILE}"
184196

185-
if [ ! -e "${KUBECTL_EDGE_INSTALL_FILE}" ]
186-
then
187-
cat<<EOF > "${KUBECTL_EDGE_INSTALL_FILE}"
197+
cat<<EOF > "${KUBECTL_EDGE_INSTALL_FILE}"
188198
export INSTALL_K3S_VERSION="${K3S_VERSION}"
189199
export K3S_TOKEN=$(cat token.${SERVERNAME})
190200
export K3S_URL=https://${HOSTIP}:${HOSTPORT}
@@ -199,9 +209,10 @@ sh -s - \\
199209
--node-taint smarter.type=edge:NoSchedule \\
200210
--node-label smarter-build=user-installed
201211
EOF
202-
chmod u+x "${KUBECTL_EDGE_INSTALL_FILE}"
203-
fi
212+
chmod u+x "${KUBECTL_EDGE_INSTALL_FILE}"
204213

214+
echo "Use this script to restart k3s server for this instance if necessary: \"${START_FILE}\""
215+
echo "Use this script to set up k3s agents running on a node that will connect to this server: \"${KUBECTL_EDGE_INSTALL_FILE}\""
205216
echo "A useful trick is to create an alias for shell like: alias kubectl='$(pwd)/${KUBECTL_FILE}', if that is your only k3s running"
206217

207218
exit 0

0 commit comments

Comments
 (0)