环境要求
- OS环境:centos 7
- kubernetes版本:v1.7.2
- docker版本
- version:1.12.6
- go version: go1.6.4
- API version: 1.24
注意:docker 版本不能用最新版本,否则不兼容1.7的k8s.
需要准备的安装包
k8s组件镜像
1.7版本的k8s组件都运行在pod里,例如flannel,etcd,apiserver等都以镜像方式启动。(Download)
部署步骤
- 安装Docker
tar zxf docker.tar.gz -C
yum localinstall -y docker/*.rpm
#配置docker
setenforce 0
sed -i -e 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
#关闭防火墙,这步不能少
systemctl disable firewalld
systemctl stop firewalld
echo DOCKER_STORAGE_OPTIONS=\" -s overlay --selinux-enabled=false\" > /etc/sysconfig/docker-storage
#配置docker私有库地址,如果没有的话可以不要执行改命令
sed -i -e 's/dockerd/dockerd --insecure=registry=192.168.31.85:5523/g' /usr/lib/systemd/system/docker.service
systemctl daemon-reload && systemctl restart docker.service
- 安装k8s
#首先将k8s安装包copy到k8s目录中
yum localinstall -y k8s/*.rpm
rm -rf k8s
# 改变 kubelet 的 cgroup-driver 需要与docker一致(docker version查看)
sed -i -e 's/cgroup-driver=systemd/cgroup-driver=cgroupfs/g' /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
systemctl enable kubelet.service && systemctl start kubelet.service && rm -rf /etc/kubernetes
- 配置启动master
kubeadm init --kubernetes-version=v1.7.2 --pod-network-cidr=10.96.0.0/12
#环境变量设置
export KUBECONFIG=/etc/kubernetes/admin.conf
source ~/.bashrc
#安装flannel network
kubectl create -f create kube-flannel-rbac.yml
kubectl create -f kube-flannel.yml --namespace=kube-system
- node节点注册到master
#--skip-preflight-checks参数需要带上
kubeadm join --skip-preflight-checks --token=token masterIP:6443
此时k8s集群搭建好了,如果不出意外的话,使用kubectl get nodes 能看到集群节点信息。
思考
这样安装方式对于一个庞大的集群来说明显不满足要求的,下面本人写了一个自动部署脚本,自需要传入集群节点的ip user password 就能自动部署一个k8s集群。(在测试过程中脚本有些问题,因作业繁忙暂时没有进行调试,个人觉得思路没有问题,仅参考)
#------------------------------------安装Master------------------------------------
systemctl disable firewalld && systemctl stop firewallddcoer
#安装docker
curl -L http://$HTTP_SERVER/install_docker.sh | bash -s $HTTP_SERVER $PRIVATE_REGISTRY
#安装kubernetes
curl -L http://$HTTP_SERVER/install_k8s.sh > /tmp/k8s/install_k8s.sh
chmod +x /tmp/k8s/install_k8s.sh
/tmp/k8s/install_k8s.sh
# Change cgroup-driver for kubelet
sed -i -e 's/cgroup-driver=systemd/cgroup-driver=cgroupfs/g' /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
systemctl enable kubelet.service && systemctl start kubelet.service &&
#设置hosts 欺骗kubeadm
echo $HTTP_SERVER storage.googleapis.com >> /etc/hosts
# 这里一定要带上--pod-network-cidr参数,不然后面的flannel网络会出问题
export KUBE_ETCD_IMAGE=gcr.io/google_containers/etcd-amd64:3.0.17
kubeadm init --kubernetes-version=v1.7.2 --pod-network-cidr=10.96.0.0/12
export KUBECONFIG=/etc/kubernetes/admin.conf
# install flannel network
kubectl apply -f http://$HTTP_SERVER/yaml/kube-flannel-rbac.yml
kubectl apply -f http://$HTTP_SERVER/yaml/kube-flannel.yml --namespace=kube-system
#install dashboard
kubectl create -f http://$HTTP_SERVER/yaml/kubernetes-dashboard.yml
# show pods
kubectl get po --all-namespaces
# show tokens
result=`kubeadm token list`
temp=${result##*DESCRIPTION}
token=${temp%%<forever*}
echo token:$token
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bashrc
source ~/.bashrc
#-------------------------------------安装minion-----------------------------------------
curl -L http://$HTTP_SERVER/login.sh > /tmp/k8s/login.sh
chmod +x /tmp/k8s/login.sh
for((i=2;i<=$#;i++));
do
param=${!i}
host=${param%%/*}
user=${host%%/*}
psw=${user%%/*}
/tmp/k8s/login.sh $host $user $psw $token $PRIVATE_REGISTRY $1:6443
done
- login.sh
#!/usr/bin/expect -f
set hostname [lindex $argv 0]
set user [lindex $argv 1]
set passwd [lindex $argv 2]
set server [lindex $argv 3]
set token [lindex $argv 4]
set master [lindex $argv 5]
set registry [lindex $argv 6]
set timeout 20
spawn ssh $user@$hostname
expect {
"*continue connecting (yes/no)?" { send "yes\r" }
"*password*" { send "$passwd\r" }
}
expect {
"*continue connecting (yes/no)?" { send "yes\r" }
"*password*" { send "$passwd\r" }
}
expect "#"
send "curl -L http://$server/install.sh | bash -s $server $registry $token $master\r"
expect "#"
send "exit\r"
- install_k8s.sh
- install_minion.sh