Kubernetes挂载存储
目录
示例说明
为演示在Kubernetes挂载存储以下面为例:

fortune进程 利用fortune游戏每10秒钟更新一次html文档;并将更新过程钟写入日志。
web-server进程 使用nginx作为web服务器向外提供服务,客户展示fortune生成的html文件。
log-server进程 使用nginx作为服务器向外提供服务,可以查询fortune日志。
分别将上述进程部署到不同的容器中,容器间通过挂载emptyDir卷共享信息。
测试代码
- fortune程序
#! /bin/bash
trap "exit" SIGINT
mkdir /var/htdocs
mkdir /var/fortune
while :
do
echo $(date) writing fortune to /var/htdocs/index.html >> /var/log/fortune/$(date +%Y-%m-%d).log
/usr/games/fortune > /var/htdocs/index.html
sleep 10
done每10秒钟生成一次html文件,并记录生成日志
- fortune的dockerfile
FROM ubuntu:latest
RUN apt update ; apt install -y fortune
ADD fortuneloop.sh /bin/fortuneloop.sh
ENTRYPOINT /bin/fortuneloop.sh- 将三个示例容器部署到一个POD中
apiVersion: v1
kind: Pod
metadata:
name: fortune
labels:
app: fortune
spec:
containers:
- name: html-gen
image: 172.17.0.1:5000/fortune
volumeMounts:
- name: html
mountPath: /var/htdocs
- name: log
mountPath: /var/log/fortune
- name: web-server
image: 172.17.0.1:500/nginx:alpine
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
readOnly: true
ports:
- containerPort: 80
protocol: TCP
- name: log-server
image: 172.17.0.1:5000/nginx:log
volumeMounts:
- name: log
mountPath: /usr/share/nginx/log
readOnly: true
ports:
- containerPort: 8080
protocol: TCP
volumes:
- name: html
emptyDir: {}
- name: log
emptyDir: {}nginx:log源于nginx:alpine镜像,只是将nginx默认80端口修改成8080端口,将默认的html root目录修改成/usr/share/nginx/log目录,具体修改方法下次在详细讲。
- 建立一个NodePort类型的Service对外提供web服务
kind: Service
apiVersion: v1
metadata:
name: fortune-web-nodeport
spec:
selector:
app: fortune
type: NodePort
ports:
- port: 8088
targetPort: 80
nodePort: 30157注意selector实际是按照POD中容器标签进行选择的,因此该信息必须和POD中设置的标签保持一致; targetPort和容器端口一致,nginx:alpine默认端口是80端口
- 建立一个NodePort类型的Service对外提供log服务
kind: Service
apiVersion: v1
metadata:
name: fortune-log-nodeport
spec:
selector:
app: fortune
type: NodePort
ports:
- port: 8090
targetPort: 8080
nodePort: 30159注意selector实际是按照POD中容器标签进行选择的,因此该信息必须和POD中设置的标签保持一致; targetPort和容器端口一致,nginx:log默认端口是8080端口
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: fortune-ingress
spec:
rules:
- host: fortune-web.local.com
http:
paths:
- path: /
backend:
serviceName: fortune-web-nodeport
servicePort: 8088
- host: fortune-log.local.com
http:
paths:
- path: /
backend:
serviceName: fortune-log-nodeport
servicePort: 8090通过Ingress对外提供访问策略,分别根据不同的域名选择到不同的服务中。 新版本的Kubernetes提供了新的网络策略接口,在后续文章中详细测试。
- 配置域名和IP地址
在minikube中创建ingress后可以通过:kubectl get ingress 查询ingress信息,该信息中包含了域名和IP地址的对应信息,在/etc/hosts中配置该信息即可在本机通过域名访问服务。
测试结果
查询资源信息

修改hosts文件

访问web和log服务



