一、前期准备
文章用到的组件下载地址连接:链接:https://pan.baidu.com/s/1ujyUcTE5MyMycczOx9FG_A
提取码:vm1s
1、生产环境可部署Kubernetes集群的两种方式
1)kubeadm:Kubeadm是一个K8s部署工具,提供kubeadm init和kubeadm join,用于快速部署Kubernetes集群。
官方地址:https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm/
2)二进制包:从github下载发行版的二进制包,手动部署每个组件,组成Kubernetes集群。
Kubeadm降低部署门槛,但屏蔽了很多细节,遇到问题很难排查。如果想更容易可控,推荐使用二进制包部署Kubernetes集群,虽然手动部署麻烦点,期间可以学习很多工作原理,也利于后期维护。
2、安装要求
1) 一台或多台机器,操作系统 CentOS7.x-86_x64
2) 硬件配置:2GB或更多RAM,2个CPU或更多CPU,硬盘30GB或更多
3) 集群中所有机器之间网络互通
4) 可以访问外网,需要拉取镜像,如果服务器不能上网,需要提前下载镜像并导入节点
5) 禁止swap分区
3、环境版本
操作系统 CentOS7.5_x64
Docker docker-20.10.5
Kubernetes 1.20.4
4、单Master服务器规划
角色 ip 组件
k8s-master 192.168.112.110 kube-apiserver,kube-controller-manager,kube-scheduler,etcd
k8s-node1 192.168.112.112 kubelet,kube-proxy,docker etcd
k8s-node2 192.168.112.113 kubelet,kube-proxy,docker etcd
5、多Master服务器规划
角色 ip 组件
k8s-master1 192.168.112.110 kube-apiserver,kube-controller-manager,kube-scheduler,etcd, nginx,flannel
k8s-master2 192.168.112.111 kube-apiserver,kube-controller-manager,kube-scheduler,etcd, nginx,flannel
k8s-node1 192.168.112.112 kubelet,kube-proxy,docker etcd、flannel
k8s-node2 192.168.112.113 kubelet,kube-proxy,docker etcd、flannel
k8s-node3 192.168.112.114 kubelet,kube-proxy,docker etcd、flannel
6、操作系统初始化配置
# 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
# 关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久
setenforce 0 # 临时
# 关闭swap
swapoff -a # 临时
sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久
# 根据规划设置主机名
hostnamectl set-hostname <hostname>
# 在master添加hosts
cat >> /etc/hosts << EOF
192.168.1.71 k8s-master
192.168.1.72 k8s-node1
192.168.1.73 k8s-node2
EOF
# 将桥接的IPv4流量传递到iptables的链
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system # 生效
# 时间同步
yum -y install chrony
vim /etc/chrony.conf
server ntp6.aliyun.com iburst
allow 192.168.112.0/24
local stratum 10
systemctl restart chronyd.service #启动服务
查看时间同步状态:
chronyc -a makestep
chronyc sourcestats
chronyc sources -v
二、部署etcd数据库
Etcd 是一个分布式键值存储系统,Kubernetes使用Etcd进行数据存储,所以先准备一个Etcd数据库,为解决Etcd单点故障,应采用集群方式部署,这里使用3台组建集群,可容忍1台机器故障,当然,你也可以使用5台组建集群,可容忍2台机器故障。
节点名称 IP地址
etcd-1 192.168.112.110
etcd-2 192.168.112.111
etcd-3 192.168.112.112
注:为了节省机器,这里与k8s节点机器复用。也可以独立于k8s集群之外部署,只要apiserver能连接到就行。
1、准备cfssl证书生成工具
cfssl是一个开源的证书管理工具,使用json文件生成证书,相比openssl更方便使用。
找任意一台服务器操作,这里用Master节点。
wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64
wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64
chmod +x cfssl_linux-amd64 cfssljson_linux-amd64 cfssl-certinfo_linux-amd64
mv cfssl_linux-amd64 /usr/local/bin/cfssl
mv cfssljson_linux-amd64 /usr/local/bin/cfssljson
mv cfssl-certinfo_linux-amd64 /usr/bin/cfssl-certinfo
2、生成Etcd证书
mkdir -p ~/TLS/{etcd,k8s} 创建工作目录
cd ~/TLS/etcd 进入工作目录
cat > ca-config.json << EOF
{
"signing": {
"default": {
"expiry": "175200h"
},
"profiles": {
"www": {
"expiry": "175200h",
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
EOF
cat > ca-csr.json << EOF
{
"CN": "etcd CA",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "Beijing",
"ST": "Beijing"
}
]
}
EOF
cfssl gencert -initca ca-csr.json | cfssljson -bare ca - 生成证书
ls *pem
ca-key.pem ca.pem
3、创建证书申请文件,使用自签CA签发Etcd HTTPS证书
cat > server-csr.json << EOF
{
"CN": "etcd",
"hosts": [
"192.168.112.110",
"192.168.112.111",
"192.168.112.112"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing"
}
]
}
EOF
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | cfssljson -bare server 生成证书
ls server*pem
server-key.pem server.pem
注:上述文件hosts字段中IP为所有etcd节点的集群内部通信IP,一个都不能少!为了方便后期扩容可以多写几个预留的IP。
4、下载etcd二进制文件
下载地址:https://github.com/etcd-io/etcd/releases/download/v3.4.9/etcd-v3.4.9-linux-amd64.tar.gz
5、部署etcd
创建工作目录并解压二进制包
mkdir /opt/etcd/{bin,cfg,ssl} -p
tar zxvf etcd-v3.4.9-linux-amd64.tar.gz
mv etcd-v3.4.9-linux-amd64/{etcd,etcdctl} /opt/etcd/bin/
6、创建etcd配置文件
cat > /opt/etcd/cfg/etcd.conf << EOF
#[Member]
ETCD_NAME="etcd-1"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.112.110:2380"
ETCD_LISTEN_CLIENT_URLS="https://192.168.112.110:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.112.110:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.112.110:2379"
ETCD_INITIAL_CLUSTER="etcd-1=https://192.168.112.110:2380,etcd-2=https://192.168.112.111:2380,etcd-3=https://192.168.112.112:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_HEARTBEAT_INTERVAL=100
ETCD_ELECTION_TIMEOUT=500
ETCD_SNAPSHOT_COUNT=5000
EOF
ETCD_NAME:节点名称,集群中唯一
ETCD_DATA_DIR:数据目录
ETCD_LISTEN_PEER_URLS:集群通信监听地址
ETCD_LISTEN_CLIENT_URLS:客户端访问监听地址
ETCD_INITIAL_ADVERTISE_PEER_URLS:集群通告地址
ETCD_ADVERTISE_CLIENT_URLS:客户端通告地址
ETCD_INITIAL_CLUSTER:集群节点地址
ETCD_INITIAL_CLUSTER_TOKEN:集群Token
ETCD_INITIAL_CLUSTER_STATE:加入集群的当前状态,new是新集群,existing表示加入已有集群
ETCD_HEARTBEAT_INTERVAL:参数叫心跳间隔
ETCD_ELECTION_TIMEOUT:选举超时
ETCD_SNAPSHOT_COUNT:快照时间
7、使用systemd管理etcd
cat > /usr/lib/systemd/system/etcd.service << EOF
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
EnvironmentFile=/opt/etcd/cfg/etcd.conf
ExecStart=/opt/etcd/bin/etcd \
--cert-file=/opt/etcd/ssl/server.pem \
--key-file=/opt/etcd/ssl/server-key.pem \
--peer-cert-file=/opt/etcd/ssl/server.pem \
--peer-key-file=/opt/etcd/ssl/server-key.pem \
--trusted-ca-file=/opt/etcd/ssl/ca.pem \
--peer-trusted-ca-file=/opt/etcd/ssl/ca.pem \
--logger=zap
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
8、拷贝刚才生成的证书,把刚才生成的证书拷贝到配置文件中的路径
cp ~/TLS/etcd/ca*pem ~/TLS/etcd/server*pem /opt/etcd/ssl/
9、拷贝所有etcd文件至etcd-2,etcd-3节点
scp -r /opt/etcd/ root@192.168.112.111:/opt/
scp /usr/lib/systemd/system/etcd.service root@192.168.112.111:/usr/lib/systemd/system/
scp -r /opt/etcd/ root@192.168.112.112:/opt/
scp /usr/lib/systemd/system/etcd.service root@192.168.112.112:/usr/lib/systemd/system/
注:在节点2和节点3分别修改etcd.conf配置文件中的节点名称和当前服务器IP:
vi /opt/etcd/cfg/etcd.conf
#[Member]
ETCD_NAME="etcd-1" #修改此处,节点2改为etcd-2,节点3改为etcd-3
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.112.111:2380" #修改此处为当前服务器IP
ETCD_LISTEN_CLIENT_URLS="https://192.168.112.111:2379" #修改此处为当前服务器IP
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.112.111:2380" #修改此处为当前服务器IP
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.112.111:2379" #修改此处为当前服务器IP
ETCD_INITIAL_CLUSTER="etcd-1=https://192.168.112.110:2380,etcd-2=https://192.168.112.111:2380,etcd-3=https://192.168.112.112:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
10、所有服务器启动etcd
systemctl daemon-reload
systemctl start etcd
systemctl enable etcd
11、查看集群状态
ETCDCTL_API=3 /opt/etcd/bin/etcdctl --cacert=/opt/etcd/ssl/ca.pem --cert=/opt/etcd/ssl/server.pem --key=/opt/etcd/ssl/server-key.pem --endpoints="https://192.168.112.110:2379,https://192.168.112.111:2379,https://192.168.112.112:2379" endpoint health
https://192.168.112.110:2379 is healthy: successfully committed proposal: took = 8.154404ms
https://192.168.112.111:2379 is healthy: successfully committed proposal: took = 9.044117ms
https://192.168.112.112:2379 is healthy: successfully committed proposal: took = 10.000825ms
如果输出上面信息,就说明集群部署成功。如果有问题第一步先看日志:/var/log/message 或 journalctl -u etcd
三、安装部署docker
1、下载docker
下载地址:https://download.docker.com/linux/static/stable/x86_64/docker-20.10.5.tgz
2、解压二进制包,拷贝至其他节点
tar zxvf docker-20.10.5.tgz
scp docker/* root@192.168.112.111:/usr/local/bin
scp docker/* root@192.168.112.112:/usr/local/bin
scp docker/* root@192.168.112.113:/usr/local/bin
scp docker/* root@192.168.112.114:/usr/local/bin
mv docker/* /usr/bin
3、systemd管理docker(docker节点)
cat > /usr/lib/systemd/system/docker.service << EOF
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
ExecStart=/usr/local/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
EOF
4、创建配置文件(docker节点)
mkdir /etc/docker
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF
5、启动并设置开机启动(docker节点)
systemctl daemon-reload
systemctl start docker
systemctl enable docker
暂无评论内容