使用kubeadm创建kubernetes集群

1 目标

  在64位centos7机器上创建一个三节点的kubernetes集群:

  • 10.58.47.8(Master)
  • 10.58.47.7(Worker)
  • 10.58.47.6(Worker)

2 步骤

  以下主要参考kubernetes官网上的创建步骤

2.1 安装kubeadm

2.2 初始化kubernetes集群

  由于后续操作需要好多镜像,我们先把它们下载导入到本地,所有镜像都已经上传到百度网盘,另外镜像文件夹同目录下附带一个脚本,直接运行下就可以将所有脚本导入进来了。

1
2
3
4
5
6
7
imageDir="images"
for imgFile in `ls ${imageDir}`;
do
echo "start loading image from ${imgFile}"
docker load < ${imageDir}/${imgFile}
echo "finish loading image from ${imgFile}"
done

在Master节点(10.58.47.8)上执行kubeadm init --kubernetes-version=v1.7.1,建议指定版本,要不init时会请求https://storage.googleapis.com/kubernetes-release/release/stable-1.7.txt查询版本,看下初始化成功的完美画面:

上面的token一定要记住,以后加入node会用到,如果忘记了,可以在master上执行kubeadm token list | grep authentication,signing | awk '{print $1}'来查看。

2.3 安装pod网络

  参照官方的步骤https://kubernetes.io/docs/setup/independent/create-cluster-kubeadm/#pod-networkhttp://docs.projectcalico.org/v2.3/getting-started/kubernetes/installation/hosted/kubeadm/我们需要在Master节点(10.58.47.8)执行:

1
2
3
4
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
kubectl apply -f http://docs.projectcalico.org/v2.3/getting-started/kubernetes/installation/hosted/kubeadm/1.6/calico.yaml

为了方便以后使用,我们可以将最后一步的http://docs.projectcalico.org/v2.3/getting-started/kubernetes/installation/hosted/kubeadm/1.6/calico.yaml下载到本地后执行kubectl apply -f /path/to/calico.yaml,镜像准备好之后一切都很成功!

2.4 安装dashboad(可选)

  dashboad对于集群搭建不是必须的,没有不影响集群运行,但是为了方便管理,建议还是安装下,步骤还是参考官方文档https://github.com/kubernetes/dashboard,先从https://git.io/kube-dashboard下载配置文件保存为kubernetes-dashboard.yaml。
修改文件内容,打开文件拉到最下面,修改类型NodePort,端口号31000(就是后面访问dashboad的端口!)

执行kubectl create -f kubernetes-dashboard.yaml,看到控制台输出

浏览器访问http://10.58.47.8:31000就可以访问dashboad了

2.5 加入节点

  Worker节点(10.58.47.6、10.58.47.7)需要先导入以下镜像:

1
2
3
4
5
6
7
8
imageDir="images"
images="kube-proxy-amd64-v1.7.1.tar node-v1.3.0.tar cni-v1.9.1.tar pause-amd64-3.0.tar kubernetes-dashboard-amd64-v1.6.1.tar"
for imgFile in ${images};
do
echo "start loading image from ${imgFile}"
docker load < ${imageDir}/${imgFile}
echo "finish loading image from ${imgFile}"
done

然后执行加入操作kubeadm join --skip-preflight-checks --token 326b9b.ab51e54655c0c738 10.58.47.8:6443,看到控制台如下提示说明node已经加入成功了!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[kubeadm] WARNING: kubeadm is in beta, please do not use it for production clusters.
[preflight] Skipping pre-flight checks
[discovery] Trying to connect to API Server "10.58.47.8:6443"
[discovery] Created cluster-info discovery client, requesting info from "https://10.58.47.8:6443"
[discovery] Cluster info signature and contents are valid, will use API Server "https://10.58.47.8:6443"
[discovery] Successfully established connection with API Server "10.58.47.8:6443"
[bootstrap] Detected server version: v1.7.1
[bootstrap] The server supports the Certificates API (certificates.k8s.io/v1beta1)
[csr] Created API client to obtain unique certificate for this node, generating keys and certificate signing request
[csr] Received signed certificate from the API server, generating KubeConfig...
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/kubelet.conf"

Node join complete:
* Certificate signing request sent to master and response
received.
* Kubelet informed of new secure connection details.

Run 'kubectl get nodes' on the master to see this machine join.

注意如果不加跳过检查–skip-preflight-checks可能会提示hostname为空,据说这是1.7.1的bug。

再在master上看下节点状态,终于成功了!

1
2
3
4
5
[root@VM-10-58-47-8 kubernetes]# kubectl get nodes
NAME STATUS AGE VERSION
vm-10-58-47-6 Ready 9m v1.7.1
vm-10-58-47-7 Ready 11s v1.7.1
vm-10-58-47-8 Ready 11m v1.7.1

看下pod的状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@VM-10-58-47-8 kubernetes]# kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system calico-etcd-lr375 1/1 Running 0 2d
kube-system calico-node-39zpk 2/2 Running 1 2d
kube-system calico-node-k396n 2/2 Running 0 2d
kube-system calico-node-tkxj0 2/2 Running 1 2d
kube-system calico-policy-controller-1727037546-0bc93 1/1 Running 0 2d
kube-system etcd-vm-10-58-47-8 1/1 Running 0 2d
kube-system kube-apiserver-vm-10-58-47-8 1/1 Running 0 2d
kube-system kube-controller-manager-vm-10-58-47-8 1/1 Running 0 2d
kube-system kube-dns-2425271678-pcrdn 3/3 Running 0 2d
kube-system kube-proxy-gn9jf 1/1 Running 0 2d
kube-system kube-proxy-k222t 1/1 Running 0 2d
kube-system kube-proxy-rc7hw 1/1 Running 0 2d
kube-system kube-scheduler-vm-10-58-47-8 1/1 Running 0 2d
kube-system kubernetes-dashboard-3044843954-kcqxz 1/1 Running 0 15m

3 其他

  有个比较坑的,官方的kubernetes-dashboard.yaml是1.6.1版本的,汉化了一部分,看着特别丑,建议还是先使用1.6.0的吧,具体就是先将kubernetes-dashboard.yaml复制一份改成kubernetes-dashboard-v1.6.0.yaml,将里面使用的镜像改成1.6.0,然后依次kubectl delete -f kubernetes-dashboard.yamlkubectl create -f kubernetes-dashboard-v1.6.0.yaml就行了,看下1.6.0的还是挺顺眼的
安装过程中遇到问题最好就是查看日志了,kubernetes查看日志的几种方式:

  • 在任意节点上查看kubernetes日志tail -fn100 /var/log/messages
  • 在master节点使用kubectllogs命令kubectl -n ${namespace-name} logs ${pod-name}
  • 在master节点使用kubectldescribe命令kubectl -n ${namespace-name} describe pod ${pod-name}

所有用到的文件均已上传到百度网盘,大家在安装完kubelet和kubeadm后,可以试下:

  1. 在master上依次执行sh load_images_master.shsh cluster_master_init.sh
  2. 在node上执行sh load_images_node.sh,然后执行加入节点命令即可

参考: