github地址:https://github.com/Johnny-Demo/deploy/tree/k8s-cluster
README.md
要修改脚本里面的 ip 地址,根据自己情况修改,然后在部署,要不然会出错。
执行 kernel.sh 升级 linux 内核,关闭 selinux 和 swap 分区,重启服务器。
执行 run.sh 部署k8s,master 和 node 手动加入集群,无法自动获取加入集群的认证。(没有dashboard部署文件,没写)
注意:
1、只要是 ip 都需要修改
2、只运行 kernel.sh 和 run.sh 脚本就行,其他脚本不需要执行
3、一共三台主节点,只有第一台主节点 IP 不用写在脚本里,其他 matster 节点 ip 和 其他 node 节点 ip 都要写在脚本里,所有脚本都是在第一台 matster 节点,需要把第一台脚本发送给其他节点,所以第一台 IP 不用写。
cat host_ip.txt
192.192.191.10 k8s-master1 root admin
192.192.191.11 k8s-master2 root admin
192.192.191.12 k8s-master3 root admin
192.192.191.13 k8s-node1 root admin
cat ssh.sh
#!/bin/bash
# 创建函数
function sshFreeLogin()
{
#1.检测expect服务是否存在,不存在则使用yum安装expect
expectIsExists=`rpm -qa | grep expect`
if [ -z $expectIsExists ]
then
yum -y install expect
fi
#2.密钥对不存在则创建密钥
[ ! -f /root/.ssh/id_rsa.pub ] && ssh-keygen -t rsa -P "" -f /root/.ssh/id_rsa
while read line;do
#提取文件中的ip
hostname=`echo $line | cut -d " " -f2`
#提取文件中的用户名
user_name=`echo $line | cut -d " " -f3`
#提取文件中的密码
pass_word=`echo $line | cut -d " " -f4`
expect <<EOF
#复制公钥到目标主机
spawn ssh-copy-id $hostname
expect {
#expect实现自动输入密码
"yes/no" { send "yes\n";exp_continue }
"password" { send "$pass_word\n";exp_continue }
eof
}
EOF
# 读取存储ip的文件,host_ip文件所在的目录地址
done < /root/host_ip.txt
}
sshFreeLogin
cat deploy.sh
#!/bin/bash
# 添加 hosts 文件解析
cat >> /etc/hosts <<EOF
192.192.191.10 k8s-master1
192.192.191.11 k8s-master2
192.192.191.12 k8s-master3
192.192.191.13 k8s-node1
EOF
# 关闭防火墙
systemctl stop firewalld && systemctl disable firewalld 2>&1 >/dev/null
# 安装 chrony
yum -y install chrony
systemctl start chronyd && systemctl enable chronyd
# 同步服务器时间
timedatectl set-timezone Asia/Shanghai && chronyc -a makestep 2>&1 >/dev/null
# 安装 ipset
yum -y install ipvsadm ipset sysstat conntrack libseccomp 2>&1 >/dev/null
# 判断 ipvs.modules 文件是否存在不存在则创建
if [ ! -f "/etc/sysconfig/modules/ipvs.modules" ]
then
touch /etc/sysconfig/modules/ipvs.modules
else
echo "文件已存在"
fi
# 开启 br_netfilter 模块 (临时,重启会失效)
modprobe br_netfilter
# 把以下内容覆盖到 ipvs.modules 文件
cat > /etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack # 低内核改成 modprobe -- nf_conntrack_ipv4
EOF
# 给脚本添加权限并执行查看是否生效
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4
# 判断 k8s.conf 文件是否存在不存在则创建
if [ ! -f "/etc/sysctl.d/k8s.conf" ]
then
touch /etc/sysctl.d/k8s.conf
else
echo "文件已存在"
fi
# 把以下内容覆盖到 k8s.conf 文件
cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
EOF
# 立即生效
sysctl -p /etc/sysctl.d/k8s.conf
# 下载 docker 源并安装 docker
wget -P /etc/yum.repos.d/ https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 2>&1 >/dev/null
yum -y install docker-ce-18.06.0.ce-3.el7 2>&1 >/dev/null
systemctl enable docker && systemctl start docker
# 判断 daemon.json 文件是否存在不存在则创建
if [ ! -f "/etc/docker/daemon.json" ]
then
touch /etc/docker/daemon.json
else
echo "文件已存在"
fi
# 配置 k8s 驱动
cat > /etc/docker/daemon.json <<EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
]
}
EOF
# 重启 docker 并查看驱动是否生效
systemctl restart docker
docker info | grep Cgroup
# 判断 kubernetes.repo 文件是否存在不存在则创建
if [ ! -f "/etc/yum.repos.d/kubernetes.repo" ]
then
touch /etc/yum.repos.d/kubernetes.repo
else
echo "文件已存在"
fi
# 把以下内容覆盖到 kubernetes.repo 文件
cat > /etc/yum.repos.d/kubernetes.repo <<EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
# 安装 kubeadm kubelet kubectl 并加入开机启动
yum -y install kubeadm-1.18.2 kubelet-1.18.2 kubectl-1.18.2 2>&1 >/dev/null
systemctl daemon-reload && systemctl enable kubelet
cat install_config.sh
#!/bin/bash
# 安装 Keepalived 和 Haproxy 并加入开机启动
yum -y install keepalived haproxy
systemctl enable keepalived && systemctl enable haproxy
# 备份原文件
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak
# 把以下内容到覆盖到 Keepalived.conf 文件
cat > /etc/keepalived/keepalived.conf <<EOF
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
# 添加如下内容
script_user root
enable_script_security
}
vrrp_script haproxy {
script "/etc/keepalived/haproxy.sh"
interval 3
weight -2
fall 10
rise 2
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.192.191.4
}
track_script {
haproxy
}
}
EOF
# 把以下内容覆盖到 Haproxy.cfg 文件
cat > /etc/haproxy/haproxy.cfg <<EOF
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend kubernetes-apiserver
mode tcp
bind *:16443
option tcplog
default_backend kubernetes-apiserver
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
listen stats
bind *:1080
stats auth admin:awesomePassword
stats refresh 5s
stats realm HAProxy\ Statistics
stats uri /admin?stats
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend kubernetes-apiserver
mode tcp
balance roundrobin
server k8s-master1 192.192.191.10:6443 check
server k8s-master2 192.192.191.11:6443 check
server k8s-master3 192.192.191.12:6443 check
EOF
# 定义 IP 组
ip="192.192.191.11 192.192.191.12"
# 发送文件到其他主机并修改文件
for file in $ip
do
ssh root@$file yum -y install keepalived haproxy
ssh root@$file systemctl enable keepalived && systemctl enable haproxy
ssh root@$file mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
ssh root@$file mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak
scp /etc/keepalived/keepalived.conf $file:/etc/keepalived/
scp /etc/haproxy/haproxy.cfg $file:/etc/haproxy/
ssh root@$file sed -i 's/MASTER/BACKUP/g' /etc/keepalived/keepalived.conf
done
# 修改其他 Keepalived 节点选举权
ip2="192.192.191.11"
ssh root@$ip2 sed -i 's/100/90/g' /etc/keepalived/keepalived.conf
ip3="192.192.191.12"
ssh root@$ip3 sed -i 's/100/80/g' /etc/keepalived/keepalived.conf
# 运行 check.sh 脚本自动创建
source /root/deploy/check.sh
# 发送 check.sh 脚本到其他主节点并运行
for file2 in $ip
do
scp /root/deploy/check.sh $file2:/root/check.sh
ssh root@$file2 sh /root/check.sh
done
cat check.sh
#!/bin/bash
# 创建 haproxy.sh 脚本
touch /etc/keepalived/haproxy.sh
# 高可用检测脚本
cat > /etc/keepalived/haproxy.sh <<EOF
#!/bin/bash
if [ ps -C haproxy --no-header | wc -l -eq 0 ]
then
systmectl start haproxy
if [ ps -C haproxy --no-header | wc -l -eq 0 ]
then
killall -9 haproxy
echo "haproxy down" | mail -s "haproxy"
sleep 3600
fi
fi
EOF
# 启动 keepalived 和 haproxy
systemctl start keepalived && systemctl start haproxy
# 创建 log.txt 文件
touch /root/log.txt
# 把启动日志保存到 log.txt
systemctl status keepalived > /root/log.txt && systemctl status haproxy >> /root/log.txt
cat yaml.sh
#!/bin/bash
# 替换参数
sed -i 's/1.2.3.4/192.192.191.10/g' /root/kubeadm-config.yaml
sed -i 's/1.18.0/1.18.2/g' /root/kubeadm-config.yaml
# 添加内容
sed -i '/clusterName/a controlPlaneEndpoint: "192.192.191.4:16443"' /root/kubeadm-config.yaml
sed -i '/dnsDomain/a\ podSubnet: "10.244.0.0/16"' /root/kubeadm-config.yaml
# 文件末尾追加内容
echo "---" >> /root/kubeadm-config.yaml
echo "apiVersion: kubeproxy.config.k8s.io/v1alpha1" >> /root/kubeadm-config.yaml
echo "kind: KubeProxyConfiguration" >> /root/kubeadm-config.yaml
echo "featureGates:" >> /root/kubeadm-config.yaml
sed -i '/featureGates/a\ SupportIPVSProxyMode: true' /root/kubeadm-config.yaml
echo "mode: ipvs" >> /root/kubeadm-config.yaml
cat cni.sh
#!/bin/bash
# 安装网络插件
git clone https://github.com/flannel-io/flannel.git && kubectl apply -f /root/deploy/flannel/Documentation/kube-flannel.yml
cat reset.sh
#!/bin/bash
# 重置k8s集群
kubeadm reset
cat kernrl.sh
#!/bin/bash
# 升级内核并修改内核启动顺序
yum -y update 2>&1 >/dev/null
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org 2>&1 >/dev/null
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm 2>&1 >/dev/null
yum -y install --enablerepo=elrepo-kernel kernel-ml 2>&1 >/dev/null
grub2-set-default 0 2>&1 >/dev/null
# 关闭swap分区
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
# 关闭 selinux
sed -i 's/^ *SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config 2>&1 >/dev/null
# 重启
reboot
cat run.sh文章来源:https://www.toymoban.com/news/detail-733804.html
#!/bin/bash
# master 组(根据自己节点添加,一共三台主节点,192.192.191.10这台主节点不用写,因为是从这台主节点往其他主节点传送文件。)
ip1="192.192.191.11 192.192.191.12"
# node 组(根据自己节点添加)
ip2="192.192.191.13"
# IP 组(master 和 node 混合,根据自己节点添加)
ip3="192.192.191.11 192.192.191.12 192.192.191.13"
# 调用 deploy.sh 脚本
source /root/deploy/deploy.sh
# 发送 deploy.sh 到其他服务器
for host in $ip3
do
scp /root/deploy/deploy.sh $host:/root/
ssh root@$host sh /root/deploy.sh
done
# 调用 install-config.sh 脚本
source /root/deploy/install_config.sh
# 获取默认配置文件
kubeadm config print init-defaults > /root/kubeadm-config.yaml
# 调用 yaml.sh 脚本
source /root/deploy/yaml.sh
# 下载 k8s 镜像
kubeadm config images pull --config /root/kubeadm-config.yaml
# 创建 k8s.txt 文件
touch /root/k8s.txt
# 初始化集群结果保存到 k8s.txt 文件
kubeadm init --config /root/kubeadm-config.yaml > /root/k8s.txt
# 发送 k8s.txt 到其他节点
for a in $ip3
do
scp /root/k8s.txt $a:/root/
done
# 执行以下命令
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# 调用 join_master.sh 把证书发送到其他 master 节点
for cer in $ip1
do
ssh root@$cer mkdir -p /etc/kubernetes/pki/etcd
scp /etc/kubernetes/pki/ca.* $cer:/etc/kubernetes/pki/
scp /etc/kubernetes/pki/sa.* $cer:/etc/kubernetes/pki/
scp /etc/kubernetes/pki/front-proxy-ca.* $cer:/etc/kubernetes/pki/
scp /etc/kubernetes/pki/etcd/ca.* $cer:/etc/kubernetes/pki/etcd/
scp /etc/kubernetes/admin.conf $cer:/etc/kubernetes/
done
# 把 admin.conf 复制到其他 node 节点
for cer2 in $ip2
do
scp /etc/kubernetes/admin.conf $cer2:/etc/kubernetes/
done
# 安装网络插件
git clone https://github.com/flannel-io/flannel.git && kubectl apply -f /root/deploy/flannel/Documentation/kube-flannel.yml
有不理解的地方可以私信我文章来源地址https://www.toymoban.com/news/detail-733804.html
到了这里,关于shell 脚本一键部署 k8s 高可用集群的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!