Skip to content

Instantly share code, notes, and snippets.

@cumet04
Created August 17, 2019 14:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cumet04/399269d3e29725f0d0a227a3fd6398f0 to your computer and use it in GitHub Desktop.
Save cumet04/399269d3e29725f0d0a227a3fd6398f0 to your computer and use it in GitHub Desktop.
k3s cloudinit script
#!/bin/bash
K3S_VERSION=v0.8.0
BIN_DIR=/usr/local/bin
# for master
#cat > /opt/k3s_env_put.sh << 'EOF'
##!/bin/bash
#
#ip=$(hostname | cut -d'.' -f1 | cut -d'-' -f2,3,4,5 | tr '-' '.')
#aws ssm put-parameter --region ap-northeast-1 --name /k3s/master/host --value "$ip" --type "String" --overwrite
#
#token=$(cat /var/lib/rancher/k3s/server/node-token)
#aws ssm put-parameter --region ap-northeast-1 --name /k3s/master/token --value "$token" --type "String" --overwrite
#EOF
#chmod +x /opt/k3s_env_put.sh
#
#service_section="
#Type=notify
#Environment=K3S_KUBECONFIG_MODE=644
#ExecStart=/usr/local/bin/k3s server --disable-agent
#ExecStartPost=/opt/k3s_env_put.sh
#"
# for agent
cat > /opt/k3s_env_setup.sh << 'EOF'
#!/bin/bash
host=$(aws ssm get-parameter --region ap-northeast-1 --name /k3s/master/host | grep '"Value"' | sed 's|[^:]*: "\([^"]*\).*|\1|')
echo "K3S_URL=https://${host}:6443" > /opt/k3s.env
token=$(aws ssm get-parameter --region ap-northeast-1 --name /k3s/master/token | grep '"Value"' | sed 's|[^:]*: "\([^"]*\).*|\1|')
echo -n "K3S_TOKEN=$token" >> /opt/k3s.env
EOF
chmod +x /opt/k3s_env_setup.sh
touch /opt/k3s.env
service_section="
Type=exec
ExecStart=/usr/local/bin/k3s agent
EnvironmentFile=/opt/k3s.env
ExecStartPre=/opt/k3s_env_setup.sh
"
# common for master / agent
curl -L https://github.com/rancher/k3s/releases/download/$K3S_VERSION/k3s -o $BIN_DIR/k3s
chown root:root $BIN_DIR/k3s
chmod +x $BIN_DIR/k3s
for cmd in kubectl crictl ctr; do
ln -s $BIN_DIR/k3s $BIN_DIR/$cmd
done
cat > /etc/systemd/system/k3s.service << EOF
[Unit]
Description=Lightweight Kubernetes
Documentation=https://k3s.io
After=network-online.target
[Service]
${service_section}
ExecStartPre=-/sbin/modprobe br_netfilter
ExecStartPre=-/sbin/modprobe overlay
KillMode=process
Delegate=yes
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
TimeoutStartSec=0
Restart=always
RestartSec=5s
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now k3s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment