环境介绍与基础配置

简介

本系列文档将介绍如何使用二进制部署 Kubernetes v1.15.6 集群的所有部署,而不是使用自动化部署(kubeadm)集群。在部署过程中,将详细列出各个组件启动参数,以及相关配置说明。

组件版本

组件说明

kube-apiserver

  • 使用节点本地Nginx 4层透明代理实现高可用 (也可以使用haproxy,只是起到代理apiserver的作用)
  • 关闭非安全端口8080和匿名访问
  • 使用安全端口6443接受https请求
  • 严格的认知和授权策略 (x509、token、rbac)
  • 开启bootstrap token认证,支持kubelet TLS bootstrapping;
  • 使用https访问kubelet、etcd

kube-controller-manager

  • 3节点高可用 (在k8s中,有些组件需要选举,所以使用奇数为集群高可用方案)
  • 关闭非安全端口,使用10252接受https请求
  • 使用kubeconfig访问apiserver的安全扣
  • 使用approve kubelet证书签名请求(CSR),证书过期后自动轮转
  • 各controller使用自己的ServiceAccount访问apiserver

kube-scheduler

  • 3节点高可用;
  • 使用kubeconfig访问apiserver安全端口

kubelet

  • 使用kubeadm动态创建bootstrap token
  • 使用TLS bootstrap机制自动生成client和server证书,过期后自动轮转
  • 在kubeletConfiguration类型的JSON文件配置主要参数
  • 关闭只读端口,在安全端口10250接受https请求,对请求进行认真和授权,拒绝匿名访问和非授权访问
  • 使用kubeconfig访问apiserver的安全端口

kube-proxy

  • 使用kubeconfig访问apiserver的安全端口
  • 在KubeProxyConfiguration类型JSON文件配置为主要参数
  • 使用ipvs代理模式

集群插件

  • DNS 使用功能、性能更好的coredns
  • 网络 使用Flanneld 作为集群网络插件

初始化环境

所有机器均使用的是 CentOS-7.7 mini 的最小化安装。

集群机器

主机名 角色 IP 系统版本 内核版本
node01.k8s.com master 10.0.20.11 CentOS 7.7 5.4.1-1.el7.elrepo.x86_64
node02.k8s.com master 10.0.20.12 CentOS 7.7 5.4.1-1.el7.elrepo.x86_64
node03.k8s.com master 10.0.20.13 CentOS 7.7 5.4.1-1.el7.elrepo.x86_64
node04.k8s.com node 10.0.20.1 4 CentOS 7.7 5.4.1-1.el7.elrepo.x86_64

配置主机名

所有机器设置永久主机名

hostnamectl set-hostname node01.k8s.com

DNS 配置

本文设计使用到内网 DNS ,或者手动写 hosts 都可以。

解析操作使用以下方法二选一

方法一、配置 hosts 文件

把一下内容写入到所有机器中

10.0.20.11 node01 node01.k8s.com
10.0.20.12 node02 node02.k8s.com
10.0.20.13 node03 node03.k8s.com
10.0.20.14 node04 node04.k8s.com

方法二、配置 DNS 服务

参考 CentOS 7 安装 bind 服务 实现内网DNS

按照上面的操作配置好DNS后,修改私有机器的DNS地址,并配置查找域

[root@node01 ~]# cat /etc/resolv.conf
nameserver 10.0.20.8    # 这是我的bind服务器
search k8s.com   # 这里配置域名,方便使用主机名直接解析

按照上面配置后效果如下:

[root@node01 ~]# ping -c 1 node01
PING node01.k8s.com (10.0.20.11) 56(84) bytes of data.
64 bytes from node01.k8s.com (10.0.20.11): icmp_seq=1 ttl=64 time=0.042 ms

[root@node01 ~]# ping -c 1 node02
PING node02.k8s.com (10.0.20.12) 56(84) bytes of data.
64 bytes from 10.0.20.12 (10.0.20.12): icmp_seq=1 ttl=64 time=0.204 ms

当直接 ping node01 类似于这种时,则会自动填充node01.k8s.com

更换yum源

更换为阿里云的 Base 源和 epel 源

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

增加 docker-ce 阿里云源

[root@node01 yum.repos.d]# cat docker-ce.repo
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-stable-debuginfo]
name=Docker CE Stable - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/stable
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-stable-source]
name=Docker CE Stable - Sources
beurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/stable
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-edge]
name=Docker CE Edge - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/edge
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-edge-debuginfo]
name=Docker CE Edge - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/edge
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-edge-source]
name=Docker CE Edge - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/edge
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-test]
name=Docker CE Test - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-test-debuginfo]
name=Docker CE Test - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-test-source]
name=Docker CE Test - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/test
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-nightly]
name=Docker CE Nightly - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-nightly-debuginfo]
name=Docker CE Nightly - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-nightly-source]
name=Docker CE Nightly - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/nightly
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

安装依赖包

因为是最小化系统安装,需要安装常规的软件包

上面配置好了yum源,下面直接安装即可

yum install -y conntrack ipvsadm ipset jq iptables curl sysstat libseccomp wget vim lrzsz tree telnet wget lsof dos2unix traceroute unzip zip bind-utils

设置免秘钥

为了方便之后的操作,在node01上配置连接其他机器的免秘钥登陆操作

安装expect

yum install -y expect

创建秘钥

ssh-keygen -t rsa -P "" -f /root/.ssh/id_rsa

分发公钥

这里我使用的root密码是 123456,如有不同,请自行更换

for i in node01 node02 node03 node04;do
expect -c "
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@$i
        expect {
                \"*yes/no*\" {send \"yes\r\"; exp_continue}
                \"*password*\" {send \"123456\r\"; exp_continue}
                \"*Password*\" {send \"123456\r\";}
        } "
