Skip to content

k8s-master-搭建操作

环境初始化

修改主机名

hostnamectl set-hostname master

添加本地解析

echo '192.168.20.50 master' >> /etc/hosts
echo '192.168.20.60 node1' >> /etc/hosts
echo '192.168.20.70 node2' >> /etc/hosts

关闭系统防火墙和系统的NetworkManager工具

systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl stop NetworkManager.service
systemctl disable NetworkManager.service

关闭系统的selinux

sed -i '/^SELINUX=.*/c SELINUX=disabled' /etc/selinux/config
sed -i 's/^SELINUXTYPE=.*/SELINUXTYPE=disabled/g' /etc/selinux/config
grep --color=auto '^SELINUX' /etc/selinux/config
setenforce 0

关闭一些防火墙规则

iptables -F
iptables -X
iptables -Z
iptables -P FORWARD ACCEPT

关闭swap交换分区<<======非常重要

swapoff -a                                           #临时关闭,但是开机还是会开启
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab        #防止开机打开swap

修改Linux内核参数,开启数据包转发功能

cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1 
net.bridge.bridge-nf-call-ip6tables = 1 
net.ipv4.ip_forward=1
vm.max_map_count=262144
EOF
modprobe br_netfilter
sysctl -p  /etc/sysctl.d/k8s.conf

配置NTP

yum -y install ntpdate chrony
systemctl start chronyd
systemctl enable chronyd
ntpdate -u ntp.aliyun.com
hwclock -w
date

配置yum源

下载一些工具包

yum install -y yum-utils device-mapper-persistent-data lvm2

添加Docker源

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

添加阿里源

curl -o /etc/yum.repos.d/aliyun.repo https://mirrors.aliyun.com/repo/Centos-7.repo

添加kubernetes源

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[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

清理yun缓存,重建yum缓存

yum clean all
yum makecache -y
yum makecache fast

安装docker环境

yum -y install docker-ce docker-ce-cli containerd.io docker-compose-plugin

配置docker加速源

mkdir /etc/docker
cat > /etc/docker/daemon.json <<EOF 
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "registry-mirrors": ["https://jy4tq1fv.mirror.aliyuncs.com"]
}
EOF

启动docker+开机自启动

systemctl start docker && systemctl enable docker

安装kubernetes环境初始化工具

yum install -y kubelet-1.22.4 kubectl-1.22.4 kubeadm-1.22.4 ipvsadm

启动kubelet+开机自启动

systemctl start kubelet && systemctl enable kubelet 

查看版本信息

kubeadm version 

初始化集群

kubeadm init \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.22.4 \
--pod-network-cidr=10.244.0.0/16
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
  • 保存一下最后一段的token:kubeadm join ip --token xxx --discovery-token-ca-cert-hash xxx
  • 如果忘记了重新获取:kubeadm token create --print-join-command

安装网络插件(其它工作节点加入集群后执行)

kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/v0.20.2/Documentation/kube-flannel.yml

查看集群节点

kubectl get nodes -owide

所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自linlink~文档