文档

参考

介绍

k8s集群环境有两种类型,一种是学习环境,一种是生产环境。

安装工具

  • kubectl
  • kind
  • minikube
  • kubeadm

主机配置

禁用 Swap 分区

禁用的原因:当内存不够时会使用磁盘来代替内存。内存和磁盘不是统一计算等级,禁用掉就不会出现计算速率不一致的问题。

1、编辑 /etc/fstab 文件,注释 /swapfile ... 这一行。

2、重启

1
swapoff -a

替换 Docker cgroup

编辑 /etc/docker/daemon.json,添加下面内容:

1
2
3
4
{
    ...
    "exec-opts": ["native.cgroupdriver=systemd"]
}

使用的是 systemd 这一 cgroup,这也是k8s推荐使用的 cgroup。

重启

1
systemctl daemon-reload && systemctl restart docker

疑问

  • cgroup 是什么?

kind 初始化集群(学习环境)

参考

注意

  • 虽然 kind 不需要 kubectl ,但是没有 kubectl 将无法使用文档中的一些示例。所以还是建议安装 kubectl。

准备

环境

Resource OS Description
chaos-4 Ubuntu20.04 LTS Master
chaos-5 Ubuntu20.04 LTS Worker
chaos-6 Ubuntu20.04 LTS Worker

硬件

Resource Minimum Recommended
CPU 2CPU 4CPU

软件

Resource Description
kind kind、docker、
Docker kind 是以容器方式运行 kubenetes 容器集群的,所以需要Docker
kubectl 使用 kind 文档中的示例

快捷安装 kubectl

1
2
3
curl -LO https://dl.k8s.io/release/v1.31.0/bin/linux/amd64/kubectl
chmod +x kubectl
mv kubectl /usr/local/bin

快捷安装 kind

1
2
3
4
# For AMD64 / x86_64
[ $(uname -m) = x86_64 ] && curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.24.0/kind-linux-amd64
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind

初始化

初始化

1
kind create cluster --name my-cluster

初始化失败:

1
2
3
4
5
6
~# kind create cluster --name my-cluster
Creating cluster "my-cluster" ...
ERROR: failed to create cluster: failed to ensure docker network: command "docker network create -d=bridge -o com.docker.network.bridge.enable_ip_masquerade=true -o com.docker.network.driver.mtu=1500 --ipv6 --subnet fc00:f853:ccd:e793::/64 kind" failed with error: exit status 1
Command Output: Error response from daemon: Failed to Setup IP tables: Unable to enable NAT rule:  (iptables failed: ip6tables --wait -t nat -I POSTROUTING -s fc00:f853:ccd:e793::/64 ! -o br-eb21611dec54 -j MASQUERADE: ip6tables v1.8.4 (legacy): can't initialize ip6tables table `nat': Table does not exist (do you need to insmod?)
Perhaps ip6tables or your kernel needs to be upgraded.
 (exit status 3))

降级 kind 版本至 v0.19.0,然后再重新初始化。

完整过程如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# kind create cluster --name my-cluster
Creating cluster "my-cluster" ...
 ✓ Ensuring node image (kindest/node:v1.27.1) 🖼
 ✓ Preparing nodes 📦
 ✓ Writing configuration 📜
 ✓ Starting control-plane 🕹️
 ✓ Installing CNI 🔌
 ✓ Installing StorageClass 💾
                                                                        
You can now use your cluster with:

kubectl cluster-info --context kind-my-cluster

Thanks for using kind!

切换kind集群

为什么要切换集群?因为 kind 可以初始化多个集群。使用某个集群时则需要切换。

1
kubectl cluster-info --context kind-my-cluster

查看集群

1
kubectl get nodes

初始化一主两从的k8s集群*

验证

1
kubeadm version

kubeadm 初始化集群(生产环境)

打印默认配置参数到指定文件

1
kubead config print init-defaults > kubeadm-init-temp.yaml

指定配置文件方式初始化

1
kubeadm init --config kubeadm-init.yaml
1
kubectl get nodes
1
kubectl get pod -A
1
kubectl apply -f kube-flannel.yml