一、所有机器执行
1.优化机器
1.关闭selinux
#永久
[root@k8s-master01 ~]# sed -i 's#enforcing#disabled#g' /etc/selinux/config
#临时
[root@k8s-master01 ~]# setenforce 0
#检查
[root@k8s-master01 ~]# /usr/sbin/sestatus -v
SELinux status: enabled
2.关闭防火墙
[root@k8s-master01 ~]# systemctl disable --now firewalld
3.关闭swap分区
[root@k8s-master01 ~]# swapoff -a
[root@k8s-master01 ~]# sed -i 's/^.*centos-swap/# &/g' /etc/fstab
[root@k8s-master01 ~]# echo 'KUBELET_EXTRA_ARGS="--fail-swap-on=false"' > /etc/sysconfig/kubelet
4.优化系统
[root@k8s-master01 ~]# rm -f /etc/yum.repos.d/*
[root@k8s-master01 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@k8s-master01 ~]# curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@k8s-master01 ~]# yum makecache
[root@k8s-master01 ~]# yum update -y --exclud=kernel*
5.配置hosts文件
[root@k8s-master01 ~]# vim /etc/hosts
10.0.0.81 k8s-master01
10.0.0.84 k8s-node01
2.更新内核版本
#下载
[root@k8s-master01 ~]# wget https://elrepo.org/linux/kernel/el7/x86_64/RPMS/kernel-lt-5.4.155-1.el7.elrepo.x86_64.rpm
[root@k8s-master01 ~]# wget https://elrepo.org/linux/kernel/el7/x86_64/RPMS/kernel-lt-devel-5.4.155-1.el7.elrepo.x86_64.rpm
#安装
[root@k8s-master01 ~]# yum localinstall -y kernel*
#设置启动优先级
[root@k8s-master01 ~]# grub2-set-default 0 && grub2-mkconfig -o /etc/grub2.cfg
#查看内核版本
[root@k8s-master01 ~]# grubby --default-kernel
/boot/vmlinuz-5.4.155-1.el7.elrepo.x86_64
3.安装ipvs
1.安装依赖
[root@k8s-master01 ~]# yum install conntrack-tools ipvsadm ipset libseccomp wget expect vim net-tools ntp bash-completion ipvsadm jq iptables conntrack sysstat yum-utils device-mapper-persistent-data lvm2 ntp ntpdate -y
2.加载ipvs模块
[root@k8s-master01 ~]# cat > /etc/sysconfig/modules/ipvs.modules << EOF
#!/bin/bash
ipvs_modules="ip_vs ip_vs_lc ip_vs_wlc ip_vs_rr ip_vs_wrr ip_vs_lblc ip_vs_lblcr ip_vs_dh ip_vs_sh ip_vs_fo ip_vs_nq ip_vs_sed ip_vs_ftp nf_conntrack"
for kernel_module in ${ipvs_modules}; do
/sbin/modinfo -F filename ${kernel_module} > /dev/null 2>&1
if [ $? -eq 0 ]; then
/sbin/modprobe ${kernel_module}
fi
done
EOF
[root@k8s-master01 ~]# chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep ip_vs
4.优化系统内核参数
[root@k8s-master01 ~]# cat > /etc/sysctl.d/k8s.conf <<EOF
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
fs.may_detach_mounts = 1
vm.overcommit_memory=1
vm.panic_on_oom=0
fs.inotify.max_user_watches=89100
fs.file-max=52706963
fs.nr_open=52706963
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp.keepaliv.probes = 3
net.ipv4.tcp_keepalive_intvl = 15
net.ipv4.tcp.max_tw_buckets = 36000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp.max_orphans = 327680
net.ipv4.tcp_orphan_retries = 3
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.ip_conntrack_max = 65536
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.top_timestamps = 0
net.core.somaxconn = 16384
EOF
#生效
[root@k8s-master01 ~]# sysctl -p
5.安装docker
[root@k8s-master01 ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@k8s-master01 ~]# yum install -y docker-ce-19.03.15
[root@k8s-master01 ~]# mkdir /etc/docker
[root@k8s-master01 ~]# cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://dp0vxr93.mirror.aliyuncs.com"]
}
EOF
[root@k8s-master01 ~]# systemctl daemon-reload
[root@k8s-master01 ~]# systemctl start docker && systemctl enable docker
6.更新时区
[root@k8s-master01 ~]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' > /etc/timezone
#同步时间
[root@k8s-master01 ~]# ntpdate ntp.aliyun.com
[root@k8s-master01 ~]# crontab -e
#每隔两小时同步时间
* */2 * * * /usr/sbin/ntpdate ntp.aliyun.com &> /dev/null
7.安装kubernetes
1.添加kubernetes源
[root@k8s-master01 ~]# 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=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
2.安装
[root@k8s-master01 ~]# yum install kubelet-1.21.3 kubeadm-1.21.3 kubectl-1.21.3 -y
3.启动
[root@k8s-master01 ~]# systemctl enable kubelet && systemctl start kubelet
#k8s中的kubelet无法启动(注意,在加入集群之前kubelet是不启动的,加入节点后或初始化为master后才会启动)
二、master节点
#初始化
[root@k8s-master01 ~]# kubeadm init --image-repository=registry.cn-hangzhou.aliyuncs.com/k8sos --kubernetes-version=v1.21.3 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16
#提示报错:可改可不改
[WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
#查看
[root@k8s-master01 ~]# docker info | grep Cgroup
Cgroup Driver: cgroupfs
#解决
[root@k8s-master01 ~]# vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --exec-opt native.cgroupdriver=systemd
#在该参数后添加 --exec-opt native.cgroupdriver=systemd
[root@k8s-master01 ~]# mkdir -p $HOME/.kube
[root@k8s-master01 ~]# cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@k8s-master01 ~]# chown $(id -u):$(id -g) $HOME/.kube/config
[root@k8s-master01 ~]# rz flannel.yaml
[root@k8s-master01 ~]# kubectl apply -f flannel.yaml
[root@k8s-master01 ~]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-978bbc4b6-bbj8w 1/1 Running 0 41m
coredns-978bbc4b6-wtrkc 1/1 Running 0 41m
etcd-k8s-master01 1/1 Running 4 41m
kube-apiserver-k8s-master01 1/1 Running 3 41m
kube-controller-manager-k8s-master01 1/1 Running 0 41m
kube-flannel-ds-jkr6t 1/1 Running 0 100s
kube-proxy-qwc6f 1/1 Running 0 41m
kube-scheduler-k8s-master01 1/1 Running 3 41m
flannel.yam文件地址 https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
三、node节点
[root@k8s-node01 ~]# kubeadm join 10.0.0.81:6443 --token nngi3o.kvfvlwbacqhmgd7d --discovery-token-ca-cert-hash sha256:8348afc461f384d3c48ee2c904315e415cf6e813fc8b7ea8baa04e0eee243fb5
#添加节点
1.查看token
[root@k8s-master01 ~]# kubeadm token list
2.如果token失效,则重新生成一个
[root@k8s-master01 ~]# kubeadm token create
3.node节点加入集群
[root@k8s-node02 ~]# kubeadm join 10.0.0.81:6443 --token nngi3o.kvfvlwbacqhmgd7d --discovery-token-ca-cert-hash sha256:8348afc461f384d3c48ee2c904315e415cf6e813fc8b7ea8baa04e0eee243fb5
四、测试集群
[root@k8s-master01 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master01 Ready control-plane,master 61m v1.21.3
k8s-node01 Ready <none> 26m v1.21.3
[root@k8s-master01 ~]# kubectl run test -it --rm --image=busybox:1.28.3
If you don't see a command prompt, try pressing enter.
/ # nslookup kubernetes
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
Name: kubernetes
Address 1: 10.96.0.1 kubernetes.default.svc.cluster.local
#设置Master角色
kubectl label nodes <节点名字> node-role.kubernetes.io/master=<节点名字>
#设置node角色
kubectl label nodes <节点名字> node-role.kubernetes.io/node=<节点名字>