done 

更新PATH变量

本次的k8s软件包的目录全部存放在/opt下

[root@node01 ~]# echo 'PATH=/opt/k8s/bin:$PATH' >>/etc/profile
[root@node01 ~]# source  /etc/profile
[root@node01 ~]# env|grep PATH
PATH=/opt/k8s/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

配置 chrony 时间同步

首先先设置时区

将硬件时钟调整为与本地时钟一致, 0 为设置为 UTC 时间

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 或者
timedatectl set-timezone Asia/Shanghai

安装

yum install chrony -y

修改配置如下

[root@node01 ~]# egrep -v '^#|^$' /etc/chrony.conf 
server time4.aliyun.com iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
logdir /var/log/chrony

启动并设置开机自启

[root@node01 ~]# systemctl start chronyd
[root@node01 ~]# systemctl enable chronyd

查看时间同步状态

[root@node01 ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 203.107.6.88                  2   8   377   115   -268us[ -318us] +/-   15ms

当看到IP地址前面有个*号的时候,表示同步成功

关闭防火墙 selinx 以及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)

关闭无用的服务

systemctl disable NetworkManager
systemctl disable postfix

升级内核

请参考文章 Centos 7.x 内核升级

加载内支持ipvs

使用systemd-modules-load加载内核模块

cat > /etc/rc.local  << EOF
modprobe ip_vs_rr
modprobe br_netfilter
EOF
cat > /etc/modules-load.d/ipvs.conf << EOF
 ip_vs_rr
 br_netfilter
EOF
systemctl enable --now systemd-modules-load.service

优化内核参数

cat > kubernetes.conf <<EOF
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
vm.swappiness=0 # 禁止使用 swap 空间,只有当系统 OOM 时才允许使用它
vm.overcommit_memory=1 # 不检查物理内存是否够用
vm.panic_on_oom=0 # 开启 OOM
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

关闭IPV6,防止触发Docker BUG

创建相关目录

mkdir -p  /opt/k8s/{bin,work} /etc/{kubernetes,etcd}/cert

#在所有节点上执行,因为flanneld是在所有节点运行的

设置分发脚本参数

后续所有的使用环境变量都定义在environment.sh中,需要根据个人机器及网络环境修改。并且需要拷贝到所有节点的/opt/k8s/bin目录下

#!/usr/bin/bash
# 生成 EncryptionConfig 所需的加密 key
export ENCRYPTION_KEY=$(head -c 32 /dev/urandom | base64)

# 集群各机器 IP 数组
export NODE_IPS=( 10.0.20.11 10.0.20.12 10.0.20.13 10.0.20.14 )

# 集群各 IP 对应的主机名数组
export NODE_NAMES=( node01 node02 node03 node04 )

# 集群MASTER机器 IP 数组
export MASTER_IPS=( 10.0.20.11 10.0.20.12 10.0.20.13 )

# 集群所有的master Ip对应的主机
export MASTER_NAMES=( node01 node02 node03 )

# etcd 集群服务地址列表
export ETCD_ENDPOINTS="https://etcd01.k8s.com:2379,https://etcd02.k8s.com:2379,https://etcd03.k8s.com:2379"

# etcd 集群间通信的 IP 和端口
export ETCD_NODES="etcd01=https://etcd01.k8s.com:2380,etcd02=https://etcd02.k8s.com:2380,etcd03=https://etcd03.k8s.com:2380"

# etcd 集群各 主机名 数组
export NODE_NAMES=( etcd01 etcd02 etcd03 )

# etcd 集群所有node ip
export ETCD_IPS=( 10.0.20.11 10.0.20.12 10.0.20.13 )

# kube-apiserver 的反向代理(kube-nginx)地址端口
export KUBE_APISERVER="https://vip.k8s.com:8443"

# 节点间互联网络接口名称
export IFACE="eth0"

# etcd 数据目录
export ETCD_DATA_DIR="/data/k8s/etcd/data"

# etcd WAL 目录,建议是 SSD 磁盘分区,或者和 ETCD_DATA_DIR 不同的磁盘分区
export ETCD_WAL_DIR="/data/k8s/etcd/wal"

# k8s 各组件数据目录
export K8S_DIR="/data/k8s/k8s"

# 服务网段,部署前路由不可达,部署后集群内路由可达(kube-proxy 保证)
SERVICE_CIDR="10.254.0.0/16"

# Pod 网段,建议 /16 段地址,部署前路由不可达,部署后集群内路由可达(flanneld 保证)
CLUSTER_CIDR="172.30.0.0/16"

# 服务端口范围 (NodePort Range)
export NODE_PORT_RANGE="1024-32767"

# flanneld 网络配置前缀
export FLANNEL_ETCD_PREFIX="/kubernetes/network"

# kubernetes 服务 IP (一般是 SERVICE_CIDR 中第一个IP)
export CLUSTER_KUBERNETES_SVC_IP="10.254.0.1"

# 集群 DNS 服务 IP (从 SERVICE_CIDR 中预分配)
export CLUSTER_DNS_SVC_IP="10.254.0.2"

# 集群 DNS 域名(末尾不带点号)
export CLUSTER_DNS_DOMAIN="cluster.local"

# 将二进制目录 /opt/k8s/bin 加到 PATH 中
export PATH=/opt/k8s/bin:$PATH

建议

如果使用的是 虚拟机,那么此时应该关机后,创建一个快照

参考链接

posted @ 2019-12-05 11:20  司家勇  阅读(1560)  评论(0编辑  收藏  举报