lvs+keepalived+nginx+mysql+redis+tomcat+docker+k8s集群(mysql和redis搭建在k8s集群外边)
时间: 2025-07-31 18:47:54 浏览: 11
### 架构概述
搭建一个包含 LVS、Keepalived、Nginx、MySQL、Redis、Tomcat、Docker 和 Kubernetes 的系统架构,其中 MySQL 和 Redis 部署在 Kubernetes(K8s)集群外部,而其他服务如 Tomcat 和 Nginx 可以运行在 Kubernetes 内部。这种架构能够提供高可用性、负载均衡和灵活的容器化管理能力。
### 系统架构设计
#### 1. 网络拓扑
- **LVS(Linux Virtual Server)**:作为前端负载均衡器,负责将流量分发到后端的 Nginx 节点。
- **Keepalived**:用于实现 LVS 的高可用性,提供虚拟 IP(VIP),确保在主 LVS 故障时自动切换到备用 LVS。
- **Nginx**:作为反向代理和负载均衡器,进一步将请求分发到 Kubernetes 集群内的 Tomcat 服务。
- **Kubernetes 集群**:托管 Tomcat 和 Docker 容器,提供容器编排和自动伸缩能力。
- **MySQL 和 Redis**:部署在 Kubernetes 集群外部,作为独立的数据库服务器,提供持久化存储和缓存服务。
#### 2. 组件配置
##### 2.1 LVS + Keepalived 配置
LVS 和 Keepalived 的配置可以参考以下步骤:
- **安装 LVS 和 Keepalived**:
```bash
yum install ipvsadm keepalived -y
```
- **配置 Keepalived**:
编辑 `/etc/keepalived/keepalived.conf` 文件,配置主备 LVS 的 VIP 和健康检查机制。
```conf
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.238.88
}
}
virtual_server 192.168.238.88 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.238.3 80 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
real_server 192.168.238.4 80 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
```
- **启动 Keepalived 服务**:
```bash
systemctl start keepalived
systemctl enable keepalived
```
##### 2.2 Nginx 配置
Nginx 作为反向代理,负责将请求转发到 Kubernetes 集群中的 Tomcat 服务。
- **安装 Nginx**:
```bash
yum install nginx -y
```
- **配置 Nginx**:
编辑 `/etc/nginx/nginx.conf` 文件,添加反向代理配置。
```nginx
http {
upstream tomcat_cluster {
server 192.168.238.10:8080;
server 192.168.238.11:8080;
}
server {
listen 80;
location / {
proxy_pass http://tomcat_cluster;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
```
- **启动 Nginx 服务**:
```bash
systemctl start nginx
systemctl enable nginx
```
##### 2.3 Kubernetes 集群部署
Kubernetes 集群可以通过 `kops` 或者云服务商工具进行部署。这里假设使用 `kubeadm` 进行本地部署。
- **初始化 Kubernetes 集群**:
```bash
kubeadm init --pod-network-cidr=10.244.0.0/16
```
- **安装网络插件(如 Flannel)**:
```bash
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
```
- **部署 Tomcat 服务**:
创建一个 `tomcat-deployment.yaml` 文件,定义 Tomcat 的 Deployment 和 Service。
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: tomcat
spec:
replicas: 2
selector:
matchLabels:
app: tomcat
template:
metadata:
labels:
app: tomcat
spec:
containers:
- name: tomcat
image: tomcat:9.0
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: tomcat-service
spec:
selector:
app: tomcat
ports:
- protocol: TCP
port: 8080
targetPort: 8080
type: NodePort
```
- **应用配置**:
```bash
kubectl apply -f tomcat-deployment.yaml
```
##### 2.4 MySQL 和 Redis 部署
MySQL 和 Redis 部署在 Kubernetes 集群外部,确保它们可以通过网络访问。
- **安装 MySQL**:
```bash
yum install mariadb-server mariadb -y
systemctl start mariadb
systemctl enable mariadb
mysql_secure_installation
```
- **安装 Redis**:
```bash
yum install redis -y
systemctl start redis
systemctl enable redis
```
- **配置防火墙**:
确保 MySQL 和 Redis 的端口(3306 和 6379)对 Kubernetes 集群内的节点开放。
### 数据库连接配置
在 Kubernetes 集群内的 Tomcat 应用中,配置数据库连接时需要指向外部的 MySQL 和 Redis 地址。
- **Tomcat 应用配置示例**:
```properties
spring.datasource.url=jdbc:mysql://192.168.238.5:3306/mydb
spring.datasource.username=root
spring.datasource.password=password
spring.redis.host=192.168.238.6
spring.redis.port=6379
```
### 高可用性与故障转移
- **LVS + Keepalived** 提供了前端负载均衡的高可用性,确保在主 LVS 故障时自动切换到备用 LVS。
- **Nginx** 作为反向代理,可以配置多个后端 Kubernetes 节点,实现负载均衡。
- **Kubernetes** 提供了 Pod 的自动重启和调度功能,确保服务的高可用性。
### 总结
通过上述配置,可以构建一个包含 LVS、Keepalived、Nginx、MySQL、Redis、Tomcat、Docker 和 Kubernetes 的高可用系统架构。该架构不仅提供了负载均衡和高可用性,还利用了 Kubernetes 的容器编排能力,确保服务的灵活性和可扩展性。
阅读全文
相关推荐














