用Kubernetes建立实验集群

我在大三下选修了软件定义网络这门选修课,其期末作业是使用Kubernetes来搭建一个实验集群。目前在互联网上有很多使用Kubernetes搭建集群的教程,但是我看了很多都感觉非常复杂。于是我参照官网的教程完成了这篇实验报告。

实验环境描述

在本实验中使用三台安装了ubuntu server的虚拟机来创建集群,其中第一台作为集群的master,后两台作为集群的node。各台虚拟机的详细情况见下表。服务器宿主机安装的ESXi,拥有双路CPU以及64G内存。虚拟机每台分配单路双核CPU,2G内存以及20G硬盘。虚拟网卡型号选择vmxnet3(ESXi支持的万兆虚拟网卡)。

Host name IP address Virtual machine name Role
Ubuntu-server 192.168.1.14 Ubuntu server 1 Master
Ubuntu-server-2 192.168.1.15 Ubuntu server 2 Node
Ubuntu-server-3 192.168.1.16 Ubuntu server 3 Node

在虚拟机上安装Ubuntu Server

在每台虚拟机上安装ubuntu server,并按照上表手动分配IP地址,安装过程中勾选SSH服务器选项。过程如下图。


在每台主机上安装docker和open-vm-tools,具体命令为

1
sudo apt install docker.io open-vm-tools

然后禁用swap,命令为

1
sudo swapoff -a

之后在/etc/fstab中注释掉swap文件的挂载项。

服务器配置完后开始在每台虚拟机上安装kubeadm,具体步骤参照官网

1
https://kubernetes.io/docs/setup/independent/install-kubeadm/

由于我没有使用root用户,所有每条命令前都要使用sudo提升权限。

安装完成后在master上面初始化集群并配置CIDR,具体命令为

1
sudo kubeadm init --pod-network-cidr 10.100.0.0/16

产生log很长,如下方文本区域所示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
[init] Using Kubernetes version: v1.14.2
[preflight] Running pre-flight checks
[WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Activating the kubelet service
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [user-server localhost] and IPs [192.168.1.14 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [user-server localhost] and IPs [192.168.1.14 127.0.0.1 ::1]
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [user-server kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.1.14]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 20.003859 seconds
[upload-config] storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.14" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --experimental-upload-certs
[mark-control-plane] Marking the node user-server as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node user-server as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: m45tm8.zn9e2p2sc8tww0u1
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] creating the "cluster-info" ConfigMap in the "kube-public" namespace
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.1.14:6443 --token m45tm8.zn9e2p2sc8tww0u1 \
--discovery-token-ca-cert-hash sha256:22b8e0acb8db0c111a8d534e62e7c2ebe46055baeac437d5062eec23aa41f6f1

按照log中的提示,将集群的配置文件拷贝到当前用户的目录下。
然后使用

1
KUBECONFIG=/etc/kubernetes/admin.conf kubectl apply -f https://raw.githubusercontent.com/cloudnativelabs/kube-router/master/daemonset/kubeadm-kuberouter-all-features.yaml

命令来配置Pod网络。
配置完master后,使用SSH登陆其他两个节点,并安装log中提示使用kubeadm join命令来加入集群。过程中产生的log如下方文本区域所示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
user@user-server-2:~$ sudo kubeadm join 192.168.1.14:6443 --token m45tm8.zn9e2p2sc8tww0u1     --discovery-token-ca-cert-hash sha256:22b8e0acb8db0c111a8d534e62e7c2ebe46055baeac437d5062eec23aa41f6f1
[preflight] Running pre-flight checks
[WARNING Service-Docker]: docker service is not enabled, please run 'systemctl enable docker.service'
[WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.14" ConfigMap in the kube-system namespace
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Activating the kubelet service
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

在两个node上配置完后,回到master,使用

1
sudo kubectl get nodes

来获取集群相关信息。集群状态如下图所示。

安装Kubernetes Dashboard

然后按照教程安装Kubernetes Dashboard,用于集群的可视化管理。具体方法采用的是

1
https://github.com/kubernetes/dashboard

的官方教程。然后使用

1
sudo kubectl create serviceaccount dashboard-admin -n kube-system

命令创建一个Service Account,并使用

1
sudo kubectl describe secret -n kube-system dashboard-admin-token-

命令来查看生成的token。
使用

1
kubectl proxy

命令打开代理,在master的浏览器中输入网址

1
http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/

打开管理界面,并输入token登陆。

登陆成功后在Nodes中查看集群节点,如图所示共有三个节点。

安装Nginx服务

使用

1
sudo kubectl run my-nginx --image=nginx --replicas=2 --port=80

命令来运行nginx服务,并使用

1
sudo kubectl expose deployment my-nginx --port=80

来将nginx服务暴露出来。安装后可以在Services中查看所有服务,并点击my-nginx查看详细信息。具体情况如下图所示。



在浏览器中输入网址来访问Nginx服务。

kubernetes分析

然后我们可以研究下Kubernetes的具体情况。分别使用SSH登陆三台虚拟机,输入

1
sudo docker container list

来查看当前虚拟机的docker中有那些容器。具体情况如下图所示。



从中我们可以看到,在master中运行了Kubernetes的相关服务,如etcd等。在两个Node中都运行了一个Nginx的容器。