使用k8s部署nginx文件服务器
需要使用nginx部署一个外网文件服务器使用k8s部署
本次需要把一个apk文件映射到外网
前提条件
- 部署好的k8s集群
- 部署好的存储集群(glusterfs,NFS)
k8s的yaml配置文件
deployment文件
使用以下命令生成再修改
# kubectl run apk-nginx --image=192.168.3.61/foundation/nginx --dry-run -o yaml
# cat apk-nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
run: apk-nginx
name: apk-nginx
spec:
replicas: 1
selector:
matchLabels:
run: apk-nginx
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
run: apk-nginx
spec:
containers:
- image: 192.168.3.61/foundation/nginx
name: apk-nginx
resources: {}
volumeMounts:
- name: apk-nginx-volume
mountPath: "/usr/share/nginx/html"
readOnly: false
- name: apk-nginx
mountPath: /etc/nginx/conf.d/default.conf
subPath: default.conf
volumes:
- name: apk-nginx-volume
persistentVolumeClaim:
claimName: apk-nginx-pvc
- name: apk-nginx
configMap:
name: apk-nginx
status: {}
service的yaml配置文件
本次固定的NodePort的端口号
# cat apk-nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
run: apk-nginx
name: apk-nginx
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
run: apk-nginx
type: NodePort
ports:
- nodePort: 30336
port: 80
status:
loadBalancer: {}
动态存储pvc的yaml文件
前提需要搭建好动态存储本次使用Heketi管理的GlusterFS存储
参考
https://www.cnblogs.com/minseo/p/12575604.html
]# cat apk-nginx-pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: apk-nginx-pvc
namespace: default
#annotations:
# volume.beta.kubernetes.io/storage-class: "glusterfs"
spec:
#与storageclass名字对应
storageClassName: gluster-heketi-storageclass
# ReadWriteOnce:简写RWO,读写权限,且只能被单个node挂载;
# ReadOnlyMany:简写ROX,只读权限,允许被多个node挂载;
# ReadWriteMany:简写RWX,读写权限,允许被多个node挂载;
accessModes:
- ReadWriteMany
resources:
requests:
storage: 2Gi
nginx配置文件
因为需要使用文件服务器修改配置文件如下
# cat default.conf
autoindex on;# 显示目录
autoindex_exact_size on;# 显示文件大小
autoindex_localtime on;# 显示文件时间
charset utf-8,gbk;#如果包含中文文件夹需要加此配置否则乱码
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
}
创建配置文件用于挂载至Pod
# 创建配置文件名称为apk-nginx 是从文件default.conf创建的
# kubectl create configmap apk-nginx --from-file=default.conf
使用以下命令查看创建的配置文件
# kubectl get configmaps apk-nginx -o yaml
apiVersion: v1
data:
default.conf: |
autoindex on;# 显示目录
autoindex_exact_size on;# 显示文件大小
autoindex_localtime on;# 显示文件时间
charset utf-8,gbk;#如果包含中文文件夹需要加此配置否则乱码
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
}
kind: ConfigMap
metadata:
creationTimestamp: "2024-01-18T07:13:53Z"
name: apk-nginx
namespace: default
resourceVersion: "11824529"
selfLink: /api/v1/namespaces/default/configmaps/apk-nginx
uid: 231faf36-b5d1-11ee-ae4a-525400e71cb5
应用所有yaml文件
# kubectl apply -f *.yaml
把需要外网访问的文件复制至容器,因为挂载了存储相当于复制到存储内
# kubectl cp README.md apk-nginx-58dd77957d-mj4k5:/usr/share/nginx/html
访问
任意node的ip+端口
# http://nodeIp:port