Linux 安装 K8s (Local K3s、Minikube、K8s Cluster 等)

搭建本地 K8s 环境

Posted by ethan.luo on October 9, 2021

Docker

install docker centos ubuntu

Local K8s

本地安装 k8s 推荐使用 K3s , dashboard 推荐 Kuboard

#install k3s
$ curl -sfL https://get.k3s.io | sh -

#check node
$ k3s kubectl get node

#install dashboard
$ kubectl apply -f https://addons.kuboard.cn/kuboard/kuboard-v3.yaml

K8s Cluster

ubuntu

1. 准备工作

$ sudo swapoff -a
$ sudo usermod -aG docker $USER && newgrp docker

$ cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF


$ cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

$ sudo sysctl --system

$ echo '{"exec-opts": ["native.cgroupdriver=systemd"]}' | sudo tee /etc/docker/daemon.json

$ systemctl daemon-reload
$ systemctl restart docker
$ systemctl restart kubelet

2. 添加源

$ sudo tee /etc/apt/sources.list.d/kubernetes.list <<EOF
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF

$ sudo apt-get update
$ sudo apt-get install -y kubelet kubeadm kubectl
$ sudo apt-mark hold kubelet kubeadm kubectl

$ sudo apt-get update && sudo apt-get install -y ca-certificates curl software-properties-common apt-transport-https curl

$ curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -

3. k8s 初始化

kubeadm init --pod-network-cidr 172.16.0.0/16 \
--apiserver-cert-extra-sans <your master ip>  \
--kubernetes-version v1.22.0 \
--image-repository registry.aliyuncs.com/google_containers

如果需要更改端口,可以使用 –apiserver-bind-port 8443

or

cat <<EOF>init-config.yaml
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
imageRepository: registry.aliyuncs.com/google_containers
apiserverCertExtraSans: <your master ip>
kubernetesVersion: v1.22.0
networking:
  podSubnet: "192.168.0.0/16"
<EOF>

$ kubeadm init --config=init-config.yaml

如果是本地部署建议 memory 16g+ 单机部署解除 master 污点设置

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

4. 安装网络插件

$ wget https://docs.projectcalico.org/v3.19/manifests/calico.yaml
$ sed -i s/192.168.0.0/172.16.0.0/g calico.yaml
$ kubectl apply -f calico.yaml

5. 安装 dashboard

$ kubectl apply -f https://addons.kuboard.cn/kuboard/kuboard-v3.yaml

centos

1. 准备工作

$ echo "1" >/proc/sys/net/bridge/bridge-nf-call-iptables

2. 添加源

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

后面的步骤和 ubuntu 安装相同

Minikube

#国内
$ minikube start --vm-driver=docker  --base-image="anjone/kicbase"  --image-mirror-country='cn' --image-repository='registry.cn-hangzhou.aliyuncs.com/google_containers'

#国外
$ minikube start --image-mirror-country cn --base-image="gcr.io/k8s-minikube/kicbase:v0.0.27" --vm-driver=docker --registry-mirror=https://9q0y37ef.mirror.aliyuncs.com

#设置内存大小
$ minikube config set memory 4384

Docker Compose

docker-compose 安装

$ sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

错误处理

kubeadm init 初始化报错: Get “http://localhost:10248/healthz“

错误:

在/etc/docker/daemon.json
文件中加入“"exec-opts": ["native.cgroupdriver=systemd"]
一行配置,重启docker跟清除一下kubeadm信息即可重新初始化。”

$ systemctl restart docker

$ kubeadm reset -f

Kuboard 安装后无法启动

可以先执行

$ kubectl delete -f https://addons.kuboard.cn/kuboard/kuboard-v3.yaml
$ sudo rm -rf /usr/share/kuboard

然后重新安装

$ kubectl apply -f https://addons.kuboard.cn/kuboard/kuboard-v3.yaml