kubernets 集群安装部署(kubeadm)
安装部署
安装前准备
设置每个机器自己的hostname
1
| hostnamectl set-hostname xxx
|
关闭seLinux
关闭swap分区
这个安装系统的时候就没有设置交换分区
- 允许 iptables 检查桥接流量
1 2 3 4 5 6 7 8
| # 设置加载br_netfilter模块 cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf overlay br_netfilter EOF
sudo modprobe overlay sudo modprobe br_netfilter
|
1 2 3 4 5 6 7 8 9 10 11
| # 开启bridge-nf-call-iptables ,设置所需的 sysctl 参数,参数在重新启动后保持不变 # 参考:https://imroc.cc/post/202105/why-enable-bridge-nf-call-iptables/
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 参数而不重新启动 sudo sysctl --system
|
安装 containerd
安装容器运行时
在所有主机上安装容器运行时,推荐使用 containerd
1 2 3 4 5 6
| yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install -y containerd.io
|
生成默认配置
1
| containerd config default > /etc/containerd/config.toml
|
修改CgroupDriver为systemd, k8s官方推荐使用systemd类型的CgroupDriver。
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
|
修改 pause 镜像地址为国内的。
1
| sandbox_image = "registry.aliyuncs.com/k8sxio/pause:3.6"
|
重启containerd,设置开机自启动
1 2
| systemctl enable containerd systemctl restart containerd
|
安装 kubeadm,kubelet,kubectl
在所有主机上安装kubeadm,kubelet,kubectl。最好版本与需要安装的k8s的版本一致。
准备 国内 yum 源
1 2 3 4 5
| [kubernetes] name=Kubernetes baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/ enabled=1 gpgcheck=0
|
1
| yum install kubeadm kubelet kubectl
|
1 2 3 4 5
| systemctl enable kubelet systemctl restart kubelet
systemctl enable --now kubelet
|
初始化master节点
1 2 3 4 5 6 7 8
| kubeadm init \ --apiserver-advertise-address=<master 节点IP,也是apiserver的IP> \ --control-plane-endpoint=k8s-master \ --image-repository registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images \ --kubernetes-version v1.20.9 \ --service-cidr=10.96.0.0/16 \ --pod-network-cidr=10.244.0.0/16
|
安装网络插件
执行如下命令部署 flannel:
1 2
| kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
|
初始化worker节点
其他参考
https://blog.51cto.com/foxhound/2517491
kubeadm-config 支持以下配置类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| apiVersion: kubeadm.k8s.io/v1beta2 kind: InitConfiguration
apiVersion: kubeadm.k8s.io/v1beta2 kind: ClusterConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1 kind: KubeletConfiguration
apiVersion: kubeproxy.config.k8s.io/v1alpha1 kind: KubeProxyConfiguration
apiVersion: kubeadm.k8s.io/v1beta2 kind: JoinConfiguration
|
https://k8s.huweihuang.com/project/setup/installer/install-k8s-by-kubeadm
问题合集
问题1: kubeadm init 时候 apiserver 不能配置成域名,只能是IP地址
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
| [root@k8s-master ~]# kubeadm init --config init-config.yaml --v=5 I0728 14:34:52.451879 2833 initconfiguration.go:255] loading configuration from "init-config.yaml" couldn't use "apiserver.magesfc.com" as "apiserver-advertise-address", must be ipv4 or ipv6 address k8s.io/kubernetes/cmd/kubeadm/app/util/config.SetAPIEndpointDynamicDefaults cmd/kubeadm/app/util/config/initconfiguration.go:135 k8s.io/kubernetes/cmd/kubeadm/app/util/config.SetInitDynamicDefaults cmd/kubeadm/app/util/config/initconfiguration.go:67 k8s.io/kubernetes/cmd/kubeadm/app/util/config.documentMapToInitConfiguration cmd/kubeadm/app/util/config/initconfiguration.go:369 k8s.io/kubernetes/cmd/kubeadm/app/util/config.BytesToInitConfiguration cmd/kubeadm/app/util/config/initconfiguration.go:290 k8s.io/kubernetes/cmd/kubeadm/app/util/config.LoadInitConfigurationFromFile cmd/kubeadm/app/util/config/initconfiguration.go:262 k8s.io/kubernetes/cmd/kubeadm/app/util/config.LoadOrDefaultInitConfiguration cmd/kubeadm/app/util/config/initconfiguration.go:274 k8s.io/kubernetes/cmd/kubeadm/app/cmd.newInitData cmd/kubeadm/app/cmd/init.go:304 k8s.io/kubernetes/cmd/kubeadm/app/cmd.newCmdInit.func3 cmd/kubeadm/app/cmd/init.go:157 k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow.(*Runner).InitData cmd/kubeadm/app/cmd/phases/workflow/runner.go:183 k8s.io/kubernetes/cmd/kubeadm/app/cmd.newCmdInit.func1 cmd/kubeadm/app/cmd/init.go:103 github.com/spf13/cobra.(*Command).execute vendor/github.com/spf13/cobra/command.go:916 github.com/spf13/cobra.(*Command).ExecuteC vendor/github.com/spf13/cobra/command.go:1040 github.com/spf13/cobra.(*Command).Execute vendor/github.com/spf13/cobra/command.go:968 k8s.io/kubernetes/cmd/kubeadm/app.Run cmd/kubeadm/app/kubeadm.go:50 main.main cmd/kubeadm/kubeadm.go:25 runtime.main /usr/local/go/src/runtime/proc.go:250 runtime.goexit /usr/local/go/src/runtime/asm_amd64.s:1598 [root@k8s-master ~]#
|