虚拟机创建
在自己的Mac系统里面利用Parallels Desktop
创建3台虚拟机,具体信息如下:
1 | CentOS7-Node1: |
Master安装
选择CentOS7-Node1
机器作为Master节点。
配置yum
更新yum源:
1 | [parallels@CentOS7-Node1 yum.repos.d]$ cd /etc/yum.repos.d |
安装Kubernetes环境
评估下来,利用kubeadm来搭建是大家比较推荐的,而且公司的集群也是。所以毫不忧虑就用kubeadm。
1 | [parallels@CentOS7-Node1 yum.repos.d]$ yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes |
关于yum的配置与升级
1 | yum install -y yum-utils device-mapper-persistent-data lvm2 |
启动docker
启动Docker,加入开启机动项:
1 | [parallels@CentOS7-Node1 ~]$ sudo systemctl enable docker && systemctl start docker |
启动kubelet
启动kubelet,加入开机启动项:
1 | sudo systemctl enable kubelet && systemctl start kubelet |
kubeadm config
1 | [parallels@CentOS7-Node1 Workspace]$ kubeadm config print init-defaults |
1 | kubeadm config print init-defaults > /home/parallels/Workspace/init.default.yaml |
配置Docker
首先要安装好Docker环境,请参考之前的 http://www.cyblogs.com/centos7shang-an-zhuang-docker/
Docker的一些相关命令
1 | yum install docker-ce-18.09.9-3.el7 # 指定版本为18.09.9-3.el7 |
下载kubernetes的相关镜像
配置镜像地址,但没什么用。后面还是需要用到国内的镜像:
1 | echo '{"registry-mirrors":["https://docker.mirrors.ustc.edu.cn"]}' > /etc/docker/daemon.json |
查看一下kubernetes依赖的镜像名称以及版本
1 | [parallels@CentOS7-Node1 Workspace]$ kubeadm config images list |
如果网络OK,应该直接执行这个命令即可,但实际会报错误。
1 | [parallels@CentOS7-Node1 Workspace]$ sudo kubeadm config images pull --config=/home/parallels/Workspace/init.default.yaml |
获取镜像
通过另外一种方式来获取镜像:
1 | touch kubeadm.sh |
拉取镜像
1 | chmod u+x kubeadm.sh # 添加权限 |
剩下的就是耐心等待……
查看最终本地的镜像
1 | [root@CentOS7-Node1 Workspace]# docker images |
1 | [parallels@CentOS7-Node1 Workspace]$ sudo kubeadm init --config=init.default.yaml |
关闭防火墙
解决掉防火墙的问题,请参阅:http://www.cyblogs.com/centos7cha-kan-he-guan-bi-fang-huo-qiang/
cgroupfs错误
1 | detected "cgroupfs" as the Docker cgroup driver |
新增:/etc/docker/daemon.json
1 | { |
禁止swap
还是发现需要禁止掉swap
1 | Oct 22 16:35:36 CentOS7-Node1 kubelet[1395]: F1022 16:35:36.065168 1395 server.go:271] failed to run Kubelet: running with swap on is not supported, please disable swap! or set --fail-swap-on flag to false. /proc/swaps contained: [Filename Type Size Used Priority /dev/dm-1 partition 2097148 29952 -1] |
1 | swapoff -a |
再次启动kubeadm init
1 | kubeadm init --config=init.default.yaml |
出现错误了,变更Docker的版本后,继续执行,还是会报错误。
1 | [ERROR FileAvailable--etc-kubernetes-manifests-kube-apiserver.yaml]: /etc/kubernetes/manifests/kube-apiserver.yaml already exists |
重设kubeadm
这里需要重设kubeadm
了。具体操作如下:
1 | kubeadm reset |
journalctl查看日志
1 | journalctl -xefu kubelet |
这里还是会报错,因为之前的
1 | apiVersion: kubeadm.k8s.io/v1beta2 |
继续执行init的过程,kubeadm init --config=/home/parallels/Workspace/init.default.yaml
1 | [addons] Applied essential addon: CoreDNS |
真心不容易,这里一直反反复复执行。只要是images
的版本问题以及init
的过程容易出错。
验证configmap
1 | [root@CentOS7-Node1 ~]# kubectl get -n kube-system configmap |
安装Node,加入集群
安装跟Master一直的基本环境,包括docker,kubelet,kubeadm等,重复上面的动作。
1 | scp root@10.211.55.7:/home/parallels/Workspace/init.default.yaml . |
为kubeadm
命令生成配置文件,创建join-config.yaml
,内容如下:
1 | apiVersion: kubeadm.k8s.io/v1beta2 |
其中,apiServerEndpoint的值来自于Master的服务器地址,这里就是10.211.55.7
。token
和tlsBootstrapToken
的值就来自于kubeadm init
安装Master
的最后一行提示信息。这里一定要注意yaml文件的格式,否则执行会报错误。
1 | [root@CentOS7-Node2 Workspace]# kubeadm join --config=join-config.yaml |
安装网络插件
去Master机器,执行:
1 | [root@CentOS7-Node1 Workspace]# kubectl get nodes |
这里显示的是NotReady
状态,是因为还没有安装CNI
网络插件。我们选择weave
插件来安装。
1 | [root@CentOS7-Node1 Workspace]# kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')" |
验证集群是否安装完成
1 | [root@CentOS7-Node1 Workspace]# kubectl get pods -n kube-system |
发现coredns一直处于ContainerCreating的状态。具体的看一下错误信息。
1 | [root@CentOS7-Node1 Workspace]# kubectl describe pod coredns-5644d7b6d9-9fr9p -n kube-system |
这里可以看出一些错误:
1 | Oct 22 10:50:15 CentOS7-Node1 kubelet[7649]: F1022 10:50:15.170550 7649 server.go:196] failed to load Kubelet config file /var/lib/kubelet/config.yaml, error failed to read kubelet config file "/var/lib/kubelet/config.yaml", |
可以删除掉一个pod的方式让它重新启动:
1 | [root@CentOS7-Node1 ~]# kubectl delete pod coredns-5644d7b6d9-9fr9p -n kube-system |
看了太多的文章与博客,发现没有几个写的太完全的,都是写的成功的经验,实际上中间不知道有各种奇怪问题。说句实话,k8s很方便,但是门槛很高,依赖的东西真的太多太多了。特别是版本问题导致的问题,很难解决掉。
最后看一下成功的图片吧
常用命令汇总
1 | systemctl daemon-reload |
参考地址
- https://www.cnblogs.com/yufeng218/p/8370670.html
- https://stackoverflow.com/questions/55531834/kubeadm-fails-to-initialize-when-kubeadm-init-is-called
- https://zhuanlan.zhihu.com/p/31398416
- https://blog.csdn.net/M82_A1/article/details/97626309
- https://blog.csdn.net/liumiaocn/article/details/99608323
- https://www.hi-linux.com/posts/54191.html
- https://blog.csdn.net/BigData_Mining/article/details/88683459
如果大家喜欢我的文章,可以关注个人订阅号。欢迎随时留言、交流。