Kubernetes安装

1.环境#

1
2
3
4
5
6
7
8
192.168.31.211 - vhost1 - 控制节点
192.168.31.212 - vhost2 - 工作节点
192.168.31.213 - vhost3 - 工作节点
192.168.31.208 - test - 本地镜像仓库

操作系统:CentOS7 Linux

Kubernetes版本:v1.26.0

2 安装docker registry本地镜像仓库#

由于官方镜像地址在国内访问不了,需要搭建本地镜像仓库,从网上找寻相关镜像文件下载并导入(见尾部 5.资源)

主机:test

1
# docker run --restart=always -d -p 15000:5000 -v /var/lib/registry:/var/lib/registry registry

配置http访问(即不需要https)

1
# cat /etc/docker/daemon.json
1
2
3
4
5
6
7
8
9
{
"registry-mirrors" : [
"https://cr.console.aliyun.com/"
],
# 这段
"insecure-registries": [
"192.168.31.208:15000"
]
}

3.集群主机配置及所需软件安装#

主机:vhost1, vhost2, vhost3

3.1 配置先决条件#

1
2
3
4
5
6
7
# cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF

# sudo modprobe overlay
# sudo modprobe br_netfilter

设置所需的 sysctl 参数,参数在重新启动后保持不变

1
2
3
4
5
# cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF

应用 sysctl 参数而不重新启动

1
sudo sysctl --system

3.2 安装kubeadm, kubectl, kubelet#

1
2
3
4
5
6
7
8
9
10
11
12
13
# 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

# setenforce 0
# yum install -y kubelet kubeadm kubectl
# systemctl enable kubelet && systemctl start kubelet

3.3 安装containerd#

3.3.1 下载并安装containerd#

1
2
3
4
# tar Cxzvf /usr/local containerd-1.6.15-linux-amd64.tar.gz
# cp containerd.service /usr/lib/systemd/system/containerd.service
# systemctl daemon-reload
# systemctl enable --now containerd

3.3.2 下载并安装runc#

1
# install -m 755 runc.amd64 /usr/local/sbin/runc

3.3.3 验证#

1
2
# ctr container list
# crictl ps

如果出现如下错误信息

