Kubernetes + containerd 部署
kubernetes 测试环境部署
一、测试环境:
主机名 | IPAdress | CPU/内存 | 角色 |
k8s-containerd-m | 192.168.194.50 | 2C/4G | master |
k8s-containerd-n1 | 192.168.194.51 | 2C/4G | node |
k8s-containerd-n2 | 192.168.194.52 | 2C/4G | node |
二、准备工作【所有节点都要操作】
1、修改主机名
[root@localhost ~]# hostnamectl set-hostname k8s-containerd-m
[root@localhost ~]# hostnamectl set-hostname k8s-containerd-n1
[root@localhost ~]# hostnamectl set-hostname k8s-containerd-n2
2、关闭防火墙
[root@k8s-containerd-m ~]# systemctl stop firewalld
[root@k8s-containerd-m ~]# systemctl disable firewalld
[root@k8s-containerd-m ~]# yum -y remove firewalld
3、关闭SELinux 和swap 分区
[root@k8s-containerd-m ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config
[root@k8s-containerd-m ~]# setenforce 0
[root@k8s-containerd-m ~]# sed '/centos-swap/s/^/#/' /etc/fstab
[root@k8s-containerd-m ~]# swapoff -a
4、添加hosts文件内容
[root@k8s-containerd-m ~]# cat >> /etc/hosts << EOF
> 192.168.194.50 k8s-containerd-m
> 192.168.194.51 k8s-containerd-n1
> 192.168.194.52 k8s-containerd-n2
> EOF
5、修改内核参数
[root@k8s-containerd-m ~]# modprobe br_netfilter
[root@k8s-containerd-m ~]# echo "modprobe br_netfilter" >> /etc/profile
[root@k8s-containerd-m ~]# cat >> /etc/sysctl.d/k8s.conf << EOF
> net.bridge.bridge-nf-call-ip6tables = 1
> net.bridge.bridge-nf-call-iptables = 1
> net.ipv4.ip_forward = 1
> EOF
[root@k8s-containerd-m ~]# sysctl -p /etc/sysctl.d/k8s.conf
[root@k8s-containerd-m ~]# sysctl --system # 生效
6、配置免密登陆
[root@k8s-containerd-m ~]# ssh-keygen (不断回车)
[root@k8s-containerd-m ~]# ssh-copy-id root@192.168.194.51
[root@k8s-containerd-m ~]# ssh-copy-id root@192.168.194.52
7、配置时间同步
[root@k8s-containerd-m ~]# rm -f /etc/localtime
[root@k8s-containerd-m ~]# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@k8s-containerd-m ~]# yum install ntpdate -y
[root@k8s-containerd-m ~]# ntpdate ntp.aliyun.com
21 Feb 06:46:27 ntpdate[18049]: adjust time server 203.107.6.88 offset 0.002559 sec
三、安装containerd/kubeadm/kubelet【所有节点】
1、 加载模块
[root@k8s-containerd-m ~]# cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
> overlay
> br_netfilter
> EOF
[root@k8s-containerd-m ~]# modprobe overlay
[root@k8s-containerd-m ~]# modprobe br_netfilter
2、设置系统参数,并永久有效
[root@k8s-containerd-m ~]# cat <<EOF | tee /etc/sysctl.d/99-kubernetes-cri.conf
> net.bridge.bridge-nf-call-iptables = 1
> net.ipv4.ip_forward = 1
> net.bridge.bridge-nf-call-ip6tables = 1
> EOF
[root@k8s-containerd-m ~]# sysctl --system # 从所有系统目录中读取数值
[root@k8s-containerd-m ~]# sysctl -p /etc/sysctl.d/99-kubernetes-cri.conf # 从文件中读取数值
3、安装containerd(通过yum安装)
[root@k8s-containerd-m ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
[root@k8s-containerd-m ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
[root@k8s-containerd-m ~]# yum clean all && yum makecache
[root@k8s-containerd-m ~]# yum -y update && yum install -y containerd.io
[root@k8s-containerd-m ~]# mkdir -p /etc/containerd
[root@k8s-containerd-m ~]# containerd config default | tee /etc/containerd/config.toml
[root@k8s-containerd-m ~]# systemctl enable containerd
[root@k8s-containerd-m ~]# systemctl restart containerd
4、修改containerd配置文件
[root@k8s-containerd-m ~]# vi /etc/containerd/config.toml
[plugins."io.containerd.grpc.v1.cri"]
sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.2" # 61行左右
...
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
SystemdCgroup = true # 125行左右
...
[plugins."io.containerd.grpc.v1.cri".registry]
config_path = "/etc/containerd/certs.d" # 145行左右
[plugins."io.containerd.grpc.v1.cri".registry.auths]
[plugins."io.containerd.grpc.v1.cri".registry.configs]
[plugins."io.containerd.grpc.v1.cri".registry.headers]
...
[root@k8s-containerd-m ~]#
[root@k8s-containerd-m ~]# mkdir /etc/containerd/certs.d/docker.io -pv
[root@k8s-containerd-m ~]# cat > /etc/containerd/certs.d/docker.io/hosts.toml << EOF
> server = "https://docker.io"
> [host."https://mn3d3160.mirror.aliyuncs.com"]
> capabilities = ["pull", "resolve"]
> EOF
[root@k8s-containerd-m ~]# systemctl restart containerd
[root@k8s-containerd-m ~]# ctr i pull docker.io/library/mysql:latest # 测试能否拉取镜像
5、添加阿里云YUM软件源,安装kubeadm,kubelet和kubectl
[root@k8s-containerd-m ~]# 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
> EOF
[root@k8s-containerd-m ~]# yum install -y kubelet-1.26.0 kubeadm-1.26.0 kubectl-1.26.0
[root@k8s-containerd-m ~]# systemctl enable kubelet
四、部署Kubernetes Master 【在master节点上执行】
[root@k8s-containerd-m ~]# kubeadm init \
> --apiserver-advertise-address=192.168.194.50 \
> --image-repository registry.aliyuncs.com/google_containers \
> --kubernetes-version v1.26.0 \
> --service-cidr=10.96.0.0/12 \
> --pod-network-cidr=10.244.0.0/16 \
> --ignore-preflight-errors=all
# 注释 --apiserver-advertise-address 集群通告地址 --image-repository 由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里指定阿里云镜像仓库地址 --kubernetes-version K8s版本,与上面安装的一致 --service-cidr 集群内部虚拟网络,Pod统一访问入口 --pod-network-cidr Pod网络,,与下面部署的CNI网络组件yaml中保持一致
[root@k8s-containerd-m ~]# mkdir -p $HOME/.kube
[root@k8s-containerd-m ~]# cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@k8s-containerd-m ~]# chown $(id -u):$(id -g) $HOME/.kube/config
[root@k8s-containerd-m ~]# kubectl get nodes # 这时候节点状态为 NotReady
五、加入Kubernetes Nodes 【在node节点上执行】
向集群添加新节点,执行在kubeadm init输出的kubeadm join命令:
[root@k8s-containerd-n1 ~]# kubeadm join 192.168.194.50:6443 --token 9h7q7y.k9aq5ve79ishux97 --discovery-token-ca-cert-hash sha256:cef6afca6bee17ac79bf4dde10f02386697bfc8831f3698e599b015bad8fafde [root@k8s-containerd-n1 ~]# kubectl get nodes
# 这时候节点状态都为 NotReady,但可以看到节点变多了
NAME STATUS ROLES AGE VERSION
k8s-containerd-m NotReady control-plane 17h v1.26.0
k8s-containerd-n1 NotReady <none> 14h v1.26.0
k8s-containerd-n2 NotReady <none> 16h v1.26.0
# 为什么是NotReady状态,我们通过命令:kubectl get pods -n kube-system ,可以看到有两个coredns 开头的pod处于pending状态,因为还没部署网络组件,所以这两个pod没法被分配到node节点上,因此节点都是NotReady状态
# 默认token有效期为24小时,当过期之后,该token就不可用了。这时就需要重新创建token,可以直接使用命令快捷生成: kubeadm token create --print-join-command
六、部署容器网络
Calico是一个纯三层的数据中心网络方案,Calico支持广泛的平台,包括Kubernetes、OpenStack等。
Calico 在每一个计算节点利用 Linux Kernel 实现了一个高效的虚拟路由器( vRouter) 来负责数据转发,而每个 vRouter 通过 BGP 协议负责把自己上运行的 workload 的路由信息向整个 Calico 网络内传播。
此外,Calico 项目还实现了 Kubernetes 网络策略,提供ACL功能。
[root@k8s-containerd-m ~]# wget https://docs.projectcalico.org/manifests/calico.yaml
# 这是国外的镜像源,可能下载不了。可通过以下连接下载,免费
https://download.csdn.net/download/weixin_44572215/87626361
[root@k8s-containerd-m ~]# kubectl apply -f calico-d.yaml # 等待安装完成
[root@k8s-containerd-m ~]# kubectl get pods -n kube-system
# 等pod安装完成,这可能需要一点时间 # 等pod 全部为Running状态后,再次查看node状态,就变Ready了
[root@k8s-containerd-n2 kubernetes]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-containerd-m Ready control-plane 17h v1.26.0
k8s-containerd-n1 Ready <none> 14h v1.26.0
k8s-containerd-n2 Ready <none> 16h v1.26.0
更多推荐
所有评论(0)