#!/bin/bash
#初始化环境
host_ip='192.168.1.161 192.168.1.162 192.168.1.163 192.168.1.164 192.168.1.165'
for ip in $host_ip;do
	##1,关闭交换分区
ssh $ip "swapoff -a && sysctl -w vm.swappiness=0 &&  sed -i '\$s/^/#/' /etc/fstab"
#	##2,安装ipvsadm相关工具
ssh $ip "yum makecache;yum install ipvsadm ipset sysstat conntrack libseccomp runc -y"
#	##3, 同步时间
ssh $ip  "yum install -y chrony && systemctl start chronyd && systemctl enable chronyd && chronyc sources"
#
#
#        ##4，加载需要模块,这步需要在修改内核参数之前，因为net.bridge.bridge-nf-call-ip6tables 两项需要先加载br_netfilter 模块
#	## 这个模块默认是不加载的
ssh $ip "cat <<EOF | sudo tee /etc/modules-load.d/loadk8s.conf
overlay
br_netfilter
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
EOF"
#        ##4.1 不重启，手动加载模块
ssh $ip "modprobe overlay" 
ssh $ip "modprobe br_netfilter" 
ssh $ip "modprobe ip_vs" 
ssh $ip "modprobe ip_vs_rr" 
ssh $ip "modprobe ip_vs_wrr" 
ssh $ip "modprobe ip_vs_sh" 
ssh $ip "modprobe nf_conntrack_ipv4" 
#	##5,需要内核参数，后面两项需要依赖 br_netfilter 模块
ssh $ip "cat > /etc/sysctl.d/k8s.conf << EOF
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF" 

ssh $ip "/usr/sbin/sysctl --system" 
#
#
#        ##6，设置/etc/hosts 文件,并这是hostname
#
ssh $ip "cat >> /etc/hosts << EOF
192.168.1.160 qm-k8s-prod-master-01
192.168.1.161 qm-k8s-prod-master-02
192.168.1.162 qm-k8s-prod-master-03
192.168.1.163 qm-k8s-prod-node-01
192.168.1.164 qm-k8s-prod-node-02
192.168.1.165 qm-k8s-prod-node-03
192.168.1.167 qm-k8s-prod-node-04
192.168.1.166 k8s-cluster-apiserver-lbs
EOF" 
#
 if [ "$ip" = "192.168.1.160" ];then
	 ssh $ip "hostnamectl set-hostname qm-k8s-prod-master-01"
 elif [ "$ip" = "192.168.1.161" ];then
	 ssh $ip "hostnamectl set-hostname qm-k8s-prod-master-02"
 elif [ "$ip" = "192.168.1.162" ];then
	 ssh $ip "hostnamectl set-hostname qm-k8s-prod-master-03"
 elif [ "$ip" = "192.168.1.163" ];then
	 ssh $ip "hostnamectl set-hostname qm-k8s-prod-node-01"
 elif [ "$ip" = "192.168.1.164" ];then
	 ssh $ip "hostnamectl set-hostname qm-k8s-prod-node-02"
 elif [ "$ip" = "192.168.1.165" ];then
	 ssh $ip "hostnamectl set-hostname qm-k8s-prod-node-03"
 elif [ "$ip" = "192.168.1.167" ];then
	 ssh $ip "hostnamectl set-hostname qm-k8s-prod-node-04"
else
	echo "没有配到任何主机"
 fi
           ####################################################################################################################
	   # Generate a new key and cert from CSR:
	   #   cfssl gencert -initca CSRJSON
           #   cfssl gencert -ca cert -ca-key key [-config config] [-profile profile] [-hostname hostname] CSRJSON
           #   cfssl gencert -remote remote_host [-config config] [-profile profile] [-label label] [-hostname hostname] CSRJSON
	   #   Re-generate a CA cert with the CA key and CSR:
	   #   cfssl gencert -initca -ca-key key CSRJSON
           #   Re-generate a CA cert with the CA key and certificate:
           #   cfssl gencert -renewca -ca cert -ca-key key
           ####################################################################################################################
           #  设置节点需要文件，并启动它
	   ####################################################################################################################
	done
