# Kubernetes kubeadm方式环境搭建

# 机器准备

  • 操作系统:CentOS 7.9
  • Docker:20+
  • K8s:1.23.6
序号 类型 节点名称 节点IP 组件
1 控制面板节点 K8s-Control 10.12.254.5 docker,kubectl,kubeadm,kubelet
2 工作节点 K8s-Work1 10.12.254.26 docker,kubectl,kubeadm,kubelet
3 工作节点 K8s-Work2 10.12.254.193 docker,kubectl,kubeadm,kubelet

# 初始化环境(3个节点都执行)

检查操作系统的版本

cat /etc/redhat-release
1
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
1
2

关闭防火墙firewalld

systemctl stop firewalld
systemctl disable firewalld
1
2

关闭selinux

setenforce 0 # 临时关闭
sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久关闭
1
2

关闭swap

swapoff -a # 临时关闭
sed -ri 's/.swap./#&/' /etc/fstab # 永久关闭
1
2

关闭完swap后,一定要重启一下虚拟机!!! 根据规划设置主机名

hostnamectl set-hostname <hostname>
1
hostnamectl set-hostname  k8s-control
hostnamectl set-hostname  k8s-work1
hostnamectl set-hostname  k8s-work2
1
2
3

添加hosts

cat >> /etc/hosts << EOF
10.12.254.5 k8s-control
10.12.254.26 k8s-work1
10.12.254.193 k8s-work2
EOF
1
2
3
4
5

将桥接的IPv4流量传递到iptables的链

cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
1
2
3
4
sysctl --system # 生效
1

时间同步

yum install ntpdate -y
ntpdate time.windows.com
1
2

# 安装docker

参考此处

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://cp5o604r.mirror.aliyuncs.com"],
  "exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
1
2
3
4
5
6
7
8
9

# 安装Kubernetes组件

由于kubernetes的镜像在国外,速度比较慢,这里切换成国内的镜像源

cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0

gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
1
2
3
4
5
6
7
8
9
10

安装kubeadm、kubelet和kubectl

yum install -y kubelet-1.23.6 kubeadm-1.23.6 kubectl-1.23.6
systemctl enable kubelet
1
2

# 部署 Kubernetes 控制面板节点

kubeadm init \
      --apiserver-advertise-address=10.12.254.5 \
      --image-repository registry.aliyuncs.com/google_containers \
      --kubernetes-version v1.23.6 \
      --service-cidr=10.96.0.0/12 \
      --pod-network-cidr=10.244.0.0/16
1
2
3
4
5
6

若网络不稳定可以提前下载相关镜像

[root@k8s-control ~]# kubeadm config images list
I1201 15:54:50.794298  101787 version.go:255] remote version is much newer: v1.28.4; falling back to: stable-1.23
k8s.gcr.io/kube-apiserver:v1.23.17
k8s.gcr.io/kube-controller-manager:v1.23.17
k8s.gcr.io/kube-scheduler:v1.23.17
k8s.gcr.io/kube-proxy:v1.23.17
k8s.gcr.io/pause:3.6
k8s.gcr.io/etcd:3.5.1-0
k8s.gcr.io/coredns/coredns:v1.8.6
1
2
3
4
5
6
7
8
9
images=(
	kube-apiserver:v1.23.17
	kube-controller-manager:v1.23.17
	kube-scheduler:v1.23.17
	kube-proxy:v1.23.17
	pause:3.6
	etcd:3.5.1-0
	coredns:v1.8.6
)

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 
don
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@k8s-control ~]# docker images
REPOSITORY                                                        TAG       IMAGE ID       CREATED         SIZE
registry.aliyuncs.com/google_containers/kube-apiserver            v1.23.6   8fa62c12256d   19 months ago   135MB
registry.aliyuncs.com/google_containers/kube-controller-manager   v1.23.6   df7b72818ad2   19 months ago   125MB
registry.aliyuncs.com/google_containers/kube-scheduler            v1.23.6   595f327f224a   19 months ago   53.5MB
registry.aliyuncs.com/google_containers/kube-proxy                v1.23.6   4c0375452406   19 months ago   112MB
registry.aliyuncs.com/google_containers/etcd                      3.5.1-0   25f8c7f3da61   2 years ago     293MB
registry.aliyuncs.com/google_containers/coredns                   v1.8.6    a4ca41631cc7   2 years ago     46.8MB
registry.aliyuncs.com/google_containers/pause                     3.6       6270bb605e12   2 years ago     683kB
1
2
3
4
5
6
7
8
9

安装失败,通过如下命令查看

systemctl status kubelet
journalctl -xeu kubelet
1
2

修改/etc/sysconfig/kubelet

KUBELET_EXTRA_ARGS="--fail-swap-on=false"
KUBE_PROXY_MODE="ipvs"
1
2

重置kubeadm

kubeadm reset
1

# 部署 kubernetes 控制工作节点

kubeadm join 10.12.254.5:6443 --token 9ayisp.eaokadftwz1lfpgq \
        --discovery-token-ca-cert-hash sha256:5938da9a2f5abab9e3d37a465fa205c8e577268d54f8f40b7453dfa41dc3c947 
1
2

#

[root@localhost k8s]# kubectl get nodes
NAME          STATUS   ROLES                  AGE   VERSION
k8s-control   Ready    control-plane,master   49m   v1.23.6
k8s-work1     Ready    <none>                 45m   v1.23.6
k8s-work2     Ready    <none>                 45m   v1.23.6
1
2
3
4
5
上次更新: 2023/12/27