我的k8s随笔:Kubernetes 1.18.0 部署讲解-centos

作者注:本文内容较多,所述仅供参考,但可保证文章质量,请谨慎阅读

本文介绍了如何在两台 centos 7 16.04 64 bit 双核 CPU 云主机上使用 kubeadm 部署 Kubernetes 1.18.0 集群的过程,网络插件为 flannel v0.11.0,镜像源为阿里云。本文具有一定实践参考意义。

一、 环境

云主机,centos 7 64 bit,内核3.10.0,8GB内存,双核 CPU。
环境要求和设置:
工程目录为:$HOME/k8s。
所有操作使用 root 权限执行(注:理论上普通用户亦可,为避免权限问题,故出此下策)。
注意,k8s要求机器的CPU必须双核心以上。
本文部署的k8s版本为1.17.0。部署日期约2019年12月中旬~下旬,请注意时效性。
本文部署镜像及版本如下:

1
2
3
4
5
6
7
8
k8s.gcr.io/kube-apiserver:v1.18.0
k8s.gcr.io/kube-controller-manager:v1.18.0
k8s.gcr.io/kube-scheduler:v1.18.0
k8s.gcr.io/kube-proxy:v1.18.0
k8s.gcr.io/pause:3.2
k8s.gcr.io/etcd:3.4.3-0
k8s.gcr.io/coredns:1.6.7
quay.io/coreos/flannel:v0.12.0-amd64

注1:k8s.gcr.io 使用阿里云镜像地址 registry.aliyuncs.com/google_containers 替换。
注2:不同时期部署,所用的 k8s 版本不同,相应的组件版本亦不同,需要重新下载。

二、 安装docker

安装系统工具:

1
yum install -y yum-utils device-mapper-persistent-data lvm2

添加国内源(阿里云):

1
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

生成缓存:

1
yum makecache

安装:

1
yum install docker

本文安装的 docker 版本为 1.13.1。
执行如下命令新建 /etc/docker/daemon.json 文件:

1
2
3
4
5
6
7
8
cat > /etc/docker/daemon.json <<-EOF
{
"registry-mirrors": [
"https://a8qh6yqv.mirror.aliyuncs.com",
"http://hub-mirror.c.163.com"
]
}
EOF

释义:
registry-mirrors 为镜像加速器地址。

启动docker,查看 cgroup:

1
2
3
# systemctl start docker
# docker info | grep -i cgroup
Cgroup Driver: systemd

默认cgroup为 systemd,与k8s保持一致,无须修改。

三、部署 k8s master 主机

k8s的部署分 master 主机和 node 节点。本节为 master 主机。

3.1 关闭swap

编辑 /etc/fstab 文件,注释掉swap分区挂载的行,示例:

1
2
# swap was on /dev/sda5 during installation
UUID=aaa38da3-6e60-4e9d-bfc6-7128fd05f1c7 none swapsw 0 0

再执行:

1
# swapoff -a

3.2 添加国内k8s源

此处选择阿里云的:

1
2
3
4
5
6
7
8
9
10
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

3.3 安装

安装 kubeadm、kubectl、kubelet、kubernetes-cni 等工具。

1
# yum install kubeadm kubectl kubelet kubernetes-cni

提示信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
依赖关系解决

================================================================================
Package 架构 版本 源 大小
================================================================================
正在安装:
kubeadm x86_64 1.18.0-0 kubernetes 8.8 M
kubectl x86_64 1.18.0-0 kubernetes 9.5 M
kubelet x86_64 1.18.0-0 kubernetes 21 M
kubernetes-cni x86_64 0.7.5-0 kubernetes 10 M
为依赖而安装:
conntrack-tools x86_64 1.4.4-5.el7_7.2 updates 187 k
cri-tools x86_64 1.13.0-0 kubernetes 5.1 M
libnetfilter_cthelper x86_64 1.0.0-10.el7_7.1 updates 18 k
libnetfilter_cttimeout x86_64 1.0.0-6.el7_7.1 updates 18 k
libnetfilter_queue x86_64 1.0.2-2.el7_2 base 23 k

