Kubernetes---高可用的 K8S 集群构建

⒈系统初始化

  1.设置系统主机名以及 Host 文件的相互解析

hostnamectl set-hostname k8s-master01

  2.安装依赖包

yum install -y conntrack ntpdate ntp ipvsadm ipset jq iptables curl sysstat libseccomp wget vim net-tools git

  3.设置防火墙为Iptables 并设置空规则 

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

  4.关闭SELINUX

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

  5.调整内核参数,对于 K8S

cat > kubernetes.conf <<EOF 
net.bridge.bridge-nf-call-iptables=1 
net.bridge.bridge-nf-call-ip6tables=1 
net.ipv4.ip_forward=1 
net.ipv4.tcp_tw_recycle=0 
vm.swappiness=0 # 禁止使用 swap 空间,只有当系统 00M时才允许使用它 
vm.overcommit_memory=1 # 不检查物理内存是否够用 
vm.panic_on_oom=0 # 开启 00M 
fs.inotify.max_user_instances=8192 
fs.inotify.max_user_watches=1048576 
fs.file-max=52706963 
fs.nr_open=52706963 
net.ipv6.conf.all.disable_ipv6=1 
net.netfilter.nf_conntrack_max=2310720 
EOF 

cp kubernetes.conf /etc/sysctl.d/kubernetes.conf 
sysctl -p /etc/sysctl.d/kubernetes.conf

  6.调整系统时区

# 设置系统时区为中国/上海 
timedatectl set-timezone Asia/Shanghai 
#将当前的 UTC 时间写入硬件时钟 
timedatectl set-local-rtc 0
# 重启依赖于系统时间的服务 
systemctl restart rsyslog 
systemctl restart crond

  7.关闭系统不需要服务

systemctl stop postfix && systemctl disable postfix

  8.设置 rsyslogd 和 systemd journald

mkdir /var/log/journal #持久化保存日志的目录 
mkdir /etc/systemd/journald.conf.d 
cat > /etc/systemd/journald.conf.d/99-prophet.conf <<E0F 
[Journal] 
# 持久化保存到磁盘 
Storage=persistent 

# 压缩历史日志 
Compress=yes 

SyncIntervalSec=5m 
RateLimitInterval=30s 
RateLimitBurst=1000 

# 最大占用空间 10G 
SystemMaxUse=10G 

#单日志文件最大200M 
SystemMaxFileSize=200M 

# 日志保存时间2周 
MaxRetentionSec=2week 

# 不将日志转发到 syslog 
ForwardToSyslog=no 
EOF 

systemctl restart systemd-journald

  9.升级系统内核为4.44

  CentOs7.x系统自带的3.10.x内核在在一些Bugs,导致运行的Docker、Kubernetes不稳定,例如:rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm

rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm 
# 安装完成后检查 /boot/grub2/grub.cfg 中对应内核 menuentry 中是否包含 initrd16 配置,如果没有,再安装 一次!

yum --enablerepo=elrepo-kernel install -y kernel-lt #设置开机从新内核启动 

grub2-set-default "CentOS Linux (4.4.182-1.el7.elrepo.x86_64) 7 (Core)"

#重启后安装内核源文件 
yum --enablerepo=elrepo-kernel install kernel-lt-devel-$(uname -r) kernel-lt-headers-$(uname -r) 

  10.关闭NUMA 

cp /etc/default/grub{,.bak}
vim /etc/default/grub #在 GRUB_CMDLINE_LINUX 一行添加`numa=off`参数,如下所示:
diff /etc/default/grub.bak /etc/default/grub 
6c6 
< GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rhgb quiet"
---
> GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rhgb quiet numa=off"
cp /boot/grub2/grub.cfg{,.bak}
grub2-mkconfig -o /boot/grub2/grub.cfg

 

⒉Kubeadm 部署安装

  1.kube-proxy开启ipvs的前置条件

modprobe br_netfilter 
cat > /etc/sysconfig/modules/ipvs.modules <<EOF 
#!/bin/bash 
modprobe -- ip_vs 
modprobe -- ip_vs_rr 
modprobe -- ip_vs_wrr 
modprobe -- ip_vs_sh 
modprobe -- nf_conntrack_ipv4 
EOF 
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4

  2.安装Docker软件

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

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

yum update -y && yum install -y docker-ce 

# 创建 /etc/docker 目录 
mkdir /etc/docker 

#配置 daemon.
cat > /etc/docker/daemon.json <<EOF 
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size""100m"
  }
}  
EOF 
mkdir -p /etc/systemd/system/docker.service.d 

#重启docker服务 
systemctl daemon-reload && systemctl restart docker && systemctl enable docker

  3.在主节点启动 Haproxy与 Keepalived容器

    导入脚本>运行>查看可用节点

  4.安装Kubeadm (主从配置)

cat <<EOF > /etc/yum.repos.d/kubernetes.repo 
[kubernetes] 
name=Kubernetes 
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64 
enabled=1 
gpgcheck=0 
repo_gpgcheck=0 
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg 
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg 
EOF 

yum -y install kubeadm-1.15.1 kubectl-1.15.1 kubelet-1.15.1 
systemctl enable kubelet.service 

  5.初始化主节点

kubeadm config print init-defaults > kubeadm-config.yaml 
kubeadm init --config=kubeadm-config.yaml --experimental-upload-certs | tee kubeadm-init.log

  6.加入主节点以及其余工作节点

    执行安装日志中的加入命令即可
 
  7.Etcd 集群状态查看
kubectl -n kube-system exec etcd-k8s-master01 -- etcdctl \
  --endpoints=https://192.168.92.10:2379 \ 
  --ca-file=/etc/kubernetes/pki/etcd/ca.crt \
  --cert-file=/etc/kubernetes/pki/etcd/server.crt \
  --key-file=/etc/kubernetes/pki/etcd/server.key cluster-health 
  
kubectl get endpoints kube-controller-manager --namespace=kube-system -o yaml 
kubectl get endpoints kube-scheduler --namespace=kube-system -o yaml 

  8.部署网络

kubectl apply -f kube-flannel.yml

 

 
posted @ 2020-05-02 10:18  SpringCore  阅读(396)  评论(0编辑  收藏  举报