基于kubeadm部署kubernetes集群(一)
一、主机规划
角色名称 配置 域名 IP地址 master 2C8G/4G master.example.com 192.168.0.1 node1 2C4G/2G node1.example.com 192.168.0.2 node2 2C4G/2G node2.example.com 192.168.0.3
二、软件要求
操作系统版本: CentOS Linux release 7.3.1611 (Core) Docker版本:1.13.1, build 07f3374/1.13.1 Kubernetes版本: v1.13.1
三、部署步骤
1.systemctl stop firewalld && systemctl disable firewalld #关闭防火墙,关闭开机自动开启防火墙
2.vim /etc/selinux/config 修改第6行,SELINUX=disabled
3.swapoff -a #关闭swap设置 将/etc/fstab文件中所有设置为swap的设备关闭
4.yes | cp /etc/fstab /etc/fstab_bak #备份/etc/fstab
5.grep -v swap /etc/fstab_bak > /etc/fstab #重写/etc/fstab
6.vim /etc/hosts #更改hosts文件,配置域名
192.168.33.9 master.example.com
192.168.33.10 node1.example.com
192.168.33.11 node2.example.com
7.安装docker
yum -y install docker
systemctl start docker && systemctl enable docker #启动docker并设置开机启动
#启动时可能会报错:Job for docker.service failed because。。。参考另一篇博客点击此处即可查看
8.配置阿里云镜像源
vim /etc/yum.repos.d/k8s.repo
[kubernetes]
name=kubernetes repo
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/gpgcheck=0
9.安装kubelet kubeadm kubectl
#这里需要注意,如果你直接运行指令yum -y install kubelet kubeadm kubectl,这里将会自动为你下载最新版本,我们这里采用低版本(1.13.0)进行安装
yum -y install kubelet-1.13.0 kubeadm-1.13.0 kubernetes-cni-0.6.0 kubectl-1.13.0
systemctl enable kubelet && systemctl start kubelet
10.执行脚本远程拉取镜像(脚本附在最后)
11.master节点-配置kubectl、flannel网络(注意这里的ip地址用本机ip)
kubeadm init --kubernetes-version=v1.13.1 --apiserver-advertise-address 192.168.0.9 --pod-network-cidr=10.244.0.0/16
注意这里完成操作后会有这样一段提示:
You can now join any number of machines by running the following on each node
as root:
kubeadm join 192.168.0.9:6443 --token vatur3.j0y4md12rz4h04mp --discovery-token-ca-cert-hash sha256:f684729944a449a5ab20d40a664b1b3fca99a8d 33c09fb5db8e1ce125c6e4632
将kubeadm这段指令记好,随后添加节点需要用到
12.配置kubectl
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
source ~/.bash_profile
13.
curl https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml -o kube-flannel.yml
docker tag k8s.gcr.io/flannel:v0.10.0-amd64 quay.io/coreos/flannel:v0.11.0-amd64 #更改镜像名
kubectl apply -f kube-flannel.yml
14.执行 kubectl get nodes
此时进行确认,若master节点 not ready,重启docker即可:
systemctl restart docker
kubectl get nodes
NAME STATUS ROLES AGE VERSION
master.example.com Ready master 25m v1.13.0
[root@bogon vagrant]# kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-86c58d9df4-njlhd 1/1 Running 0 21h
kube-system coredns-86c58d9df4-v4p2j 1/1 Running 0 21h
kube-system etcd-bogon 1/1 Running 5 21h
kube-system kube-apiserver-bogon 1/1 Running 5 21h
kube-system kube-controller-manager-bogon 1/1 Running 5 21h
kube-system kube-flannel-ds-amd64-5426r 1/1 Running 0 20h
kube-system kube-proxy-f6txg 1/1 Running 0 21h
kube-system kube-scheduler-bogon 1/1 Running 5 21h
15.此时master节点已初步部署完成
###远程拉取镜像,tag镜像py脚本
#coding:utf8 import os S_registry = 'registry.cn-beijing.aliyuncs.com/imcto/' #S_registry = 'registry.cn-shenzhen.aliyuncs.com/cp_m/' D_registry = 'k8s.gcr.io/' master_image = ['kube-apiserver:v1.13.1','kube-controller-manager:v1.13.1', 'kube-scheduler:v1.13.1','kube-proxy:v1.13.1','pause:3.1', 'etcd:3.2.24','coredns:1.2.6','flannel:v0.10.0-amd64'] #master_image = ['flannel:v0.10.0-amd64'] #下载镜像 def PullImage(registry,images): print("-共%s个镜像" %(len( images),)) index = 1 for image in images: print("开始下载第[%d]个镜像---> [%s]" %(index, image)) cmd = "docker pull " + registry + image os.system(cmd) print("done!") index += 1 #更改tag def TagImage(sregistry,dregistry,images): print("一共%s个镜像" %(len(images),)) index = 1 for image in images : print("开始tag第[%d]个镜像---> [%s] ---> [%s]" %(index, sregistry+image, dregistry+image)) cmd = "docker tag " + sregistry+image + " " + dregistry + image os. system(cmd) print("done!") index += 1 #run if __name__=='__main__': # PullImage(S_registry , master_image) TagImage(S_registry, D_registry, master_image)