企业级实战模块一:Kubernetes 二进制安装
1 准备环境
服务器规划信息:
服务器IP | 服务器名称 | 服务器配置 | 组件 |
---|---|---|---|
192.168.5.3 | k8s-master-01 | 2C2G | kube-apiserver,kube-controller-manager ,kube-scheduler,etcd |
192.168.5.4 | k8s-node-01 | 2C2G | kubelet,kube-proxy,docker etcd |
192.168.5.5 | k8s-node-02 | 2C2G | kubelet,kube-proxy,docker etcd |
软件版本:
软件 | 版本 |
---|---|
操作系统 | CentOS 7.9.2009 |
Docker | 19.03.9 |
Etcd | 3.4.9 |
Kubernetes | 1.18.3 |
2 操作系统初始化配置(所有节点)
# 关闭防火墙
systemctl stop firewalld && systemctl disable firewalld
# 关闭selinux
setenforce 0 && sed -i 's/enforcing/disabled/' /etc/selinux/config
# 关闭swap
swapoff -a && sed -ri 's/.*swap.*/#&/' /etc/fstab
# 根据规划设置主机名
hostnamectl set-hostname k8s-node-01 && bash
# 将集群节点添加到hosts文件
cat <<EOF>> /etc/hosts
192.168.5.3 k8s-master-01
192.168.5.4 k8s-node-01
192.168.5.5 k8s-node-02
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 ntpdate vim git wget && ntpdate time.windows.com
# 创建所需文件夹
mkdir -p /usr/local/kubernetes/etcd/{bin,cfg,ssl}
mkdir -p /usr/local/kubernetes/kubernetes/{bin,cfg,ssl,logs}
3 部署Etcd 集群(所有节点)
Etcd 是一个分布式键值存储系统,Kubernetes 使用Etcd 进行数据存储,所以先准备一个Etcd 数据库,为解决Etcd 单点故障,应采用集群方式部署,这里使用 3 台组建集群,可容忍1 台机器故障,当然,你也可以使用5 台组建集群,可容忍2 台机器故障。
服务器名称 | 节点名称 | IP地址 |
---|---|---|
k8s-master-01 | etcd-1 | 192.168.5.3 |
k8s-node-01 | etcd-2 | 192.168.5.4 |
k8s-node-02 | etcd-3 | 192.168.5.5 |
注:为了节省机器,这里与K8s 节点机器复用。也可以独立于k8s 集群之外部署,只要apiserver 能连接到就行。
3.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
注:部分可能因为网络因素无法下载证书工具,需要自行解决外网下载问题,如果无法解决,也可以使用以下地址进行下载
wget https://cunqi0105-1300757323.cos.ap-shanghai.myqcloud.com/configuration-file/cfssl.tar.gz
3.2 生成Etcd 证书
1)自签证书颁发机构(CA)
创建工作目录:
mkdir -p /usr/local/kubernetes/TLS/{etcd,k8s}
cd /usr/local/kubernetes/TLS/etcd
自签CA证书:
cat <<EOF> /usr/local/kubernetes/TLS/etcd/ca-config.json
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"www": {
"expiry": "87600h",
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
EOF
cat <<EOF> /usr/local/kubernetes/TLS/etcd/ca-csr.json
{
"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
2)使用自签CA 签发Etcd HTTPS 证书
创建证书申请文件:
cat <<EOF> /usr/local/kubernetes/TLS/etcd/server-csr.json
{
"CN": "etcd",
"hosts": [
"192.168.5.3",
"192.168.5.4",
"192.168.5.5"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing"
}
]
}
EOF
注:上述文件hosts 字段中IP 为所有etcd 节点的集群内部通信IP,一个都不能少!为了方便后期扩容可以多写几个预留的IP。
生成证书:
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
3.3 下载Etcd二进制文件
cd /root/ && wget https://github.com/etcd-io/etcd/releases/download/v3.4.9/etcd-v3.4.9-linux-amd64.tar.gz
个人收集:
cd /root/ && wget https://cunqi0105-1300757323.cos.ap-shanghai.myqcloud.com/install-pkg/etcd-v3.4.9-linux-amd64.tar.gz
3.4 创建工作目录并解压二进制包
tar -zxvf etcd-v3.4.9-linux-amd64.tar.gz && mv etcd-v3.4.9-linux-amd64/{etcd,etcdctl} /usr/local/kubernetes/etcd/bin/
3.5 拷贝证书
把刚才生成的证书拷贝到配置文件中的路径:
cp /usr/local/kubernetes/TLS/etcd/ca*pem /usr/local/kubernetes/etcd/ssl/
cp /usr/local/kubernetes/TLS/etcd/server*pem /usr/local/kubernetes/etcd/ssl/
3.6 创建etcd 配置文件
cat <<EOF> /usr/local/kubernetes/etcd/cfg/etcd.conf
#[Member]
ETCD_NAME="etcd-1"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.5.3:2380"
ETCD_LISTEN_CLIENT_URLS="https://192.168.5.3:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.5.3:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.5.3:2379"
ETCD_INITIAL_CLUSTER="etcd-1=https://192.168.5.3:2380,etcd-2=https://192.168.5.4:2380,etcd-3=https://192.168.5.5: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 表示加入已有集群
3.7 systemd 管理etcd
cat <<EOF> /usr/lib/systemd/system/etcd.service
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
EnvironmentFile=/usr/local/kubernetes/etcd/cfg/etcd.conf
ExecStart=/usr/local/kubernetes/etcd/bin/etcd \
--cert-file=/usr/local/kubernetes/etcd/ssl/server.pem \
--key-file=/usr/local/kubernetes/etcd/ssl/server-key.pem \
--peer-cert-file=/usr/local/kubernetes/etcd/ssl/server.pem \
--peer-key-file=/usr/local/kubernetes/etcd/ssl/server-key.pem \
--trusted-ca-file=/usr/local/kubernetes/etcd/ssl/ca.pem \
--peer-trusted-ca-file=/usr/local/kubernetes/etcd/ssl/ca.pem \
--logger=zap
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
3.8 分发配置文件
scp -r /usr/local/kubernetes/etcd root@192.168.5.4:/usr/local/kubernetes
scp /usr/lib/systemd/system/etcd.service root@192.168.5.4:/usr/lib/systemd/system/
scp -r /usr/local/kubernetes/etcd root@192.168.5.5:/usr/local/kubernetes
scp /usr/lib/systemd/system/etcd.service root@192.168.5.5:/usr/lib/systemd/system/
然后在节点2 和节点3 分别修改etcd.conf 配置文件中的节点名称和当前服务器IP:
vim /usr/local/kubernetes/etcd/cfg/etcd.conf
#[Member]
ETCD_NAME="etcd-2" # 修改此处,节点2 改为etcd-2,节点3 改为etcd-3
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.5.4:2380" # 修改此处为当前服务器
ETCD_LISTEN_CLIENT_URLS="https://192.168.5.4:2379" # 修改此处为当前服务器
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.5.4:2380" # 修改此处为当前服务器
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.5.4:2379" # 修改此处为当前服务器
ETCD_INITIAL_CLUSTER="etcd-1=https://192.168.5.3:2380,etcd-2=https://192.168.5.4:2380,etcd-3=https://192.168.5.5:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
3.9 启动并设置开机启动(所有服务器同时启动)
systemctl daemon-reload && systemctl restart etcd && systemctl enable etcd && systemctl status etcd
3.10 查看集群状态
/usr/local/kubernetes/etcd/bin/etcdctl --cacert=/usr/local/kubernetes/etcd/ssl/ca.pem --cert=/usr/local/kubernetes/etcd/ssl/server.pem --key=/usr/local/kubernetes/etcd/ssl/server-key.pem --endpoints="https://192.168.5.3:2379,https://192.168.5.4:2379,https://192.168.5.5:2379" endpoint health
如果输出上面信息,就说明集群部署成功。如果有问题第一步先看日志:/var/log/message 或journalctl -u etcd
4 安装Docker(所有节点)
4.1 下载Docker二进制文件
cd /root && wget https://download.docker.com/linux/static/stable/x86_64/docker-19.03.9.tgz
个人收集:
cd /root && wget https://cunqi0105-1300757323.cos.ap-shanghai.myqcloud.com/install-pkg/docker-19.03.9.tgz
4.2 解压二进制包
tar -zxvf docker-19.03.9.tgz && mv docker/* /usr/bin
4.3 systemd 管理docker
cat <<EOF> /usr/lib/systemd/system/docker.service
[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
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.4 添加阿里云镜像加速
mkdir /etc/docker
cat <<EOF> /etc/docker/daemon.json
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF
4.5 启动并设置开机启动
systemctl daemon-reload && systemctl start docker && systemctl enable docker && systemctl status docker
5 部署Master(Master节点操作)
5.1 生成kube-apiserver 证书
1)自签证书颁发机构(CA)
cd /usr/local/kubernetes/TLS/k8s
cat <<EOF> /usr/local/kubernetes/TLS/k8s/ca-config.json
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"kubernetes": {
"expiry": "87600h",
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
EOF
cat <<EOF> /usr/local/kubernetes/TLS/k8s/ca-csr.json
{
"CN": "kubernetes",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "Beijing",
"ST": "Beijing",
"O": "k8s",
"OU": "System"
}
]
}
EOF
2)生成证书
cfssl gencert -initca ca-csr.json | cfssljson -bare ca -ls *pem && ls*.pem
3)使用自签CA 签发kube-apiserver HTTPS 证书
创建证书申请文件:
cat <<EOF> /usr/local/kubernetes/TLS/k8s/server-csr.json
{
"CN": "kubernetes",
"hosts": [
"10.0.0.1",
"127.0.0.1",
"192.168.5.3",
"192.168.5.4",
"192.168.5.5",
"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
4)生成证书
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare server && ls server*.pem
5.2 下载kubernetes-server二进制文件
https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG/CHANGELOG-1.18.md#v1183
注:打开链接你会发现里面有很多包,下载一个server 包就够了,包含了Master 和Worker Node 二进制文件。
cd /root && wget https://dl.k8s.io/v1.18.3/kubernetes-server-linux-amd64.tar.gz
注:部分可能因为网络因素无法下载证书工具,需要自行解决外网下载问题,如果无法解决,可以使用以下地址进行下载
wget https://cunqi0105-1300757323.cos.ap-shanghai.myqcloud.com/install-pkg/kubernetes-server-linux-amd64.tar
5.3 解压二进制包
mkdir -p /usr/local/kubernetes/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 /usr/local/kubernetes/kubernetes/bin && cp kubectl /usr/bin/
5.4 部署kube-apiserver
1)创建配置文件
cat <<EOF> /usr/local/kubernetes/kubernetes/cfg/kube-apiserver.conf
KUBE_APISERVER_OPTS="--logtostderr=true \\
--v=2 \\
--log-dir=/usr/local/kubernetes/kubernetes/logs \\
--etcd-servers=https://192.168.5.3:2379,https://192.168.5.4:2379,https://192.168.5.5:2379 \\
--bind-address=192.168.5.3 \\
--secure-port=6443 \\
--advertise-address=192.168.5.3 \\
--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=/usr/local/kubernetes/kubernetes/cfg/token.csv \\
--service-node-port-range=30000-32767 \\
--kubelet-client-certificate=/usr/local/kubernetes/kubernetes/ssl/server.pem \\
--kubelet-client-key=/usr/local/kubernetes/kubernetes/ssl/server-key.pem \\
--tls-cert-file=/usr/local/kubernetes/kubernetes/ssl/server.pem \\
--tls-private-key-file=/usr/local/kubernetes/kubernetes/ssl/server-key.pem \\
--client-ca-file=/usr/local/kubernetes/kubernetes/ssl/ca.pem \\
--service-account-key-file=/usr/local/kubernetes/kubernetes/ssl/ca-key.pem \\
--etcd-cafile=/usr/local/kubernetes/etcd/ssl/ca.pem \\
--etcd-certfile=/usr/local/kubernetes/etcd/ssl/server.pem \\
--etcd-keyfile=/usr/local/kubernetes/etcd/ssl/server-key.pem \\
--audit-log-maxage=30 \\
--audit-log-maxbackup=3 \\
--audit-log-maxsize=100 \\
--audit-log-path=/usr/local/kubernetes/kubernetes/logs/k8s-audit.log"
EOF
注:上面两个\ \ 第一个是转义符,第二个是换行符,使用转义符是为了使用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:审计日志
2)拷贝生成的证书
cp /usr/local/kubernetes/TLS/k8s/ca*pem /usr/local/kubernetes/TLS/k8s/server*pem /usr/local/kubernetes/kubernetes/ssl
3)启用TLS Bootstrapping 机制
TLS Bootstraping:Master apiserver 启用TLS 认证后,Node 节点kubelet 和kubeproxy要与kube-apiserver 进行通信,必须使用CA 签发的有效证书才可以,当Node节点很多时,这种客户端证书颁发需要大量工作,同样也会增加集群扩展复杂度。
为了简化流程,Kubernetes 引入了TLS bootstraping 机制来自动颁发客户端证书,kubelet会以一个低权限用户自动向apiserver 申请证书,kubelet 的证书由apiserver 动态签署。所以强烈建议在Node 上使用这种方式,目前主要用于kubelet,kube-proxy 还是由我们统一颁发一个证书。
自动生成token
head -c 16 /dev/urandom | od -An -t x | tr -d ' '
# 格式:token,用户名,UID,用户组
cat <<EOF> /usr/local/kubernetes/kubernetes/cfg/token.csv
f4bddf7f232ea4005ee4f6e119dd2230,kubelet-bootstrap,10001,"system:nodebootstrapper"
EOF
4)systemd 管理apiserver
cat <<EOF> /usr/lib/systemd/system/kube-apiserver.service
[Unit]
Description=Kubernetes API Server
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=/usr/local/kubernetes/kubernetes/cfg/kube-apiserver.conf
ExecStart=/usr/local/kubernetes/kubernetes/bin/kube-apiserver \$KUBE_APISERVER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
5)启动并设置开机启动
systemctl daemon-reload && systemctl restart kube-apiserver && systemctl enable kube-apiserver && systemctl status kube-apiserver
6)授权kubelet-bootstrap 用户允许请求证书
kubectl create clusterrolebinding kubelet-bootstrap \
--clusterrole=system:node-bootstrapper \
--user=kubelet-bootstrap
5.5 部署kube-controller-manager
1)创建配置文件
cat <<EOF> /usr/local/kubernetes/kubernetes/cfg/kube-controller-manager.conf
KUBE_CONTROLLER_MANAGER_OPTS="--logtostderr=true \\
--v=2 \\
--log-dir=/usr/local/kubernetes/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=/usr/local/kubernetes/kubernetes/ssl/ca.pem \\
--cluster-signing-key-file=/usr/local/kubernetes/kubernetes/ssl/ca-key.pem \\
--root-ca-file=/usr/local/kubernetes/kubernetes/ssl/ca.pem \\
--service-account-private-key-file=/usr/local/kubernetes/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 保持一致
2)systemd 管理controller-manager
cat <<EOF> /usr/lib/systemd/system/kube-controller-manager.service
[Unit]
Description=Kubernetes Controller Manager
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=/usr/local/kubernetes/kubernetes/cfg/kube-controller-manager.conf
ExecStart=/usr/local/kubernetes/kubernetes/bin/kube-controller-manager \$KUBE_CONTROLLER_MANAGER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
3)启动并设置开机启动
systemctl daemon-reload && systemctl restart kube-controller-manager && systemctl enable kube-controller-manager && systemctl status kube-controller-manager
5.6 部署kube-scheduler
1)创建配置文件
cat <<EOF> /usr/local/kubernetes/kubernetes/cfg/kube-scheduler.conf
KUBE_SCHEDULER_OPTS="--logtostderr=true \
--v=2 \--log-dir=/usr/local/kubernetes/kubernetes/logs \
--leader-elect \
--master=127.0.0.1:8080 \
--bind-address=127.0.0.1"
EOF
-
–master:通过本地非安全本地端口8080 连接apiserver。
-
–leader-elect:当该组件启动多个时,自动选举(HA)
2)systemd 管理scheduler
cat <<EOF> /usr/lib/systemd/system/kube-scheduler.service
[Unit]
Description=Kubernetes Scheduler
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=/usr/local/kubernetes/kubernetes/cfg/kube-scheduler.conf
ExecStart=/usr/local/kubernetes/kubernetes/bin/kube-scheduler \$KUBE_SCHEDULER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
3)启动并设置开机启动
systemctl daemon-reload && systemctl restart kube-scheduler && systemctl enable kube-scheduler && systemctl status kube-scheduler
4)查看集群状态
kubectl get cs
5.7 部署kubelet
1)拷贝执行文件
cd /root && cp kubernetes/server/bin/{kubelet,kube-proxy} /usr/local/kubernetes/kubernetes/bin/
2)创建配置文件
cat <<EOF> /usr/local/kubernetes/kubernetes/cfg/kubelet.conf
KUBELET_OPTS="--logtostderr=true \\
--v=2 \\
--log-dir=/usr/local/kubernetes/kubernetes/logs \\
--hostname-override=k8s-master-01 \\
--network-plugin=cni \\
--kubeconfig=/usr/local/kubernetes/kubernetes/cfg/kubelet.kubeconfig \\
--bootstrap-kubeconfig=/usr/local/kubernetes/kubernetes/cfg/bootstrap.kubeconfig \\
--config=/usr/local/kubernetes/kubernetes/cfg/kubelet-config.yml \\
--cert-dir=/usr/local/kubernetes/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 网络容器的镜像
3)配置参数文件
cat <<EOF> /usr/local/kubernetes/kubernetes/cfg/kubelet-config.yml
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: /usr/local/kubernetes/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
4)生成bootstrap.kubeconfig 文件
生成文件
# 生成临时变量
KUBE_APISERVER="https://192.168.5.3:6443" # apiserver IP:PORT
TOKEN="f4bddf7f232ea4005ee4f6e119dd2230" # 与token.csv 里保持一致
# 生成kubelet bootstrap kubeconfig 配置文件
kubectl config set-cluster kubernetes \
--certificate-authority=/usr/local/kubernetes/kubernetes/ssl/ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=bootstrap.kubeconfig
kubectl config set-credentials "kubelet-bootstrap" \
--token=${TOKEN} \
--kubeconfig=bootstrap.kubeconfig
kubectl config set-context default \
--cluster=kubernetes \
--user="kubelet-bootstrap" \
--kubeconfig=bootstrap.kubeconfig
kubectl config use-context default --kubeconfig=bootstrap.kubeconfig
拷贝到配置文件路径:
mv bootstrap.kubeconfig /usr/local/kubernetes/kubernetes/cfg
5)systemd 管理kubelet
cat <<EOF> /usr/lib/systemd/system/kubelet.service
[Unit]
Description=Kubernetes Kubelet
After=docker.service
[Service]
EnvironmentFile=/usr/local/kubernetes/kubernetes/cfg/kubelet.conf
ExecStart=/usr/local/kubernetes/kubernetes/bin/kubelet \$KUBELET_OPTS
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
6)启动并设置开机启动
systemctl daemon-reload && systemctl restart kubelet && systemctl enable kubelet && systemctl status kubelet
6.1 生成kube-proxy 证书
1)自签证书颁发机构(CA)
cd /usr/local/kubernetes/TLS/k8s
cat <<EOF> /usr/local/kubernetes/TLS/k8s/kube-proxy-csr.json
{
"CN": "system:kube-proxy",
"hosts": [],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing",
"O": "k8s",
"OU": "System"
}
]
}
EOF
2)生成证书
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxy && ls kube-proxy*pem
3)拷贝证书
cp /usr/local/kubernetes/TLS/k8s/kube-proxy*pem /usr/local/kubernetes/kubernetes/ssl
4)从master 节点分发证书
scp -r /usr/local/kubernetes/kubernetes/ssl root@192.168.5.4:/usr/local/kubernetes/kubernetes
scp -r /usr/local/kubernetes/kubernetes/ssl root@192.168.5.5:/usr/local/kubernetes/kubernetes
5.8 部署kube-proxy
1)创建配置文件
cat <<EOF> /usr/local/kubernetes/kubernetes/cfg/kube-proxy.conf
KUBE_PROXY_OPTS="--logtostderr=true \\
--v=2 \\
--log-dir=/usr/local/kubernetes/kubernetes/logs \\
--config=/usr/local/kubernetes/kubernetes/cfg/kube-proxy-config.yml"
EOF
2)配置参数文件
cat <<EOF> /usr/local/kubernetes/kubernetes/cfg/kube-proxy-config.yml
kind: KubeProxyConfiguration
apiVersion: kubeproxy.config.k8s.io/v1alpha1
bindAddress: 0.0.0.0
metricsBindAddress: 0.0.0.0:10249
clientConnection:
kubeconfig: /usr/local/kubernetes/kubernetes/cfg/kube-proxy.kubeconfig
hostnameOverride: k8s-master-01
clusterCIDR: 10.0.0.0/24
EOF
3)生成kube-proxy.kubeconfig 文件
cd /root && KUBE_APISERVER="https://192.168.5.3:6443"
kubectl config set-cluster kubernetes \
--certificate-authority=/usr/local/kubernetes/kubernetes/ssl/ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=kube-proxy.kubeconfig
kubectl config set-credentials kube-proxy \
--client-certificate=/usr/local/kubernetes/kubernetes/ssl/kube-proxy.pem \
--client-key=/usr/local/kubernetes/kubernetes/ssl/kube-proxy-key.pem \
--embed-certs=true \
--kubeconfig=kube-proxy.kubeconfig
kubectl config set-context default \
--cluster=kubernetes \
--user=kube-proxy \
--kubeconfig=kube-proxy.kubeconfig
kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig
mv kube-proxy.kubeconfig /usr/local/kubernetes/kubernetes/cfg/
4)systemd 管理kube-proxy
cat <<EOF> /usr/lib/systemd/system/kube-proxy.service
[Unit]
Description=Kubernetes Proxy
After=network.target
[Service]
EnvironmentFile=/usr/local/kubernetes/kubernetes/cfg/kube-proxy.conf
ExecStart=/usr/local/kubernetes/kubernetes/bin/kube-proxy \$KUBE_PROXY_OPTS
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
5)启动并设置开机启动
systemctl daemon-reload && systemctl restart kube-proxy && systemctl enable kube-proxy && systemctl status kube-proxy
6 部署Worker Node
6.1 创建工作目录并拷贝二进制文件
从master 节点拷贝:
cd kubernetes/server/bin/
scp kubectl kubelet kube-proxy root@192.168.5.4:/usr/local/kubernetes/kubernetes/bin/
scp kubectl kubelet kube-proxy root@192.168.5.5:/usr/local/kubernetes/kubernetes/bin/
6.2 部署kubelet
1)创建配置文件
注:修改--hostname-override字段,为对应节点的主机名
cat <<EOF> /usr/local/kubernetes/kubernetes/cfg/kubelet.conf
KUBELET_OPTS="--logtostderr=true \\
--v=2 \\
--log-dir=/usr/local/kubernetes/kubernetes/logs \\
--hostname-override=k8s-node-02 \\
--network-plugin=cni \\
--kubeconfig=/usr/local/kubernetes/kubernetes/cfg/kubelet.kubeconfig \\
--bootstrap-kubeconfig=/usr/local/kubernetes/kubernetes/cfg/bootstrap.kubeconfig \\
--config=/usr/local/kubernetes/kubernetes/cfg/kubelet-config.yml \\
--cert-dir=/usr/local/kubernetes/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 网络容器的镜像
2)配置参数文件
cat <<EOF> /usr/local/kubernetes/kubernetes/cfg/kubelet-config.yml
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: /usr/local/kubernetes/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
3)生成bootstrap.kubeconfig 文件
移动kubectl文件
cp /usr/local/kubernetes/kubernetes/bin/kubectl /usr/bin/
生成文件
# 生成临时变量
KUBE_APISERVER="https://192.168.5.3:6443" # apiserver IP:PORT
TOKEN="f4bddf7f232ea4005ee4f6e119dd2230" # 与token.csv 里保持一致
# 生成kubelet bootstrap kubeconfig 配置文件
kubectl config set-cluster kubernetes \
--certificate-authority=/usr/local/kubernetes/kubernetes/ssl/ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=bootstrap.kubeconfig
kubectl config set-credentials "kubelet-bootstrap" \
--token=${TOKEN} \
--kubeconfig=bootstrap.kubeconfig
kubectl config set-context default \
--cluster=kubernetes \
--user="kubelet-bootstrap" \
--kubeconfig=bootstrap.kubeconfig
kubectl config use-context default --kubeconfig=bootstrap.kubeconfig
拷贝到配置文件路径:
mv bootstrap.kubeconfig /usr/local/kubernetes/kubernetes/cfg
4)systemd 管理kubelet
cat <<EOF> /usr/lib/systemd/system/kubelet.service
[Unit]
Description=Kubernetes Kubelet
After=docker.service
[Service]
EnvironmentFile=/usr/local/kubernetes/kubernetes/cfg/kubelet.conf
ExecStart=/usr/local/kubernetes/kubernetes/bin/kubelet \$KUBELET_OPTS
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
5)启动并设置开机启动
systemctl daemon-reload && systemctl restart kubelet && systemctl enable kubelet && systemctl status kubelet
6.3 批准kubelet 证书申请并加入集群(在Master节点操作)
1)在Master主节点上查看kubelet 证书请求
kubectl get csr
2)批准申请
kubectl certificate approve $NAME
kubectl certificate approve node-csr-l8BA9pX1ytE8mEwl7t1dgB1m_rHIr-g8FA345SFfBP4
3)查看节点
kubectl get node
6.4 部署kube-proxy
1)创建配置文件
cat <<EOF> /usr/local/kubernetes/kubernetes/cfg/kube-proxy.conf
KUBE_PROXY_OPTS="--logtostderr=true \\
--v=2 \\
--log-dir=/usr/local/kubernetes/kubernetes/logs \\
--config=/usr/local/kubernetes/kubernetes/cfg/kube-proxy-config.yml"
EOF
2)配置参数文件
cat <<EOF> /usr/local/kubernetes/kubernetes/cfg/kube-proxy-config.yml
kind: KubeProxyConfiguration
apiVersion: kubeproxy.config.k8s.io/v1alpha1
bindAddress: 0.0.0.0
metricsBindAddress: 0.0.0.0:10249
clientConnection:
kubeconfig: /usr/local/kubernetes/kubernetes/cfg/kube-proxy.kubeconfig
hostnameOverride: k8s-master-01
clusterCIDR: 10.0.0.0/24
EOF
3)生成kube-proxy.kubeconfig 文件
cd /root && KUBE_APISERVER="https://192.168.5.3:6443"
kubectl config set-cluster kubernetes \
--certificate-authority=/usr/local/kubernetes/kubernetes/ssl/ca.pem \
--embed-certs=true \
--server=${KUBE_APISERVER} \
--kubeconfig=kube-proxy.kubeconfig
kubectl config set-credentials kube-proxy \
--client-certificate=/usr/local/kubernetes/kubernetes/ssl/kube-proxy.pem \
--client-key=/usr/local/kubernetes/kubernetes/ssl/kube-proxy-key.pem \
--embed-certs=true \
--kubeconfig=kube-proxy.kubeconfig
kubectl config set-context default \
--cluster=kubernetes \
--user=kube-proxy \
--kubeconfig=kube-proxy.kubeconfig
kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig
mv kube-proxy.kubeconfig /usr/local/kubernetes/kubernetes/cfg/
4)systemd 管理kube-proxy
cat <<EOF> /usr/lib/systemd/system/kube-proxy.service
[Unit]
Description=Kubernetes Proxy
After=network.target
[Service]
EnvironmentFile=/usr/local/kubernetes/kubernetes/cfg/kube-proxy.conf
ExecStart=/usr/local/kubernetes/kubernetes/bin/kube-proxy $KUBE_PROXY_OPTS
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
5)启动并设置开机启动
systemctl daemon-reload && systemctl restart kube-proxy && systemctl enable kube-proxy && systemctl status kube-proxy
6.5 部署CNI 网络(全部节点)
准备好CNI 二进制文件:
wget https://github.com/containernetworking/plugins/releases/download/v0.8.7/cni-plugins-linux-amd64-v0.8.7.tgz
解压二进制包并移动到默认工作目录:
mkdir -p /opt/cni/bin && tar zxvf cni-plugins-linux-amd64-v0.8.7.tgz -C /opt/cni/bin
注意:如果想要修改CNI网络路径,则需要修改kube-flannel .yml文件中对应的路径
6.6 部署flannel网络(Master节点操作)
cd /root && wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
# 默认镜像地址无法访问,修改为docker hub 镜像仓库。
sed -i -r "s#quay.io/coreos/flannel:.*-amd64#lizhenliang/flannel:v0.12.0-amd64#g" kube-flannel.yml
kubectl apply -f kube-flannel.ymlkubectl get pods -n kube-system
kubectl get node
6.6 授权apiserver 访问kubelet
cat <<EOF> apiserver-to-kubelet-rbac.yaml
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
7 部署其他扩展组件
7.1 部署dashboard
1)官网下载yml文件
wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta8/aio/deploy/recommended.yaml
2)暴露端口:
默认Dashboard只能集群内部访问,修改Service为NodePort类型,暴露到外部:
vim recommended.yaml
---
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
spec:
ports:
- port: 443
targetPort: 8443
nodePort: 30001
type: NodePort
selector:
k8s-app: kubernetes-dashboard
---
kubectl apply -f recommended.yaml
3)查看pod情况
kubectl get pods,svc -n kubernetes-dashboard
4)绑定集群角色
kubectl create serviceaccount dashboard-admin -n kube-systemkubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-adminkubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')
5)token登录Dashboard
https://NodeIP:30001
如果出现这种情况,在当前页面用键盘输入 thisisunsafe
,不是在地址栏输入,就直接敲键盘就行了,页面即会自动刷新进入网页。
7.2 部署ingress-nginx
1)下载部署
# 创建文件夹
mkdir ingress-controller && cd ingress-controller/
# 获取ingress-nginx
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/mandatory.yaml
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/provider/baremetal/service-nodeport.yaml
kubectl apply -f ./
2)查看pod情况
kubectl get pod,svc -n ingress-nginx
7.3 部署helm
1)下载部署
wget https://get.helm.sh/helm-v3.2.1-linux-amd64.tar.gztar zxvf helm-v3.2.1-linux-amd64.tar.gzmv linux-amd64/helm /usr/bin/
2)查看pod情况
helm version
# 添加国内存储库
helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
helm repo update
helm repo list
7.4 安装NFS动态存储卷
1)在一台服务器上安装nfs服务
yum -y install nfs-utils
2)准备一个共享目录
mkdir -pv /root/data/nfs
3)将共享目录以读写权限暴露给192.168.5.0/24网段中的所有主机
cat <<EOF> /etc/exports/root/data/nfs 192.168.5.0/24(rw,no_root_squash)EOF
4)启动nfs服务
systemctl restart nfs
5)集群其他节点安装nfs服务
yum -y install nfs-utils
6)查看共享存储信息
showmount -e 192.168.5.3
7)添加choerodon chart仓库
helm repo add c7n https://openchart.choerodon.com.cn/choerodon/c7n/helm repo update
8)安装nfs-client-provisioner
export KUBERNETES_MASTER=http://127.0.0.1:8080
helm upgrade --install nfs-client-provisioner c7n/nfs-client-provisioner \
--set rbac.create=true \
--set persistence.enabled=true \
--set storageClass.name=nfs-provisioner \
--set persistence.nfsServer=192.168.5.3 \
--set persistence.nfsPath=/root/data/nfs \
--version 0.1.1 \
--namespace kube-system
9)查看服务是否启动
kubectl get sc
kubectl get pods -n kube-system
cat <<EOF> write-pod.yaml
kind: Pod
apiVersion: v1
metadata:
name: write-pod
spec:
containers:
- name: write-pod
image: busybox
command:
- "/bin/sh"
args:
- "-c"
- "touch /mnt/SUCCESS && exit 0 || exit 1"
volumeMounts:
- name: nfs-pvc
mountPath: "/mnt"
restartPolicy: "Never"
volumes:
- name: nfs-pvc
persistentVolumeClaim:
claimName: myclaim
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: myclaim
spec:
accessModes:
- ReadWriteOnce
storageClassName: nfs-provisioner
resources:
requests:
storage: 1Mi
EOF
kubectl apply -f write-pod.yaml
kubectl get po
清除测试
kubectl delete -f write-pod.yaml