k8s中nginx+tomcat实现动静分离

网友投稿 288 2022-09-11

k8s中nginx+tomcat实现动静分离

1.在k8s环境运行tomcat

镜像构建参考:​​linux36.yaml------------------------------------apiVersion: v1 #API版本kind: Namespace #类型为namespacmetadata: #定义元数据 name: linux36 #namespace名称------------------------------------root@k8s-master:~/images/k8s-tomcat# kubectl apply -f linux36.yaml namespace/linux36 createdroot@k8s-master:~/images/k8s-tomcat# kubectl get namespacesNAME STATUS AGEdefault Active 43hkube-public Active 43hkube-system Active 43hlinux36 Active 47s

1.2.nfs

apt install nfs-kernel-serversystemctl status nfs-server/etc/init.d/nfs-kernel-server status#共享目录root@k8s-harbor:/data/k8sdata/linux36# pwd/data/k8sdata/linux36root@k8s-harbor:/data/k8sdata/linux36# lsimages staticvim /etc/exports ---------------------------------------------/data/k8sdata/linux36 *(rw,no_root_squash)---------------------------------------------showmount -e 192.168.47.47exportfs -arv

1.3.创建tomcat业务pod

kind: DeploymentapiVersion: extensions/v1beta1metadata: labels: app: linux36-tomcat-app1-deployment-label name: linux36-tomcat-app1-deployment namespace: linux36spec: 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: 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: ServiceapiVersion: v1metadata: labels: app: linux36-tomcat-app1-service-label name: linux36-tomcat-app1-service namespace: linux36spec: type: NodePort ports: - name: 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 configuredservice/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

镜像创建可参考:​​tomcat_webserver { server linux36-tomcat-app1-service.linux36.svc.linux36.local:80;}server { location /myapp { proxy_pass 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: DeploymentapiVersion: extensions/v1beta1metadata: labels: app: linux36-nginx-deployment-label name: linux36-nginx-deployment namespace: linux36spec: 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: - containerPort: 443 protocol: TCP name: 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: ServiceapiVersion: v1metadata: labels: app: linux36-nginx-service-label name: linux36-nginx-service namespace: linux36spec: type: NodePort ports: - name: port: 80 protocol: TCP targetPort: 80 nodePort: 30002 - name: port: 443 protocol: TCP targetPort: 443 nodePort: 30443 selector: app: linux36-nginx-selector

root@k8s-master:~/images/k8s-tomcat# kubectl apply -f nginx.yamldeployment.extensions/linux36-nginx-deployment createdservice/linux36-nginx-service created

2.3.查看

3.重新创建业务nginx pod

方式1.删除并重新创建nginx业务镜像

kubectl delete -f nginx.yamlvim 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:v2docker push harbor.magedu.net/linux36/nginx-web1:v2#获取当前deploymentkubectl get deployment -n linux36----------------------------------------------------------------------------------NAME READY UP-TO-DATE AVAILABLE AGElinux36-nginx-deployment 1/1 1 1 7m30slinux36-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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:优秀的营销模式一定要是好的表达!
下一篇:免费常用的快递物流查询api接口介绍,快递接口api
相关文章

 发表评论

暂时没有评论,来抢沙发吧~