mirror of https://github.com/k3s-io/k3s
Add OpenRC support to installer
parent
4af06882b6
commit
eabc1154b1
145
install.sh
145
install.sh
|
@ -68,11 +68,17 @@ fatal()
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- fatal if no systemd ---
|
# --- fatal if no systemd or openrc ---
|
||||||
verify_systemd() {
|
verify_system() {
|
||||||
if [ ! -d /run/systemd ]; then
|
if [ -x /sbin/openrc-run ]; then
|
||||||
fatal "Can not find systemd to use as a process supervisor for k3s"
|
HAS_OPENRC=true
|
||||||
|
return
|
||||||
fi
|
fi
|
||||||
|
if [ -d /run/systemd ]; then
|
||||||
|
HAS_SYSTEMD=true
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
fatal "Can not find systemd or openrc to use as a process supervisor for k3s"
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- define needed environment variables ---
|
# --- define needed environment variables ---
|
||||||
|
@ -102,16 +108,16 @@ setup_env() {
|
||||||
|
|
||||||
# --- use systemd name if defined or create default ---
|
# --- use systemd name if defined or create default ---
|
||||||
if [ -n "${INSTALL_K3S_NAME}" ]; then
|
if [ -n "${INSTALL_K3S_NAME}" ]; then
|
||||||
SYSTEMD_NAME=k3s-${INSTALL_K3S_NAME}
|
SYSTEM_NAME=k3s-${INSTALL_K3S_NAME}
|
||||||
else
|
else
|
||||||
if [ "${CMD_K3S}" = "server" ]; then
|
if [ "${CMD_K3S}" = "server" ]; then
|
||||||
SYSTEMD_NAME=k3s
|
SYSTEM_NAME=k3s
|
||||||
else
|
else
|
||||||
SYSTEMD_NAME=k3s-${CMD_K3S}
|
SYSTEM_NAME=k3s-${CMD_K3S}
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
SERVICE_K3S=${SYSTEMD_NAME}.service
|
SERVICE_K3S=${SYSTEM_NAME}.service
|
||||||
UNINSTALL_K3S_SH=${SYSTEMD_NAME}-uninstall.sh
|
UNINSTALL_K3S_SH=${SYSTEM_NAME}-uninstall.sh
|
||||||
|
|
||||||
# --- use systemd type if defined or create default ---
|
# --- use systemd type if defined or create default ---
|
||||||
if [ -n "${INSTALL_K3S_TYPE}" ]; then
|
if [ -n "${INSTALL_K3S_TYPE}" ]; then
|
||||||
|
@ -138,6 +144,16 @@ setup_env() {
|
||||||
SYSTEMD_DIR="/etc/systemd/system"
|
SYSTEMD_DIR="/etc/systemd/system"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# --- use servive or environment location depending on systemd/openrc ---
|
||||||
|
if [ "${HAS_SYSTEMD}" = "true" ]; then
|
||||||
|
FILE_K3S_SERVICE=${SYSTEMD_DIR}/${SERVICE_K3S}
|
||||||
|
FILE_K3S_ENV=${SYSTEMD_DIR}/${SERVICE_K3S}.env
|
||||||
|
elif [ "${HAS_OPENRC}" = "true" ]; then
|
||||||
|
$SUDO mkdir -p /etc/rancher/k3s
|
||||||
|
FILE_K3S_SERVICE=/etc/init.d/${SYSTEM_NAME}
|
||||||
|
FILE_K3S_ENV=/etc/rancher/k3s/${SYSTEM_NAME}.env
|
||||||
|
fi
|
||||||
|
|
||||||
# --- use sudo if we are not already root ---
|
# --- use sudo if we are not already root ---
|
||||||
SUDO=sudo
|
SUDO=sudo
|
||||||
if [ `id -u` = 0 ]; then
|
if [ `id -u` = 0 ]; then
|
||||||
|
@ -317,19 +333,21 @@ create_uninstall() {
|
||||||
$SUDO tee ${BIN_DIR}/${UNINSTALL_K3S_SH} >/dev/null << EOF
|
$SUDO tee ${BIN_DIR}/${UNINSTALL_K3S_SH} >/dev/null << EOF
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
set -x
|
set -x
|
||||||
systemctl kill ${SYSTEMD_NAME}
|
if which systemctl; then
|
||||||
systemctl disable ${SYSTEMD_NAME}
|
systemctl kill ${SYSTEM_NAME}
|
||||||
systemctl reset-failed ${SYSTEMD_NAME}
|
systemctl disable ${SYSTEM_NAME}
|
||||||
systemctl daemon-reload
|
systemctl reset-failed ${SYSTEM_NAME}
|
||||||
rm -f ${SYSTEMD_DIR}/${SERVICE_K3S}
|
systemctl daemon-reload
|
||||||
rm -f ${SYSTEMD_DIR}/${SERVICE_K3S}.env
|
fi
|
||||||
|
rm -f ${FILE_K3S_SERVICE}
|
||||||
|
rm -f ${FILE_K3S_ENV}
|
||||||
|
|
||||||
remove_uninstall() {
|
remove_uninstall() {
|
||||||
rm -f ${BIN_DIR}/${UNINSTALL_K3S_SH}
|
rm -f ${BIN_DIR}/${UNINSTALL_K3S_SH}
|
||||||
}
|
}
|
||||||
trap remove_uninstall EXIT
|
trap remove_uninstall EXIT
|
||||||
|
|
||||||
if ls ${SYSTEMD_DIR}/k3s*.service >/dev/null 2>&1; then
|
if (ls ${SYSTEMD_DIR}/k3s*.service || ls /etc/init.d/k3s*) >/dev/null 2>&1; then
|
||||||
set +x; echo "Additional k3s services installed, skipping uninstall of k3s"; set -x
|
set +x; echo "Additional k3s services installed, skipping uninstall of k3s"; set -x
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
@ -369,22 +387,22 @@ EOF
|
||||||
systemd_disable() {
|
systemd_disable() {
|
||||||
$SUDO rm -f /etc/systemd/system/${SERVICE_K3S} || true
|
$SUDO rm -f /etc/systemd/system/${SERVICE_K3S} || true
|
||||||
$SUDO rm -f /etc/systemd/system/${SERVICE_K3S}.env || true
|
$SUDO rm -f /etc/systemd/system/${SERVICE_K3S}.env || true
|
||||||
$SUDO systemctl disable ${SYSTEMD_NAME} >/dev/null 2>&1 || true
|
$SUDO systemctl disable ${SYSTEM_NAME} >/dev/null 2>&1 || true
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- capture current env and create file containing k3s_ variables ---
|
# --- capture current env and create file containing k3s_ variables ---
|
||||||
create_env_file() {
|
create_env_file() {
|
||||||
info "systemd: Creating environment file ${SYSTEMD_DIR}/${SERVICE_K3S}.env"
|
info "env: Creating environment file ${FILE_K3S_ENV}"
|
||||||
UMASK=`umask`
|
UMASK=`umask`
|
||||||
umask 0377
|
umask 0377
|
||||||
env | grep '^K3S_' | $SUDO tee ${SYSTEMD_DIR}/${SERVICE_K3S}.env >/dev/null
|
env | grep '^K3S_' | $SUDO tee ${FILE_K3S_ENV} >/dev/null
|
||||||
umask $UMASK
|
umask $UMASK
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- write service file ---
|
# --- write systemd service file ---
|
||||||
create_service_file() {
|
create_systemd_service_file() {
|
||||||
info "systemd: Creating service file ${SYSTEMD_DIR}/${SERVICE_K3S}"
|
info "systemd: Creating service file ${FILE_K3S_SERVICE}"
|
||||||
$SUDO tee ${SYSTEMD_DIR}/${SERVICE_K3S} >/dev/null << EOF
|
$SUDO tee ${FILE_K3S_SERVICE} >/dev/null << EOF
|
||||||
[Unit]
|
[Unit]
|
||||||
Description=Lightweight Kubernetes
|
Description=Lightweight Kubernetes
|
||||||
Documentation=https://k3s.io
|
Documentation=https://k3s.io
|
||||||
|
@ -392,7 +410,7 @@ After=network.target
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=${SYSTEMD_TYPE}
|
Type=${SYSTEMD_TYPE}
|
||||||
EnvironmentFile=${SYSTEMD_DIR}/${SERVICE_K3S}.env
|
EnvironmentFile=${FILE_K3S_ENV}
|
||||||
ExecStartPre=-/sbin/modprobe br_netfilter
|
ExecStartPre=-/sbin/modprobe br_netfilter
|
||||||
ExecStartPre=-/sbin/modprobe overlay
|
ExecStartPre=-/sbin/modprobe overlay
|
||||||
ExecStart=${BIN_DIR}/k3s ${CMD_K3S_EXEC}
|
ExecStart=${BIN_DIR}/k3s ${CMD_K3S_EXEC}
|
||||||
|
@ -409,19 +427,86 @@ WantedBy=multi-user.target
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# --- write openrc service file ---
|
||||||
|
create_openrc_service_file() {
|
||||||
|
LOG_FILE=/var/log/${SYSTEM_NAME}.log
|
||||||
|
|
||||||
|
info "openrc: Creating service file ${FILE_K3S_SERVICE}"
|
||||||
|
$SUDO tee ${FILE_K3S_SERVICE} >/dev/null << EOF
|
||||||
|
#!/sbin/openrc-run
|
||||||
|
|
||||||
|
depend() {
|
||||||
|
after net-online
|
||||||
|
need net
|
||||||
|
}
|
||||||
|
|
||||||
|
start_pre() {
|
||||||
|
rm -f /tmp/k3s.*
|
||||||
|
}
|
||||||
|
|
||||||
|
supervisor=supervise-daemon
|
||||||
|
name="${SYSTEM_NAME}"
|
||||||
|
command="/usr/local/bin/k3s"
|
||||||
|
command_args="${CMD_K3S_EXEC} >>${LOG_FILE} 2>&1"
|
||||||
|
pidfile="/var/run/${SYSTEM_NAME}.pid"
|
||||||
|
respawn_delay=5
|
||||||
|
|
||||||
|
set -o allexport
|
||||||
|
if [ -f /etc/environment ]; then source /etc/environment; fi
|
||||||
|
if [ -f ${FILE_K3S_ENV} ]; then source ${FILE_K3S_ENV}; fi
|
||||||
|
set +o allexport
|
||||||
|
EOF
|
||||||
|
$SUDO chmod 0755 ${FILE_K3S_SERVICE}
|
||||||
|
|
||||||
|
$SUDO tee /etc/logrotate.d/${SYSTEM_NAME} >/dev/null << EOF
|
||||||
|
${LOG_FILE} {
|
||||||
|
missingok
|
||||||
|
notifempty
|
||||||
|
copytruncate
|
||||||
|
}
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
# --- write systemd or openrc service file ---
|
||||||
|
create_service_file() {
|
||||||
|
if [ "${HAS_SYSTEMD}" = "true" ]; then
|
||||||
|
create_systemd_service_file
|
||||||
|
elif [ "${HAS_OPENRC}" = "true" ]; then
|
||||||
|
create_openrc_service_file
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
# --- enable and start systemd service ---
|
# --- enable and start systemd service ---
|
||||||
systemd_enable_and_start() {
|
systemd_enable_and_start() {
|
||||||
info "systemd: Enabling ${SYSTEMD_NAME} unit"
|
info "systemd: Enabling ${SYSTEM_NAME} unit"
|
||||||
$SUDO systemctl enable ${SYSTEMD_DIR}/${SERVICE_K3S} >/dev/null
|
$SUDO systemctl enable ${FILE_K3S_SERVICE} >/dev/null
|
||||||
$SUDO systemctl daemon-reload >/dev/null
|
$SUDO systemctl daemon-reload >/dev/null
|
||||||
|
|
||||||
info "systemd: Starting ${SYSTEMD_NAME}"
|
info "systemd: Starting ${SYSTEM_NAME}"
|
||||||
$SUDO systemctl restart ${SYSTEMD_NAME}
|
$SUDO systemctl restart ${SYSTEM_NAME}
|
||||||
|
}
|
||||||
|
|
||||||
|
# --- enable and start openrc service ---
|
||||||
|
openrc_enable_and_start() {
|
||||||
|
info "openrc: Enabling ${SYSTEM_NAME} service for default runlevel"
|
||||||
|
$SUDO rc-update add ${SYSTEM_NAME} default >/dev/null
|
||||||
|
|
||||||
|
info "openrc: Starting ${SYSTEM_NAME}"
|
||||||
|
$SUDO ${FILE_K3S_SERVICE} restart
|
||||||
|
}
|
||||||
|
|
||||||
|
# --- startup systemd or openrc service ---
|
||||||
|
service_enable_and_start() {
|
||||||
|
if [ "${HAS_SYSTEMD}" = "true" ]; then
|
||||||
|
systemd_enable_and_start
|
||||||
|
elif [ "${HAS_OPENRC}" = "true" ]; then
|
||||||
|
openrc_enable_and_start
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- run the install process --
|
# --- run the install process --
|
||||||
{
|
{
|
||||||
verify_systemd
|
verify_system
|
||||||
setup_env ${INSTALL_K3S_EXEC} $@
|
setup_env ${INSTALL_K3S_EXEC} $@
|
||||||
download_and_verify
|
download_and_verify
|
||||||
create_symlinks
|
create_symlinks
|
||||||
|
@ -429,5 +514,5 @@ systemd_enable_and_start() {
|
||||||
systemd_disable
|
systemd_disable
|
||||||
create_env_file
|
create_env_file
|
||||||
create_service_file
|
create_service_file
|
||||||
systemd_enable_and_start
|
service_enable_and_start
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue