#!/bin/sh
#****************************************************************#
# ScriptName: ngkek3s.sh
# Author: xhy
# Create Date: 2020-05-13 16:32
# Modify Author: xhy
# Modify Date: 2022-09-15 13:34
# Version: 
#***************************************************************#

#######################################
# set variables below to create the config files, all files will create at ./config directory
#######################################

# master keepalived virtual ip address
export K3SHA_VIP=172.24.8.100

# master01 ip address
export K3SHA_IP1=172.24.8.71

# master02 ip address
export K3SHA_IP2=172.24.8.72

# master03 ip address
export K3SHA_IP3=172.24.8.73

# master01 hostname
export K3SHA_HOST1=master01

# master02 hostname
export K3SHA_HOST2=master02

# master03 hostname
export K3SHA_HOST3=master03

# master01 network interface name
export K3SHA_NETINF1=eth0

# master02 network interface name
export K3SHA_NETINF2=eth0

# master03 network interface name
export K3SHA_NETINF3=eth0

# keepalived auth_pass config
export K3SHA_KEEPALIVED_AUTH=412f7dc3bfed32194d1600c483e10ad1d

##############################
# please do not modify anything below
##############################

mkdir -p config/$K3SHA_HOST1/{keepalived,nginx-lb}
mkdir -p config/$K3SHA_HOST2/{keepalived,nginx-lb}
mkdir -p config/$K3SHA_HOST3/{keepalived,nginx-lb}
mkdir -p config/keepalived
mkdir -p config/nginx-lb

wget -c -P config/keepalived/ http://down.linuxsb.com/keepalived/k3s-keepalived.conf.tpl
wget -c -P config/keepalived/ http://down.linuxsb.com/keepalived/check_apiserver.sh
wget -c -P config/nginx-lb/ http://down.linuxsb.com/nginx-lb/k3s-nginx-lb.conf.tpl
wget -c -P config/nginx-lb/ http://down.linuxsb.com/nginx-lb/k3s-nginx-lb.conf.default
wget -c -P config/nginx-lb/ http://down.linuxsb.com/nginx-lb/k3s-nginx-lb.yaml

# create all keepalived files
chmod u+x config/keepalived/check_apiserver.sh
cp config/keepalived/check_apiserver.sh config/$K3SHA_HOST1/keepalived
cp config/keepalived/check_apiserver.sh config/$K3SHA_HOST2/keepalived
cp config/keepalived/check_apiserver.sh config/$K3SHA_HOST3/keepalived

sed \
-e "s/K3SHA_KA_STATE/BACKUP/g" \
-e "s/K3SHA_KA_INTF/${K3SHA_NETINF1}/g" \
-e "s/K3SHA_IPLOCAL/${K3SHA_IP1}/g" \
-e "s/K3SHA_KA_PRIO/102/g" \
-e "s/K3SHA_VIP/${K3SHA_VIP}/g" \
-e "s/K3SHA_KA_AUTH/${K3SHA_KEEPALIVED_AUTH}/g" \
config/keepalived/k3s-keepalived.conf.tpl > config/$K3SHA_HOST1/keepalived/keepalived.conf

sed \
-e "s/K3SHA_KA_STATE/BACKUP/g" \
-e "s/K3SHA_KA_INTF/${K3SHA_NETINF2}/g" \
-e "s/K3SHA_IPLOCAL/${K3SHA_IP2}/g" \
-e "s/K3SHA_KA_PRIO/101/g" \
-e "s/K3SHA_VIP/${K3SHA_VIP}/g" \
-e "s/K3SHA_KA_AUTH/${K3SHA_KEEPALIVED_AUTH}/g" \
config/keepalived/k3s-keepalived.conf.tpl > config/$K3SHA_HOST2/keepalived/keepalived.conf

sed \
-e "s/K3SHA_KA_STATE/BACKUP/g" \
-e "s/K3SHA_KA_INTF/${K3SHA_NETINF3}/g" \
-e "s/K3SHA_IPLOCAL/${K3SHA_IP3}/g" \
-e "s/K3SHA_KA_PRIO/100/g" \
-e "s/K3SHA_VIP/${K3SHA_VIP}/g" \
-e "s/K3SHA_KA_AUTH/${K3SHA_KEEPALIVED_AUTH}/g" \
config/keepalived/k3s-keepalived.conf.tpl > config/$K3SHA_HOST3/keepalived/keepalived.conf

echo "create keepalived files success. config/$K3SHA_HOST1/keepalived/"
echo "create keepalived files success. config/$K3SHA_HOST2/keepalived/"
echo "create keepalived files success. config/$K3SHA_HOST3/keepalived/"

# create all nginx files
sed \
-e "s/K3SHA_IP1/$K3SHA_IP1/g" \
-e "s/K3SHA_IP2/$K3SHA_IP2/g" \
-e "s/K3SHA_IP3/$K3SHA_IP3/g" \
config/nginx-lb/k3s-nginx-lb.conf.tpl > config/$K3SHA_HOST1/nginx-lb/nginx-lb.conf

sed \
-e "s/K3SHA_IP1/$K3SHA_IP1/g" \
-e "s/K3SHA_IP2/$K3SHA_IP2/g" \
-e "s/K3SHA_IP3/$K3SHA_IP3/g" \
config/nginx-lb/k3s-nginx-lb.conf.tpl > config/$K3SHA_HOST2/nginx-lb/nginx-lb.conf

sed \
-e "s/K3SHA_IP1/$K3SHA_IP1/g" \
-e "s/K3SHA_IP2/$K3SHA_IP2/g" \
-e "s/K3SHA_IP3/$K3SHA_IP3/g" \
config/nginx-lb/k3s-nginx-lb.conf.tpl > config/$K3SHA_HOST3/nginx-lb/nginx-lb.conf

echo "create nginx-lb files success. config/$K3SHA_HOST1/nginx-lb/"
echo "create nginx-lb files success. config/$K3SHA_HOST2/nginx-lb/"
echo "create nginx-lb files success. config/$K3SHA_HOST3/nginx-lb/"

cp config/nginx-lb/k3s-nginx-lb.conf.default config/$K3SHA_HOST1/nginx-lb/nginx-lb.conf.default
cp config/nginx-lb/k3s-nginx-lb.conf.default config/$K3SHA_HOST2/nginx-lb/nginx-lb.conf.default
cp config/nginx-lb/k3s-nginx-lb.conf.default config/$K3SHA_HOST3/nginx-lb/nginx-lb.conf.default

scp -rp config/$K3SHA_HOST1/nginx-lb root@$K3SHA_HOST1:/etc/kubernetes
scp -rp config/$K3SHA_HOST2/nginx-lb root@$K3SHA_HOST2:/etc/kubernetes
scp -rp config/$K3SHA_HOST3/nginx-lb root@$K3SHA_HOST3:/etc/kubernetes

scp -rp config/$K3SHA_HOST1/keepalived/* root@$K3SHA_HOST1:/etc/keepalived/
scp -rp config/$K3SHA_HOST2/keepalived/* root@$K3SHA_HOST2:/etc/keepalived/
scp -rp config/$K3SHA_HOST3/keepalived/* root@$K3SHA_HOST3:/etc/keepalived/

# create all nginx-lb pods
kubectl create -f config/nginx-lb/k3s-nginx-lb.yaml
