Kubernetes 高可用集群(一)

本文档详细介绍了如何在Ubuntu 18.04.3 LTS上部署Kubernetes v1.18.0的高可用集群,包括Docker的配置、kubeadm、kubelet和kubectl的安装、主机名和hosts的配置、HAProxy与Keepalived的设置,以及节点的加入和Kubernetes Dashboard的安装与配置。
摘要由CSDN通过智能技术生成

Ubuntu 18.04.3 LTS 部署k8s v1.18.0 高可用集群(主备模式)

版本:
Ubuntu 18.04.3 LTS
Docker 19.03.5
Kubernetes v1.18.0
Calico v3.15 ----- 对应 Kubernetes 1.16/1.17/1.18 贴子忘在看到的
kubernetes-dashboard v2.0.0
HAProxy-k8s latest
Keepalived latest
先启一台虚拟机,以作备用,取名 base

#关闭交换空间
swapoff -a

#避免开机启动交换空间
# 注释 swap 开头的行
vim /etc/fstab

#关闭防火墙
ufw disable

#配置 DNS
# 取消 DNS 行注释,并增加 DNS 配置如:114.114.114.114,修改后重启下计算机
vim /etc/systemd/resolved.conf

reboot

安装 Docker
# 更新软件源
sudo apt-get update
# 安装所需依赖
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# 安装 GPG 证书
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# 新增软件源信息
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# 再次更新软件源
sudo apt-get -y update
# 安装 Docker CE 版
sudo apt-get -y install docker-ce 

配置 Docker 加速器
在 /etc/docker/daemon.json 中写入如下内容(如果文件不存在,请新建该文件)

{
   
  "registry-mirrors": [
    "https://6kx4zyno.mirror.aliyuncs.com",
    "https://docker.mirrors.ustc.edu.cn",
    "https://registry.docker-cn.com"
  ],
  "exec-opts": ["native.cgroupdriver=systemd"]
}

安装 kubeadm,kubelet,kubectl

# 安装系统工具
apt-get update && apt-get install -y apt-transport-https

# 安装 GPG 证书
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -

# 写入软件源;注意:我们用系统代号为 bionic,但目前阿里云不支持,所以沿用 16.04 的 xenial  命令一句一句执行
cat << EOF >/etc/apt/sources.list.d/kubernetes.list
> deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
> EOF

#安装 这里指定了1.18.0
apt-get update && apt-get install -y kubelet=1.18.0-00 kubeadm=1.18.0-00 kubectl=1.18.0-00

同步时间

#设置时区
dpkg-reconfigure tzdata
#选择Asia
#选择shanghai

时间同步

# 安装 ntpdate
apt-get install ntpdate
# 设置系统时间与网络时间同步(cn.pool.ntp.org 位于中国的公共 NTP 服务器)
ntpdate cn.pool.ntp.org
# 将系统时间写入硬件时间
hwclock --systohc
date
# 输出如下(自行对照与系统时间是否一致)
|Sun Aug 30 01:03:22 CST 2020

配置 IPVS

# 安装系统工具
apt-get install -y ipset ipvsadm

# 配置并加载 IPVS 模块
mkdir -p /etc/sysconfig/modules/
vim /etc/sysconfig/modules/ipvs.modules

# 输入如下内容
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4

# 执行脚本,注意:如果重启则需要重新运行该脚本
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4

# 执行脚本输出如下
ip_vs_sh               16384  0
ip_vs_wrr              16384  0
ip_vs_rr               16384  0
ip_vs                 147456  6 ip_vs_rr,ip_vs_sh,ip_vs_wrr
nf_conntrack_ipv4      16384  3
nf_defrag_ipv4         16384  1 nf_conntrack_ipv4
nf_conntrack          131072  8 xt_conntrack,nf_nat_masquerade_ipv4,nf_conntrack_ipv4,nf_nat,ipt_MASQUERADE,nf_nat_ipv4,nf_conntrack_netlink,ip_vs
libcrc32c              16384  4 nf_conntrack,nf_nat,raid456,ip_vs

配置内核参数

# 配置参数
vim /etc/sysctl.d/k8s.conf

# 输入如下内容
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
vm.swappiness=0

# 应用参数
sysctl --system

# 应用参数输出如下(找到 Applying /etc/sysctl.d/k8s.conf 开头的日志)
* Applying /etc/sysctl.d/10-console-messages.conf ...
kernel.printk = 4 4 1 7
* Applying /etc/sysctl.d/10-ipv6-privacy.conf ...
* Applying /etc/sysctl.d/10-kernel-hardening.conf ...
kernel.kptr_restrict = 1
* Applying /etc/sysctl.d/10-link-restrictions.conf ...
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
* Applying /etc/sysctl.d/10-lxd-inotify.conf ...
fs.inotify.max_user_instances = 1024
* Applying /etc/sysctl.d/10-magic-sysrq.conf ...
kernel.sysrq = 176
* Applying /etc/sysctl.d/10-network-security.conf ...
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.tcp_syncookies = 1
* Applying /etc/sysctl.d/10-ptrace.conf ...
kernel.yama.ptrace_scope = 1
* Applying /etc/sysctl.d/10-zeropage.conf ...
vm.mmap_min_addr = 65536
* Applying /usr/lib/sysctl.d/50-default.conf ...
net.ipv4.conf.all.promote_secondaries = 1
net.core.default_qdisc = fq_codel
* Applying /etc/sysctl.d/99-sysctl.conf ...
* Applying /etc/sysctl.d/k8s.conf ...
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
vm.swappiness = 0
* Applying /etc/sysctl.conf ...

修改 cloud.cfg

vim /etc/cloud/cloud.cfg

# 该配置默认为 false,修改为 true 
preserve_hostname: true

-------------------------------------以下在base机的克隆体下安装------------------------------------------

k8s-ms1

修改50-cloud-init.yaml 配置文件

vim /etc/netplan/50-cloud-init.yaml
network:
    ethernets:
        ens33:
          addresses: [192.168.186.160/24]
          gateway4: 192.168.186.2 ## ip route show 查看第一行
          nameservers:
            addresses: [192.168.186.2]
    version: 2
    

配置主机名

hostnamectl set-hostname k8s-m1

#查看
hostnamectl

配置 hosts

cat >> /etc/hosts << EOF
192.168.186.160 k8s-m1
192.168.186.161 k8s-m2
192.168.186.162 k8s-m3
192.168.186.170 k8s-node1
192.168.186.171 k8s-node2
192.168.186.172 k8s-node3
EOF
netplan apply
reboot

k8s-ms2

修改50-cloud-init.yaml 配置文件

vim /etc/netplan/50-cloud-init.yaml
network:
    ethernets:
        ens33:
          addresses: [192.168.186.161/24]
          gateway4: 192.168.186.2 ## ip route show 查看第一行
          nameservers:
            addresses: [192.168.186.2]
    version: 2
    

配置主机名

hostnamectl set-hostname k8s-m2

配置 hosts

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值