CentOS7使用kubeadm搭建K8s容器集群平台

k8s集群的安装方式分为基于二进制包或源码编译安装所需组件的安装方式,但该方式较为繁琐。另外一种是基于开源工具的简化安装方式,目前流行的有若干种,应用度最高的是kubeadm,这里实验3节点的Kubernetes集群


系统配置

#修改计算机名
#节点1
hostnamectl --static set-hostname k8s-master && su
#节点2
hostnamectl --static set-hostname k8s-work1 && su
#节点3
hostnamectl --static set-hostname k8s-work2 && su

#关闭防火墙和selinux
setenforce 0 && sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
systemctl stop firewalld && systemctl disable firewalld && systemctl status firewalld

#禁用swap
swapoff -a && sed -i '/ swap / s/^/#/' /etc/fstab

#更换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=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

#更换系统源
yum install wget ntpdate -y \
&& wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo \
&& sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

#更换docker国内源
wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo \
&& yum makecache

#将桥接的流量传递到iptables链
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
vm.swappiness=0
EOF

#校准时间
ntpdate ntp.aliyun.com
hwclock --systohc

#配置host文件
cat <<EOF > /etc/hosts
192.168.3.210 k8s-master
192.168.3.220 k8s-work1
192.168.3.230 k8s-work2
EOF

#验证swap是否关闭
sysctl --system
sysctl -p /etc/sysctl.d/k8s.conf
sysctl -p 
free -g

CentOS7使用kubeadm搭建K8s容器集群平台


安装docker

yum install yum-utils device-mapper-persistent-data lvm2  -y
rpm -Uvh https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
yum --enablerepo=elrepo-kernel install kernel-lt -y
grub2-set-default 'CentOS Linux (4.4.236-1.el7.elrepo.x86_64) 7 (Core)' && reboot
yum install docker-ce-19.03.12 docker-ce-cli-19.03.12 containerd.io -y

#添加网络模块
cat > /etc/modules-load.d/docker.conf <<EOF
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter

lsmod | grep overlay
lsmod | grep br_netfilter  

#修改docker Cgroup Driver为systemd
sed -i "s#^ExecStart=/usr/bin/dockerd.*#ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --exec-opt native.cgroupdriver=systemd#g" /usr/lib/systemd/system/docker.service

#使用国内镜像加速docker拉取
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io
systemctl restart docker && systemctl enable docker && systemctl status docker

#测试
docker run --rm hello-world
docker rmi hello-world

CentOS7使用kubeadm搭建K8s容器集群平台


安装kubeadm

yum install kubelet-1.19.1 kubeadm-1.19.1 kubectl-1.19.1 --disableexcludes=kubernetes -y
systemctl enable kubelet

#配置自动补全
yum install bash-completion -y
kubectl completion bash >/etc/bash_completion.d/kubectl
kubeadm completion bash > /etc/bash_completion.d/kubeadm

CentOS7使用kubeadm搭建K8s容器集群平台


配置k8s环境

#查看需要的镜像
kubeadm config images list

CentOS7使用kubeadm搭建K8s容器集群平台

kubeadm config images list --image-repository registry.aliyuncs.com/google_containers

CentOS7使用kubeadm搭建K8s容器集群平台


vi get-k8s.sh
#制作拉取镜像脚本
#!/bin/bash
 
K8S_VERSION=v1.19.2
PAUSE_VERSION=3.2
ETCD_VERSION=3.4.13-0
CORE_DNS_VERSION=1.7.0

#pull from aliyun
docker pull registry.aliyuncs.com/google_containers/kube-apiserver:${K8S_VERSION} 
docker pull registry.aliyuncs.com/google_containers/kube-controller-manager:${K8S_VERSION} 
docker pull registry.aliyuncs.com/google_containers/kube-scheduler:${K8S_VERSION} 
docker pull registry.aliyuncs.com/google_containers/kube-proxy:${K8S_VERSION} 
docker pull registry.aliyuncs.com/google_containers/pause:${PAUSE_VERSION} 
docker pull registry.aliyuncs.com/google_containers/etcd:${ETCD_VERSION} 
docker pull registry.aliyuncs.com/google_containers/coredns:${CORE_DNS_VERSION} 
 
#retag to k8s.gcr.io
docker tag registry.aliyuncs.com/google_containers/kube-apiserver:${K8S_VERSION} k8s.gcr.io/kube-apiserver:${K8S_VERSION} 
docker tag registry.aliyuncs.com/google_containers/kube-controller-manager:${K8S_VERSION} k8s.gcr.io/kube-controller-manager:${K8S_VERSION} 
docker tag registry.aliyuncs.com/google_containers/kube-scheduler:${K8S_VERSION} k8s.gcr.io/kube-scheduler:${K8S_VERSION} 
docker tag registry.aliyuncs.com/google_containers/kube-proxy:${K8S_VERSION} k8s.gcr.io/kube-proxy:${K8S_VERSION} 
docker tag registry.aliyuncs.com/google_containers/pause:${PAUSE_VERSION} k8s.gcr.io/pause:${PAUSE_VERSION} 
docker tag registry.aliyuncs.com/google_containers/etcd:${ETCD_VERSION} k8s.gcr.io/etcd:${ETCD_VERSION} 
docker tag registry.aliyuncs.com/google_containers/coredns:${COREDNS_VERSION} k8s.gcr.io/coredns:${CORE_DNS_VERSION} 
 
#remove
docker rmi registry.aliyuncs.com/google_containers/kube-apiserver:${K8S_VERSION} 
docker rmi registry.aliyuncs.com/google_containers/kube-controller-manager:${K8S_VERSION} 
docker rmi registry.aliyuncs.com/google_containers/kube-scheduler:${K8S_VERSION} 
docker rmi registry.aliyuncs.com/google_containers/kube-proxy:${K8S_VERSION} 
docker rmi registry.aliyuncs.com/google_containers/pause:${PAUSE_VERSION} 
docker rmi registry.aliyuncs.com/google_containers/etcd:${ETCD_VERSION} 
docker rmi registry.aliyuncs.com/google_containers/coredns:${CORE_DNS_VERSION}

#执行
chmod +x get-k8s.sh && ./get-k8s.sh
docker images



更新日期: 2020年08月12日
文章标签: Linux
文章链接: https://www.vos.cn/os/449.html  
版权说明:如非注明,本站文章均为vOS原创,转载请注明出处和附带本文链接。