本文介绍基于二进制方式,进行单Master、多Master的K8S集群搭建全过程。
经本人手动测试无误后整理成下文,建议先查看目录详情了解整体流程,后进行实操。
目录
1 环境要求、节点规划、配置
1.1 环境要求
1.2 节点规划
1.3 操作系统初始化配置
2 ETCD集群部署
2.1 准备cfssl证书生成工具
2.2 生成ETCD证书
2.3 下载ETCD的二进制文件
2.4 部署ETCD集群
3 安装Docker
4 部署 Master-Node
4.1 生成kube-apiserver证书
4.2 下载并解压二进制文件
4.3 部署 kube-apiserver
4.4 部署 kube-controller-manager
4.5 部署 kube-scheduler
5 部署 Work-Node
5.1 创建工作目录并拷贝二进制文件
5.2 部署kubelet
5.3 批准 kubelet 证书申请并加入集群
5.4 部署 kube-proxy
5.5 部署网络组件
5.6 授权 apiserver 访问 kubelet
5.7 新增加 Worker-Node
6 部署 DashBoard 和 CoreDNS
6.1 部署 DashBoard
6.2 部署 CoreDNS
7 扩容多 Master(高可用架构)
7.1 部署 Master2 Node
7.2 部署 Nginx+Keepalived 高可用负载均衡器
由于我只有一台笔记本电脑(WIN10,16G内存),为了在VMWare中少开启几台虚拟机,就把ETCD集群分别放到仅有的3个节点中,单Master节点的K8S集群部署架构及组件安装如下图:
因此,本次环境搭建,一共用到3台虚拟机(单Master节点、双Node节点,1+2=3台)
单Master的K8S集群节点规划如下 :
节点 | ip | 需要安装的组件 |
k8s-master1 | 192.168.200.200 | kube-apiserver、kube-controller-manager、kube-scheduler、etcd |
k8s-node1 | 192.168.200.201 | kubelet、kube-proxy、docker、etcd |
k8s-node2 | 192.168.200.202 | kubelet、kube-proxy、docker、etcd |
如下的8个步骤,需要在三台主机上分别执行一遍:
# 1、关闭防火墙
systemctl stop firewalld && systemctl disable firewalld
# 2、关闭selinux(我选择永久关闭,需要重启机器)
永久关闭:sed -i 's/enforcing/disabled/' /etc/selinux/config
临时关闭:setenforce 0
# 3、关闭swap(我选择永久关闭)
永久关闭:sed -ri 's/.*swap.*/#&/' /etc/fstab
临时关闭:swapoff -a
# 4、根据规划设置主机名(更改后命令行输入:bash 直接生效)
hostnamectl set-hostname k8s-master1
hostnamectl set-hostname k8s-node1
hostnamectl set-hostname k8s-node2
# 5、在master节点中添加集群中各个节点的主机名与ip的对应关系
#只有这个部分是只在Master中执行,其他的指令需要在3台机器上都执行一遍
cat >> /etc/hosts << EOF
192.168.200.200 k8s-master
192.168.200.201 k8s-node1
192.168.200.202 k8s-node2
EOF
# 6、将桥接的IPv4流量传递到iptables的链
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
# 7、配置生效
sysctl --system
# 8、时间同步
yum install ntpdate -y && ntpdate time.windows.com
Etcd 是一个分布式键值存储系统,Kubernetes使用Etcd进行数据存储,所以先准备一个Etcd数据库,为解决Etcd单点故障,应采用集群方式部署,这里使用3台组建集群,可容忍1台机器故障。为了节省机器,这里把3个ETCD实例分别部署在一个Matser节点和两个Node节点上。
ETCD实例 | IP |
etcd-1 | 192.168.200.200 |
etcd-2 | 192.168.200.201 |
etcd-3 | 192.168.200.202 |
#1、安装cfssl证书生成工具(如果下载不了,可尝试换成http协议)
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、生成自签证书的根证书:创建文件夹
mkdir -p ~/TLS/{etcd,k8s}
cd TLS/etcd
#3、生成自签证书的根证书:文件夹中写入配置文件2个
#配置文件1
cat > ca-config.json << EOF
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"www": {
"expiry": "87600h",
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
EOF
#配置文件2
cat > ca-csr.json << EOF
{
"CN": "etcd CA",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "Beijing",
"ST": "Beijing"
}
]
}
EOF
#4、执行如下指令生成证书
cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
接下来,使用自签的根证书签发ETCD HTTPS证书:
#5、创建证书申请文件
cat > server-csr.json << EOF
{
"CN": "etcd",
"hosts": [
"192.168.200.200",
"192.168.200.201",
"192.168.200.202"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing"
}
]
}
EOF
注意:上面配置文件中hosts字段中的IP为所有etcd节点的集群内部通信IP,为了方便后期扩容可以多写几个预留IP(我没有预留)。
#6、生成证书
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | cfssljson -bare server
到目前为止,已经基于ETCD的根证书,生成了两个域名证书如下,用于ETCD集群使用HTTPS进行通信 :
浏览器输入地址直接下载,下载之后把压缩包拖动到root目录下。
https://github.com/etcd-io/etcd/releases/download/v3.4.9/etcd-v3.4.9-linux-amd64.tar.gz
以下操作只在节点1(Master)上进行一遍,至于节点2、3,直接从节点1中把结果拷贝过去即可。
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/
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.200.200:2380"
ETCD_LISTEN_CLIENT_URLS="https://192.168.200.200:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.200.200:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.200.200:2379"
ETCD_INITIAL_CLUSTER="etcd-1=https://192.168.200.200:2380,etcd-2=https://192.168.200.201:2380,etcd-3=https://192.168.200.202:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
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表示加入已有集群
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
cp ~/TLS/etcd/ca*pem ~/TLS/etcd/server*pem /opt/etcd/ssl/
systemctl daemon-reload
systemctl enable etcd
systemctl start etcd
注意:systemctl start etcd 指令执行之后会“卡住”因为其他的ETCD节点还没部署呢。输入以下的指令可以查看日志:
#把和etcd有关的日志保存到a文件中,再从a中检索和ip相关的内容,下图截取了其中的一部分
#注意:本步骤不是必须要执行的
journalctl -u etcd > a
cat a grep|192.168.200
#分别拷贝etcd的工作目录、管理服务的配置文件到另外两台主机
scp -r /opt/etcd/ [email protected]:/opt/
scp /usr/lib/systemd/system/etcd.service [email protected]:/usr/lib/systemd/system/
scp -r /opt/etcd/ [email protected]:/opt/
scp /usr/lib/systemd/system/etcd.service [email protected]:/usr/lib/systemd/system/
注意:需要修改ETCD配置文件参数(共5处修改点),分别修改另外2个ETCD的参数
vi /opt/etcd/cfg/etcd.conf
#[Member]
ETCD_NAME="etcd-1" # 修改1,节点2改为etcd-2,节点3改为etcd-3
ETCD_LISTEN_PEER_URLS="https://192.168.200.201:2380" # 修改2 当前服务器IP
ETCD_LISTEN_CLIENT_URLS="https://192.168.200.201:2379" # 修改3 当前服务器IP
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.200.201:2380" # 修改4 当前服务器IP
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.200.201:2379" # 修改5 当前服务器IP
启动并设置开机启动(同上):
systemctl daemon-reload
systemctl enable etcd
systemctl start etcd
在任意一个ETCD节点上输入如下指令,可以查看集群状态情况true表示健康(保证IP正确)
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.200.200:2379,https://192.168.200.201:2379,https://192.168.200.202:2379" endpoint health --write-out=table
其实,我这三台虚拟机上本来就安装好了Docker,如果没有安装Docker,可以参考该教程,当然,如果虚拟机本来就安装了Docker,我也会把修改点指出来。
如果虚拟机上本来就没安装Docker,安装步骤分为如下5步:
https://download.docker.com/linux/static/stable/x86_64/docker-19.03.9.tgz
tar zxvf docker-19.03.9.tgz
mv docker/* /usr/bin
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/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
mkdir /etc/docker
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl start docker
systemctl enable docker
如果虚拟机上本来就安装了Docker,只需要把修改2个配置文件即可:
执行上面的step3。
执行上面的step4(去掉第一行创建文件夹的操作)。
执行上面的step5。最后,命令行输入:docker info 查看docker是否成功启动,上面所有的操作在3台机器上分别执行。
到目前为止,前期准备工作已经完成了(环境准备、部署ETCD集群、安装Docker),接下来基于K8S中的各种组件的二进制文件,进行组件的配置与安装。不过,在进行配置安装操作之前先了解一下K8S的证书机制:
cd TLS/k8s
cat > ca-config.json << EOF
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"kubernetes": {
"expiry": "87600h",
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
EOF
cat > ca-csr.json << EOF
{
"CN": "kubernetes",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "Beijing",
"ST": "Beijing",
"O": "k8s",
"OU": "System"
}
]
}
EOF
cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
注:上述文件hosts字段中IP为所有Master节点ip、负载均衡器LB的ip、负载均衡器的VIP,一个都不能少!为了方便后期扩容可以多写几个预留的IP(203是第二个Mstser、88是VIP)。
cat > server-csr.json << EOF
{
"CN": "kubernetes",
"hosts": [
"10.0.0.1",
"127.0.0.1",
"192.168.200.200",
"192.168.200.201",
"192.168.200.202",
"192.168.200.203",
"192.168.200.88",
"kubernetes",
"kubernetes.default",
"kubernetes.default.svc",
"kubernetes.default.svc.cluster",
"kubernetes.default.svc.cluster.local"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing",
"O": "k8s",
"OU": "System"
}
]
}
EOF
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare server
下载
https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG/CHANGELOG-1.18.md#v1183
下载如下这个二进制压缩包即可。
解压
mkdir -p /opt/kubernetes/{bin,cfg,ssl,logs}
tar zxvf kubernetes-server-linux-amd64.tar.gz
cd kubernetes/server/bin
cp kube-apiserver kube-scheduler kube-controller-manager /opt/kubernetes/bin
cp kubectl /usr/bin/
etcd-servers:分别填写3台机器的ip,bind-address、advertise-address填写Master节点ip
cat > /opt/kubernetes/cfg/kube-apiserver.conf << EOF
KUBE_APISERVER_OPTS="--logtostderr=false \\
--v=2 \\
--log-dir=/opt/kubernetes/logs \\
--etcd-servers=https://192.168.200.200:2379,https://192.168.200.201:2379,https://192.168.200.202:2379 \\
--bind-address=192.168.200.200 \\
--secure-port=6443 \\
--advertise-address=192.168.200.200 \\
--allow-privileged=true \\
--service-cluster-ip-range=10.0.0.0/24 \\
--enable-admission-plugins=NamespaceLifecycle,LimitRanger,ServiceAccount,ResourceQuota,NodeRestriction \\
--authorization-mode=RBAC,Node \\
--enable-bootstrap-token-auth=true \\
--token-auth-file=/opt/kubernetes/cfg/token.csv \\
--service-node-port-range=30000-32767 \\
--kubelet-client-certificate=/opt/kubernetes/ssl/server.pem \\
--kubelet-client-key=/opt/kubernetes/ssl/server-key.pem \\
--tls-cert-file=/opt/kubernetes/ssl/server.pem \\
--tls-private-key-file=/opt/kubernetes/ssl/server-key.pem \\
--client-ca-file=/opt/kubernetes/ssl/ca.pem \\
--service-account-key-file=/opt/kubernetes/ssl/ca-key.pem \\
--etcd-cafile=/opt/etcd/ssl/ca.pem \\
--etcd-certfile=/opt/etcd/ssl/server.pem \\
--etcd-keyfile=/opt/etcd/ssl/server-key.pem \\
--audit-log-maxage=30 \\
--audit-log-maxbackup=3 \\
--audit-log-maxsize=100 \\
--audit-log-path=/opt/kubernetes/logs/k8s-audit.log"
EOF
配置文件中参数解释如下:
logtostderr:启用日志
v:日志等级
log-dir:日志目录
etcd-servers:etcd集群地址
bind-address:监听地址
secure-port:https安全端口
advertise-address:集群通告地址
allow-privileged:启用授权
service-cluster-ip-range:Service虚拟IP地址段
enable-admission-plugins:准入控制模块
authorization-mode:认证授权,启用RBAC授权和节点自管理
enable-bootstrap-token-auth:启用TLS bootstrap机制
token-auth-file:bootstrap token文件
service-node-port-range:Service nodeport类型默认分配端口范围
kubelet-client-xxx:apiserver访问kubelet客户端证书
tls-xxx-file:apiserver https证书
etcd-xxxfile:连接Etcd集群证书
audit-log-xxx:审计日志
把刚才生成的证书拷贝到配置文件中的路径:
cp ~/TLS/k8s/ca*pem ~/TLS/k8s/server*pem /opt/kubernetes/ssl/
生成token:
head -c 16 /dev/urandom | od -An -t x | tr -d ' '
创建上述配置文件中token文件(把生成的token替换,格式:token,用户名,UID,用户组)
cat > /opt/kubernetes/cfg/token.csv << EOF
4ec49cd2f31388108e1c8958a805c8d4,kubelet-bootstrap,10001,"system:node-bootstrapper"
EOF
cat > /usr/lib/systemd/system/kube-apiserver.service << EOF
[Unit]
Description=Kubernetes API Server
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=/opt/kubernetes/cfg/kube-apiserver.conf
ExecStart=/opt/kubernetes/bin/kube-apiserver \$KUBE_APISERVER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start kube-apiserver
systemctl enable kube-apiserver
kubectl create clusterrolebinding kubelet-bootstrap \
--clusterrole=system:node-bootstrapper \
--user=kubelet-bootstrap
cat > /opt/kubernetes/cfg/kube-controller-manager.conf << EOF
KUBE_CONTROLLER_MANAGER_OPTS="--logtostderr=false \\
--v=2 \\
--log-dir=/opt/kubernetes/logs \\
--leader-elect=true \\
--master=127.0.0.1:8080 \\
--bind-address=127.0.0.1 \\
--allocate-node-cidrs=true \\
--cluster-cidr=10.244.0.0/16 \\
--service-cluster-ip-range=10.0.0.0/24 \\
--cluster-signing-cert-file=/opt/kubernetes/ssl/ca.pem \\
--cluster-signing-key-file=/opt/kubernetes/ssl/ca-key.pem \\
--root-ca-file=/opt/kubernetes/ssl/ca.pem \\
--service-account-private-key-file=/opt/kubernetes/ssl/ca-key.pem \\
--experimental-cluster-signing-duration=87600h0m0s"
EOF
配置文件中参数介绍如下:
#参数介绍
master:通过本地非安全本地端口8080连接apiserver。
leader-elect:当该组件启动多个时,自动选举(HA)
cluster-signing-cert-file/–cluster-signing-key-file:自动为kubelet颁发证书的CA,与apiserver保持一致
除了为该组件创建 .conf 主配置文件之外,还需要生成一个该组件专属的 .kubeconfig 配置文件用于连接apiserver
先生成kube-controller-manager证书
#切换工作目录
cd ~/TLS/k8s
#创建证书请求文件
cat > kube-controller-manager-csr.json << EOF
{
"CN": "system:kube-controller-manager",
"hosts": [],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing",
"O": "system:masters",
"OU": "System"
}
]
}
EOF
#生成controller-manager 证书
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-controller-manager-csr.json | cfssljson -bare kube-controller-manager
再生成kubeconfig配置文件
KUBE_CONFIG="/opt/kubernetes/cfg/kube-controller-manager.kubeconfig"
KUBE_APISERVER="https://192.168.200.200:6443"
kubectl config set-cluster kubernetes \
--certificate-authority=/opt/kubernetes/ssl/ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=${KUBE_CONFIG}
kubectl config set-credentials kube-controller-manager \
--client-certificate=./kube-controller-manager.pem \
--client-key=./kube-controller-manager-key.pem \
--embed-certs=true \
--kubeconfig=${KUBE_CONFIG}
kubectl config set-context default \
--cluster=kubernetes \
--user=kube-controller-manager \
--kubeconfig=${KUBE_CONFIG}
kubectl config use-context default --kubeconfig=${KUBE_CONFIG}
kubeconfig配置文件生成之后,可以用以下指令查看:
cat /opt/kubernetes/cfg/kube-controller-manager.kubeconfig
cat > /usr/lib/systemd/system/kube-controller-manager.service << EOF
[Unit]
Description=Kubernetes Controller Manager
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=/opt/kubernetes/cfg/kube-controller-manager.conf
ExecStart=/opt/kubernetes/bin/kube-controller-manager \$KUBE_CONTROLLER_MANAGER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start kube-controller-manager
systemctl enable kube-controller-manager
cat > /opt/kubernetes/cfg/kube-scheduler.conf << EOF
KUBE_SCHEDULER_OPTS="--logtostderr=false \
--v=2 \
--log-dir=/opt/kubernetes/logs \
--leader-elect \
--master=127.0.0.1:8080 \
--bind-address=127.0.0.1"
EOF
配置文件中参数介绍如下:
#参数介绍
master:通过本地非安全本地端口8080连接apiserver。
leader-elect:当该组件启动多个时,自动选举(HA)
除了为该组件创建 .conf 主配置文件之外,还需要生成一个该组件专属的 .kubeconfig 配置文件用于连接apiserver
先生成 kube-scheduler证书
#切换工作目录
cd ~/TLS/k8s
#创建证书请求文件
cat > kube-scheduler-csr.json << EOF
{
"CN": "system:kube-scheduler",
"hosts": [],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing",
"O": "system:masters",
"OU": "System"
}
]
}
EOF
#生成 kube-scheduler 证书
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-scheduler-csr.json | cfssljson -bare kube-scheduler
再生成kubeconfig配置文件
KUBE_CONFIG="/opt/kubernetes/cfg/kube-scheduler.kubeconfig"
KUBE_APISERVER="https://192.168.200.200:6443"
kubectl config set-cluster kubernetes \
--certificate-authority=/opt/kubernetes/ssl/ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=${KUBE_CONFIG}
kubectl config set-credentials kube-scheduler \
--client-certificate=./kube-scheduler.pem \
--client-key=./kube-scheduler-key.pem \
--embed-certs=true \
--kubeconfig=${KUBE_CONFIG}
kubectl config set-context default \
--cluster=kubernetes \
--user=kube-scheduler \
--kubeconfig=${KUBE_CONFIG}
kubectl config use-context default --kubeconfig=${KUBE_CONFIG}
kubeconfig配置文件生成之后,可以用以下指令查看:
cat /opt/kubernetes/cfg/kube-scheduler.kubeconfig
cat > /usr/lib/systemd/system/kube-scheduler.service << EOF
[Unit]
Description=Kubernetes Scheduler
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=/opt/kubernetes/cfg/kube-scheduler.conf
ExecStart=/opt/kubernetes/bin/kube-scheduler \$KUBE_SCHEDULER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start kube-scheduler
systemctl enable kube-scheduler
查看集群状态用到kubectl工具,kubectl连接集群前进行配置(生成证书+生成连接用配置文件)
#1、切换工作目录
cd ~/TLS/k8s
#2、生成kubectl连接K8S集群的证书
cat > admin-csr.json << EOF
{
"CN": "admin",
"hosts": [],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing",
"O": "system:masters",
"OU": "System"
}
]
}
EOF
#3、生成证书
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes admin-csr.json | cfssljson -bare admin
#4、生成kubeconfig配置文件(放到kubectl默认的配置文件夹/root/.kube/config下)
mkdir /root/.kube
KUBE_CONFIG="/root/.kube/config"
KUBE_APISERVER="https://192.168.200.200:6443"
kubectl config set-cluster kubernetes \
--certificate-authority=/opt/kubernetes/ssl/ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=${KUBE_CONFIG}
kubectl config set-credentials cluster-admin \
--client-certificate=./admin.pem \
--client-key=./admin-key.pem \
--embed-certs=true \
--kubeconfig=${KUBE_CONFIG}
kubectl config set-context default \
--cluster=kubernetes \
--user=cluster-admin \
--kubeconfig=${KUBE_CONFIG}
kubectl config use-context default --kubeconfig=${KUBE_CONFIG}
到目前为止,Matser节点上的所有组件都已经启动成功(scheduler、controller-manager、etcd-0),通过kubectl工具查看当前集群组件状态:
kubectl get cs
在所有worker node创建工作目录,并从master节点上把二进制文件拷贝到Node节点工作目录
mkdir -p /opt/kubernetes/{bin,cfg,ssl,logs}
cd kubernetes/server/bin
cp kubelet kube-proxy /opt/kubernetes/bin # 本地拷贝
cat > /opt/kubernetes/cfg/kubelet.conf << EOF
KUBELET_OPTS="--logtostderr=false \\
--v=2 \\
--log-dir=/opt/kubernetes/logs \\
--hostname-override=k8s-master1 \\
--network-plugin=cni \\
--kubeconfig=/opt/kubernetes/cfg/kubelet.kubeconfig \\
--bootstrap-kubeconfig=/opt/kubernetes/cfg/bootstrap.kubeconfig \\
--config=/opt/kubernetes/cfg/kubelet-config.yml \\
--cert-dir=/opt/kubernetes/ssl \\
--pod-infra-container-image=lizhenliang/pause-amd64:3.0"
EOF
#配置文件参数解释
hostname-override:显示名称,集群中唯一
network-plugin:启用CNI
kubeconfig:空路径,会自动生成,后面用于连接apiserver
bootstrap-kubeconfig:首次启动向apiserver申请证书
config:配置参数文件
cert-dir:kubelet证书生成目录
pod-infra-container-image:管理Pod网络容器的镜像
cat > /opt/kubernetes/cfg/kubelet-config.yml << EOF
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
address: 0.0.0.0
port: 10250
readOnlyPort: 10255
cgroupDriver: cgroupfs
clusterDNS:
- 10.0.0.2
clusterDomain: cluster.local
failSwapOn: false
authentication:
anonymous:
enabled: false
webhook:
cacheTTL: 2m0s
enabled: true
x509:
clientCAFile: /opt/kubernetes/ssl/ca.pem
authorization:
mode: Webhook
webhook:
cacheAuthorizedTTL: 5m0s
cacheUnauthorizedTTL: 30s
evictionHard:
imagefs.available: 15%
memory.available: 100Mi
nodefs.available: 10%
nodefs.inodesFree: 5%
maxOpenFiles: 1000000
maxPods: 110
EOF
KUBE_CONFIG="/opt/kubernetes/cfg/bootstrap.kubeconfig"
KUBE_APISERVER="https://192.168.200.200:6443" # apiserver IP:PORT
TOKEN="4ec49cd2f31388108e1c8958a805c8d4" # 与token.csv里保持一致
# 生成 kubelet bootstrap kubeconfig 配置文件
kubectl config set-cluster kubernetes \
--certificate-authority=/opt/kubernetes/ssl/ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=${KUBE_CONFIG}
kubectl config set-credentials "kubelet-bootstrap" \
--token=${TOKEN} \
--kubeconfig=${KUBE_CONFIG}
kubectl config set-context default \
--cluster=kubernetes \
--user="kubelet-bootstrap" \
--kubeconfig=${KUBE_CONFIG}
kubectl config use-context default --kubeconfig=${KUBE_CONFIG}
cat > /usr/lib/systemd/system/kubelet.service << EOF
[Unit]
Description=Kubernetes Kubelet
After=docker.service
[Service]
EnvironmentFile=/opt/kubernetes/cfg/kubelet.conf
ExecStart=/opt/kubernetes/bin/kubelet \$KUBELET_OPTS
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start kubelet
systemctl enable kubelet
查看kubelet证书请求,批准请求(请求name按实际填写)
kubectl get csr
kubectl certificate approve node-csr-BWueBGbAmxsFfA8nydpgvg0wEX592ppsxXVChZZGapU
查看节点
kubectl get node
查看节点时,由于 网络插件还没有部署,所以显示NotReady状态:
cat > /opt/kubernetes/cfg/kube-proxy.conf << EOF
KUBE_PROXY_OPTS="--logtostderr=false \\
--v=2 \\
--log-dir=/opt/kubernetes/logs \\
--config=/opt/kubernetes/cfg/kube-proxy-config.yml"
EOF
cat > /opt/kubernetes/cfg/kube-proxy-config.yml << EOF
kind: KubeProxyConfiguration
apiVersion: kubeproxy.config.k8s.io/v1alpha1
bindAddress: 0.0.0.0
metricsBindAddress: 0.0.0.0:10249
clientConnection:
kubeconfig: /opt/kubernetes/cfg/kube-proxy.kubeconfig
hostnameOverride: k8s-master1
clusterCIDR: 10.244.0.0/16
EOF
# 1、切换工作目录
cd ~/TLS/k8s
# 2、创建证书请求文件
cat > kube-proxy-csr.json << EOF
{
"CN": "system:kube-proxy",
"hosts": [],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing",
"O": "k8s",
"OU": "System"
}
]
}
EOF
# 3、生成证书
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxy
# 4、生成kubeconfig文件:
KUBE_CONFIG="/opt/kubernetes/cfg/kube-proxy.kubeconfig"
KUBE_APISERVER="https://192.168.200.200:6443"
kubectl config set-cluster kubernetes \
--certificate-authority=/opt/kubernetes/ssl/ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=${KUBE_CONFIG}
kubectl config set-credentials kube-proxy \
--client-certificate=./kube-proxy.pem \
--client-key=./kube-proxy-key.pem \
--embed-certs=true \
--kubeconfig=${KUBE_CONFIG}
kubectl config set-context default \
--cluster=kubernetes \
--user=kube-proxy \
--kubeconfig=${KUBE_CONFIG}
kubectl config use-context default --kubeconfig=${KUBE_CONFIG}
cat > /usr/lib/systemd/system/kube-proxy.service << EOF
[Unit]
Description=Kubernetes Proxy
After=network.target
[Service]
EnvironmentFile=/opt/kubernetes/cfg/kube-proxy.conf
ExecStart=/opt/kubernetes/bin/kube-proxy \$KUBE_PROXY_OPTS
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start kube-proxy
systemctl enable kube-proxy
Calico是一个纯三层的数据中心网络方案,是目前Kubernetes主流的网络方案
首先把Calico的配置文件 calico.yaml 拖动到:/root/ 目录下,并cd回到root下:cd,然后执行如下两条指令:
#安装calico
kubectl apply -f calico.yaml
#列出“kube-system”命名空间下的所有pod
kubectl get pods -n kube-system
当Calico Pod都Running起来后,节点网络也会准备就绪,此时查看集群中的节点信息:
kubectl get node
应用场景:kubectl logs
cat > apiserver-to-kubelet-rbac.yaml << EOF
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
annotations:
rbac.authorization.kubernetes.io/autoupdate: "true"
labels:
kubernetes.io/bootstrapping: rbac-defaults
name: system:kube-apiserver-to-kubelet
rules:
- apiGroups:
- ""
resources:
- nodes/proxy
- nodes/stats
- nodes/log
- nodes/spec
- nodes/metrics
- pods/log
verbs:
- "*"
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: system:kube-apiserver
namespace: ""
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:kube-apiserver-to-kubelet
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: User
name: kubernetes
EOF
kubectl apply -f apiserver-to-kubelet-rbac.yaml
到目前为止,在Matser节点中要部署的所有组件都已经安装配置到Matser节点中了
(1)Matser组件:apiserver、controller-manager、scheduler
(2)Node组件:kubelet、kube-proxy
当然,Node工作节点中的组件可以不部署在Matser节点中,但是本案例还是这样做了,无伤大雅,接下来就把在Master节点中所部署的Node组件直接拷贝到各个Node节点上来,并修改必要的配置信息。Node节点的部署就轻松很多了。
在Master节点中,将Worker Node涉及文件拷贝到另外2个新节点192.168.200.201/202
#拷贝工作目录(连带Master的内容一起拷贝了,没关系,反正也用不到)
scp -r /opt/kubernetes [email protected]:/opt/
#拷贝配置文件
scp -r /usr/lib/systemd/system/{kubelet,kube-proxy}.service [email protected]:/usr/lib/systemd/system
#拷贝证书文件
scp /opt/kubernetes/ssl/ca.pem [email protected]:/opt/kubernetes/ssl
#对202机器同样执行一遍
scp -r /opt/kubernetes [email protected]:/opt/
scp -r /usr/lib/systemd/system/{kubelet,kube-proxy}.service [email protected]:/usr/lib/systemd/system
scp /opt/kubernetes/ssl/ca.pem [email protected]:/opt/kubernetes/ssl
由于这几个文件是证书申请审批后自动生成的,每个Node不同,因此必须删除
#在node1、node2两个节点上分别执行如下命令
rm -f /opt/kubernetes/cfg/kubelet.kubeconfig
rm -f /opt/kubernetes/ssl/kubelet*
在两台机器上分别打开2个配置文件,并修改主机名
#修改kubelet配置文件
vi /opt/kubernetes/cfg/kubelet.conf
--hostname-override=k8s-node1
#修改kube-proxy配置文件
vi /opt/kubernetes/cfg/kube-proxy-config.yml
hostnameOverride: k8s-node1
#名称换成:k8s-node2再执行一遍
systemctl daemon-reload
systemctl start kubelet kube-proxy
systemctl enable kubelet kube-proxy
注意:该操作在Master节点上完成!
先查看证书请求:
kubectl get csr
在两台node机器上分别执行完前4步操作之后(node节点中启动kubelet后就会有请求信息),查看证书请求会看到2条信息:
根据证书请求name进行授权
#授权ndoe1
kubectl certificate approve node-csr-MYWfZXy8o2n8Gb6dZ-fVKw1qVFkmgEkSFdg7m1VL56w
#授权node2
kubectl certificate approve node-csr-XAp5v8JU6qlAkDwzXPiN0DkJ9xlqsq4KbMekUFqBwKM
kubectl get node
kubectl apply -f kubernetes-dashboard.yaml
kubectl get pods,svc -n kubernetes-dashboard
此时需要一个token,生成token步骤如下(Master主机下操作):
#创建service account
kubectl create serviceaccount dashboard-admin -n kube-system
#将service account绑定到集群角色,具备管理员权限
kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
#获取token
kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')
输入token后进入DashBoard界面:
kubectl apply -f coredns.yaml
kubectl get pods -n kube-system
到目前为止,一个单Master集群就搭建完成了,接下来进行多Master集群扩容。
如果你的机器配置不够用,到这里就已经可以进行后续的K8S学习了。
但是如果追求K8S集群的高可用的话,就再搞一台虚拟机作为第二个Matser-Node,操作如下。
现在需要再增加一台新服务器(虚拟机),作为Master2 Node,IP是192.168.200.203。
Master2 与已部署的Master1所有操作一致,只需将Master1所有K8s文件拷贝过来,再修改下服务器IP和主机名启动即可。
##在Matser1节点上进行如下拷贝操作,拷贝到Master2节点
scp /usr/bin/docker* [email protected]:/usr/bin
scp /usr/bin/runc [email protected]:/usr/bin
scp /usr/bin/containerd* [email protected]:/usr/bin
scp /usr/lib/systemd/system/docker.service [email protected]:/usr/lib/systemd/system
scp -r /etc/docker [email protected]:/etc
# 在Master2中启动Docker
systemctl daemon-reload
systemctl start docker
systemctl enable docker
mkdir -p /opt/etcd/ssl
scp -r /opt/kubernetes [email protected]:/opt
scp -r /opt/etcd/ssl [email protected]:/opt/etcd
scp /usr/lib/systemd/system/kube* [email protected]:/usr/lib/systemd/system
scp /usr/bin/kubectl [email protected]:/usr/bin
scp -r ~/.kube [email protected]:~
rm -f /opt/kubernetes/cfg/kubelet.kubeconfig
rm -f /opt/kubernetes/ssl/kubelet*
修改apiserver、kubelet和kube-proxy配置文件为本地IP及主机名:
vi /opt/kubernetes/cfg/kube-apiserver.conf
...
--bind-address=192.168.200.203 \
--advertise-address=192.168.200.203 \
...
vi /opt/kubernetes/cfg/kube-controller-manager.kubeconfig
server: https://192.168.200.203:6443
vi /opt/kubernetes/cfg/kube-scheduler.kubeconfig
server: https://192.168.200.203:6443
vi /opt/kubernetes/cfg/kubelet.conf
--hostname-override=k8s-master2
vi /opt/kubernetes/cfg/kube-proxy-config.yml
hostnameOverride: k8s-master2
vi ~/.kube/config
...
server: https://192.168.200.203:6443
systemctl daemon-reload
systemctl start kube-apiserver kube-controller-manager kube-scheduler kubelet kube-proxy
systemctl enable kube-apiserver kube-controller-manager kube-scheduler kubelet kube-proxy
kubectl get cs
查看证书请求:
kubectl get csr
授权请求:
kubectl certificate approve node-csr-pt7afV7SOLIx5F-CMdAhzoes2hbVL7E-NmipvtcEEy8
查看node(如果状态没变成 Ready 就多等几分钟):
kubectl get node
kube-apiserver高可用架构图如下:
在两台Master节点上分别进行如下操作:
yum install epel-release -y
yum install nginx keepalived -y
cat > /etc/nginx/nginx.conf << "EOF"
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
# 四层负载均衡,为两台Master apiserver组件提供负载均衡
stream {
log_format main '$remote_addr $upstream_addr - [$time_local] $status $upstream_bytes_sent';
access_log /var/log/nginx/k8s-access.log main;
upstream k8s-apiserver {
server 192.168.200.200:6443; # Master1 APISERVER IP:PORT
server 192.168.200.203:6443; # Master2 APISERVER IP:PORT
}
server {
listen 16443; # 由于nginx与master节点复用,这个监听端口不能是6443,否则会冲突
proxy_pass k8s-apiserver;
}
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
listen 80 default_server;
server_name _;
location / {
}
}
}
EOF
cat > /etc/keepalived/keepalived.conf << EOF
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id NGINX_MASTER
}
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
}
vrrp_instance VI_1 {
state MASTER
interface ens33 # 修改为实际网卡名
virtual_router_id 51 # VRRP 路由 ID实例,每个实例是唯一的
priority 100 # 优先级,备服务器设置 90
advert_int 1 # 指定VRRP 心跳包通告间隔时间,默认1秒
authentication {
auth_type PASS
auth_pass 1111
}
# 虚拟IP
virtual_ipaddress {
192.168.200.88/24
}
track_script {
check_nginx
}
}
EOF
此外,需要准备上述配置文件中检查nginx运行状态的脚本:
cat > /etc/keepalived/check_nginx.sh << "EOF"
#!/bin/bash
count=$(ss -antp |grep 16443 |egrep -cv "grep|$$")
if [ "$count" -eq 0 ];then
exit 1
else
exit 0
fi
EOF
chmod +x /etc/keepalived/check_nginx.sh
cat > /etc/keepalived/keepalived.conf << EOF
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id NGINX_BACKUP
}
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51 # VRRP 路由 ID实例,每个实例是唯一的
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.88/24
}
track_script {
check_nginx
}
}
EOF
此外,需要准备上述配置文件中检查nginx运行状态的脚本:
cat > /etc/keepalived/check_nginx.sh << "EOF"
#!/bin/bash
count=$(ss -antp |grep 16443 |egrep -cv "grep|$$")
if [ "$count" -eq 0 ];then
exit 1
else
exit 0
fi
EOF
chmod +x /etc/keepalived/check_nginx.sh
#左边的ip是Nginx-Master的ip(200),右边的ip是VIP(88)
sed -i 's#192.168.200.200:6443#192.168.200.88:16443#' /opt/kubernetes/cfg/*
systemctl restart kubelet kube-proxy
之前我们是让Matser1节点和其他各个Node节点进行直连,而现在,断开了这个连接(而是让node连接VIP),由于目前Nginx还没有启动,所以执行kubectl get node 指令时会出现如下无法连接的情况:
systemctl daemon-reload
systemctl start nginx keepalived
systemctl enable nginx keepalived
注意:如果在任何环节出现操作系统软死锁的问题,如下:
这个bug没有让系统彻底死机,但是会让Nginx被锁死在了某个状态,而造成死锁的原因我估计是因为虚拟机的CPU太忙了或者CPU电压不稳定。 请输入如下几行指令解决:
#查看参数 watchdog_thresh(结果应该是10)
tail -1 /proc/sys/kernel/watchdog_thresh
#把这个参数改为30
echo 30 > /proc/sys/kernel/watchdog_thresh
#临时生效
sysctl -w kernel.watchdog_thresh=30
此时,在ens33网卡绑定了192.168.200.88 虚拟IP,说明Nginx-Master工作正常。
现在访问192.168.200.88这个虚拟ip实际上就是在访问K8S-Matser集群中某个实例的apiserver。
(vip->Nginx-Master->K8S-Matser-apiserver,vip是Nginx的虚拟ip,与K8S-Matser节点无关)
(1) Nginx代理测试
在K8s集群中选择任意节点,使用curl查看K8s版本测试,此时使用VIP进行访问:
curl -k https://192.168.200.88:16443/version
如果能够获得k8s信息,说明Nginx搭建正常,且curl->vip(nginx)->apiserver(k8s-matser) 的链路被打通了。
当然,通过Nginx日志也可以看到转发apiserver Ip:
tail /var/log/nginx/k8s-access.log -f
(2)K8S创建容器后访问测试
在Matser节点上创建一个容器,然后访问该容器,测试是否能成功访问:
#创建容器,访问该容器的ip就是node1或者node2的ip,因为node1和node2完全一样,用来部署容器应用,实现高可用
kubectl create deployment web --image=nginx
#暴露端口80
kubectl expose deployment web --port=80 --target-port=80 --type=NodePort
#查看service(获取端口号)
kubectl get svc
到这里就算结束了,实际上还有个测试没做,就是:Nginx+Keepalived的高可用测试。
为什么没做这个测试呢?是因为我的电脑在一次系统内核软死锁之后,Nginx的启动总是失败,网络上的方法不能很好的解决我的问题,然而当我使用了step6中的方法之后,在matser1中部署的ngxin可以正常启动了,然而master2中的nginx仍不能正常启动,很奇怪,哭唧唧,后续针对这个问题再研究一下吧,这篇文章写得蛮长,mark一下。