二进制安装K8s之部署kube-apiserver
一、生成 kube-apiserver 证书
1、自签证书颁发机构(CA)
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
- 生成证书ca-key.pem 、 ca.pem:
cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
cp /data/docker/TSL/k8s/*.pem /data/k8s/ssl/
2、使用自签CA签发kube-apiserver HTTPS证书
#ip地址包含k8s集群所有ip,LBIP以及负载均衡的虚拟IP建议多写
#文件hosts字段中IP为所有Master/LB/VIP IP,一个都不能少!为了方便后期扩容可以多写几个预留的IP。
cat > server-csr.json <<EOF
{
"CN": "kubernetes",
"hosts": [
"127.0.0.1",
"10.0.0.1",
"192.168.100.170",
"192.168.100.171",
"192.168.100.172",
"192.168.100.173",
"192.168.100.174",
"192.168.100.175",
"192.168.100.176",
"192.168.100.177",
"192.168.100.178",
"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
cp ./server*.pem /data/k8s/ssl/
二、下载k8s 二进制包
1、下载k8s
github 地址
https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG
注:打开链接你会发现里面有很多包,下载一个server包就够了,包含了Master和Worker Node二进制文件。
1、解压
tar -zxvf kubernetes-server-linux-amd64.tar.gz
#复制二进制文件
cp kube-apiserver kube-controller-manager kube-scheduler kubelet kube-proxy /data/k8s/bin/
cp kubectl /usr/local/bin/
2、部署kube-apiserver,创建配置文件
cat > /data/k8s/config/kube-apiserver.conf << EOF
KUBE_APISERVER_OPTS="--logtostderr=false \\
--v=2 \\
--log-dir=/data/k8s/logs \\
--etcd-servers=https://192.168.100.170:2379,https://192.168.100.171:2379,https://192.168.100.172:2379 \\
--bind-address=192.168.100.170 \\
--secure-port=6443 \\
--advertise-address=192.168.100.170 \\
--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=/data/k8s/config/token.csv \\
--service-node-port-range=30000-32767 \\
--kubelet-client-certificate=/data/k8s/ssl/server.pem \\
--kubelet-client-key=/data/k8s/ssl/server-key.pem \\
--tls-cert-file=/data/k8s/ssl/server.pem \\
--tls-private-key-file=/data/k8s/ssl/server-key.pem \\
--client-ca-file=/data/k8s/ssl/ca.pem \\
--service-account-key-file=/data/k8s/ssl/ca-key.pem \\
--service-account-issuer=api \\
--service-account-signing-key-file=/data/k8s/ssl/server-key.pem \\
--etcd-cafile=/data/etcd/ssl/ca.pem \\
--etcd-certfile=/data/etcd/ssl/server.pem \\
--etcd-keyfile=/data/etcd/ssl/server-key.pem \\
--requestheader-allowed-names=kubernetes \\
--requestheader-extra-headers-prefix=X-Remote-Extra- \\
--requestheader-group-headers=X-Remote-Group \\
--requestheader-username-headers=X-Remote-User \\
--proxy-client-cert-file=/data/k8s/ssl/server.pem \\
--proxy-client-key-file=/data/k8s/ssl/server-key.pem \\
--audit-log-maxage=30 \\
--audit-log-maxbackup=3 \\
--audit-log-maxsize=100 \\
--audit-log-path=/data/k8s/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:审计日志
4、启用 TLS Bootstrapping 机制
TLS Bootstraping:Master apiserver启用TLS认证后,Node节点kubelet和kube-proxy要与kube-apiserver进行通信,必须使用CA签发的有效证书才可以,当Node节点很多时,这种客户端证书颁发需要大量工作,同样也会增加集群扩展复杂度。为了简化流程,Kubernetes引入了TLS bootstraping机制来自动颁发客户端证书,kubelet会以一个低权限用户自动向apiserver申请证书,kubelet的证书由apiserver动态签署。所以强烈建议在Node上使用这种方式,目前主要用于kubelet,kube-proxy还是由我们统一颁发一个证书。
TLS bootstraping 工作流程:
创建上述配置文件中token文件:
- 生成随机token
格式:token,用户名,UID,用户组
head -c 16 /dev/urandom | od -An -t x | tr -d ' '
a752d78ab37a51fa7c38ad94346317ac
- 创建上述配置文件中token文件:
下面文件中的token,替换成上面生成的token
cat >/data/k8s/config/token.csv << EOF
a752d78ab37a51fa7c38ad94346317ac,kubelet-bootstrap,10001,"system:node-bootstrapper"
EOF
5、systemd管理apiserver
注意修改里面的路径比如:kube-apiserver.conf 文件路径
cat > /usr/lib/systemd/system/kube-apiserver.service << EOF
[Unit]
Description=Kubernetes API Server
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=/data/k8s/config/kube-apiserver.conf
ExecStart=/data/k8s/bin/kube-apiserver \$KUBE_APISERVER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
6、启动并设置开机启动
systemctl daemon-reload
systemctl start kube-apiserver
systemctl enable kube-apiserver