1 配置configMap
1.1 配置cm
[root@k8s-master ~]# kubectl exec -it nginx-deploy-78d8bf4fd7-2xtd2 -n test -- sh -c "cat /etc/nginx/nginx.conf"
[root@k8s-master ~]# kubectl exec -it nginx-deploy-78d8bf4fd7-2xtd2 -n test -- sh -c "cat /usr/share/nginx/html/index.html"
# 在本地创建一个nginx.conf的配置文件
vi /k8s-test/subpath/nginx.conf
vi /k8s-test/subpath/index.html
vi /k8s-test/subpath/nginx-front.html
# 使用nginx.conf创建一个configmap
[root@k8s-master subpath]# kubectl create configmap nginx-conf-cm --from-file=/k8s-test/subpath/nginx.conf
[root@k8s-master subpath]# kubectl create configmap nginx-front-cm --from-file=/k8s-test/subpath/nginx-front.conf
[root@k8s-master subpath]# kubectl create configmap nginx-index-cm --from-file=/k8s-test/subpath/index.html
1.2 文件内容
[root@k8s-master subpath]# cat index.html
<h1>Welcome to nginx from backend!</h1>
###################################################################################################
[root@k8s-master subpath]# cat nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 10249;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
########################################################################
[root@k8s-master subpath]# cat nginx-front.conf
# Backend 是 nginx 的内部标识符,用于命名以下特定的 upstream
upstream Backend {
# hello 是 Kubernetes 中的后端服务所使用的内部 DNS 名称
server nginx-backend;
}
server {
listen 80;
location / {
# 以下语句将流量通过代理方式转发到名为 Backend 的上游
proxy_pass http://Backend;
}
}
2 创建后端
2.1 配置文件
[root@k8s-master nginx]# cat nginx-depoly-backend.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
app: nginx
spec:
selector:
matchLabels:
app: nginx
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.16.1
ports:
- containerPort: 80
volumeMounts:
- name: nginx-index
mountPath: 'usr/share/nginx/html/index.html'
subPath: usr/share/nginx/html/index.html
volumes:
- name: nginx-index
configMap:
name: nginx-index-cm
items:
- key: index.html
path: usr/share/nginx/html/index.html
---
apiVersion: v1
kind: Service
metadata:
name: nginx-backend
labels:
app: nginx
spec:
selector:
app: nginx
ports:
- port: 80
targetPort: 80
3 创建前端
3.1 配置文件
[root@k8s-master nginx]# cat nginx-depoly-frontend.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-frontend
labels:
app: nginx-frontend
spec:
selector:
matchLabels:
app: nginx-frontend
replicas: 1
template:
metadata:
labels:
app: nginx-frontend
spec:
containers:
- name: nginx-frontend
image: nginx:1.16.1
ports:
- containerPort: 80
volumeMounts: # 挂载数据卷
- name: nginx-conf # 要挂载数据卷的名称
mountPath: '/etc/nginx/nginx.conf' # 数据卷的挂载路径
subPath: etc/ngxin/nginx.conf
- name: nginx-front # 要挂载数据卷的名称
mountPath: '/etc/nginx/conf.d/default.conf' # 数据卷的挂载路径
subPath: etc/nginx/conf.d/default.conf
volumes: # 数据卷的定义
- name: nginx-conf # 数据卷的名称
configMap: # 数据卷类型为 configmap
name: nginx-conf-cm # 使用的configmap的名字
items: # 对configMap中的key进行映射,如果不指定,默认会将configmap中所有key全部转换为一个同名的文件
- key: nginx.conf # 指定挂载那个key
path: etc/ngxin/nginx.conf # 挂载后该key重命名为什么名字
- name: nginx-front # 数据卷的名称
configMap: # 数据卷类型为 configmap
name: nginx-front-cm # 使用的configmap的名字
items: # 对configMap中的key进行映射,如果不指定,默认会将configmap中所有key全部转换为一个同名的文件
- key: nginx-front.conf # 指定挂载那个key
path: etc/nginx/conf.d/default.conf # 挂载后该key重命名为什么名字
---
apiVersion: v1
kind: Service
metadata:
name: nginx-frontend
spec:
selector:
app: nginx-frontend
ports:
- protocol: "TCP"
port: 80
targetPort: 80
type: NodePort
4 测试
[root@k8s-node1 ~]# kubectl run -it test --image=busybox:1.28 --rm --restart=Never -- /bin/sh
# wget http://nginx-frontend
Connecting to nginx-frontend (10.105.228.85:80)
index.html 100% |****************************************************************************************************************************| 40 0:00:00 ETA
/ # cat index.html
<h1>Welcome to nginx from backend!</h1>