输入y确认。

注:从上述信息看,安装的版本为1.18.0,kubernetes-cni 为0.7.5。

3.4 获取部署所需的镜像版本

1
# kubeadm config images list

输出如下:

1
2
3
4
5
6
7
8
W0327 16:16:50.268440    3424 configset.go:202] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
k8s.gcr.io/kube-apiserver:v1.18.0
k8s.gcr.io/kube-controller-manager:v1.18.0
k8s.gcr.io/kube-scheduler:v1.18.0
k8s.gcr.io/kube-proxy:v1.18.0
k8s.gcr.io/pause:3.2
k8s.gcr.io/etcd:3.4.3-0
k8s.gcr.io/coredns:1.6.7

前面提示的警告信息可不理会。此处是确认本版本 kubeadm 匹配的镜像的版本,因为各组件版本不同可能出现兼容性问题。

3.5 拉取镜像文件。

一般地,国内无法直接下载 k8s.gcr.io 的镜像。方式有二:
1、在初始化k8s时,使用阿里云镜像地址,此地址可以顺利下载,见下初始化命令,初始化时如无镜像会自动下载。也可以预先下载,将上一节的镜像地址前缀改为 registry.cn-hangzhou.aliyuncs.com/google_containers 即可。

2、自行下载好前述镜像。使用如下脚本pullk8s.sh(注意脚本必须添加x属性):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
# 下面的镜像应该去除"k8s.gcr.io/"的前缀,版本换成kubeadm config images list命令获取到的版本
images=(
kube-apiserver:v1.18.0
kube-controller-manager:v1.18.0
kube-scheduler:v1.18.0
kube-proxy:v1.18.0
pause:3.2
etcd:3.4.3-0
coredns:1.6.7
)

for imageName in ${images[@]} ; do
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/$imageName
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/$imageName k8s.gcr.io/$imageName
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/$imageName
done

拉取:

1
2
chmod +x pullk8s.sh
bash pullk8s.sh (或 ./pullk8s.sh)

3.6 网络

设置网络配置:

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
mkdir -p /etc/cni/net.d

cat >/etc/cni/net.d/10-mynet.conf <<-EOF
{
"cniVersion": "0.3.0",
"name": "mynet",
"type": "bridge",
"bridge": "cni0",
"isGateway": true,
"ipMasq": true,
"ipam": {
"type": "host-local",
"subnet": "10.244.0.0/16",
"routes": [
{"dst": "0.0.0.0/0"}
]
}
}
EOF

cat >/etc/cni/net.d/99-loopback.conf <<-EOF
{
"cniVersion": "0.3.0",
"type": "loopback"
}
EOF

经实践,此步骤不做亦可。

3.7 下载flannel镜像

1
docker pull quay.io/coreos/flannel:v0.12.0-amd64

注:如果无法下载,需要使用其它方法。
flannel 镜像信息:

1
2
# docker images | grep flannel
quay.io/coreos/flannel v0.12.0-amd64 4e9f801d2217 2 weeks ago 52.8 MB

注意,这里是先下载好 flannel 镜像,其版本由官方 yaml 文件确认,地址见下文。

3.8 初始化

版本一:

1
2
kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-bind-port=10010 \
--image-repository registry.aliyuncs.com/google_containers

释义:
–pod-network-cidr 指定了网络段,后续网络插件会使用到(本文使用 flannel)。
–image-repository 指定了镜像地址,默认为 k8s.gcr.io,此处指定为阿里云镜像地址 registry.aliyuncs.com/google_containers。
–pod-network-cidr 指定了 CIDR 的网段,默认是192.168.0.0/16,笔者网段也是192.168,为避免冲突,故修改。
–apiserver-bind-port 指定了服务端口,默认是6443,因为该云主机其它程序占用,故改。
注意,其它参数默认。

上述命令等同如下命令:

1
2
3
4
5
6
kubeadm init \
--apiserver-advertise-address=192.168.0.102 --apiserver-bind-port=10010\
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.18.0 \
--service-cidr=10.1.0.0/16\
--pod-network-cidr=10.244.0.0/16

