发布于 

Linux部署K8S单机版

在 centos 上搭建单机版 k8s

1.服务器配置

名称腾讯云服务器
内存2G
CPU单核
系统CentOS 7
硬盘50G

2.准备工作

1.关闭防火墙

1
2
systemctl stop firewalld
systemctl disable firewalld

2.关闭 selinux

1
2
sed -i 's/enforcing/disabled/' /etc/selinux/config 
setenforce 0

3.关闭 swap

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

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

1
2
3
4
5
6
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

sysctl --system

3.开始安装

1.安装 docker

1.1 安装 docker 源
1
yum install -y wget && wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
1.2 安装 docker
1
yum -y install docker-ce-18.06.1.ce-3.el7
1.3 启动 docker 服务并设置开机自启
1
systemctl enable docker && systemctl start docker
1.4 查看 docker 版本,验证是否安装成功
1
docker --version

2. 安装 kubeadm、kubelet 和 kubectl

2.1 添加阿里云YUM的软件源
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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

# centos 8
[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
2.2 安装kubeadm,kubelet和kubectl
1
yum install -y kubelet-1.15.0 kubeadm-1.15.0 kubectl-1.15.0
2.3 设置开机自启
1
systemctl enable kubelet

3.初始化 k8s

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
kubeadm init \
--apiserver-advertise-address=10.0.4.11 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.15.0 \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.244.0.0/16 \
--ignore-preflight-errors=NumCPU

kubeadm init --apiserver-advertise-address=0.0.0.0 \
--apiserver-cert-extra-sans=127.0.0.1 \
--image-repository=registry.aliyuncs.com/google_containers \
--ignore-preflight-errors=all \
--kubernetes-version=v1.24.0 \
--service-cidr=10.10.0.0/16 \
--pod-network-cidr=10.18.0.0/16


# centos8 报错
cat > /etc/containerd/config.toml <<EOF
[plugins."io.containerd.grpc.v1.cri"]
systemd_cgroup = true
EOF
systemctl restart containerd
参考: https://blog.csdn.net/shanxuanang/article/details/124613577
# 以上无效

rm /etc/containerd/config.toml
systemctl restart containerd
# 以上有效(移动文件,不要直接删除)

继续参照: https://blog.csdn.net/liufuling14/article/details/120801912

mkdir -p $HOME/.kube

sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

sudo chown $(id -u):$(id -g) $HOME/.kube/config

4.安装网络插件

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

5.开启单机模式

1
kubectl taint nodes --all node-role.kubernetes.io/master-

6.验证是否安装成功

1
kubectl get pod -n kube-system

可以看到一切正常

1
2
3
4
5
6
7
8
9
NAME                             READY   STATUS    RESTARTS   AGE
coredns-bccdc95cf-7zvlh 1/1 Running 2 2d20h
coredns-bccdc95cf-pb54b 1/1 Running 2 2d20h
etcd-centos 1/1 Running 1 2d20h
kube-apiserver-centos 1/1 Running 1 2d20h
kube-controller-manager-centos 1/1 Running 6 2d20h
kube-flannel-ds-amd64-8j9xb 1/1 Running 1 2d20h
kube-proxy-n5tqz 1/1 Running 1 2d20h
kube-scheduler-centos 1/1 Running 6 2d20h

参考链接

  1. kubeadm部署Kubernetes(k8s)完整版详细教程
  2. Kubernetes error: taint “node-role.kubernetes.io/master:”