0》、 虚拟机需要固定IP :etc/sysconfig/network-scripts/ifcfg-ens33
BOOTPROTO=dhcp
改成:
BOOTPROTO= static
IPADDR=192.168.31.160
DNS1=192.168.31.2
GATEWAY=192.168.31.2
重启网络
service network restart
1》、 所有机器关闭防火墙 和firewalld服务
systemctl stop firewalld && systemctl disable firewalld
systemctl stop iptables && systemctl disable iptables
2》关闭selinux
setenforce 0
sed -i '/^SELINUX=/cSELINUX=disabled' /etc/sysconfig/selinux
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
3》关闭swap
swapoff -a # 临时
sed -ri 's/.*swap.*/#&/' /etc/fstab
4》只主机:编辑添加hosts
cat > /etc/hosts << EOF
192.168.31.160 master1
192.168.31.159 node1
EOF
hostnamectl set-hostname 主机名
5》将桥接的IPv4流量传递到iptables的链
cat <
EOF
cat <
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system # 生效
6》同步时间:
yum install -y ntpdate
ntpdate cn.pool.ntp.org
# 启动chronyd服务 设置chronyd服务开机自启
systemctl start chronyd && systemctl enable chronyd
=============================== yum install -y chrony
=============================== chronyc sources -v time.windows.com
7》安装docker:
最新版本:
wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum -y install docker-ce
systemctl enable docker && systemctl start docker
指定版本:
yum install -y yum-utils
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install -y docker-ce-20.10.7 docker-ce-cli-20.10.7 containerd.io-1.4.6
systemctl enable docker && systemctl start docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://mcxfjzdt.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
8》添加阿里云YUM软件源
cat <
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
9》安装kubeadm、kubelet和kubectl 由于版本更新频繁 此处指定版本安装
yum install -y kubelet-1.23.6 kubeadm-1.23.6 kubectl-1.23.6
重启docker让配置文件生效:
systemctl daemon-reload && systemctl restart docker
开机自启:
systemctl enable kubelet
卸载
yum remove kubeadm.x86_64 kubectl.x86_64 kubelet.x86_64
10》在master节点上部署kubetnets 注:该操作只需要在master上面执行
kubeadm init --kubernetes-version v1.23.6 --apiserver-advertise-address 192.168.31.160 --pod-network-cidr 10.244.0.0/16 --service-cidr 10.245.0.0/16 --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers
返回结果记录:
kubeadm join 192.168.31.160:6443 --token rwrjw2.gs9keeb8jlfgjhou \
--discovery-token-ca-cert-hash sha256:7d050c32a9a067f6beb4befa4cc57d23204c20c91b51af262467d149775dfa2f
11》普通用户执行
mkdir -p $HOME/.kube #创建一个家目录
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config #拷贝一个文件到家目录里(该文件相当于管理员文件,拥有此文件即可使用kubectl)
sudo chown $(id -u):$(id -g) $HOME/.kube/config #给文件赋予一个权限,
如果是root用户也可以执行
export KUBECONFIG=/etc/kubernetes/admin.conf #整体设置一个环境变量
12》
在副节点执行:
kubeadm join 192.168.31.160:6443 --token szat3f.g89m4ali0vdtqe31 --discovery-token-ca-cert-hash sha256:7d050c32a9a067f6beb4befa4cc57d23204c20c91b51af262467d149775dfa2f
如果token失效:在主节点生成新的
kubeadm token list
kubeadm token create --print-join-command
报错的话可能是版本不一致导致
查看docker版本,并重新安装
yum list docker-ce --showduplicates | sort -r
yum downgrade --setopt=obsoletes=0 -y docker-ce-20.10.9-3.el7 docker-ce-cli-20.10.9-3.el7 containerd.io
将主节点(master)中的“/etc/kubernetes/admin.conf”文件拷贝到两个node节点相同目录下
配置环境变量 立即生效
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
source ~/.bash_profile
13》主节点执行 安装部署CNI网络插件
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
成功后验证一下
kubectl get pods -n kube-system
// 集群节点
kubectl get nodes
重启网络
service network restart
重启计算机
reboot
查看pod服务
kubectl get pods,service
创建Nginx测试:
docker pull nginx
#重新标记
docker tag nginx:latest kittod/nginx:1.21.5
kubectl create deployment nginx --image=nginx
暴露端口
kubectl expose deployment nginx --port=80 --type=NodePort
查看Nginx端口
kubectl get pod,svc
任意一个节点的ip加上端口访问看是否成功