Kubernetes 脚本
准备:
CPU 2核(必须2核以上)
内存 2G
硬盘 20G
安装脚本
#!/bin/bash
# 脚本名称:init.sh
# 脚本功能:初始化
# 执行权限:chmod +x init.sh
# 执行命令:./init.sh
# 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
# 关闭selinux
setenforce 0
sed -i 's/SELINUX=enforcing/\SELINUX=disabled/' /etc/selinux/config
# 关闭swap
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
# 在主机名静态查询表中添加3台主机
cat >> /etc/hosts << EOF
192.168.60.151 k8smaster
192.168.60.152 k8snode1
192.168.60.153 k8snode2
EOF
# 将桥接的IPv4流量传递到iptables的链
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
# 使k8s配置生效
sysctl --system
# 时间同步
yum install ntpdate -y
ntpdate time.windows.com
# 根据规划设置主机名
echo "
1 k8smaster 192.168.60.151
2 k8snode1 192.168.60.152
3 k8snode2 192.168.60.153
"
# 循环检测
while :
do
read -p "更改当前主机名为(请输入数字):" num
case $num in
1) echo '你选择了 1'
hostnamectl set-hostname k8smaster
exit
;;
2) echo '你选择了 2'
hostnamectl set-hostname k8snode1
exit
;;
3) echo '你选择了 3'
hostnamectl set-hostname k8snode2
exit
;;
*) echo '请输入 1 到 3 之间的数字'
;;
esac
done
# 配置一下Docker的yum源【阿里云】
cat >/etc/yum.repos.d/docker.repo<<EOF
[docker-ce-edge]
name=Docker CE Edge - \$basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/\$basearch/edge
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
EOF
# 然后yum方式安装docker
yum -y install docker-ce
# 配置docker的镜像源【阿里云】
cat >> /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF
# 启动docker
systemctl enable docker
systemctl start docker
systemctl status docker
# 查看docker版本
docker --version
#!/bin/bash
# 脚本名称:k8s.sh
# 脚本功能:安装k8s
# 执行权限:chmod +x k8s.sh
# 执行命令:./k8s.sh
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=0
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
# 安装kubelet、kubeadm、kubectl,同时指定版本
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
# 设置开机自启【这里暂时先不启动kubelet】
systemctl enable kubelet
# k8s初始化
kubeadm init --apiserver-advertise-address=${ip} --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.18.0 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
|