1
2
3
4
5
WARN[0000] runtime connect using default endpoints: [unix:///var/run/dockershim.sock unix:///run/containerd/containerd.sock unix:///run/crio/crio.sock unix:///var/run/cri-dockerd.sock]. As the default settings are now deprecated, you should set the endpoint instead. 
ERRO[0000] unable to determine runtime API version: rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing dial unix /var/run/dockershim.sock: connect: no such file or directory"
WARN[0000] image connect using default endpoints: [unix:///var/run/dockershim.sock unix:///run/containerd/containerd.sock unix:///run/crio/crio.sock unix:///var/run/cri-dockerd.sock]. As the default settings are now deprecated, you should set the endpoint instead.
ERRO[0000] unable to determine image API version: rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing dial unix /var/run/dockershim.sock: connect: no such file or directory"
CONTAINER IMAGE CREATED STATE NAME ATTEMPT POD ID POD

执行修复

1
2
# crictl config runtime-endpoint unix:///run/containerd/containerd.sock
# crictl config image-endpoint unix:///run/containerd/containerd.sock

3.3.4 下载并安装CNI插件#

1
2
# mkdir -p /opt/cni/bin
# tar Cxzvf /opt/cni/bin cni-plugins-linux-amd64-v1.1.1.tgz

3.3.5 配置systemd cgroup驱动#

1
2
# mkdir /etc/containerd
# containerd config default > /etc/containerd/config.toml
1
# vim /etc/containerd/config.toml # 修改如下
1
2
3
4
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
...
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
SystemdCgroup = true

3.3.6 修改沙窗(pause)镜像地址#

1
# vim /etc/containerd/config.toml # 修改如下
1
2
[plugins."io.containerd.grpc.v1.cri"]
sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.2"

3.3.7 配置containerd使用http#

1
# vim /etc/containerd/config.toml # 修改如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[plugins."io.containerd.grpc.v1.cri".registry]
config_path = ""

[plugins."io.containerd.grpc.v1.cri".registry.auths]

[plugins."io.containerd.grpc.v1.cri".registry.mirrors]
# 这段
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."192.168.31.208:15000"]
endpoint = ["http://192.168.31.208:15000"]

[plugins."io.containerd.grpc.v1.cri".registry.configs]
# 这段
[plugins."io.containerd.grpc.v1.cri".registry.configs."192.168.31.208:15000".tls]
insecure_skip_verify = true

[plugins."io.containerd.grpc.v1.cri".registry.headers]

3.3.8 重启containerd#

1
# systemctl restart containerd

4.启动集群#

4.1 使用kubeadm初始化集群#

主机:vhost1

1
# kubeadm init --image-repository=registry.aliyuncs.com/google_containers
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
[init] Using Kubernetes version: v1.26.0
[preflight] Running pre-flight checks
[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'
[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 [kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local vhost1] and IPs [10.96.0.1 192.168.31.211]
[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 [localhost vhost1] and IPs [192.168.31.211 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [localhost vhost1] and IPs [192.168.31.211 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
[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
[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
[kubelet-check] Initial timeout of 40s passed.
[apiclient] All control plane components are healthy after 44.098707 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config" 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 vhost1 as control-plane by adding the labels: [node-role.kubernetes.io/control-plane node.kubernetes.io/exclude-from-external-load-balancers]
[mark-control-plane] Marking the node vhost1 as control-plane by adding the taints [node-role.kubernetes.io/control-plane:NoSchedule]
[bootstrap-token] Using token: 9pk20q.rk8jrrx5valnw0di
[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

Alternatively, if you are the root user, you can run:

export KUBECONFIG=/etc/kubernetes/admin.conf

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.31.211:6443 --token 9pk20q.rk8jrrx5valnw0di \
--discovery-token-ca-cert-hash sha256:0c2746c2e25ebb8543c5e905454a54887562bafacf69465fc7a83d6159bf3a1c

4.2 安装Pod网络附加组件#

主机:vhost1

下载calico.yaml

1
2
# export KUBECONFIG=/etc/kubernetes/admin.conf
# kubectl apply -f calico.yaml

4.3 其他节点加入集群#

主机:vhost2, vhost3

1
# kubeadm join 192.168.31.211:6443 --token 9pk20q.rk8jrrx5valnw0di --discovery-token-ca-cert-hash sha256:0c2746c2e25ebb8543c5e905454a54887562bafacf69465fc7a83d6159bf3a1c
1
2
3
4
5
6
7
8
9
10
11
12
13
[preflight] Running pre-flight checks
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[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] Starting the kubelet
[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.

4.4 验证及其他命令#

主机:vhost1

4.4.1 查询集群节点看是否启动成功#

1
# kubectl get nodes
1
2
3
4
NAME     STATUS   ROLES           AGE   VERSION
vhost1 Ready control-plane 19d v1.26.0
vhost2 Ready <none> 19d v1.26.0
vhost3 Ready <none> 19d v1.26.0

4.4.2 令牌相关#

1
# kubeadm token list

默认情况下,令牌会在 24 小时后过期。如果要在当前令牌过期后将节点加入集群, 则可以通过在控制平面节点上运行以下命令来创建新令牌:

1
# kubeadm token create

如果你没有 –discovery-token-ca-cert-hash 的值,则可以通过在控制平面节点上执行以下命令链来获取它:

1
# openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'

4.4.3 重启集群#

1
2
3
4
5
6
7
8
# 重置
kubeadm reset

# 控制平面
kubeadm init ...

# 子节点
kubeadm join ...

5.资源#

6.资料#