k8s中nginx+tomcat实现动静分离
Posted 小怪獣55
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了k8s中nginx+tomcat实现动静分离相关的知识,希望对你有一定的参考价值。
1.在k8s环境运行tomcat
镜像构建参考:https://blog.51cto.com/taowenwu/5235431
yum语法基础参考:https://blog.51cto.com/taowenwu/5235191
1.1.创建业务namespace
cat linux36.yaml
------------------------------------
apiVersion: v1 #API版本
kind: Namespace #类型为namespac
metadata: #定义元数据
name: linux36 #namespace名称
------------------------------------
root@k8s-master:~/images/k8s-tomcat# kubectl apply -f linux36.yaml
namespace/linux36 created
root@k8s-master:~/images/k8s-tomcat# kubectl get namespaces
NAME STATUS AGE
default Active 43h
kube-public Active 43h
kube-system Active 43h
linux36 Active 47s
1.2.nfs
apt install nfs-kernel-server
systemctl status nfs-server
/etc/init.d/nfs-kernel-server status
#共享目录
root@k8s-harbor:/data/k8sdata/linux36# pwd
/data/k8sdata/linux36
root@k8s-harbor:/data/k8sdata/linux36# ls
images static
vim /etc/exports
---------------------------------------------
/data/k8sdata/linux36 *(rw,no_root_squash)
---------------------------------------------
showmount -e 192.168.47.47
exportfs -arv
1.3.创建tomcat业务pod
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
labels:
app: linux36-tomcat-app1-deployment-label
name: linux36-tomcat-app1-deployment
namespace: linux36
spec:
replicas: 1
selector:
matchLabels:
app: linux36-tomcat-app1-selector
template:
metadata:
labels:
app: linux36-tomcat-app1-selector
spec:
containers:
- name: linux36-tomcat-app1-container
image: harbor.gesila.com/k8s/tomcat-app1:v1
#command: ["/apps/tomcat/bin/run_tomcat.sh"]
#imagePullPolicy: IfNotPresent
imagePullPolicy: Always
ports:
- containerPort: 8080
protocol: TCP
name: http
env:
- name: "password"
value: "123456"
- name: "age"
value: "18"
resources:
limits:
cpu: 2
memory: "2048Mi"
requests:
cpu: 500m
memory: "1024Mi"
volumeMounts:
- name: linux36-images
mountPath: /data/tomcat/webapps/myapp/images
readOnly: false
- name: linux36-static
mountPath: /data/tomcat/webapps/myapp/static
readOnly: false
volumes:
- name: linux36-images
nfs:
server: 192.168.47.47
path: /data/k8sdata/linux36/images
- name: linux36-static
nfs:
server: 192.168.47.47
path: /data/k8sdata/linux36/static
#nodeSelector: #位置在当前containers参数结束后的部分
# project: linux36 #指定的label标签
---
kind: Service
apiVersion: v1
metadata:
labels:
app: linux36-tomcat-app1-service-label
name: linux36-tomcat-app1-service
namespace: linux36
spec:
type: NodePort
ports:
- name: http
port: 80
protocol: TCP
targetPort: 8080
nodePort: 30003
selector:
app: linux36-tomcat-app1-selector
root@k8s-master:~/images/k8s-tomcat# kubectl apply -f tomcat-app1.yaml
deployment.extensions/linux36-tomcat-app1-deployment configured
service/linux36-tomcat-app1-service unchanged
kubectl get pods -n linux36
2.k8s中nginx+tomcat实现动静分离
实现一个通用的nginx+tomcat动静分离web架构,即用户访问的静态页面和图片在由nginx直接响应,而动态请求则基于location转发至tomcat
2.1.nginx配置文件修改
修改了nginx配置文件,重新打镜像,上传到本地harbor
镜像创建可参考:https://blog.51cto.com/taowenwu/5235431
upstream tomcat_webserver
server linux36-tomcat-app1-service.linux36.svc.linux36.local:80;
server
location /myapp
proxy_pass http://tomcat_webserver;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
2.2.创建nginx业务pod
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
labels:
app: linux36-nginx-deployment-label
name: linux36-nginx-deployment
namespace: linux36
spec:
replicas: 1
selector:
matchLabels:
app: linux36-nginx-selector
template:
metadata:
labels:
app: linux36-nginx-selector
spec:
containers:
- name: linux36-nginx-container
image: harbor.gesila.com/k8s/nginx-web1:v1
#command: ["/apps/tomcat/bin/run_tomcat.sh"]
#imagePullPolicy: IfNotPresent
imagePullPolicy: Always
ports:
- containerPort: 80
protocol: TCP
name: http
- containerPort: 443
protocol: TCP
name: https
env:
- name: "password"
value: "123456"
- name: "age"
value: "18"
resources:
limits:
cpu: 2
memory: 2Gi
requests:
cpu: 500m
memory: 1Gi
volumeMounts:
- name: linux36-images
mountPath: /usr/local/nginx/html/webapp/images
readOnly: false
- name: linux36-static
mountPath: /usr/local/nginx/html/webapp/static
readOnly: false
volumes:
- name: linux36-images
nfs:
server: 192.168.47.47
path: /data/k8sdata/linux36/images
- name: linux36-static
nfs:
server: 192.168.47.47
path: /data/k8sdata/linux36/static
---
kind: Service
apiVersion: v1
metadata:
labels:
app: linux36-nginx-service-label
name: linux36-nginx-service
namespace: linux36
spec:
type: NodePort
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
nodePort: 30002
- name: https
port: 443
protocol: TCP
targetPort: 443
nodePort: 30443
selector:
app: linux36-nginx-selector
root@k8s-master:~/images/k8s-tomcat# kubectl apply -f nginx.yaml
deployment.extensions/linux36-nginx-deployment created
service/linux36-nginx-service created
2.3.查看
3.重新创建业务nginx pod
方式1.删除并重新创建nginx业务镜像
kubectl delete -f nginx.yaml
vim nginx.yaml
-------------------------------------------------------------
image: harbor.magedu.net/linux36/nginx-web1:v1 #更新镜像地址
-------------------------------------------------------------
kubectl apply -f nginx.yaml
方式2.更新nginx业务镜像版本号
#准备新版本nginx业务镜像
docker tag harbor.magedu.net/linux36/nginx-web1:v1 harbor.magedu.net/linux36/nginxweb1:v2
docker push harbor.magedu.net/linux36/nginx-web1:v2
#获取当前deployment
kubectl get deployment -n linux36
----------------------------------------------------------------------------------
NAME READY UP-TO-DATE AVAILABLE AGE
linux36-nginx-deployment 1/1 1 1 7m30s
linux36-tomcat-app1-deployment 1/1 1 1 45m
----------------------------------------------------------------------------------
#执行更新nginx业务镜像版本
kubectl set image deployment/linux36-nginx-deployment linux36-nginxcontainer=harbor.magedu.net/linux36/nginx-web1:v2 \\
-n linux36deployment.extensions/linux36-nginx-deployment image updated
以上是关于k8s中nginx+tomcat实现动静分离的主要内容,如果未能解决你的问题,请参考以下文章