写一个k8s加docker集群的项目案例
时间: 2023-10-19 12:12:57 浏览: 164
k8s + docker
好的,以下是一个简单的Kubernetes与Docker集成的项目案例:
项目概述:使用Kubernetes和Docker搭建一个高可用的Web应用程序,应用程序将在多个节点上运行,并且在节点故障时,它们将自动迁移到其他可用节点上。
组件:
1. Docker:用于打包和部署应用程序的容器化技术。
2. Kubernetes:用于容器编排、自动化部署、自动扩展和管理容器化应用程序的开源平台。
3. Nginx:用于反向代理和负载均衡的Web服务器。
4. Flask:用于构建Web应用程序的Python框架。
5. Redis:用于缓存应用程序数据的内存数据库。
步骤:
1. 安装Docker和Kubernetes
2. 构建Web应用程序Docker镜像
```
FROM python:3.8-alpine
COPY requirements.txt /app/
RUN pip install --no-cache-dir -r /app/requirements.txt
COPY . /app/
WORKDIR /app
CMD ["python", "app.py"]
```
3. 部署Web应用程序到Kubernetes集群
```
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:latest
ports:
- containerPort: 5000
imagePullSecrets:
- name: my-registry-key
```
4. 创建Service对象
```
apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
selector:
app: myapp
ports:
- protocol: TCP
port: 80
targetPort: 5000
type: LoadBalancer
```
5. 部署Nginx反向代理和负载均衡器
```
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
volumeMounts:
- name: nginx-conf
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
volumes:
- name: nginx-conf
configMap:
name: nginx-conf
---
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-conf
data:
nginx.conf: |
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
upstream myapp {
server myapp-service;
}
server {
listen 80;
location / {
proxy_pass http://myapp;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
```
6. 部署Redis
```
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
spec:
replicas: 1
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis:latest
ports:
- containerPort: 6379
volumeMounts:
- name: redis-data
mountPath: /data
volumes:
- name: redis-data
emptyDir: {}
```
7. 使用Kubernetes的Volume功能将Redis数据存储在本地磁盘上
```
apiVersion: v1
kind: Pod
metadata:
name: redis-data
spec:
containers:
- name: redis-data
image: busybox
command: ["/bin/sh"]
args: ["-c", "while true; do sleep 3600; done"]
volumeMounts:
- name: redis-data
mountPath: /data
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: redis-data
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /var/data/redis-data
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: redis-data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
```
8. 使用Kubernetes的ConfigMap功能将Nginx配置文件存储在集群中
9. 运行应用程序并测试
```
kubectl apply -f app.yaml
kubectl apply -f nginx.yaml
kubectl apply -f redis.yaml
kubectl apply -f redis-data.yaml
kubectl apply -f nginx-conf.yaml
kubectl expose deployment myapp --type=LoadBalancer --name=myapp-service
```
测试:使用Web浏览器访问Nginx的公共IP地址,应该可以看到Web应用程序的主页。同时,停止一个节点,应用程序将自动迁移到其他节点上,而不会影响其可用性。
阅读全文