架构介绍:1台master,2台nodes

master :192.168.28.200

node1: 192.168.28.171

node2: 192.168.28.172

k8s每个节点安装containerd。

一、安装kubeadm

containerd安装参考《containerd安装博文》:​containerd的安装与使用_51CTO博客_containerd 安装​

自动化安装脚本见​​脚本自动安装containerd_我与linux的技术博客_51CTO博客​

修改containerd的config.toml配置文件,修改k8s的镜像初始化pause底层网络镜像的下载地址

root@master:~# vim /etc/containerd/config.toml

sandbox_image = "​​registry.aliyuncs.com/google_containers/pause:3.7​​"

配置本地源

配置文件/etc/yum.repos.d/kubernetes.repo

[kubernetes]
name=kubernetes
baseurl=https://mirrors.tuna.tsinghua.edu.cn/kubernetes/yum/repos/kubernetes-el7-$basearch
enabled=1
gpgcheck=0

关闭防火墙

systemctl stop firewalld.service && systemctl disable firewalld.service

安装软件

yum install ipset vim ipvsadm wget bash-completion.noarch -y
yum install -y yum-utils device-mapper-persistent-data lvm2
yum install -y kubelet-1.24.3 kubeadm-1.24.3 kubectl-1.24.3

查看k8s初始化所需的镜像

kubeadm config images list --kubernetes-version v1.24.3

Kubernetes v1.24 基于containerd部署_ico

编写脚本自动下载

vim ​​imagedown.sh​

nerdctl -n k8s.io pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.24.3
nerdctl -n k8s.io pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.24.3
nerdctl -n k8s.io pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.24.3
nerdctl -n k8s.io pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.24.3
nerdctl -n k8s.io pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.7
nerdctl -n k8s.io pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.5.3-0

二、初始化k8s

环境优化

cat <<EOF >> /etc/security/limits.conf
root soft nofile 1048576
root hard nofile 1048576
root soft stack 10240
EOF

# 关闭selinux
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config
grep --color=auto '^SELINUX' /etc/selinux/config

#关闭Swap
swapoff -a
sed -i 's/.*swap.*/#&/' /etc/fstab



配置内核参数

cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.bridge.bridge-nf-call-arptables = 1
net.ipv4.ip_forward=1
net.ipv4.tcp_tw_recycle=0
net.core.somaxconn = 32768
vm.swappiness=0 # 禁止使用 swap 空间,只有当系统 OOM 时才允许使用它 vm.overcommit_memory=1 # 不检查物理内存是否够用
vm.panic_on_oom=0 # 开启 OOM
fs.inotify.max_user_instances=8192
fs.inotify.max_user_watches=1048576
fs.file-max=52706963
fs.nr_open=52706963
net.ipv6.conf.all.disable_ipv6=1
net.netfilter.nf_conntrack_max=2310720
net.ipv4.conf.all.rp_filter = 1
net.ipv4.neigh.default.gc_thresh1 = 80000
net.ipv4.neigh.default.gc_thresh2 = 90000
net.ipv4.neigh.default.gc_thresh3 = 100000
EOF

sysctl -p /etc/sysctl.d/k8s.conf

加载模块

modprobe br_netfilter
cat > /etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack
modprobe br_netfilter
EOF

加载ipvs脚本

chmod +x /etc/sysconfig/modules/ipvs.modules && /etc/sysconfig/modules/ipvs.modules

master执行初始化集群

root@master:~# kubeadm init --apiserver-advertise-address=192.168.28.220 \
--apiserver-bind-port=6443 \
--kubernetes-version=v1.24.3 \
--pod-network-cidr=10.100.0.0/16 \
--service-cidr=10.200.0.0/16 \
--service-dns-domain=cluster.local \
--image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers \
--ignore-preflight-errors=swap

  选项解释:

       --apiserver-advertise-address  api-server地址也就是master节点地址

       --apiserver-bind-port  api-server服务端口号

       --kubernetes-version  k8s版本号

       --pod-network-cidr  pod网络的地址建议16位或者8位地址

       --service-cidr  svc网络地址建议16位或者8位地址与pod网络区别开来

       --service-dns-domain=cluster.local  集群dns域名地址,默认为cluster.local

       --image-repository  k8s镜像下载地址

       --ignore-preflight-errors=swap  忽略初始化错位,如果开启了swap,可以忽略这个错误

