部署集群之前,可以检查设置机器配置

关闭防火墙 Linux 以及swap分区等

systemctl stop firewalld
systemctl disable firewalld
iptables -F && iptables -X && iptables -F -t nat && iptables -X -t nat
iptables -P FORWARD ACCEPT
swapoff -a
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
#如果开启了swap分区,kubelet会启动失败(可以通过设置参数——-fail-swap-on设置为false)

配置内核参数,将桥接的IPv4流量传递到iptables的链,然后执行sysctl --system

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

 执行命令使修改生效

modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf

设置docker源

yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

yum install docker;docker --version可查看版本

yum list docker-ce --showduplicates | sort -r 可查看可指定docker-ce版本安装

yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
[root@localhost yum.repos.d]# docker --version
Docker version 1.13.1, build 64e9980/1.13.1
[root@localhost yum.repos.d]# yum list docker-ce --showduplicates | sort -r
 * updates: mirrors.aliyun.com
This system is not registered with an entitlement server. You can use subscription-manager to register.
              : subscription-manager
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror, langpacks, product-id, search-disabled-repos,
 * extras: mirrors.aliyun.com
docker-ce.x86_64            3:19.03.9-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.8-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.7-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.6-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.5-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.4-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.3-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.2-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.1-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.12-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.11-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.10-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.0-3.el7                     docker-ce-stable

修改Docker驱动类型为systemd

vim /etc/docker/daemon.json;重启docker后可docker info查看

{
"exec-opts": ["native.cgroupdriver=systemd"]
}

systemctl start docker启动docker

systemctl status docker查看docker状态

[root@localhost yum.repos.d]# systemctl start docker
[root@localhost yum.repos.d]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2020-08-16 21:28:02 CST; 4s ago
     Docs: http://docs.docker.com
 Main PID: 21497 (dockerd-current)
    Tasks: 21
   CGroup: /system.slice/docker.service
           ├─21497 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=d...
           └─21503 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-in...

Aug 16 21:28:00 localhost.localdomain dockerd-current[21497]: time="2020-08-16T21:28:00.319452641+08:00" level=info msg="libc...503"
Aug 16 21:28:01 localhost.localdomain dockerd-current[21497]: time="2020-08-16T21:28:01.466479955+08:00" level=info msg="Grap...nds"
Aug 16 21:28:01 localhost.localdomain dockerd-current[21497]: time="2020-08-16T21:28:01.469339682+08:00" level=info msg="Load...rt."
Aug 16 21:28:01 localhost.localdomain dockerd-current[21497]: time="2020-08-16T21:28:01.509188279+08:00" level=info msg="Fire...rue"
Aug 16 21:28:01 localhost.localdomain dockerd-current[21497]: time="2020-08-16T21:28:01.825402806+08:00" level=info msg="Defa...ess"
Aug 16 21:28:02 localhost.localdomain dockerd-current[21497]: time="2020-08-16T21:28:02.170305513+08:00" level=info msg="Load...ne."
Aug 16 21:28:02 localhost.localdomain dockerd-current[21497]: time="2020-08-16T21:28:02.224402724+08:00" level=info msg="Daem...ion"
Aug 16 21:28:02 localhost.localdomain dockerd-current[21497]: time="2020-08-16T21:28:02.225358648+08:00" level=info msg="Dock...13.1
Aug 16 21:28:02 localhost.localdomain dockerd-current[21497]: time="2020-08-16T21:28:02.239844751+08:00" level=info msg="API ...ock"
Aug 16 21:28:02 localhost.localdomain systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost yum.repos.d]#

为了快速搭建k8s集群,本教程是基于kubeadm进行kubernetes的自动化安装部署

设置拉取k8s的源

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

安装kubelet、kubeadm、kubectl,选择的k8s版本为v1.15.1 

yum -y install kubelet-1.15.1 kubeadm-1.15.1 kubectl-1.15.1

查看下载节点初始化所需镜像

kubeadm config images list

部署master节点,在master进行Kubernetes集群初始化。

kubeadm init --kubernetes-version=1.15.1 --apiserver-advertise-address=192.168.218.5 --image-repository registry.aliyuncs.com/google_containers --service-cidr=10.1.0.0/16 --pod-network-cidr=10.222.0.0/16

--kubernetes-version=1.15.1中的1.15.1是对应上面的安装的k8s版本,

--apiserver-advertise-address=192.168.218.5 为master的ip地址

--service-cidr=10.1.0.0/16 为service分配的虚拟地址空间

--pod-network-cidr=10.222.0.0/16 为pod的分配虚拟地址空间

--image-repository registry.aliyuncs.com/google_containers由于国外镜像地址被墙了,所以要特别指定使用国内的镜像地址

成功后会显示类似如下信息:

 记录上面这个信息,用于后面集群中加入work node节点用,但是上面这个token是有时间限制的,超出时间范围就失效了,所以还是建议生成一个永久的token

mkdir -p /root/.kube
cp /etc/kubernetes/admin.conf /root/.kube/config

重新生成

kubeadm token create --print-join-command

 

Logo

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

更多推荐