K8S 二进制集群部署--------单master集群
K8S 二进制集群部署--------单master集群
一、集群环境
在上篇博客介绍过了,我的搭建部署也是在上一篇的基础上做的。
二、部署master节点组件
在 Master 上要部署以下三大核心组件:
kube-apiserver:是集群的统一入口,各组件协调者,所有对象资源的增删改查和监听操作都交给 APIServer 处理后再提交给 Etcd 存储;
kube-controller-manager:处理群集中常规后台任务,一个资源对应一个控制器,而 controller-manager 就是负责管理这些控制器的;
kube-scheduler:根据调度算法为新创建的 Pod 选择一个 Node 节点,可以任意部署,可以部署在同一个节点上,也可以部署在不同节点上。
操作流程:配置文件 -----> systemd 管理组件 -----> 启动
先把master压缩包放在k8s目录下
[ k8s]# unzip master.zip
解压出来的三个组件脚本我自己编写的,内容在下面操作中展示。
[ k8s]# chmod +x controller-manager.sh #给controll脚本加上权限 [ k8s]# mkdir -p /opt/kubernetes/{cfg,ssl,bin}
2.1、部署 apiserver
2.1.1、制作api-server证书
1、创建api-server证书目录
[ k8s]# mkdir k8s-cert [ k8s]# cd k8s-cert/
2、编写证书生成脚本
[ k8s-cert]# vim k8s-cert.sh # 编写ca证书的配置文件 cat > ca-config.json <<EOF { "signing": { "default": { "expiry": "87600h" }, "profiles": { "kubernetes": { "expiry": "87600h", "usages": [ "signing", "key encipherment", "server auth", "client auth" ] } } } } EOF # 编写ca 签名证书文件 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.pem、ca-key.pem文件 cfssl gencert -initca ca-csr.json | cfssljson -bare ca - #编写apiserver的签名证书 注意:master2节点和LB节点的IP地址是 为了之后的多master节点部署添加的 cat > server-csr.json <<EOF { "CN": "kubernetes", "hosts": [ "10.0.0.1", "127.0.0.1", "192.168.66.130", //master1节点 "192.168.66.131", //master2节点 "192.168.66.100", //vip地址 "192.168.66.134", //负载调度器 (master) "192.168.66.139", //负载调度器 (backup) "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 #使用之前的文件生成server证书 cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare server #编写用户证书 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 cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes admin-csr.json | cfssljson -bare admin # 编写 kube-proxy 证书 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-proxy
3、执行脚本,并把通信证书拷贝到 /opt/kubernetes/ssl目录下
[ k8s-cert]# bash k8s-cert.sh [ k8s-cert]# cp ca*pem server*pem /opt/kubernetes/ssl/
2.1.2、解压二进制文件
1、解压k8s安装包
[ k8s]# tar zxvf kubernetes-server-linux-amd64.tar.gz
2、复制关键的命令文件到/opt/kubernetes/bin/
[ k8s]# cd /root/k8s/kubernetes/server/bin [ bin]# cp kube-apiserver kubectl kube-controller-manager kube-scheduler /opt/kubernetes/bin/
2.1.3、制作token令牌
//使用下面的 head命令 随机生成序列号 [ k8s]# head -c 16 /dev/urandom | od -An -t x | tr -d ‘ ‘ 5215984ab589158ffaf315249da4c0c9 #复制序列号写入 token.csv 中 [ k8s]# vim /opt/kubernetes/cfg/token.csv #写入内容:序列号,用户名,id,角色 5215984ab589158ffaf315249da4c0c9,kubelet-bootstrap,10001,"system:kubelet-bootstrap"
2.1.4、开启 apiserver
1、二进制文件、token令牌、证书都准备好了,开启apiserver,指向ETCD集群,把信息保存到ETCD中。
[ k8s]# bash apiserver.sh 192.168.66.130 https://192.168.66.130:2379,https://192.168.66.132:2379,https://192.168.66.133:2379
apiserver.sh脚本
#!/bin/bash MASTER_ADDRESS=$1 ETCD_SERVERS=$2 #在k8s工作目录里生成kube-apiserver 配置文件 cat <<EOF >/opt/kubernetes/cfg/kube-apiserver KUBE_APISERVER_OPTS="--logtostderr=true \--v=4 \--etcd-servers=${ETCD_SERVERS} \--bind-address=${MASTER_ADDRESS} \--secure-port=6443 \--advertise-address=${MASTER_ADDRESS} \--allow-privileged=true \--service-cluster-ip-range=10.0.0.0/24 \--enable-admission-plugins=NamespaceLifecycle,LimitRanger,ServiceAccount,ResourceQuota,NodeRestriction \--authorization-mode=RBAC,Node \--kubelet-https=true \--enable-bootstrap-token-auth \--token-auth-file=/opt/kubernetes/cfg/token.csv \--service-node-port-range=30000-50000 \--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" EOF #生成启动脚本 cat <<EOF >/usr/lib/systemd/system/kube-apiserver.service [Unit] Description=Kubernetes API Server Documentation=https://github.com/kubernetes/kubernetes [Service] EnvironmentFile=-/opt/kubernetes/cfg/kube-apiserver ExecStart=/opt/kubernetes/bin/kube-apiserver \$KUBE_APISERVER_OPTS Restart=on-failure [Install] WantedBy=multi-user.target EOF #启动apiserver组件 systemctl daemon-reload systemctl enable kube-apiserver systemctl restart kube-apiserver
2、检查进程是否启动成功。
[ k8s]# ps aux | grep kube
3、查看监听的https端口
[ k8s]# netstat -natp | grep 6443 [ k8s]# netstat -natp | grep 8080
2.2、部署scheduler
scheduler.sh脚本内容:
[ k8s]# vim scheduler.sh #!/bin/bash MASTER_ADDRESS=$1 cat <<EOF >/opt/kubernetes/cfg/kube-scheduler KUBE_SCHEDULER_OPTS="--logtostderr=true \--v=4 \--master=${MASTER_ADDRESS}:8080 \--leader-elect" EOF cat <<EOF >/usr/lib/systemd/system/kube-scheduler.service [Unit] Description=Kubernetes Scheduler Documentation=https://github.com/kubernetes/kubernetes [Service] EnvironmentFile=-/opt/kubernetes/cfg/kube-scheduler ExecStart=/opt/kubernetes/bin/kube-scheduler \$KUBE_SCHEDULER_OPTS Restart=on-failure [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl enable kube-scheduler systemctl restart kube-scheduler
启动scheduler服务
[ k8s]# ./scheduler.sh 127.0.0.1
2.3、部署controller-manager
controller-manager.sh脚本编写内容:
[ k8s]# vim controller-manager.sh #!/bin/bash MASTER_ADDRESS=$1 cat <<EOF >/opt/kubernetes/cfg/kube-controller-manager KUBE_CONTROLLER_MANAGER_OPTS="--logtostderr=true \--v=4 \--master=${MASTER_ADDRESS}:8080 \--leader-elect=true \--address=127.0.0.1 \--service-cluster-ip-range=10.0.0.0/24 \--cluster-name=kubernetes \--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 cat <<EOF >/usr/lib/systemd/system/kube-controller-manager.service [Unit] Description=Kubernetes Controller Manager Documentation=https://github.com/kubernetes/kubernetes [Service] EnvironmentFile=-/opt/kubernetes/cfg/kube-controller-manager ExecStart=/opt/kubernetes/bin/kube-controller-manager \$KUBE_CONTROLLER_MANAGER_OPTS Restart=on-failure [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl enable kube-controller-manager systemctl restart kube-controller-manager
启动controller-manager
[ k8s]# ./controller-manager.sh 127.0.0.1
查看master节点状态
[ k8s]# /opt/kubernetes/bin/kubectl get cs
三、部署node节点组件
在 node上要部署以下三大核心组件:
kubelet:是master在node节点上的agent,可以管理本机运行容器的生命周期,例如创建容器、Pod挂载数据卷、下载secret、获取容器和节点状态等工作,kubelet 将每个 Pod转换成一组容器。
kube-proxy:在 node节点上实现 Pod网络代理,维护网络规划和四层负载均衡工作。
docker:容器(我们前面已经安装好了)
3.1、部署kubeconfig
- 在master节点上进行操作
1、在master上,把 kubelet、kube-proxy 拷贝到 两个node节点上去
[ k8s]# cd kubernetes/server/bin/ [ bin]# scp kubelet kube-proxy :/opt/kubernetes/bin/ [ bin]# scp kubelet kube-proxy :/opt/kubernetes/bin/
2、编写kubeconfig脚本
[ k8s]# mkdir kubeconfig [ k8s]# cd kubeconfig/ //获取token信息 [ kubeconfig]# cat /opt/kubernetes/cfg/token.csv 5215984ab589158ffaf315249da4c0c9,kubelet-bootstrap,10001,"system:kubelet-bootstrap" //拷贝kubeconfig.sh文件进行重命名 [ kubeconfig]# mv kubeconfig.sh kubeconfig [ kubeconfig]# vim kubeconfig # 创建 TLS Bootstrapping Token #BOOTSTRAP_TOKEN=$(head -c 16 /dev/urandom | od -An -t x | tr -d ‘ ‘) BOOTSTRAP_TOKEN=0fb61c46f8991b718eb38d27b605b008 cat > token.csv <<EOF ${BOOTSTRAP_TOKEN},kubelet-bootstrap,10001,"system:kubelet-bootstrap" EOF #---------------------- APISERVER=$1 SSL_DIR=$2 # 创建kubelet bootstrapping kubeconfig export KUBE_APISERVER="https://$APISERVER:6443" # 设置集群参数 kubectl config set-cluster kubernetes --certificate-authority=$SSL_DIR/ca.pem --embed-certs=true --server=${KUBE_APISERVER} --kubeconfig=bootstrap.kubeconfig # 设置客户端认证参数 kubectl config set-credentials kubelet-bootstrap --token=${BOOTSTRAP_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 #---------------------- # 创建kube-proxy kubeconfig文件 kubectl config set-cluster kubernetes --certificate-authority=$SSL_DIR/ca.pem --embed-certs=true --server=${KUBE_APISERVER} --kubeconfig=kube-proxy.kubeconfig kubectl config set-credentials kube-proxy --client-certificate=$SSL_DIR/kube-proxy.pem --client-key=$SSL_DIR/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
3、设置环境变量,使可以在任意目录下识别kubectl命令
[ kubeconfig]# export PATH=$PATH:/opt/kubernetes/bin/
4、检查健康状态
[ kubeconfig]# kubectl get cs
5、运行kubeconfig脚本生成配置文件
[ kubeconfig]# bash kubeconfig 192.168.66.130 /root/k8s/k8s-cert/
将生成的配置文件传给两个node节点
[ kubeconfig]# scp bootstrap.kubeconfig kube-proxy.kubeconfig :/opt/kubernetes/cfg/ [ kubeconfig]# scp bootstrap.kubeconfig kube-proxy.kubeconfig :/opt/kubernetes/cfg/
6、创建 bootstrap角色赋予权限,用于连接 apiserver请求签名(重要)
[ kubeconfig]# kubectl create clusterrolebinding kubelet-bootstrap --clusterrole=system:node-bootstrapper --user=kubelet-bootstrap
3.2、部署node1的kubelet组件
在node1节点上操作**
//nod01节点操作(复制node.zip到/root目录下再解压) [ ~]# ls anaconda-ks.cfg flannel-v0.10.0-linux-amd64.tar.gz node.zip 公共 视频 文档 音乐 flannel.sh initial-setup-ks.cfg README.md 模板 图片 下载 桌面 //解压node.zip,获得kubelet.sh proxy.sh [ ~]# unzip node.zip
1、编写kubelet.sh脚本
#!/bin/bash NODE_ADDRESS=$1 DNS_SERVER_IP=${2:-"10.0.0.2"} cat <<EOF >/opt/kubernetes/cfg/kubelet KUBELET_OPTS="--logtostderr=true \--v=4 \--hostname-override=${NODE_ADDRESS} \--kubeconfig=/opt/kubernetes/cfg/kubelet.kubeconfig \--bootstrap-kubeconfig=/opt/kubernetes/cfg/bootstrap.kubeconfig \--config=/opt/kubernetes/cfg/kubelet.config \--cert-dir=/opt/kubernetes/ssl \--pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0" EOF cat <<EOF >/opt/kubernetes/cfg/kubelet.config kind: KubeletConfiguration apiVersion: kubelet.config.k8s.io/v1beta1 address: ${NODE_ADDRESS} port: 10250 readOnlyPort: 10255 cgroupDriver: cgroupfs clusterDNS: - ${DNS_SERVER_IP} clusterDomain: cluster.local. failSwapOn: false authentication: anonymous: enabled: true EOF cat <<EOF >/usr/lib/systemd/system/kubelet.service [Unit] Description=Kubernetes Kubelet After=docker.service Requires=docker.service [Service] EnvironmentFile=/opt/kubernetes/cfg/kubelet ExecStart=/opt/kubernetes/bin/kubelet \$KUBELET_OPTS Restart=on-failure KillMode=process [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl enable kubelet systemctl restart kubelet
2、加执行权限
[ ~]# chmod +x /root/kubelet.sh
3、运行脚本启动kubelet,向master集群发送请求
[ ~]# bash kubelet.sh 192.168.66.132
4、检查 kubelet 服务启动
[ ~]# ps aux | grep kube
5、在master节点上,检查 node01 节点的请求
//用下面这个命令检查得到node1请求,并复制node1请求的名字 [ kubeconfig]# kubectl get csr
6、master设置同意连接请求,颁发证书
//同意请求的命令格式:kubectl certificate approve node1请求的NAME [ kubeconfig]# kubectl certificate approve node-csr-UWHx5twdszUBAKSIajgOat46vnZQ2IpZa4-_E_FkB4w [ kubeconfig]# kubectl get csr #发现状态变成允许
7、查看群集节点,成功加入 node01 节点
[ kubeconfig]# kubectl get node
3.3、部署node1 的kube-proxy组件
1、编写proxy.sh 脚本
#!/bin/bash NODE_ADDRESS=$1 cat <<EOF >/opt/kubernetes/cfg/kube-proxy KUBE_PROXY_OPTS="--logtostderr=true \--v=4 \--hostname-override=${NODE_ADDRESS} \--cluster-cidr=10.0.0.0/24 \--proxy-mode=ipvs \--kubeconfig=/opt/kubernetes/cfg/kube-proxy.kubeconfig" EOF cat <<EOF >/usr/lib/systemd/system/kube-proxy.service [Unit] Description=Kubernetes Proxy After=network.target [Service] EnvironmentFile=-/opt/kubernetes/cfg/kube-proxy ExecStart=/opt/kubernetes/bin/kube-proxy \$KUBE_PROXY_OPTS Restart=on-failure [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl enable kube-proxy systemctl restart kube-proxy
2、加执行权限
[ ~]# chmod +x /root/proxy.sh
3、在 node1节点上操作,启动 proxy服务,并查看状态是否正常
[ ~]# bash proxy.sh 192.168.66.132 [ ~]# systemctl status kube-proxy.service
3.4、部署node2节点的 kubelet 和 kube-proxy 服务
1、为了提高效率,我们将 node01上现成的 /opt/kubernetes目录复制到其他节点进行修改。
[ ~]# scp -r /opt/kubernetes/ :/opt/
再把kubelet,kube-proxy的service文件拷贝到node2中
scp /usr/lib/systemd/system/{kubelet,kube-proxy}.service :/usr/lib/systemd/system/
2、接下来在 node02 节点上的操作
首先,先删除复制过来的证书,因为待会 node02 会自行申请属于自己的证书
[ ~]# cd /opt/kubernetes/ssl/ [ ssl]# rm -rf *
修改配置文件 kubelet 、kubelet.config 、kube-proxy(三个配置文件)中node1的IP地址
[ ssl]# cd /opt/kubernetes/cfg/ [ cfg]# vim kubelet
[ cfg]# vim kubelet.config
[ cfg]# vim kube-proxy
3、启动node2节点的 kubelet、kube-proxy 服务,并设置开机自启
[ cfg]# systemctl start kubelet.service [ cfg]# systemctl enable kubelet.service [ cfg]# systemctl start kube-proxy.service [ cfg]# systemctl enable kube-proxy.service
4、在master上查看node2节点的请求
[ kubeconfig]# kubectl get csr
接下来和刚刚一样,授权许可加入集群
[ kubeconfig]# kubectl certificate approve node-csr-ygkcxiRs2_EJw6A8N8m2SHnXOqU_I1Bs1BrfP21GzfM
四、验证集群
查看群集中的节点
[ ~]# kubectl get node
到这里,单master节点的K8s集群部署完成了。
相关推荐
kunyus 2020-10-28
hubanbei00的家园 2020-10-25
btqszl 2020-10-21
shurenyun 2020-08-19
CurrentJ 2020-08-18
88427810 2020-11-02
XiaoMuFireAnt 2020-09-02
hegaoye0 2020-08-18
WFMoonlight 2020-08-17
xiunai 2020-08-02
技术积累LZ 2020-07-28
lilygg 2020-07-22
akcsdno 2020-07-21
winc 2020-07-05
Dannyvon 2020-07-04
###host字段指定授权使用该证书的etcd节点IP或子网列表,需要将etcd集群的3个节点都添加其中。cp etcd-v3.3.13-linux-amd64/etcd* /opt/k8s/bin/
xiunai 2020-07-04
hevenue 2020-06-28
xiunai 2020-06-28
ajuan 2020-06-25