必备:

     Centos7 服务器 or 虚拟机 两台。

第一步更新系统:

这里提供一个shell脚本方便我们进行一系列的设置:

curl -O https://raw.githubusercontent.com/bboysoulcn/centos/master/centos.sh

之后我们可以使用vim编辑器进到这个shell脚本里面看一下,大致上我们安装k8s之前的准备工作都在里面了:

开始

第一步更新系统:

       查看一下更新系统的脚本命令:

install_software()
{
    echo "starting install software ..."
    yum install epel-release -y
    yum update -y
    yum install git wget screen  nmap vim htop iftop iotop gcc gcc-c++ net-tools unzip nfs-utils psmisc zip rsync telnet nano -y
    echo "software installed !!!"
}

基本都是一些常用的东西,既然没什么问题,我们就开始执行吧。

sh centos.sh

     第二步关闭防火墙:

  其实这里我们可以不关闭防火墙只要保证k8s所使用的端口没有被拦截即可,这里为了方便选择直接关闭。

这里列出所有需要验证的端口:

Control-plane node(s)

ProtocolDirectionPort RangePurposeUsed By
TCPInbound6443*Kubernetes API serverAll
TCPInbound2379-2380etcd server client APIkube-apiserver, etcd
TCPInbound10250Kubelet APISelf, Control plane
TCPInbound10251kube-schedulerSelf
TCPInbound10252kube-controller-managerSelf

Worker node(s)

ProtocolDirectionPort RangePurposeUsed By
TCPInbound10250Kubelet APISelf, Control plane
TCPInbound30000-32767NodePort Services†All

 首先再次让我们看一下关闭防火墙的脚本命令:

close_firewalld()
{
    echo "starting stop firewalld ..."
    systemctl stop firewalld
    systemctl disable firewalld
    echo "firewalld stoped !!!"
}

运行shell脚本关闭防火墙

设置防火墙为 iptables

yum -y install iptables-services && systemctl start iptables && systemctl enable iptables && iptables -F && service iptables save

第三步设置hostname:

set_hostname()
{
    echo "start set your hostname ..."
    read -p "please input your hostname: " hostname
    hostnamectl set-hostname $hostname
    echo "your hostname is set to" $hostname
}

第四步关闭swap

#(1)临时关闭swap分区, 重启失效;
   swapoff  -a

#(2)永久关闭swap分区
swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

 这里说明一下为甚么需要关闭swap

kubernetes的想法是将实例紧密包装到尽可能接近100%。 所有的部署应该与CPU /内存限制固定在一起。 所以如果调度程序发送一个pod到一台机器,它不应该使用交换。 设计者不想交换,因为它会减慢速度。

第四步关闭SeLinux

swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

这里说明一下为甚么需要关闭SeLinux直接贴出官方原文。

Setting SELinux in permissive mode by running setenforce 0 and sed ... effectively disables it. This is required to allow containers to access the host filesystem, which is needed by pod networks for example. You have to do this until SELinux support is improved in the kubelet.

第五步安装docker-ce:

https://blog.csdn.net/weixin_40165163/article/details/103882868

第六步设置 net.bridge.bridge-nf-call-iptables 值为1

cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv6.conf.all.disable_ipv6=1
EOF
sysctl --system

原因:

Some users on RHEL/CentOS 7 have reported issues with traffic being routed incorrectly due to iptables being bypassed. You should ensure net.bridge.bridge-nf-call-iptables is set to 1 in your sysctl config, e.g.

第七步下载所需docker images

其实至此大部分的准备工作都已经结束了,但是我们在安装k8s后启动时会发生无法下载依赖组件的问题(问题在于局域网无法访问互联网)所以我们需要在这里下载好所需的组件。

依次执行即可(这里需要根据k8s版本来下载对应版本的组件):

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.17.3
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.17.3
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.17.3
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.17.3
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.3-0
docker pull mirrorgooglecontainers/pause:3.1
docker pull coredns/coredns:1.6.5

  修改成k8s认识的image

docker tag docker.io/registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.17.3 k8s.gcr.io/kube-apiserver:v1.17.3
docker tag docker.io/registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.17.3 k8s.gcr.io/kube-controller-manager:v1.17.3
docker tag docker.io/registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.17.3 k8s.gcr.io/kube-scheduler:v1.17.3
docker tag docker.io/registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.17.3 k8s.gcr.io/kube-proxy:v1.17.3
docker tag docker.io/registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.3-0 k8s.gcr.io/etcd:3.4.3-0
docker tag docker.io/mirrorgooglecontainers/pause:3.1 k8s.gcr.io/pause:3.1
docker tag docker.io/coredns/coredns:1.6.5 k8s.gcr.io/coredns:1.6.5

第八步

# 设置系统时区为中国/上海

timedatectl set-timezone Asia/Shanghai

# 将当前的 UTC 时间写入硬件时钟

timedatectl set-local-rtc 0

# 重启依赖于系统时间的服务

systemctl restart rsyslog

systemctl restart crond

第九步关闭不需要的服务

systemctl stop postfix && systemctl disable postfix 

 第十步 切换系统内核

rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
yum --enablerepo=elrepo-kernel install -y kernel-lt
grub2-set-default 'CentOS Linux (4.4.189-1.el7.elrepo.x86_64) 7 (Core)'

参考:https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/

 

Logo

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

更多推荐