kubernetes(七) 二进制部署k8s(1.18.4版本)
2021-01-23 15:14
标签:自动 本地 node节点 source connect ups yml uid 谷歌浏览器 Etcd 是一个分布式键值存储系统,Kubernetes使用Etcd进行数据存储,所以先准备一个Etcd数据库, 为解决Etcd单点故障,应采用集群方式部署,这里使用3台组建集群,可容忍1台机器故障,当然,你也 可以使用5台组建集群,可容忍2台机器故障。 注:为了节省机器,这里与K8s节点机器复用。也可以独立于k8s集群之外部署,只要apiserver能 连接到就行。 注:上述文件hosts字段中IP为所有etcd节点的集群内部通信IP,一个都不能少!为了方便后期扩 容可以多写几个预留的IP。 配置文件字段介绍 ETCD_NAME:节点名称,集群中唯一 然后在节点2和节点3分别修改etcd.conf配置文件中的节点名称和当前服务器IP 最后启动etcd并设置开机启动。 任意节点执行如下命令 正常返回结果 当前部署master节点 ip: 192.168.56.14 注:上述文件hosts字段中IP为所有Master/LB/VIP IP,一个都不能少!为了方便后期扩容可以多 写几个预留的IP。 生成apiserver证书 注意事项: --logtostderr:启用日志 --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:审计日志 TLS Bootstraping:Master apiserver启用TLS认证后,Node节点kubelet和kube-proxy要与kube- apiserver进行通信,必须使用CA签发的有效证书才可以,当Node节点很多时,这种客户端证书颁发需 要大量工作,同样也会增加集群扩展复杂度。为了简化流程,Kubernetes引入了TLS bootstraping机制 来自动颁发客户端证书,kubelet会以一个低权限用户自动向apiserver申请证书,kubelet的证书由 apiserver动态签署。所以强烈建议在Node上使用这种方式,目前主要用于kubelet,kube-proxy还是 由我们统一颁发一个证书。 格式:token,用户名,UID,用户组 token也可自行生成替换: head -c 16 /dev/urandom | od -An -t x | tr -d ‘ ‘ --master:通过本地非安全本地端口8080连接apiserver。 --leader-elect:当该组件启动多个时,自动选举(HA) --cluster-signing-cert-file/--cluster-signing-key-file:自动为kubelet颁发证书的CA,与apiserver 保持一致 --master:通过本地非安全本地端口8080连接apiserver。 --leader-elect:当该组件启动多个时,自动选举(HA) 至此master部署完成,集群状态查看 返回如下结果,证明mater部署ok 当前操作节点是: 192.168.56.14(将这个master也作为node) --hostname-override:显示名称,集群中唯一 --network-plugin:启用CNI --kubeconfig:空路径,会自动生成,后面用于连接apiserver --bootstrap-kubeconfig:首次启动向apiserver申请证书 --config:配置参数文件 --cert-dir:kubelet证书生成目录 --pod-infra-container-image:管理Pod网络容器的镜像 如有异常,及时查看日志解决,大多数问题会出现在/data/kubernetes/cfg/kubelet-config.yml格式上 返回结果 返回结果 注:由于网络插件还没有部署,节点会没有准备就绪 NotReady 192.168.56.14 注:这几个文件是证书申请审批后自动生成的,每个Node不同,必须删除重新生成。 如果新加的node不是Ready,那就重新apply 一下kube-flannel.yml 此时可以直接访问https://NodeIP:30001 NodeIP也就是宿主机的IP 用于集群内部Service名称解析 Kubernetes作为容器集群系统,通过健康检查+重启策略实现了Pod故障自我修复能力,通过调度算法 实现将Pod分布式部署,并保持预期副本数,根据Node失效状态自动在其他Node拉起Pod,实现了应用层的高可用性。 针对Kubernetes集群,高可用性还应包含以下两个层面的考虑:Etcd数据库的高可用性和Kubernetes Master组件的高可用性。 而Etcd我们已经采用3个节点组建集群实现高可用,本节将对Master节点高可 用进行说明和实施。 Master节点扮演着总控中心的角色,通过不断与工作节点上的Kubelet和kube-proxy进行通信来维护整 个集群的健康工作状态。如果Master节点故障,将无法使用kubectl工具或者API做任何集群管理。 Master节点主要有三个服务kube-apiserver、kube-controller-manager和kube-scheduler,其中kube- controller-manager和kube-scheduler组件自身通过选择机制已经实现了高可用,所以Master高可用主 要针对kube-apiserver组件,而该组件是以HTTP API提供服务,因此对他高可用与Web服务器类似,增 加负载均衡器对其负载均衡即可,并且可水平扩容。 多Master架构图: 新增主机:centos7-node7, 角色k8s-master2 Nginx是一个主流Web服务和反向代理服务器,这里用四层实现对apiserver实现负载均衡。 Keepalived是一个主流高可用软件,基于VIP绑定实现服务器双机热备,在上述拓扑中, Keepalived主要根据Nginx运行状态判断是否需要故障转移(偏移VIP),例如当Nginx主节点挂 掉,VIP会自动绑定在Nginx备节点,从而保证VIP一直可用,实现Nginx高可用。 注意: virtual_router_id # VRRP 路由 ID实例,每个实例是唯一的 priority 100 # 优先级,备服务器设置 90,所以两台机器上的这个优先级设置是不一样的 kubernetes(七) 二进制部署k8s(1.18.4版本) 标签:自动 本地 node节点 source connect ups yml uid 谷歌浏览器 原文地址:https://blog.51cto.com/13812615/2509816部署说明
软件名称
下载地址
备注
centos7.7+
https://mirrors.aliyun.com/centos/7.7.1908/isos/x86_64/CentOS-7-x86_64-Minimal-1908.iso
宿主机操作系统
kubernetes-server
https://dl.k8s.io/v1.18.4/kubernetes-server-linux-amd64.tar.gz
etcd
https://github.com/etcd-io/etcd/releases/download/v3.4.9/etcd-v3.4.9-linux-amd64.tar.gz
k8s数据存储
cfssl
https://pkg.cfssl.org/R1.2/cfssl_linux-amd64
https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64证书签发工具
docker
https://download.docker.com/linux/static/stable/x86_64/docker-19.03.9.tgz
CNR运行引擎
cni
https://github.com/containernetworking/plugins/releases/download/v0.8.6/cni-plugins-linux-amd64-v0.8.6.tgz
网络环境
部署规划
主机
ip
作用
部署的软件
备注
centos7-node4
192.168.56.14
master
kube-apiserver,kube-controller-manager,kube-scheduler,docker,etcd
后期介绍master扩容
centos7-node5
192.168.56.15
node
kubelet,kube-proxy,docker,etcd
后期介绍master扩容
centos7-node6
192.168.56.16
node
kubelet,kube-proxy,docker,etcd
后期介绍master扩容
系统初始化(所有节点执行)
/data
# 更新yum源
yum -y install wget && wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo && yum -y install epel-release
# 关闭selinux,firewalld,swap
sed -i ‘s/enforcing/disabled/‘ /etc/selinux/config
systemctl disable firewalld && systemctl stop firewalld
sed -ri ‘s/.*swap.*/#&/‘ /etc/fstab && swapoff -a
# 设置好主机名与主机名解析
cat >> /etc/hosts /etc/sysctl.d/k8s.conf
部署ETCD集群
节点hostname
节点名称
ip
centos7-node4
etcd-1
192.168.56.14
centos7-node5
etcd-2
192.168.56.15
centos7-node6
etcd-3
192.168.56.16
生成etcd证书配置
# 软件安装
wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 -O /usr/local/bin/cfssl
wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 -O /usr/local/bin/cfssljson
wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 -O /usr/local/bin/cfssl-certinfo
chmod +x /usr/local/bin/cfssl*
mkdir ~/TLS/{etcd,k8s} && cd ~/TLS/etcd
# 自谦CA配置文件
cat > ca-config.json ca-csr.json server.json
签发证书
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 #签发证书文件
部署ETCD集群
单个节点配置
# 安装路径准备
mkdir /data/etcd/{bin,cfg,ssl,data} -p
# 二进制文件准备
wget https://github.com/etcd-io/etcd/releases/download/v3.4.9/etcd-v3.4.9-linux-amd64.tar.gz && tar xf etcd-v3.4.9-linux-amd64.tar.gz
mv etcd-v3.4.9-linux-amd64/etcd* /data/etcd/bin/
# 当前节点192.168.56.14配置文件
cat > /data/etcd/cfg/etcd.conf
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表示加入已有集 群其余两个节点配置
scp -rp /data/etcd 192.1658.56.15:/data
scp -rp /data/etcd 192.1658.56.16:/data
vi /data/etcd/cfg/etcd.conf
#[Member]
ETCD_NAME="etcd-1" # 修改此处,节点2改为etcd-2,节点3改为etcd-3 ETCD_DATA_DIR="/var/lib/etcd/default.etcd" ETCD_LISTEN_PEER_URLS="https://192.168.56.14:2380" # 修改此处为当前服务器IP ETCD_LISTEN_CLIENT_URLS="https://192.168.56.14:2379" # 修改此处为当前服务器IP
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.56.14:2380" # 修改此处为当前服务 器IP
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.56.14:2379" # 修改此处为当前服务器IP ETCD_INITIAL_CLUSTER="etcd-1=https://192.168.56.14:2380,etcd-2=https://192.168.56.15:2380,etcd-3=https://192.168.56.16:2380" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
systemctl daemon-reload && systemctl enable etcd && systemctl start etcd
验证Etcd部署状态是否成功
ETCDCTL_API=3 /data/etcd/bin/etcdctl --cacert=/data/etcd/ssl/ca.pem --cert=/data/etcd/ssl/server.pem --key=/data/etcd/ssl/server-key.pem --endpoints="https://192.168.56.14:2379,https://192.168.56.15:2379,https://192.168.56.16:2379" endpoint health
https://192.168.56.15:2379 is healthy: successfully committed proposal: took = 11.567437ms
https://192.168.56.14:2379 is healthy: successfully committed proposal: took = 11.946454ms
https://192.168.56.16:2379 is healthy: successfully committed proposal: took = 13.121313ms
集群异常排查
1. 查看/var/log/message日志或者journalctl -xe -f -uetcd
2. 一般配置文件没问题的话就ok,最大的问题还有一点就是网络通信和防火墙,注意响应的策略放开即可
所有节点安装docker
# 下载和解压docker二进制文件
wget https://download.docker.com/linux/static/stable/x86_64/docker-19.03.9.tgz && tar xf docker-19.03.9.tgz
# 转移可执行文件
scp docker/* 192.168.56.15:/usr/bin/
scp docker/* 192.168.56.16:/usr/bin/
mv docker/* /usr/bin/
# 配置systemd管理docker (其余的两个节点也需要安装)
cat > /usr/lib/systemd/system/docker.service
docker配置与启动
# 配置docker阿里云镜像加速和存储路径(graph)
mkdir /etc/docker
cat > /etc/docker/daemon.json
k8s master节点的安装与部署
生成k8s证书配置
cd ~/TLS/k8s
cat > ca-config.json ca-csr.json
自签apiserver证书
cd ~/TLS/k8s
cat > server-csr.json
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare server
ls *pem
安装ApiServer
# 创建软件目录
mkdir -p /data/kubernetes/{cfg,bin,ssl,logs}
# 文件下载与拷贝
wget https://dl.k8s.io/v1.18.4/kubernetes-server-linux-amd64.tar.gz && tar xf kubernetes-server-linux-amd64.tar.gz
cp kubernetes/server/bin/kube-apiserver /data/kubernetes/bin/
cp kubernetes/server/bin/kube-controller-manager /data/kubernetes/bin/
cp kubernetes/server/bin/kube-scheduler /data/kubernetes/bin/
cp kubernetes/server/bin/kubectl /usr/bin/
创建apiserver配置文件
# 创建配置文件
cat > /data/kubernetes/cfg/kube-apiserver.conf
---v:日志等级
--log-dir:日志目录
--etcd-servers:etcd集群地址
--bind-address:监听地址
--secure-port:https安全端口
--advertise-address:集群通告地址
--allow-privileged:启用授权 --service-cluster-ip-range:Service虚拟IP地址段 启用TLS Bootstrap机制
TLS bootstraping 工作流程:
cat > /data/kubernetes/cfg/token.csv
使用systemctl管理apiserver
cat > /usr/lib/systemd/system/kube-apiserver.service
systemctl daemon-reload && systemctl start kube-apiserver && systemctl enable kube-apiserver
kubectl create clusterrolebinding kubelet-bootstrap --clusterrole=system:node-bootstrapper --user=kubelet-bootstrap
部署kube-controller-manager
创建配置文件
cat > /data/kubernetes/cfg/kube-controller-manager.conf
systemctl管理controller-manager
cat > /usr/lib/systemd/system/kube-controller-manager.service
systemctl daemon-reload && systemctl start kube-controller-manager && systemctl enable kube-controller-manager
部署kube-scheduler
创建配置文件
cat > /data/kubernetes/cfg/kube-scheduler.conf
systemctl管理kube-scheduler
cat > /usr/lib/systemd/system/kube-scheduler.service
systemctl daemon-reload && systemctl start kube-scheduler && systemctl enable kube-scheduler
kubectl get cs
NAME STATUS MESSAGE ERROR
controller-manager Healthy ok
scheduler Healthy ok
etcd-2 Healthy {"health":"true"}
etcd-0 Healthy {"health":"true"}
etcd-1 Healthy {"health":"true"}
部署WorkNode
基础软件包准备
# 创建软件目录
mkdir -p /data/kubernetes/{cfg,bin,ssl,logs}
# 文件下载与拷贝
wget https://dl.k8s.io/v1.18.4/kubernetes-server-linux-amd64.tar.gz && tar xf kubernetes-server-linux-amd64.tar.gz
cp kubernetes/server/bin/kube-proxy /data/kubernetes/bin/
cp kubernetes/server/bin/kubelet /data/kubernetes/bin/
部署kubelet
创建kubelet配置文件
cat > /data/kubernetes/cfg/kubelet.conf
创建参数配置文件
cat > /data/kubernetes/cfg/kubelet-config.yml
生成bootstrap.kubeconfig文件
KUBE_APISERVER="https://192.168.56.14:6443" # apiserver IP:PORT TOKEN="2b4b65d2e33e24dc0beafddda6dd4b23" # 与token.csv里保持一致
# 生成 kubelet bootstrap kubeconfig 配置文件
kubectl config set-cluster kubernetes --certificate-authority=/data/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
#拷贝生成的配置到cfg
cp bootstrap.kubeconfig /data/kubernetes/cfg
systemctl管理kubelet
cat > /usr/lib/systemd/system/kubelet.service
systemctl daemon-reload && systemctl start kubelet && systemctl enable kubelet
批准kubelet证书申请并加入集群
kubectl get csr
NAME AGE SIGNERNAME REQUESTOR CONDITION
node-csr-zJmrG00TW4zKRNPKoNo3ag0ojgPwEM2M3ARCsvVVyiI 60s kubernetes.io/kube-apiserver-client-kubelet kubelet-bootstrap Pending
kubectl certificate approve node-csr-zJmrG00TW4zKRNPKoNo3ag0ojgPwEM2M3ARCsvVVyiI
kubectl get node
NAME STATUS ROLES AGE VERSION
k8s-master NotReady
部署kube-proxy
创建配置文件
cat > /data/kubernetes/cfg/kube-proxy.conf
配置参数文件
cat > /data/kubernetes/cfg/kube-proxy-config.yml
生成kube-proxy.kubeconfig文件
cd ~/TLS/k8s/
cat > kube-proxy-csr.json
KUBE_APISERVER="https://192.168.56.14:6443"
kubectl config set-cluster kubernetes --certificate-authority=/data/kubernetes/ssl/ca.pem --embed-certs=true --server=${KUBE_APISERVER} --kubeconfig=kube-proxy.kubeconfig
kubectl config set-credentials kube-proxy --client-certificate=./kube-proxy.pem --client-key=./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
# 拷贝配置到cfg下
cp kube-proxy.kubeconfig /data/kubernetes/cfg/
systemctl管理kube-proxy
cat > /usr/lib/systemd/system/kube-proxy.service
systemctl daemon-reload && systemctl start kube-proxy && systemctl enable kube-proxy
部署CNI网络
wget https://github.com/containernetworking/plugins/releases/download/v0.8.6/cni-plugins-linux-amd64-v0.8.6.tgz && mkdir /opt/cni/bin -p
tar zxvf cni-plugins-linux-amd64-v0.8.6.tgz -C /opt/cni/bin/
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
sed -i -r "s#quay.io/coreos/flannel:.*-amd64#lizhenliang/flannel:v0.12.0-amd64#g" kube-flannel.yml
kubectl apply -f kube-flannel.yml
kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master Ready
授权apiserver访问kubelet
cat > apiserver-to-kubelet-rbac.yaml
新增workNode
同步文件和配置
# kubelet,kube-proxy拷贝
scp /data/kubernetes/bin/kubelet root@192.168.56.15:/data/kubernetes/bin/
scp /data/kubernetes/bin/kube-proxy root@192.168.56.15:/data/kubernetes/bin/
scp /data/kubernetes/bin/kubelet root@192.168.56.16:/data/kubernetes/bin/
scp /data/kubernetes/bin/kube-proxy root@192.168.56.16:/data/kubernetes/bin/
# cni插件拷贝
scp -rp /opt/cni/ root@192.168.56.15:/opt
scp -rp /opt/cni/ root@192.168.56.16:/opt
# 证书拷贝
scp /data/kubernetes/ssl/ca.pem 192.168.56.15:/data/kubernetes/ssl/
scp /data/kubernetes/ssl/ca.pem 192.168.56.16:/data/kubernetes/ssl/
# 配置文件拷贝
scp /data/kubernetes/cfg/kube-proxy* 192.168.56.15:/data/kubernetes/cfg/
scp /data/kubernetes/cfg/kube-proxy* 192.168.56.16:/data/kubernetes/cfg/
scp /data/kubernetes/cfg/kubelet* 192.168.56.15:/data/kubernetes/cfg/
scp /data/kubernetes/cfg/kubelet* 192.168.56.16:/data/kubernetes/cfg/
scp /data/kubernetes/cfg/bootstrap.kubeconfig 192.168.56.15:/data/kubernetes/cfg/
scp /data/kubernetes/cfg/bootstrap.kubeconfig 192.168.56.16:/data/kubernetes/cfg/
# 启动文件拷贝
scp /usr/lib/systemd/system/kubelet.service 192.168.56.15:/usr/lib/systemd/system/
scp /usr/lib/systemd/system/kubelet.service 192.168.56.16:/usr/lib/systemd/system/
scp /usr/lib/systemd/system/kube-proxy.service 192.168.56.15:/usr/lib/systemd/system/
scp /usr/lib/systemd/system/kube-proxy.service 192.168.56.16:/usr/lib/systemd/system/
删除证书和配置文件
rm /data/kubernetes/cfg/kubelet.kubeconfig
rm -f /data/kubernetes/ssl/kubelet*
配置新的Node节点
vi /opt/kubernetes/cfg/kubelet.conf
--hostname-override=k8s-node1
vi /opt/kubernetes/cfg/kube-proxy-config.yml
hostnameOverride: k8s-node1
systemctl daemon-reload && systemctl start kubelet && systemctl start kube-proxy
systemctl enable kubelet && systemctl enable kube-proxy
在master节点上准许node加入
kubectl get csr
NAME AGE SIGNERNAME REQUESTOR CONDITION
node-csr-63HqXs5ifBWopOS6dZAO8bRJ8PImXljxbOt-2wV5hHg 7m57s kubernetes.io/kube-apiserver-client-kubelet kubelet-bootstrap Pending
node-csr-t6XNO793xatm4gCwQiYH4QDOeIY4yMx8C0SUXSNye7c 38s kubernetes.io/kube-apiserver-client-kubelet kubelet-bootstrap Pending
kubectl certificate approve node-csr-63HqXs5ifBWopOS6dZAO8bRJ8PImXljxbOt-2wV5hHg
kubectl certificate approve node-csr-t6XNO793xatm4gCwQiYH4QDOeIY4yMx8C0SUXSNye7c
kubectl get node
NAME STATUS ROLES AGE VERSION
k8s-master Ready
部署Dashboard和CoreDNS
部署Dashboard
wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.3/aio/deploy/recommended.yaml
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
# 部署dashboard
kubectl apply -f recommended.yml
# 查看状态
kubectl get pods,svc -n kubernetes-dashboard
kubectl create serviceaccount dashboard-admin -n kube-system
kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk ‘/dashboard-admin/{print $1}‘) #获取token
# 使用cfssl生成证书,继续在192.168.56.14 master节点操作
cd ~/TLS/k8s/
cat > dashboard-csr.json
#删除secret
kubectl delete secret kubernetes-dashboard-certs -n kubernetes-dashboard
#自签证书创建新的secret
kubectl create secret generic kubernetes-dashboard-certs --from-file=/data/kubernetes/ssl/kubernetes-dashboard-key.pem --from-file=/data/kubernetes/ssl/kubernetes-dashboard.pem -n kubernetes-dashboard
vim recommended.yaml
args:
- --auto-generate-certificates
- --tls-key-file=kubernetes-dashboard-key.pem
- --tls-cert-file=kubernetes-dashboard.pem
- --namespace=kubernetes-dashboard
# apply
kubectl apply -f recommended.yaml
# 查看token,使用这个token,可以直接访问https://NodeIP:30001 NodeIP也就是宿主机的IP
kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk ‘/dashboard-admin/{print $1}‘)
部署coreDNS
# 下载coredns
git clone https://github.com/coredns/deployment.git
cd deployment/kubernetes/
# 修改部署脚本
vim deploy.sh
if [[ -z $CLUSTER_DNS_IP ]]; then
# Default IP to kube-dns IP
# CLUSTER_DNS_IP=$(kubectl get service --namespace kube-system kube-dns -o jsonpath="{.spec.clusterIP}")
CLUSTER_DNS_IP=10.10.0.2
# 执行部署
yum -y install epel-release jq
./deploy.sh | kubectl apply -f -
kubectl run -it --rm dns-test --image=busybox:1.28.4 sh
If you don‘t see a command prompt, try pressing enter.
/ # nslookup kubernetes
Server: 10.0.0.2
Address 1: 10.0.0.2 kube-dns.kube-system.svc.cluster.local
Name: kubernetes
Address 1: 10.0.0.1 kubernetes.default.svc.cluster.local
高可用架构
扩容流程
wget https://download.docker.com/linux/static/stable/x86_64/docker-19.03.9.tgz
tar xf docker-19.03.9.tgz
mv docker/* /usr/bin/
mkdir /data/docker
mkdir /etc/docker
# 创建目录
mkdir /data/kubernetes/{ssl,bin,cfg,logs} -pv
# CNI
scp -rp /opt/cni/ 192.168.56.17:/opt
# 证书
scp -rp /data/etcd/ssl/* 192.168.56.17:/data/etcd/ssl/
scp -rp /data/kubernetes/ssl/* 192.168.56.17:/data/kubernetes/ssl/
# 二进制文件
scp -rp /data/kubernetes/bin/kube* 192.168.56.17:/data/kubernetes/bin
# 配置文件
scp /data/kubernetes/cfg/* 192.168.56.17:/data/kubernetes/cfg/
# 启动脚本
scp -rp /usr/lib/systemd/system/kube* 192.168.56.17:/usr/lib/systemd/system/
scp -rp /usr/lib/systemd/system/docker.service 192.168.56.17:/usr/lib/systemd/system/
$ vim /data/kubernetes/cfg/kube-apiserver.conf
--bind-address=192.168.56.17 --advertise-address=192.168.56.17 $ vim /data/kubernetes/cfg/kubelet.conf
--hostname-override=k8s-master2
$ vim /data/kubernetes/cfg/kube-proxy-config.yml
hostnameOverride: k8s-master2
systemctl daemon-reload && systemctl start docker && systemctl start kube-apiserver && systemctl start kube-controller-manager && systemctl start kube-scheduler && systemctl start kubelet && systemctl start kube-proxy
systemctl enable kube-apiserver && systemctl enable docker && systemctl enable kube-controller-manager && systemctl enable kube-scheduler && systemctl enable kubelet && systemctl enable kube-proxy
$ kubectl get cs
NAME STATUS MESSAGE ERROR
controller-manager Healthy ok
scheduler Healthy ok
etcd-2 Healthy {"health":"true"}
etcd-0 Healthy {"health":"true"}
etcd-1 Healthy {"health":"true"}
$ kubectl get csr
NAME AGE SIGNERNAME REQUESTOR CONDITION
node-csr-wAP8aDK22Olbn5G34KDaH9xvAn49UyE2DkacElw4SFE 2m19s kubernetes.io/kube-apiserver-client-kubelet kubelet-bootstrap Pending
$ kubectl certificate approve node-csr-wAP8aDK22Olbn5G34KDaH9xvAn49UyE2DkacElw4SFE
$ kubectl get node
# 创建 授权
cat > admin.yml
部署ngixn负载均衡
资源规划
节点hostname
ip
角色
软件
centos7-node8
192.168.56.18
nginx+keepalived
nginx+keepalived
centos7-node9
192.168.56.19
nginx+keepalived
nginx+keepalived
软件安装与配置
# 软件安装
yum install epel-release -y
yum install nginx keepalived -y
# nginx配置
$ vim /etc/nginx/nginx.conf
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.56.14:6443;
server 192.168.56.74:6443;
}
server {
listen 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;
server_name _;
location / {
}
}
}
$ vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
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 MASTER
interface eth1
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.56.111/24
}
track_script {
check_nginx
}
}
$ vim /etc/keepalived/check_nginx.sh
#!/bin/bash
count=$(ps -ef |grep nginx |egrep -cv "grep|$$")
if [ "$count" -eq 0 ];then
exit 1
else
exit 0
fi
$ chmod +x /etc/keepalived/check_nginx.sh
systemctl daemon-reload && systemctl start nginx && systemctl start keepalived && systemctl enable nginx && systemctl enable keepalived
上一篇:VueJs echarts使用
文章标题:kubernetes(七) 二进制部署k8s(1.18.4版本)
文章链接:http://soscw.com/index.php/essay/45939.html