角色 | IP |
---|---|
master | 192.168.185.6 |
slave1 | 192.168.185.7 |
slave2 | 192.168.185.8 |
systemctl stop firewalld && systemctl disable firewalld
# 临时 setenforce 0 # 永久 sed -i 's/enforcing/disabled/' /etc/selinux/config # 重启 reboot
# 临时关闭swap分区 swapoff -a # 永久 sed -ri 's/.*swap.*/#&/' /etc/fstab # 重启 reboot
hostnamectl set-hostname master # 主节点(ip:192.168.185.6)上设置 hostnamectl set-hostname slave1 # 主节点(ip:192.168.185.7)上设置 hostnamectl set-hostname slave2 # 主节点(ip:192.168.185.8)上设置
每个机器上都执行
cat >> /etc/hosts << EOF 192.168.185.6 master 192.168.185.7 slave1 192.168.185.8 slave2 EOF
每个机器上都执行
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 vm.swappiness = 0 EOF
# 加载br_netfilter模块 modprobe br_netfilter
# 查看是否加载 lsmod | grep br_netfilter
# 生效 sysctl --system
每个节点都执行
yum install ntpdate -y ntpdate ntp1.aliyun.com
在每个节点都执行
yum -y install ipset ipvsadm
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_ipv4 EOF
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4
lsmod | grep -e ipvs -e nf_conntrack_ipv4
yum install -y yum-utils evice-mapper-persistent-data lvm2 yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo yum install docker-ce docker-ce-cli containerd.io -y
mkdir -p /etc/docker tee /etc/docker/daemon.json <<-'EOF' { "exec-opts": ["native.cgroupdriver=systemd"], "registry-mirrors" : [ "https://registry.docker-cn.com", "https://docker.mirrors.ustc.edu.cn", "http://hub-mirror.c.163.com", "https://cr.console.aliyun.com/" ] } EOF
systemctl daemon-reload systemctl restart docker && systemctl enable docker
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 https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg EOF
yum install -y kubelet kubeadm kubectl vi /etc/sysconfig/kubelet # 将文件内容修改如下 KUBELET_EXTRA_ARGS="--cgroup-driver=systemd"
systemctl enable kubelet && systemctl start kubelet
# 由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里需要指定阿里云镜像仓库地址 kubeadm init \ --apiserver-advertise-address=192.168.185.6 \ --image-repository registry.aliyuncs.com/google_containers \ --kubernetes-version v1.23.1 \ --service-cidr=10.96.0.0/12 \ --pod-network-cidr=10.244.0.0/16
根据提示执行
mkdir -p $HOME/.kube cp -i /etc/kubernetes/admin.conf $HOME/.kube/config chown $(id -u):$(id -g) $HOME/.kube/config export KUBECONFIG=/etc/kubernetes/admin.conf
到其他两个节点机器里执行
kubeadm join 192.168.185.6:6443 --token vj9im7.mouxm59dtbdcmq9l \ --discovery-token-ca-cert-hash sha256:080c4b13b9fade0815d2f7df612bfdd522d484641fc3b2ec3b05cd1e348f219c
执行以上命令就是将节点添加到集群里,在master节点查看集群里的节点
kubectl get nodes
# 默认的token有效期为24小时,当过期之后,该token就不能用了,这时可以使用如下的命令创建token kubeadm token create --print-join-command # 生成一个永不过期的token kubeadm token create --ttl 0
在Master节点部署CNI网络插件(可能会失败,如果失败,请下载到本地,然后安装)
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml kubectl apply -f kube-flannel.yml
查看部署CNI网络插件进度
kubectl get pods -n kube-system
在Master节点使用kubectl工具查看节点状态
kubectl get nodes
查看集群健康状态
kubectl get cs
查看集群信息
kubectl cluster-info