[root@master1 src]# kubeadm init --apiserver-advertise-address=192.168.28.220 \
> --apiserver-bind-port=6443 \
> --kubernetes-versinotallow=v1.24.3 \
> --pod-network-cidr=10.100.0.0/16 \
> --service-cidr=10.200.0.0/16 \
> --service-dns-domain=cluster.local \
> --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers \
> --ignore-preflight-errors=swap
[init] Using Kubernetes version: v1.24.3
[preflight] Running pre-flight checks
[WARNING Firewalld]: firewalld is active, please ensure ports [6443 10250] are open or your cluster m
[WARNING Service-Kubelet]: kubelet service is not enabled, please run 'systemctl enable kubelet.servi
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [kubernetes kubernetes.default kubernetes.default.svc
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [localhost master1.cib.com.cn] and IPs [192.168.28.2
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [localhost master1.cib.com.cn] and IPs [192.168.28.220
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Starting the kubelet
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc
[apiclient] All control plane components are healthy after 6.502259 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config" in namespace kube-system with the configuration for the kubel
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node master1.cib.com.cn as control-plane by adding the labels: [node-role.ku
[mark-control-plane] Marking the node master1.cib.com.cn as control-plane by adding the taints [node-role.kub
[bootstrap-token] Using token: 4xtnd2.6ppjdo16s1laj5of
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] Configured RBAC rules to allow Node Bootstrap tokens to get nodes
[bootstrap-token] Configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get
[bootstrap-token] Configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a
[bootstrap-token] Configured RBAC rules to allow certificate rotation for all node client certificates in the
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Alternatively, if you are the root user, you can run:

export KUBECONFIG=/etc/kubernetes/admin.conf

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.28.220:6443 --token 4xtnd2.6ppjdo16s1laj5of \
--discovery-token-ca-cert-hash sha256:67f051d5d024de3612336780b9d253ff2c32d09fa104cf8b4ba39e079d5560d

 kubectl凭据配置

  kubectl默认使用~/.kube/config文件中凭据信息管理kubernetes。

To start using your cluster, you need to run the following as a regular user:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

查看节点

kubectl get nodes

Kubernetes v1.24 基于containerd部署_ico_02

三、使用calico配置网络

这里采用calico网络。

  官方地址:​​http://projectcalico.org 或者 https://www.tigera.io/project-calico/​

  产品文档:​​https://projectcalico.docs.tigera.io/about/about-calico​

 calico项目下载地址:

  ​​​​https://projectcalico.docs.tigera.io/getting-started/kubernetes/installation/config-options​​​

Kubernetes v1.24 基于containerd部署_linux_03

修改POD地址

Kubernetes v1.24 基于containerd部署_bootstrap_04

calico在多网络接口时自动检测到错误的网络接口,导致网络无法连通,通过指定网络接口(网卡名)解决问题

修改​​calico.yaml​

- name: CLUSTER_TYPE
value: "k8s,bgp"

下增加两行

- name: IP_AUTODETECTION_METHOD
value: "interface=ens33"

​ens33​​是我机器的网卡名

确保每个节点网卡名称一致

Kubernetes v1.24 基于containerd部署_ico_05

初始化calico

[root@master1 ~]# kubectl apply -f calico.yaml

Kubernetes v1.24 基于containerd部署_bootstrap_06

安装calico后,立即创建pod,由于工作节点还未加入到集群,所以calico网络还处于pending状态

Kubernetes v1.24 基于containerd部署_linux_07


四、加入节点

如果master节点初始化集群时终端输出的加入集群命令丢失,可以使用以下命令重新获取:

kubeadm token create --print-join-command

Kubernetes v1.24 基于containerd部署_bootstrap_08

分别在node节点运行以下命令

Kubernetes v1.24 基于containerd部署_linux_09

等待全部安装完毕,查看状态

1、查看pods状态


kubectl get pods -n kube-system -owide

Kubernetes v1.24 基于containerd部署_bootstrap_10

查看nodes状态

kubectl get nodes -n kube-system -owide

Kubernetes v1.24 基于containerd部署_linux_11

五、k8s管理

 验证k8s正常提供服务:

         部署一个名为web的deployment并在宿主机可以访问:


root@master:~# kubectl create deployment web --image nginx:latest --replicas 2 --dry-run=client -o yaml > web.yaml

修改一下镜像拉去规则imagePullPolicy: IfNotPresent

Kubernetes v1.24 基于containerd部署_ico_12

查看pods状态

Kubernetes v1.24 基于containerd部署_linux_13

创建service暴露容器端口


root@master:~# kubectl expose deployment web --target-port 80 --port 80 --type NodePort

查看services 

Kubernetes v1.24 基于containerd部署_bootstrap_14


 kubectl命令补全


root@master:~# kubectl completion bash > /etc/bash_completion.d/kubectl
root@master:~# kubeadm completion bash > /etc/bash_completion.d/kubeadm
root@master:~# source /etc/bash_completion.d/kubectl
root@master:~# source /etc/bash_completion.d/kubeadm

   

 kubectl edit 高亮显示


[root@master ~]# vim .bashrc
[root@master ~]# export EDITOR=vim
[root@master ~]# source /root/.bashrc