1 脚本地址
链接:https://pan.baidu.com/s/1uut9qXxVmcnlZXq3v7Dw-g
提取码:qujc
2 前置准备
2.1 环境
192.168.38.38 master 4g
192.168.38.39 node1 4g
192.168.38.40 node2 4g
2.2 安装基础软件包
yum -y install wget net-tools nfs-utils lrzsz gcc gcc-c++ make cmake libxml2-devel openssl-devel curl curl-devel unzip sudo ntp libaio-devel wget vim ncurses-devel autoconf automake zlib-devel python-devel epel-release openssh-server socat ipvsadm conntrack ntpdate
2.3 关闭防火墙
systemctl stop firewalld && systemctl disable firewalld
2.4 时间同步
ntpdate cn.pool.ntp.org
2.5 关闭selinux
#所有节点
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
2.6 关闭交换分区
swapoff -a
sed -i 's/.*swap.*/#&/' /etc/fstab
2.7 修改内核
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
2.8 修改主机名
hostnamectl set-hostname master
hostnamectl set-hostname node1
hostnamectl set-hostname node2
2.9 修改host
vim /etc/hosts
192.168.38.38 master
192.168.38.39 node1
192.168.38.40 node2
2.10 设置网桥包经IPTables,core文件生成路径,配置永久生效
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables
echo """
vm.swappiness = 0
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
""" > /etc/sysctl.conf
sysctl -p
3 安装Docker
3.1 前置准备
#备份原来的yum
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
#下载阿里的yum源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
#生成新的yum缓存
yum makecache fast
#配置安装k8s需要的yum源
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
EOF
#清理缓存
yum clean all
#生成新的yum缓存
yum makecache fast
#更新软件包
yum -y update
#安装软件包
yum -y install yum-utils device-mapper-persistent-data lvm2
#添加新的软件源
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
3.2 安装docker
#安装
yum install -y docker-ce-19*
#启动
systemctl start docker
#查看状态
systemctl status docker
#开机自启
systemctl enable docker
#配置镜像加速
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"
],
"registry-mirrors": ["https://ep6965hb.mirror.aliyuncs.com"]
}
EOF
#重启
systemctl daemon-reload && systemctl restart docker
4 安装k8s
4.1 配置k8s,yum源
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
4.2 安装kubeadm、kubelet和kubectl
#安装
yum install -y kubelet-1.17.3 kubectl-1.17.3 kubeadm-1.17.3
#开机启动
systemctl enable kubelet && systemctl start kubelet
4.3 部署Master
#master节点,下载该节点需要的镜像
chmod +x master_images.sh
./master_images.sh
#master节点初始化,修改apiserver地址为master地址
kubeadm init \
--apiserver-advertise-address=192.168.38.38 \
--image-repository registry.cn-hangzhou.aliyuncs.com/google_containers \
--kubernetes-version v1.17.3 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=10.244.0.0/16
#记录token
kubeadm join 192.168.38.38:6443 --token mka9n9.tk47pjuno0aguc7d \
--discovery-token-ca-cert-hash sha256:1caac4d6cc02d3f3fd4da09be9e8cf8a80655ea35bc21a2cec530933478c023b
#master执行
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
#部署flannel
kubectl apply -f kube-flannel.yml
#查看flannel
kubectl get pods --all-namespaces
#查看master状态
kubectl get nodes
4.4 加入node
#在node1、node2节点执行命令
kubeadm join 192.168.38.38:6443 --token mka9n9.tk47pjuno0aguc7d \
--discovery-token-ca-cert-hash sha256:1caac4d6cc02d3f3fd4da09be9e8cf8a80655ea35bc21a2cec530933478c023b
#master查看节点
kubectl get nodes
5 部署ingress
#部署
kubectl apply -f ingress-controller.yaml
#查看所有pods
kubectl get pods --all-namespaces