社区app软件开发如何提升用户互动与品牌忠诚度
811
2022-11-30
Kubernetes 部署一套高可用集群(二进制,v1.18)上
生产环境K8S平台规划服务器硬件配置推荐官方提供三种部署方式为Etcd和APISever自签SSL证书Etcd数据库集群署部署Master组件部署Node组件部署K8S集群网络部署集群内部DNS解析服务(CoreDNS)
Kubernetes集群架构与组件
单Master架构图:
etcd是独立的数据库,非k8s组件,所以etcd数据库可以部署在任何的地方,只要k8s能够连接到etcd即可。单master架构的话,如果master挂掉了,集群管理功能就会遭到破坏,就不能去管理应用,部署应用了。但是已经部署的应用还是可以正常工作的。
多Master架构图:
LB会负载到多个apiserver。node由原来的直接连接apiserver改为连接LB了,所有的请求都会分摊到某个节点上面。
Master组件
kube-apiserver :Kubernetes API,集群的统一入口,各组件协调者,以RESTful API提供接口服务,所有对象资源的增删改查和监听操作都交给APIServer处理后再提交给Etcd存储。kube-controller-manager :处理集群中常规后台任务,一个资源对应一个控制器,而ControllerManager就是负责管理这些控制器的。kube-scheduler:根据调度算法为新创建的Pod选择一个Node节点,可以任意部署, 可以部署在同一个节点上,也可以部署在不同的节点上。etcd: 分布式键值存储系统。用于保存集群状态数据,比如Pod、Service 等对象信息。
生产环境在部署集群的时候,注意给集群预留一些资源,不要一上应用将集群全部的资源全部用光。
kubelet
kubelet是Master在Node节点上的Agent,管理本机运行容器的生命周期,比如创建容器、Pod挂载数据卷、 -secret 、获取容器和节点状态等工作。kubelet将每个Pod转换成一组容器。
kube-proxy
在Node节点上实现Pod网络代理,维护网络规则和四层负载均衡工作。
docker或rocket
容器引擎,运行容器。
生产环境可部署Kubernetes集群的两种方式
目前生产部署Kubernetes集群主要有两种方式:
kubeadm Kubeadm是一个K8s部署工具,提供kubeadm init和kubeadm join,用于快速部署Kubernetes集群。官方地址: 从github-发行版的二进制包,手动部署每个组件,组成Kubernetes集群。-:操作系统初始化配置
# 关闭防火墙,内网安全系数高,也可以不关闭,但是要注意相关k8s的端口要开放systemctl stop firewalldsystemctl disable firewalld# 关闭selinuxsed -i 's/enforcing/disabled/' /etc/selinux/config # 永久setenforce 0 # 临时# 关闭swapswapoff -a # 临时sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久# 根据规划设置主机名hostnamectl set-hostname
二、部署Etcd集群
Etcd 是一个分布式键值存储系统,Kubernetes使用Etcd进行数据存储,所以先准备一个Etcd数据库,为解决Etcd单点故障,应采用集群方式部署,这里使用3台组建集群,可容忍1台机器故障,当然,你也可以使用5台组建集群,可容忍2台机器故障。
节点名称 | IP |
etcd-1 | 192.168.159.3 |
etcd-2 | 192.168.159.4 |
etcd-3 | 192.168.159.5 |
注:为了节省机器,这里与K8s节点机器复用。也可以独立于k8s集群之外部署,只要apiserver能连接到就行。
2.1 准备cfssl证书生成工具
集群当中通信都是基于HTTPS去做的,所以需要有证书然后配置到服务当中,然后使用HTTPS去交互。
这里有两套证书,一套是etcd,一套是k8s(最重要的就是apiserver,不仅仅每个node要和apiserver交互,apiserver还需要由管理员去访问的,所以这套证书是为apiserver去颁发的)。
证书分为两种 自签(一般用在内部服务之间) 权威机构(需要购买),不管怎么颁发,总得有个根证书的,需要根证书去做颁发证书。
证书一般会颁发两个,一个是是crt(数字证书),一个是key(私钥)。
cfssl是一个开源的证书管理工具,使用json文件生成证书,相比openssl更方便使用。
找任意一台服务器操作,这里用Master节点。
wget +x cfssl_linux-amd64 cfssljson_linux-amd64 cfssl-certinfo_linux-amd64mv cfssl_linux-amd64 /usr/local/bin/cfsslmv cfssljson_linux-amd64 /usr/local/bin/cfssljsonmv cfssl-certinfo_linux-amd64 /usr/bin/cfssl-certinfo
2.2 生成Etcd证书
1. 自签证书颁发机构(CA)
创建工作目录:(两套证书,所以建立两个目录)
mkdir -p ~/TLS/{etcd,k8s}cd TLS/etcd
自签CA:
cat > ca-config.json << EOF{ "signing": { "default": { "expiry": "87600h" }, "profiles": { "{ "expiry": "87600h", "usages": [ "signing", "key encipherment", "server auth", "client auth" ] } } }}EOFcat > ca-csr.json << EOF{ "CN": "etcd CA", "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "L": "Beijing", "ST": "Beijing" } ]}EOF
生成证书:(CA数字证书,CA私钥,可以拿着这个CA去颁发不同的域名证书)
cfssl gencert -initca ca-csr.json | cfssljson -bare ca -ls *pemca-key.pem ca.pem[root@master etcd]# cfssl gencert -initca ca-csr.json | cfssljson -bare ca -2022/03/01 20:07:42 [INFO] generating a new CA key and certificate from CSR2022/03/01 20:07:42 [INFO] generate received request2022/03/01 20:07:42 [INFO] received CSR2022/03/01 20:07:42 [INFO] generating key: rsa-20482022/03/01 20:07:42 [INFO] encoded CSR2022/03/01 20:07:42 [INFO] signed certificate with serial number 619358095601261026676993554954398436962496116518
2. 使用自签CA签发Etcd HTTPS证书
创建证书申请文件:
cat > server-csr.json << EOF{ "CN": "etcd", "hosts": [ "192.168.31.71", "192.168.31.72", "192.168.31.73" ], "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "L": "BeiJing", "ST": "BeiJing" } ]}EOF
注:上述文件hosts字段中IP为所有etcd节点的集群内部通信IP,一个都不能少!为了方便后期扩容可以多写几个预留的IP。
生成证书:(-bare server生成以server开头的证书名字)
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=server-csr.json | cfssljson -bare serverls server*pemserver-key.pem server.pem[root@master etcd]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=server-csr.json | cfssljson -bare server2022/03/01 20:10:23 [INFO] generate received request2022/03/01 20:10:23 [INFO] received CSR2022/03/01 20:10:23 [INFO] generating key: rsa-20482022/03/01 20:10:23 [INFO] encoded CSR2022/03/01 20:10:23 [INFO] signed certificate with serial number 473013560742757233460183577098226052037618689032022/03/01 20:10:23 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable forwebsites. For more information see the Baseline Requirements for the Issuance and Managementof Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (section 10.2.3 ("Information Requirements").
2.3 从Github-二进制文件
-: 部署Etcd集群 以下在节点1上操作,为简化操作,待会将节点1生成的所有文件拷贝到节点2和节点3.
1. 创建工作目录并解压二进制包
mkdir /opt/etcd/{bin,cfg,ssl} -ptar zxvf etcd-v3.4.9-linux-amd64.tar.gzmv etcd-v3.4.9-linux-amd64/{etcd,etcdctl} /opt/etcd/bin/rm -rf etcd-v3.4.9-linux-amd64[root@master ~]# ls /opt/etcd/bin/etcd etcdctl
2. 创建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="systemd管理etcd
cat > /usr/lib/systemd/system/etcd.service << EOF[Unit]Description=Etcd ServerAfter=network.targetAfter=network-online.targetWants=network-online.target[Service]Type=notifyEnvironmentFile=/opt/etcd/cfg/etcd.confExecStart=/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=zapRestart=on-failureLimitNOFILE=65536[Install]WantedBy=multi-user.targetEOF
上面配置里面的证书分为两部分,一部分是集群内部使用的,也就是2380端口,\--peer-key-file=/opt/etcd/ssl/server-key.pem \--peer-trusted-ca-file=/opt/etcd/ssl/ca.pem \
另外一部分证书是面向客户端的,客户端也是通过\--key-file=/opt/etcd/ssl/server-key.pem \--trusted-ca-file=/opt/etcd/ssl/ca.pem \
4. 拷贝刚才生成的证书
把刚才生成的证书拷贝到配置文件中的路径:
cp ~/TLS/etcd/ca*pem ~/TLS/etcd/server*pem /opt/etcd/ssl/[root@master ~]# ls /opt/etcd/ssl/ca-key.pem ca.pem server-key.pem server.pem
[root@master ~]# tree /opt/etcd//opt/etcd/├── bin│ ├── etcd│ └── etcdctl├── cfg│ └── etcd.conf└── ssl ├── ca-key.pem ├── ca.pem ├── server-key.pem └── server.pem
5. 启动并设置开机启动
systemctl daemon-reloadsystemctl start etcdsystemctl enable etcd
6. 将上面节点1所有生成的文件拷贝到节点2和节点3(将配置下发到其他节点)
scp -r /opt/etcd/ root@192.168.159.4:/opt/scp /usr/lib/systemd/system/etcd.service root@192.168.159.4:/usr/lib/systemd/system/scp -r /opt/etcd/ root@192.168.159.5:/opt/scp /usr/lib/systemd/system/etcd.service root@192.168.159.5:/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-3ETCD_DATA_DIR="/var/lib/etcd/default.etcd"ETCD_LISTEN_PEER_URLS=" # 修改此处为当前服务器IPETCD_LISTEN_CLIENT_URLS="# 修改此处为当前服务器IP#[Clustering]ETCD_INITIAL_ADVERTISE_PEER_URLS="# 修改此处为当前服务器IPETCD_ADVERTISE_CLIENT_URLS="# 修改此处为当前服务器IPETCD_INITIAL_CLUSTER="etcd-1=查看集群状态
这里需要指定etcd用到的CA,数字证书和私钥,指定这三个证书,也就是说别的程序或者组件想要访问etcd必须要指定这三个证书,下面就相当于客户端去访问,访问的地址为2379,使用的是/opt/etcd/bin/etcdctl --cacert=/opt/etcd/ssl/ca.pem --cert=/opt/etcd/ssl/server.pem --key=/opt/etcd/ssl/server-key.pem --endpoints="endpoint healthis healthy: successfully committed proposal: took = 8.154404msis healthy: successfully committed proposal: took = 9.044117msis healthy: successfully committed proposal: took = 10.000825ms
openssl查看证书命令
验证证书有效时间是否延长到10年openssl x509 -in /etc/kubernetes/pki/ca.crt -noout -text |grep Not显示如下,通过下面可看到ca证书有效期是10年,从2020到2030年:Not Before: Apr 22 04:09:07 2020 GMTNot After : Apr 20 04:09:07 2030 GMT显示如下,通过下面可看到etcd证书有效期是10年,从2020到2030年:Not Before: Apr 22 11:32:24 2020 GMTNot After : Apr 20 11:32:24 2030 GMT[root@master ssl]# openssl x509 -in ca.pem -noout -text | grep Not Not Before: Mar 1 12:03:00 2022 GMT Not After : Feb 28 12:03:00 2027 GMT[root@master ssl]# openssl x509 -in server.pem -noout -text | grep Not Not Before: Mar 1 12:05:00 2022 GMT Not After : Feb 27 12:05:00 2032 GMT
三、安装Docker
这次不使用yum去安装,而是使用二进制的方式去安装,二进制安装更加方便,而且是离线安装。
-:
解压二进制包
tar zxvf docker-19.03.9.tgzmv docker/* /usr/binrm -rf docker
3.2 systemd管理docker
cat > /usr/lib/systemd/system/docker.service << EOF[Unit]Description=Docker Application Container EngineDocumentation=firewalld.serviceWants=network-online.target[Service]Type=notifyExecStart=/usr/bin/dockerdExecReload=/bin/kill -s HUP $MAINPIDLimitNOFILE=infinityLimitNPROC=infinityLimitCORE=infinityTimeoutStartSec=0Delegate=yesKillMode=processRestart=on-failureStartLimitBurst=3StartLimitInterval=60s[Install]WantedBy=multi-user.targetEOF
3.3 创建配置文件
mkdir /etc/dockercat > /etc/docker/daemon.json << EOF{ "registry-mirrors": ["阿里云镜像加速器
3.4 启动并设置开机启动
systemctl daemon-reloadsystemctl start dockersystemctl enable docker
四、部署Master Node(生成证书,然后部署三个组件 )
现在要开始部署k8s了。节点分为master和worker,这里要先部署master再去部署worker。
master上面有三个组件(kube-apiserver,kube-controller-manager,kube-scheduler),我们这里要先部署apiserver组件, 因为apiserver是整个集群的入口,它启动完成之后,其他组件才可以去启动,所以它必须是先部署的。
因为是基于生成kube-apiserver证书
1. 自签证书颁发机构(CA)(这个CA和etcd的CA是独立的一套,它们之间不能相互的使用)
kube-proxy-csr.json这个请求文件是为worker node节点上面proxy组件所准备的证书,也是由apiserver的CA证书颁发的。(这里还没有,到后面可以看到这个证书,这里提一下)server-csr.json:为apiserver颁发的证书
cd TLS/k8s/cat > ca-config.json << EOF{ "signing": { "default": { "expiry": "87600h" }, "profiles": { "kubernetes": { "expiry": "87600h", "usages": [ "signing", "key encipherment", "server auth", "client auth" ] } } }}EOFcat > 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 -ls *pemca-key.pem ca.pem
2. 使用自签CA签发kube-apiserver HTTPS证书 创建证书申请文件:(这里面最重要的是hosts字段,这在证书里面添加了ip可信任)
cd TLS/k8scat > server-csr.json << EOF{ "CN": "kubernetes", "hosts": [ "10.0.0.1", "127.0.0.1", "192.168.159.3", "192.168.159.4", "192.168.159.5", "192.168.159.6", "192.168.159.7", "192.168.159.8", "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
注:上述文件hosts字段中IP为所有Master/LB/VIP IP,一个都不能少!为了方便后期扩容可以多写几个预留的IP。(lb要访问apiserver,与apiserver交互的都要写进去)
生成证书
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare serverls server*pemserver-key.pem server.pem
证书生成完之后就可以去部署master节点了
Node二进制文件。
4.3 解压二进制包(bin目录下面包含了所有的二进制包,如果要部署其他版本,替换这些二进制包即可)
mkdir -p /opt/kubernetes/{bin,cfg,ssl,logs} tar zxvf kubernetes-server-linux-amd64.tar.gzcd kubernetes/server/bincp kube-apiserver kube-scheduler kube-controller-manager /opt/kubernetes/bincp kubectl /usr/bin/
4.4 部署kube-apiserver 1. 创建配置文件
cat > /opt/kubernetes/cfg/kube-apiserver.conf << EOFKUBE_APISERVER_OPTS="--logtostderr=false \\--v=2 \\--log-dir=/opt/kubernetes/logs \\--etcd-servers=\\--bind-address=192.168.159.3 \\--secure-port=6443 \\--advertise-address=192.168.159.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=/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
注:上面两个\ \ 第一个是转义符,第二个是换行符,使用转义符是为了使用EOF保留换行符。
–logtostderr:启用日志 —v:日志等级 –log-dir:日志目录 –bind-address:监听地址 –secure-port:–advertise-address:集群通告地址
–allow-privileged:启用授权(允许创建的容器具有高权限) –enable-admission-plugins:准入控制模块 –authorization-mode:认证授权,启用RBAC授权和节点自管理
–service-cluster-ip-range:Service虚拟IP地址段,service ip就是从这个段里面去分配的–service-node-port-range:Service nodeport类型默认分配端口范围–enable-bootstrap-token-auth:启用TLS bootstrap机制(为了能够为每个node颁发证书,所以引入了这个机制也就是我要加入集群,那么帮我颁发一个证书)–token-auth-file:bootstrap token文件–kubelet-client-xxx:apiserver访问kubelet客户端证书(去访问kubelet使用什么证书去访问)–tls-xxx-file:apiserver \\--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-servers:etcd集群地址–etcd-xxxfile:连接Etcd集群证书–audit-log-xxx:审计日志
2. 拷贝刚才生成的证书 把刚才生成的证书拷贝到配置文件中的路径:
cp ~/TLS/k8s/ca*pem ~/TLS/k8s/server*pem /opt/kubernetes/ssl/
[root@master ~]# tree /opt/kubernetes//opt/kubernetes/├── bin│ ├── kube-apiserver│ ├── kube-controller-manager│ └── kube-scheduler├── cfg│ └── kube-apiserver.conf├── logs└── ssl ├── ca-key.pem ├── ca.pem ├── server-key.pem └── server.pem4 directories, 8 files
3. systemd管理apiserver
cat > /usr/lib/systemd/system/kube-apiserver.service << EOF[Unit]Description=Kubernetes API ServerDocumentation=\$KUBE_APISERVER_OPTSRestart=on-failure[Install]WantedBy=multi-user.targetEOF
4. 启动并设置开机启动
systemctl daemon-reloadsystemctl start kube-apiserversystemctl enable kube-apiserver
Mar 21 01:32:43 master systemd[1]: Started Kubernetes API Server.Mar 21 01:32:43 master systemd[1]: Starting Kubernetes API Server...Mar 21 01:32:44 master kube-apiserver[3063]: Error: invalid authentication config: open /opt/kubernetes/cfg/token.csv: no such file or dMar 21 01:32:44 master kube-apiserver[3063]: Usage:
5. 启用 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还是由我们统一颁发一个证书。(当机器越来越多的时候,手动的为kubelet颁发证书,还是比较麻烦的,应用这个机制就是为自动的为kubelet颁发证书)
TLS bootstraping 工作流程:
创建上述配置文件中token文件:
cat > /opt/kubernetes/cfg/token.csv << EOFc47ffb939f5ca36231d9e3121a252940,kubelet-bootstrap,10001,"system:node-bootstrapper"EOF
格式:token,用户名,UID,用户组 这个是系统自带的用户组,主要就是让node使用strap颁发证书,低权限用户。
node上拿着这个token c47ffb939f5ca36231d9e3121a252940 请求过来,就会以最低权限颁发证书。
token也可自行生成替换:
head -c 16 /dev/urandom | od -An -t x | tr -d ' '
6.授权kubelet-bootstrap用户允许请求证书
要给这个用户授予权限
[root@k8s-master logs]# kubectl get clusterroleNAME CREATED ATsystem:node-bootstrapper 2022-03-25T12:33:52Z
kubectl create clusterrolebinding kubelet-bootstrap \--clusterrole=system:node-bootstrapper \--user=kubelet-bootstrap
systemctl start kube-apiserver[root@master ~]# systemctl status kube-apiserver● kube-apiserver.service - Kubernetes API Server Loaded: loaded (/usr/lib/systemd/system/kube-apiserver.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2022-03-21 01:47:55 CST; 29min ago Docs: Main PID: 3243 (kube-apiserver) CGroup: /system.slice/kube-apiserver.service └─3243 /opt/kubernetes/bin/kube-apiserver --logtostderr=false --v=2 --log-dir=/opt/kubernetes/logs --etcd-servers=21 01:47:55 master systemd[1]: Started Kubernetes API Server.Mar 21 01:47:55 master systemd[1]: Starting Kubernetes API Server...Mar 21 01:48:09 master kube-apiserver[3243]: E0321 01:48:09.472454 3243 controller.go:152] Unable to remove old endpoints f...rorMsg:Hint: Some lines were ellipsized, use -l to show in full.
4.5 部署kube-controller-manager
1. 创建配置文件
cat > /opt/kubernetes/cfg/kube-controller-manager.conf << EOFKUBE_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。
[root@k8s-master logs]# netstat -tpln | grep 8080tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 2557/kube-apiserver
–leader-elect:当该组件启动多个时,自动选举(HA)–cluster-signing-cert-file/–cluster-signing-key-file:自动为kubelet颁发证书的CA,与apiserver保持一致
--cluster-signing-cert-file=/opt/kubernetes/ssl/ca.pem \\--cluster-signing-key-file=/opt/kubernetes/ssl/ca-key.pem \\
–leader-elect:当该组件启动多个时,自动选举(HA) 在做高可用的时候,在做高可用的时候其实要做的就是apiserver的高可用,controller-manager scheduler这两个组件并不需要单独的为他们做高可用,因为本身就会选举一个出来进行工作。
–master:通过本地非安全本地端口8080连接本地的apiserver ,由apiserver下发任务去完成
--bind-address=127.0.0.1" 不需要对外提供服务,只需要协助本地的apiserver
允许安装cni的插件,能够让其自动的分配ip。
--allocate-node-cidrs=true--cluster-cidr=10.244.0.0/16 //集群pod的ip段,这个要和cni的段保持一致--service-cluster-ip-range=10.0.0.0/24 //service ip范围,和apiserver配置保持一致
集群签名两个证书,node加入集群会自动的颁发kubelet证书,kubelet证书由controller-manager去帮忙颁发,controller-manager使用这两个证书为kubelet颁发证书
--cluster-signing-cert-file=/opt/kubernetes/ssl/ca.pem--cluster-signing-key-file=/opt/kubernetes/ssl/ca-key.pem
签署serviceaccount用到的证书
--root-ca-file=/opt/kubernetes/ssl/ca.pem--service-account-private-key-file=/opt/kubernetes/ssl/ca-key.pem
为每个node颁发kubelet证书有效期时间,这里设置为10年
--experimental-cluster-signing-duration=87600h0m0s"
2. systemd管理controller-manager
cat > /usr/lib/systemd/system/kube-controller-manager.service << EOF[Unit]Description=Kubernetes Controller ManagerDocumentation=\$KUBE_CONTROLLER_MANAGER_OPTSRestart=on-failure[Install]WantedBy=multi-user.targetEOF
3. 启动并设置开机启动
systemctl daemon-reloadsystemctl start kube-controller-managersystemctl enable kube-controller-manager
4.6 部署kube-scheduler
1. 创建配置文件
cat > /opt/kubernetes/cfg/kube-scheduler.conf << EOFKUBE_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)
2. systemd管理scheduler
cat > /usr/lib/systemd/system/kube-scheduler.service << EOF[Unit]Description=Kubernetes SchedulerDocumentation=\$KUBE_SCHEDULER_OPTSRestart=on-failure[Install]WantedBy=multi-user.targetEOF
3. 启动并设置开机启动
systemctl daemon-reloadsystemctl start kube-schedulersystemctl enable kube-scheduler
4. 查看集群状态
所有组件都已经启动成功,通过kubectl工具查看当前集群组件状态:
for i in $(ls /opt/kubernetes/bin/);do systemctl status $i;done
查看组件是否启动成功可以使用cs这个命令,然后通过ps查看进程是否存在,最后通过日志来定位启动错误信息。
kubectl get csNAME STATUS MESSAGE ERRORscheduler Healthy ok controller-manager Healthy ok etcd-2 Healthy {"health":"true"} etcd-1 Healthy {"health":"true"} etcd-0 Healthy {"health":"true"}
如上输出说明Master节点组件运行正常。
五、部署Worker Node
下面还是在Master Node上操作,即同时作为Worker Node,我们这先在master节点将kube-proxy和kubelet部署好,再去部署worker节点上面的kube-proxy,kubelet。
5.1 创建工作目录并拷贝二进制文件 在所有worker node创建工作目录:
[root@k8s-node1 ~]# mkdir -p /opt/kubernetes/{bin,cfg,ssl,logs} [root@k8s-node2 ~]# mkdir -p /opt/kubernetes/{bin,cfg,ssl,logs}
从master节点拷贝:
[root@k8s-master bin]# lsapiextensions-apiserver kube-apiserver.tar kubectl kube-proxy.tar mounterkubeadm kube-controller-manager kubelet kube-schedulerkube-apiserver kube-controller-manager.docker_tag kube-proxy kube-scheduler.docker_tagkube-apiserver.docker_tag kube-controller-manager.tar kube-proxy.docker_tag kube-scheduler.tar[root@k8s-master bin]# cp kubelet kube-proxy /opt/kubernetes/bin [root@k8s-master bin]# pwd/root/kubernetes/server/bin
5.2 部署kubelet 1. 创建配置文件
cat > /opt/kubernetes/cfg/kubelet.conf << EOFKUBELET_OPTS="--logtostderr=false \\--v=2 \\--log-dir=/opt/kubernetes/logs \\--hostname-override=k8s-master \\--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网络容器的镜像
2. 配置参数文件
cat > /opt/kubernetes/cfg/kubelet-config.yml << EOFkind: KubeletConfigurationapiVersion: kubelet.config.k8s.io/v1beta1address: 0.0.0.0port: 10250readOnlyPort: 10255cgroupDriver: cgroupfsclusterDNS:- 10.0.0.2clusterDomain: cluster.local failSwapOn: falseauthentication: anonymous: enabled: false webhook: cacheTTL: 2m0s enabled: true x509: clientCAFile: /opt/kubernetes/ssl/ca.pem authorization: mode: Webhook webhook: cacheAuthorizedTTL: 5m0s cacheUnauthorizedTTL: 30sevictionHard: imagefs.available: 15% memory.available: 100Mi nodefs.available: 10% nodefs.inodesFree: 5%maxOpenFiles: 1000000maxPods: 110EOF
cgroupDriver: cgroupfs 要和docker info里面显示的驱动保持一致。
3. 生成bootstrap.kubeconfig文件(为了解决,当kubelet很多的时候,为了避免手动颁发证书,引入tls机制,能够自动的为将要加入集群的node颁发证书。所有kubelet要链接apiserver都需要证书,要不然使用手签,要不然使用tls,这里使用tls,使用这个文件去连接apiserver,然后颁发证书,加入集群。)
KUBE_APISERVER="# apiserver IP:PORTTOKEN="c47ffb939f5ca36231d9e3121a252940" # 与token.csv里保持一致# 生成 kubelet bootstrap kubeconfig 配置文件kubectl config set-cluster kubernetes \ --certificate-authority=/opt/kubernetes/ssl/ca.pem \ --embed-certs=true \ --server=${KUBE_APISERVER} \ --kubeconfig=bootstrap.kubeconfigkubectl config set-credentials "kubelet-bootstrap" \ --token=${TOKEN} \ --kubeconfig=bootstrap.kubeconfigkubectl config set-context default \ --cluster=kubernetes \ --user="kubelet-bootstrap" \ --kubeconfig=bootstrap.kubeconfigkubectl config use-context default --kubeconfig=bootstrap.kubeconfig
[root@k8s-master cfg]# cat bootstrap.kubeconfig apiVersion: v1clusters:- cluster: certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUR2akNDQXFhZ0F3SUJBZ0lVY0tqQnBLWnBOWDBvd1FMZlZYYUhHMmlaYXJrd0RRWUpLb1pJaHZjTkFRRUwKQlFBd1pURUxNQWtHQTFVRUJoTUNRMDR4RURBT0JnTlZCQWdUQjBKbGFXcHBibWN4RURBT0JnTlZCQWNUQjBKbAphV3BwYm1jeEREQUtCZ05WQkFvVEEyczRjekVQTUEwR0ExVUVDeE1HVTNsemRHVnRNUk13RVFZRFZRUURFd3ByCmRXSmxjbTVsZEdWek1CNFhEVEl5TURNeU5URXhOVGd3TUZvWERUSTNNRE15TkRFeE5UZ3dNRm93WlRFTE1Ba0cKQTFVRUJoTUNRMDR4RURBT0JnTlZCQWdUQjBKbGFXcHBibWN4RURBT0JnTlZCQWNUQjBKbGFXcHBibWN4RERBSwpCZ05WQkFvVEEyczRjekVQTUEwR0ExVUVDeE1HVTNsemRHVnRNUk13RVFZRFZRUURFd3ByZFdKbGNtNWxkR1Z6Ck1JSUJJakFOQmdrcWhraUc5dzBCQVFFRkFBT0NBUThBTUlJQkNnS0NBUUVBeUNRUlZTWmhHUnJaNEV6TjZVbGQKNE9nVFRGVDhjNU95MW1ka24wbVgxYmxDdFRGckNBZ0VQOGdyRTZGVWI1T0k5SU1na3JlUVVvSlVoTGtrVVBIaApURnBzcXA0cmgyVUlKVjJFZ3FPKzNpbUc4NWJtdjBPNE5hb3B2cThCQXZVREZKdWcxOHJYK2ZYeEx5VFE5V0tRCkpmUWNhVmQ4V2FYbWFrMHJhU0JEeDhtNUZpdjEvekV1YmlEZmVCUTJ0NFRvQllkd1k0MmpyN1k3VW95ZVExZlgKTndxUUpjTVFsS1h0c0ZwZGE5T2pycE5PcWkrZHBISXJXYkVBZGhYTkNJZy9odzBuNFJ4OTRFSU0yUVhTWUJOZApMZHpqamU4UFZPenAyVXpnYTZJZ294L3JXRVcvVnA5MTM0YlZGcU0veTJjajB6TnZzS2xJUEVnbENMUE92cUh5ClB3SURBUUFCbzJZd1pEQU9CZ05WSFE4QkFmOEVCQU1DQVFZd0VnWURWUjBUQVFIL0JBZ3dCZ0VCL3dJQkFqQWQKQmdOVkhRNEVGZ1FVSjYzTXZVSlNPbzNDendOSWdsa3dIL2ozTnM0d0h3WURWUjBqQkJnd0ZvQVVKNjNNdlVKUwpPbzNDendOSWdsa3dIL2ozTnM0d0RRWUpLb1pJaHZjTkFRRUxCUUFEZ2dFQkFJdG1ha3M4cy9hYnhSc1Z5NHhmClhEVzlaeHNObFp2dzc2YnhxeTYvem56RW5UV0JyMHdpV0tmRXFtbk5xNE8rdnZxT1hVQklSMVBaamVYY3FYNlIKN2dlMWRmRGE3dVNqNFdYU2tONS9xbG5xMkNKVGxteVFKQUdHcTZpdnIzQ2ppQ0FKWlU4Z0FRTkg0Q2hJc0NlcwpXMy9HQjZ4a2xFdkkydm5pS2w4MEFxK0tXcXg2c0xCbmIrTlFiOW1zOStRN250ZDBveFBOK04wcnpOZ0JVUUwvCldsUUw0b1ZLWDN2VElqVzlEVkRrdVh0YjdzbzJkZ2JXM3FxRy9scUhDb1NYZ3c5bjdab2c0MmNqS0VOSUc5R1AKMnlWUmlMUHRjRVlOYStybXJ2SytncTZ0WWc2MTM4OXpIbXBxY0FvQW1kYVZQUTlsQzlSK1IweVNGNkZmenJmaQpJWG89Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K server: name: kubernetescontexts:- context: cluster: kubernetes user: kubelet-bootstrap name: defaultcurrent-context: defaultkind: Configpreferences: {}users:- name: kubelet-bootstrap user: token: c47ffb939f5ca36231d9e3121a252940
上面这个就是通过kubectl config生成的文件,指定了CA的证书,token要保持一致,因为是用token来请求的。
拷贝到配置文件路径:
cp bootstrap.kubeconfig /opt/kubernetes/cfg
当你启动kubelet,它就会拿着bootstrap.kubeconfig向apiserver发起请求,apiserver会去验证这个token是不是可信任的,是否有权限,ca证书是不是正确的,通过之后才会为其颁发证书,最后kubelet就启动成功了。
4. systemd管理kubelet
cat > /usr/lib/systemd/system/kubelet.service << EOF[Unit]Description=Kubernetes KubeletAfter=docker.service[Service]EnvironmentFile=/opt/kubernetes/cfg/kubelet.confExecStart=/opt/kubernetes/bin/kubelet \$KUBELET_OPTSRestart=on-failureLimitNOFILE=65536[Install]WantedBy=multi-user.targetEOF
5. 启动并设置开机启动
systemctl daemon-reloadsystemctl start kubeletsystemctl enable kubelet
5.3 批准kubelet证书申请并加入集群
# 查看kubelet证书请求kubectl get csrNAME AGE SIGNERNAME REQUESTOR CONDITIONnode-csr-uCEGPOIiDdlLODKts8J658HrFq9CZ--K6M4G7bjhk8A 6m3s kubernetes.io/kube-apiserver-client-kubelet kubelet-bootstrap Pending# 批准申请kubectl certificate approve node-csr-uCEGPOIiDdlLODKts8J658HrFq9CZ--K6M4G7bjhk8A# 查看节点kubectl get nodeNAME STATUS ROLES AGE VERSIONk8s-master NotReady
注:由于网络插件还没有部署,节点会没有准备就绪 NotReady
5.4 部署kube-proxy
1. 创建配置文件
cat > /opt/kubernetes/cfg/kube-proxy.conf << EOFKUBE_PROXY_OPTS="--logtostderr=false \\--v=2 \\--log-dir=/opt/kubernetes/logs \\--config=/opt/kubernetes/cfg/kube-proxy-config.yml"EOF
2. 配置参数文件
cat > /opt/kubernetes/cfg/kube-proxy-config.yml << EOFkind: KubeProxyConfigurationapiVersion: kubeproxy.config.k8s.io/v1alpha1bindAddress: 0.0.0.0metricsBindAddress: 0.0.0.0:10249clientConnection: kubeconfig: /opt/kubernetes/cfg/kube-proxy.kubeconfighostnameOverride: k8s-masterclusterCIDR: 10.0.0.0/24EOF
3. 生成kube-proxy.kubeconfig文件 生成kube-proxy证书:
# 切换工作目录cd TLS/k8s# 创建证书请求文件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# 生成证书cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxyls kube-proxy*pemkube-proxy-key.pem kube-proxy.pem
生成kubeconfig文件:
KUBE_APISERVER="config set-cluster kubernetes \ --certificate-authority=/opt/kubernetes/ssl/ca.pem \ --embed-certs=true \ --server=${KUBE_APISERVER} \ --kubeconfig=kube-proxy.kubeconfigkubectl config set-credentials kube-proxy \ --client-certificate=./kube-proxy.pem \ --client-key=./kube-proxy-key.pem \ --embed-certs=true \ --kubeconfig=kube-proxy.kubeconfigkubectl config set-context default \ --cluster=kubernetes \ --user=kube-proxy \ --kubeconfig=kube-proxy.kubeconfigkubectl config use-context default --kubeconfig=kube-proxy.kubeconfig
拷贝到配置文件指定路径:
cp kube-proxy.kubeconfig /opt/kubernetes/cfg/
4. systemd管理kube-proxy
cat > /usr/lib/systemd/system/kube-proxy.service << EOF[Unit]Description=Kubernetes ProxyAfter=network.target[Service]EnvironmentFile=/opt/kubernetes/cfg/kube-proxy.confExecStart=/opt/kubernetes/bin/kube-proxy \$KUBE_PROXY_OPTSRestart=on-failureLimitNOFILE=65536[Install]WantedBy=multi-user.targetEOF
5. 启动并设置开机启动
systemctl daemon-reloadsystemctl start kube-proxysystemctl enable kube-proxy
6. 新增加Worker Node
1. 拷贝已部署好的Node相关文件到新节点
在master节点将Worker Node涉及文件拷贝到新节点192.168.111.4/5
scp /opt/kubernetes root@192.168.159.4:/opt/scp -r /usr/lib/systemd/system/{kubelet,kube-proxy}.service root@192.168.159.4:/usr/lib/systemd/systemscp -r /opt/cni/ root@192.168.159.5:/opt/scp /opt/kubernetes/ssl/ca.pem root@192.168.159.5:/opt/kubernetes/ssl
2. 删除kubelet证书和kubeconfig文件
rm -rf /opt/kubernetes/cfg/kubelet.kubeconfig rm -rf /opt/kubernetes/ssl/kubelet*
3. 修改主机名
vi /opt/kubernetes/cfg/kubelet.conf--hostname-override=k8s-node1vi /opt/kubernetes/cfg/kube-proxy-config.ymlhostnameOverride: k8s-node1
4. 启动并设置开机启动
systemctl daemon-reloadsystemctl start kubeletsystemctl enable kubeletsystemctl start kube-proxysystemctl enable kube-proxy
5. 在Master上批准新Node kubelet证书申请
kubectl get csrNAME AGE SIGNERNAME REQUESTOR CONDITIONnode-csr-4zTjsaVSrhuyhIGqsefxzVoZDCNKei-aE2jyTP81Uro 89s kubernetes.io/kube-apiserver-client-kubelet kubelet-bootstrap Pendingkubectl certificate approve node-csr-4zTjsaVSrhuyhIGqsefxzVoZDCNKei-aE2jyTP81Uro
6. 授权apiserver访问kubelet
cat > apiserver-to-kubelet-rbac.yaml << EOFapiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata: annotations: rbac.authorization.kubernetes.io/autoupdate: "true" labels: kubernetes.io/bootstrapping: rbac-defaults name: system:kube-apiserver-to-kubeletrules: - apiGroups: - "" resources: - nodes/proxy - nodes/stats - nodes/log - nodes/spec - nodes/metrics - pods/log verbs: - "*"---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata: name: system:kube-apiserver namespace: ""roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: system:kube-apiserver-to-kubeletsubjects: - apiGroup: rbac.authorization.k8s.io kind: User name: kubernetesEOFkubectl apply -f apiserver-to-kubelet-rbac.yaml
7. 部署CNI网络
先准备好CNI二进制文件:
-:
-p /opt/cni/bintar zxvf cni-plugins-linux-amd64-v0.8.6.tgz -C /opt/cni/bin
每个node上面都得有这个可执行文件,得拷贝到每个节点
scp -r /opt/cni/ root@192.168.159.4:/opt/scp -r /opt/cni/ root@192.168.159.5:/opt/
部署CNI网络:
wget -i -r "s#quay.io/coreos/flannel:.*-amd64#lizhenliang/flannel:v0.12.0-amd64#g" kube-flannel.yml
默认镜像地址无法访问,修改为docker hub镜像仓库。
kubectl apply -f kube-flannel.ymlkubectl get pods -n kube-systemNAME READY STATUS RESTARTS AGEkube-flannel-ds-amd64-2pc95 1/1 Running 0 72s[root@k8s-master ~]# kubectl get nodeNAME STATUS ROLES AGE VERSIONk8s-master Ready
注意kube-flannel.yml这个配置要和kube-controller-manager.conf保持一致,两个网络保持一致。
net-conf.json: | { "Network": "10.244.0.0/16", "Backend": { "Type": "vxlan" } }
--cluster-cidr=10.244.0.0/16
8.部署CoreDNS
CoreDNS用于集群内部Service名称解析。
kubectl apply -f coredns.yamlkubectl get pods -n kube-system NAME READY STATUS RESTARTS AGEcoredns-5ffbfd976d-j6shb 1/1 Running 0 32skube-flannel-ds-amd64-2pc95 1/1 Running 0 38mkube-flannel-ds-amd64-7qhdx 1/1 Running 0 15mkube-flannel-ds-amd64-99cr8 1/1 Running 0 26m
DNS解析测试:
kubectl run -it --rm dns-test --image=busybox:1.28.4 shIf you don't see a command prompt, try pressing enter./ # nslookup kubernetesServer: 10.0.0.2Address 1: 10.0.0.2 kube-dns.kube-system.svc.cluster.localName: kubernetesAddress 1: 10.0.0.1 kubernetes.default.svc.cluster.local
解析没问题。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~