文章目录

命令脚本

各个节点设置

yum update -y
systemctl stop firewalld
systemctl disable firewalld
sed -i 's/enforcing/disabled/' /etc/selinux/config   
sed -ri 's/.*swap.*/#&/' /etc/fstab   

重启服务器

reboot

master节点设置

cat >> /etc/hosts << EOF
192.168.160.130  k8s-master
192.168.160.131  k8s-node1
192.168.160.132  k8s-node2
EOF

各个节点设置

将桥接的IPv4流量传递到iptables的链

cat > /etc/sysctl.d/kubernetes.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF

生效并同步时间

sysctl --system 
systemctl start chronyd
systemctl enable chronyd
date 

下载安装docker

yum list docker-ce --showduplicates | sort -r
yum install -y yum-utils
sudo 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

启动docker

systemctl enable docker && systemctl start docker
systemctl list-unit-files | grep docker
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
	"exec-opts": ["native.cgroupdriver=systemd"], 	
	"registry-mirrors": ["https://kn0t2bca.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
systemctl enable docker.service
systemctl status docker

添加阿里云YUM软件源

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

安装kubeadm,kubelet和kubectl

yum install -y kubelet-1.23.0 kubeadm-1.23.0 kubectl-1.23.0
systemctl enable kubelet
systemctl start kubelet
export KUBECONFIG=/etc/kubernetes/kubelet.conf 
source /etc/profile

master节点安装

kubeadm init \
  --apiserver-advertise-address=192.168.160.130 \
  --image-repository registry.aliyuncs.com/google_containers \
  --kubernetes-version v1.23.0 \
  --service-cidr=10.96.0.0/12 \
  --pod-network-cidr=10.244.0.0/16 \
  --ignore-preflight-errors=all

一般网速比较快的也要等个一二分钟的样子,才行。
安装失败后需要重新执行,此时要先执行

kubeadm reset

在这里插入图片描述
master节点

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

安装 Pod 网络插件(CNI)

wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
sudo kubectl apply -f kube-flannel.yml

非master节点执行

kubeadm join 192.168.160.130:6443 --token 910dhz.k4v3mjcuuq1q0t6o \
	--discovery-token-ca-cert-hash sha256:ee7b23304f1331e1f40d2064d585cff29ce0385d35bcad3076ebf4822e21a53f 

将主节点的这个/etc/kubernetes/admin.conf文件复制到从节点
主节点查看复制

cat /etc/kubernetes/admin.conf

从节点编辑

vim /etc/kubernetes/admin.conf

追加路径到bash_profile中

echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
source ~/.bash_profile
systemctl restart kubelet

查看节点情况

kubectl get nodes

在这里插入图片描述
三个k8s节点都已经准备就绪
安装nginx

sudo kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pod,svc

各节点访问nginx(示例)
在这里插入图片描述

http://192.168.160.130:32354/
在这里插入图片描述

http://192.168.160.131:32354/
在这里插入图片描述

http://192.168.160.132:32354/
在这里插入图片描述


文章参考:
https://blog.csdn.net/YangCheney/article/details/124893351
https://blog.csdn.net/java_wxid/article/details/127967699
https://docs.qq.com/doc/DUWRRQmZpeE1Sd1dC

视频参考:
哔哩哔哩安装k8s

总结

提示:以上就是今天要讲的内容,本文仅仅简单介绍了k8s安装配置。

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