文章目录
- 一、master02 节点部署(在上期博客部署完成的情况下)
- 1、准备master02节点需要的文件
- 2、修改配置文件kube-apiserver中的IP
- 3、启动各服务并设置开机自启
- 4、查看node节点状态
- 二、负载均衡部署
- 1、配置load balancer集群双机热备负载均衡
- 1.1 准备nginx编译安装脚本
- 1.2 修改nginx配置文件
- 1.3 启动nginx服务
- 2、部署keepalived服务
- 2.1 修改keepalive配置文件
- 2.2 创建nginx状态检查脚本
- 2.3 启动keepalive服务
- 3、修改node节点上的配置文件
- 4、查看连接状态
- 三、部署 Dashboard
- 1、Dashboard 介绍
- 2、部署Dashboard
-
一、master02 节点部署(在上期博客部署完成的情况下)
- master02环境准备,详细过程参考上一期博客环境准备
服务器 | 主机名 | IP地址 | 主要组件/说明 |
---|
master01节点+etcd01节点 | master01 | 192.168.10.11 | kube-apiserver、kube-controller-manager、kube-schedular etcd |
master02节点 | master02 | 192.168.10.20 | kube-apiserver、kube-controller-manager、kube-schedular |
node01节点+etcd02节点 | node01 | 192.168.10.12 | kubelet、kube-proxy、docker、calico |
node02节点+etcd03节点 | node02 | 192.168.10.13 | kubelet、kube-proxy、docker、calico |
nginx01节点 | nginx01 | 192.168.10.14 | keepalived负载均衡(主) |
nginx02节点 | nginx02 | 192.168.10.15 | keepalived负载均衡(备) |
vim /etc/hosts
192.168.10.11 master01
192.168.10.20 master02
192.168.10.12 node01
192.168.10.13 node02
1、准备master02节点需要的文件
- 从 master01 节点上拷贝证书文件、各master组件的配置文件和服务管理文件到 master02 节点
scp -r /opt/etcd/ @192.168.10.20:/opt/
scp -r /opt/kubernetes/ @192.168.10.20:/opt
scp -r /root/.kube @192.168.10.20:/root
scp /usr/lib/systemd/system/{kube-apiserver,kube-controller-manager,kube-scheduler}.service root@192.168.10.20:/usr/lib/systemd/system/
2、修改配置文件kube-apiserver中的IP
vim /opt/kubernetes/cfg/kube-apiserver
--bind-address=192.168.10.20 \
--secure-port=6443 \
--advertise-address=192.168.10.20 \
3、启动各服务并设置开机自启
systemctl enable --now kube-apiserver.service
systemctl status kube-apiserver.service
systemctl enable --now kube-controller-manager.service
systemctl status kube-controller-manager.service
systemctl enable --now kube-scheduler.service
systemctl status kube-scheduler.service
4、查看node节点状态
ln -s /opt/kubernetes/bin/* /usr/local/bin/
kubectl get nodes
kubectl get nodes -o wide
二、负载均衡部署
1、配置load balancer集群双机热备负载均衡
- nginx实现负载均衡,keepalived实现双机热备
- 在nginx01、nginx02节点上操作
1.1 准备nginx编译安装脚本
cat nginx.sh
systemctl start nginx >>/dev/null
if [ $? -eq 0 ];then
echo "nginx服务已安装"
else
useradd -M -s /sbin/nologin nginx
cd /opt
wget http://nginx.org/download/nginx-1.18.0.tar.gz >>/dev/null
echo "正在安装,请耐心等待"
tar xf nginx-1.18.0.tar.gz
cd /opt/nginx-1.18.0
yum -y install gcc pcre-devel openssl-devel zlib-devel openssl openssl-devel &>>/dev/null
./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module
make -j `lscpu|sed -n '4p'|awk '{print $2}'`&>>/dev/null
make install &>>/dev/null
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
cat > /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -1 $MAINPID
ExecStop=/bin/kill -3 $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
chown -R nginx.nginx /usr/local/nginx
systemctl daemon-reload &>>/dev/null
systemctl enable --now nginx
echo "nginx服务已开启"
fi
cd /opt/
chmod +x nginx.sh
./nginx.sh
1.2 修改nginx配置文件
- 配置四层反向代理负载均衡,指定k8s群集2台master的节点ip和6443端口
vim /usr/local/nginx/conf/nginx.conf
stream {
log_format main '$remote_addr $upstream_addr - [$time_local] $status $upstream_bytes_sent';
access_log /usr/local/nginx/logs/k8s-access.log main;
upstream k8s-apiserver {
server 192.168.10.11:6443;
server 192.168.10.20:6443;
}
server {
listen 6443;
proxy_pass k8s-apiserver;
}
}
scp /usr/local/nginx/conf/nginx.conf 192.168.10.15:/usr/local/nginx/conf/nginx.conf
1.3 启动nginx服务
nginx -t
systemctl enable --now nginx
systemctl restart nginx
ss -natp | grep nginx
2、部署keepalived服务
2.1 修改keepalive配置文件
yum install -y keepalived.x86_64
vim /etc/keepalived/keepalived.conf
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_MASTER
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script check_nginx {
script "/usr/local/nginx/conf/check_nginx.sh"
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.100/24
}
track_script {
check_nginx
}
}
scp /etc/keepalived/keepalived.conf 192.168.10.15:/etc/keepalived/keepalived.conf
2.2 创建nginx状态检查脚本
vim /usr/local/nginx/conf/check_nginx.sh
count=$(ps -ef | grep nginx | egrep -cv "grep|$$")
if [ "$count" -eq 0 ];then
systemctl stop keepalived
fi
chmod +x /usr/local/nginx/conf/check_nginx.sh
2.3 启动keepalive服务
systemctl start keepalived
systemctl enable keepalived
systemctl status keepalived.service
ip a
3、修改node节点上的配置文件
- bootstrap.kubeconfig 和 kubelet.kubeconfig配置文件配置vip地址
cd /opt/kubernetes/cfg/
vim bootstrap.kubeconfig
server: https://192.168.10.100:6443
vim kubelet.kubeconfig
server: https://192.168.10.100:6443
vim kube-proxy.kubeconfig
server: https://192.168.10.100:6443
systemctl restart kubelet.service
systemctl restart kube-proxy.service
4、查看连接状态
- 在nginx01节点上查看 nginx 和 node 、 master 节点的连接状态
ss -natp|grep nginx
kubectl run nginx --image=nginx
kubectl get pods
---------------------------------------------------------------------------------------------------------------------
ContainerCreating
ImagePullBackOff
Running
---------------------------------------------------------------------------------------------------------------------
kubectl get pods -o wide
[root@node01 cfg]
kubectl logs nginx
三、部署 Dashboard
1、Dashboard 介绍
- 仪表板是基于Web的Kubernetes用户界面。您可以使用仪表板将容器化应用程序部署到Kubernetes集群,对容器化应用程序进行故障排除,并管理集群本身及其伴随资源。您可以使用仪表板来概述群集上运行的应用程序,以及创建或修改单个Kubernetes资源(例如deployment,job,daemonset等)。例如,您可以使用部署向导扩展部署,启动滚动更新,重新启动Pod或部署新应用程序。仪表板还提供有关群集中Kubernetes资源状态以及可能发生的任何错误的信息。
2、部署Dashboard
cd /opt/k8s
--------------------------------------------------------------------------------------------------------
vim recommended.yaml
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-das/hboard
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
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}')
https://NodeIP:30001
部署多master集群架构过程
- 首先部署master02等其他master节点(master01节点的配置文件(私钥文件、执行文件、服务文件)拷贝到master02 节点)
- 对master节点搭建nginx/haproxy+keepalive高可用负载均衡器
- 修改node节点上的kubelet、kube-proxy的kubeconfig配置文件对接vip
- kubectl配置文件也要对接vip或者当前的节点