系统 centos 7.5 机器数量3台 ,主机名 k8s-m1 k8s-n1 k8s-n2
可以直接使用这个脚本安装 到初始化的时候手动一下就可以 修改ip 和主机名为自己的

export NODE_IPS=(172.16.129.130 172.16.129.131 172.16.129.132)
export NODE_NAME=(k8s-m1 k8s-n1 k8s-n2)

#关闭防火墙

for ip in ${NODE_IPS[@]}
do
ssh root@${ip} "systemctl stop firewalld && systemctl disable firewalld"
ssh root@${ip} "swapoff -a && setenforce 0"
done

#修改主机名

i=0
for ip in ${NODE_IPS[@]}
do
let i++
ssh root@${ip} "hostnamectl --static set-hostname echo ${NODE_NAME[@]} | cut -d " " -f $i"
done

#添加hosts文件

for (( i=0; i<=3; i++ ))
do
echo "${NODE_IPS[i]} ${NODE_NAME[i]} ${NODE_NAME[i]}" >> /tmp/hosts
done

#分发hosts 文件

for ip in ${NODE_IPS[@]}
do
scp /tmp/hosts root@${ip}:/etc/
done

#添加系统参数文件

cat > /tmp/k8s.conf <<EOF
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
vm.swappiness=0
vm.overcommit_memory=1
vm.panic_on_oom=0
fs.inotify.max_user_watches=89100
EOF

#让系统参数生效

for ip in ${NODE_IPS[@]}
do
scp /tmp/k8s.conf root@${ip}:/etc/sysctl.d/
ssh root@${ip} "sysctl -p /etc/sysctl.d/k8s.conf || modprobe br_netfilter && sysctl -p /etc/sysctl.d/k8s.conf"
done

#添加docker源 并安装docker

YUM=https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
for ip in ${NODE_IPS[@]}
do
ssh root@${ip} "yum install -y yum-utils"
ssh root@${ip} "yum-config-manager --add-repo $YUM"
ssh root@${ip} "yum install -y docker-ce-18.06.0.ce"
ssh root@${ip} "systemctl enable docker && systemctl restart docker"
done

#安装kubeadm kubectl kubelet 这三个插件
#安装yum 源

cat > /etc/yum.repos.d/kubernetes.repo <<EOF
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

#安装这个三个命令

for ip in ${NODE_IPS[@]}
do
ssh root@${ip} "yum install kubectl-1.12.5 kubelet-1.12.5 kubeadm-1.12.5"
ssh root@${ip} "systemctl enable kubelet && systemctl restart kubelet"
done

###到这里就截止了为了不出错还是手动初始化一下系统 如果下面的方式有问题可以使用镜像的方式

TOKEN=kubeadm token generate
kubeadm init --image-repository registry.aliyuncs.com/google_containers \
--service-cidr 10.96.0.0/12 \
--kubernetes-version v1.12.5 \
--pod-network-cidr 10.244.0.0/16 \
--token $TOKEN \
--apiserver-advertise-address 172.16.129.130

mkdir ~/.kube && cp /etc/kubernetes/admin.conf ~/.kube/config
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.10.0/Documentation/kube-flannel.yml
systemctl daemon-reload ; systemctl restart kubelet

如果上面的方式有问题,可以使用下面拉镜像的方式来安装

IMGFILE=“registry.cn-hangzhou.aliyuncs.com/google_containers”

#获取镜像apiserver 镜像

docker pull $IMGFILE/kube-apiserver:v1.12.5
docker tag $IMGFILE/kube-apiserver:v1.12.5 k8s.gcr.io/kube-apiserver:v1.12.5
docker rmi $IMGFILE/kube-apiserver:v1.12.5
docker pull $IMGFILE/kube-controller-manage:v1.12.5
docker tag $IMGFILE/kube-controller-manage:v1.12.5 k8s.gcr.io/kube-controller-manage:v.1.12.5
docker rmi $IMGFILE/kube-controller-manage:v1.12.5
docker pull $IMGFILE/kube-scheduler:v1.12.5
docker tag $IMGFILE/kube-scheduler:v1.12.5 k8s.gcr.io/kube-scheduler:v1.12.5
docker rmi $IMGFILE/kube-scheduler:v1.12.5
docker pull $IMGFILE/kube-proxy:v1.12.5
docker tag $IMGFILE/kube-proxy:v1.12.5 k8s.gcr.io/kube-proxy:v1.12.5
docker rmi $IMGFILE/kube-proxy:v1.12.5
docker pull $IMGFILE/etcd:3.2.24
docker tag $IMGFILE/etcd:3.2.24 k8s.gcr.io/etcd:3.2.24
docker rmi $IMGFILE/etcd:3.2.24
docker pull $IMGFILE/pause:3.1
docker tag $IMGFILE/pause:3.1 k8s,gcr.io/pause:3.1
docker rmi $IMGFILE/pause:3.1
docker pull $IMGFILE/coredns:1.2.2
docker tag $IMGFILE/coredns:1.2.2 k8s.gcr.io/coredns:1.2.2
docker rmi $IMGFILE/coredns:1.2.2

echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
echo 1 > /proc/sys/net/ipv4/ip_forward
初始化k8s 集群

TOKEN=kubeadm token generate
kubeadm init --service-cidr 10.96.0.0/12 \
--kubernetes-version v1.12.5 \
--pod-network-cidr 10.244.0.0/16 \
--token $TOKEN \
--apiserver-advertise-address 172.16.129.130
mkdir ~/.kube && cp /etc/kubernetes/admin.conf ~/.kube/config
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.10.0/Documentation/kube-flannel.yml
systemctl daemon-reload ; systemctl restart kubelet

#部署node 分别在每台机器上拉取proxy 和pause 镜像 然后使用初始化时生成的命令,注册一下就可以了

docker pull $IMGFILE/kube-proxy:v1.12.5
docker tag $IMGFILE/kube-proxy:v1.12.5 k8s.gcr.io/kube-proxy:v1.12.5
docker rmi $IMGFILE/kube-proxy:v1.12.5
docker pull $IMGFILE/pause:3.1
docker tag $IMGFILE/pause:3.1 k8s,gcr.io/pause:3.1
docker rmi $IMGFILE/pause:3.1

#下面的这条命令注册node这个应该是部署完master以后
#kubeadm join --token b0f7b8.8d1767876297d85c 172.16.129.160:6443 --discovery-token-ca-cert-hash sha256:739d936954a752d44d2f2282dd645083259826f2c24a651608a6ac2081106cd
#如果有不对的地方希望指出,新手记录部署过程,