版本二,根据前文脚本自行拉取版本:

1
kubeadm init --pod-network-cidr=10.244.0.0/16

本文使用版本一部署

初始化过程的提示信息如下:

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
67
68
69
70
71
72
W0327 16:35:43.258829    4726 configset.go:202] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
[init] Using Kubernetes version: v1.18.0
[preflight] Running pre-flight checks
[WARNING Service-Docker]: docker service is not enabled, please run 'systemctl enable docker.service'
[WARNING Service-Kubelet]: kubelet service is not enabled, please run 'systemctl enable kubelet.service'
[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] Starting the kubelet
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [izwz9hs1zswgl6frxwsnhhz kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 119.23.174.153]
[certs] Generating "apiserver-kubelet-client" certificate and key
[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/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [izwz9hs1zswgl6frxwsnhhz localhost] and IPs [119.23.174.153 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [izwz9hs1zswgl6frxwsnhhz localhost] and IPs [119.23.174.153 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-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"
W0327 16:36:10.648368 4726 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC"
[control-plane] Creating static Pod manifest for "kube-scheduler"
W0327 16:36:10.649340 4726 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC"
[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 22.002445 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.18" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node izwz9hs1zswgl6frxwsnhhz as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node izwz9hs1zswgl6frxwsnhhz as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: 5nx6xk.ufqgazdygjbo31k1
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes
[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
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[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 119.911.109.901:10010 --token 5nx6xk.ufqgazdygjbo31k1 \
--discovery-token-ca-cert-hash sha256:fb2b5d905f931b999df435b6c2079fdc5d42959b6b5fb7e2f609b34c1b571a97

首先确认了k8s版本。
接着创建配置文件,如证书等。
再创建 pod。
最后提示加入集群的命令。
部署时不建议深入了解 k8s 概念。最后出现kubeadm join表示初始化成功。

如果忘记,可 kubeadm token create –print-join-command 查看,示例如下:

1
2
W0327 16:41:28.351647    6107 configset.go:202] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
kubeadm join 123.231.312.123:10010 --token x04h7k.rvx3xeyc0us0aop2 --discovery-token-ca-cert-hash sha256:fb2b5d905f931b999df435b6c2079fdc5d42959b6b5fb7e2f609b34c1b571a97

释义:前后的token值不同,但 hash 值相同,不影响。

根据提示,根据拷贝 admin.conf 文件到当前用户相应目录下。admin.conf 文件后续会使用到(需要拷贝到 node 节点)。

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

注:如果是使用普通用户切换为 root 权限的,$HOME为普通用户的 HOME 目录路径。目录与用户必须一致,如以 latelee 用户切换 root 权限执行命令的,admin.conf 必须在/home/latelee/.kube目录,而不是 root 用户的/root/.kube目录。如果无此步骤,在执行 kubectl 命令时提示:

1
The connection to the server localhost:8080 was refused - did you specify the right host or port?

初始化时,如不存在则自动下载镜像,初始化后镜像如下:

1
2
3
4
5
6
7
8
9
10
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.aliyuncs.com/google_containers/kube-proxy v1.18.0 43940c34f24f 41 hours ago 117 MB
registry.aliyuncs.com/google_containers/kube-scheduler v1.18.0 a31f78c7c8ce 41 hours ago 95.3 MB
registry.aliyuncs.com/google_containers/kube-apiserver v1.18.0 74060cea7f70 41 hours ago 173 MB
registry.aliyuncs.com/google_containers/kube-controller-manager v1.18.0 d3e55153f52f 41 hours ago 162 MB
quay.io/coreos/flannel v0.12.0-amd64 4e9f801d2217 2 weeks ago 52.8 MB
registry.aliyuncs.com/google_containers/pause 3.2 80d28bedfe5d 5 weeks ago 683 kB
registry.aliyuncs.com/google_containers/coredns 1.6.7 67da37a9a360 8 weeks ago 43.8 MB
registry.aliyuncs.com/google_containers/etcd 3.4.3-0 303ce5db0e90 5 months ago 288 MB

此时 pod 状态如下:

1
2
3
4
5
6
7
8
9
# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-7ff77c879f-mjbm9 0/1 Pending 0 6m1s
coredns-7ff77c879f-x7jjn 0/1 Pending 0 6m1s
etcd-izwz9hs1zswgl6frxwsnhhz 1/1 Running 0 6m10s
kube-apiserver-izwz9hs1zswgl6frxwsnhhz 1/1 Running 0 6m10s
kube-controller-manager-izwz9hs1zswgl6frxwsnhhz 1/1 Running 0 6m10s
kube-proxy-2mxmx 1/1 Running 0 6m1s
kube-scheduler-izwz9hs1zswgl6frxwsnhhz 1/1 Running 0 6m10s

除 coredns 状态为 Pending外,其它 pod 均运行。这是因为没有部署网络插件导致的。本文选用 flannel 。

3.9 部署flannel

执行如下命令部署 flannel:

1
# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

释义:
使用 flannel 仓库的 kube-flannel.yml 文件部署。详细的信息,如所用版本号,可参考该文件。
如果无法访问,则可手动下载 https://github.com/coreos/flannel/blob/master/Documentation/kube-flannel.yml 文件到当前目录,再执行 kubectl apply -f kube-flannel.yml 命令。

1
2
3
4
5
6
7
8
9
10
11
12
13
# kubectl apply -f kube-flannel.yml
podsecuritypolicy.policy/psp.flannel.unprivileged created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.apps/kube-flannel-ds-amd64 created
daemonset.apps/kube-flannel-ds-arm64 created
daemonset.apps/kube-flannel-ds-arm created
daemonset.apps/kube-flannel-ds-ppc64le created
daemonset.apps/kube-flannel-ds-s390x created
```
部署 flannel 时如 flannel 镜像不存在会自动下载,前文已下载,故启动较快。启动过程中,flannel 状态变化如下:

kube-flannel-ds-amd64-zk6np 0/1 Init:0/1 0 3s
kube-flannel-ds-amd64-zk6np 1/1 Running 0 9s

1
2
这个步骤会创建 cni0 和 flannel.1 网络设备。  
部署 flannel后。查看 pod:

kubectl get pod -n kube-system

NAME READY STATUS RESTARTS AGE
GE
coredns-7ff77c879f-mjbm9 1/1 Running 0 8m46s
coredns-7ff77c879f-x7jjn 1/1 Running 0 8m46s
etcd-izwz9hs1zswgl6frxwsnhhz 1/1 Running 0 8m55s
kube-apiserver-izwz9hs1zswgl6frxwsnhhz 1/1 Running 0 8m55s
kube-controller-manager-izwz9hs1zswgl6frxwsnhhz 1/1 Running 0 8m55s
kube-flannel-ds-amd64-zk6np 1/1 Running 0 2m18s
kube-proxy-2mxmx 1/1 Running 0 8m46s
kube-scheduler-izwz9hs1zswgl6frxwsnhhz 1/1 Running 0 8m55s

1
2
3
4
5
6
全部 pod 已全部运行。  
注1:与在本地 ubuntu 系统部署稍不同,此处的 coredns 十分正常,可能是云主机的原因。

**至此,master 节点已部署成功**。

查看 flannel 网络信息:

cat /run/flannel/subnet.env

FLANNEL_NETWORK=10.244.0.0/16
FLANNEL_SUBNET=10.244.0.1/24
FLANNEL_MTU=1450
FLANNEL_IPMASQ=true

1
2

查看 flannel 网络配置:

cat /etc/cni/net.d/10-flannel.conflist

{
“name”: “cbr0”,
“cniVersion”: “0.3.1”,
“plugins”: [
{
“type”: “flannel”,
“delegate”: {
“hairpinMode”: true,
“isDefaultGateway”: true
}
},
{
“type”: “portmap”,
“capabilities”: {
“portMappings”: true
}
}
]
}
`

四、node 节点

k8s的部署分 master 主机和 node 节点。node 节点的部署,与前面文章没有差别,此处从略。

参考资源

本文部署时主要参考如下文章并根据实际情况调整: