龙空技术网

Kubernetes中简单实现基于nginx+tomcat动静分离

技术怪圈 236

前言:

此时咱们对“nginxtomcat502”大概比较着重,小伙伴们都想要分析一些“nginxtomcat502”的相关内容。那么小编在网摘上网罗了一些关于“nginxtomcat502””的相关知识,希望看官们能喜欢,你们快快来了解一下吧!

案例说明:静态页面直接访问nginx, 动态请求通过tomcat

架构图如下:

nginx服务的配置文件nginx.yaml如下:

kind: Deployment#apiVersion: extensions/v1beta1apiVersion: apps/v1metadata:  labels:    app: linux66-nginx-deployment-label  name: linux66-nginx-deployment  namespace: linux66spec:  replicas: 1  selector:    matchLabels:      app: linux66-nginx-selector  template:    metadata:      labels:        app: linux66-nginx-selector    spec:      containers:      - name: linux66-nginx-container        image: nginx        #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---kind: ServiceapiVersion: v1metadata:  labels:    app: linux66-nginx-service-label  name: linux66-nginx-service  namespace: linux66spec:  type: NodePort  ports:  - name: http    port: 80    protocol: TCP    targetPort: 80    nodePort: 30006  - name: https    port: 443    protocol: TCP    targetPort: 443    nodePort: 30443  selector:    app: linux66-nginx-selector

tomcat服务的配置文件tomcat.yaml如下:

root@k8s-master:~/yaml/1202# cat tomcat.yamlkind: Deployment#apiVersion: extensions/v1beta1apiVersion: apps/v1metadata:  labels:    app: linux66-tomcat-app1-deployment-label  name: linux66-tomcat-app1-deployment  namespace: linux66spec:  replicas: 1  selector:    matchLabels:      app: linux66-tomcat-app1-selector  template:    metadata:      labels:        app: linux66-tomcat-app1-selector    spec:      containers:      - name: linux66-tomcat-app1-container        image: tomcat:7.0.94-alpine        #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: 1            memory: "512Mi"          requests:            cpu: 500m            memory: "512Mi"---kind: ServiceapiVersion: v1metadata:  labels:    app: linux66-tomcat-app1-service-label  name: linux66-tomcat-app1-service  namespace: linux66spec:  #type: NodePort  ports:  - name: http    port: 80    protocol: TCP    targetPort: 8080    #nodePort: 40003  selector:    app: linux66-tomcat-app1-selector

创建namespace

kubectl create ns linux66

添加tomcat页面

root@k8s-master:~/yaml/1202# kubectl exec -it -n linux66 linux66-tomcat-app1-deployment-7946f87f8f-28r7b bashkubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.bash-4.4# pwd/usr/local/tomcatbash-4.4# lsBUILDING.txt     NOTICE           RUNNING.txt      include          native-jni-lib   workCONTRIBUTING.md  README.md        bin              lib              tempLICENSE          RELEASE-NOTES    conf             logs             webappsbash-4.4# cd webapps/bash-4.4# lsROOT          docs          examples      host-manager  managerbash-4.4# mkdir loginbash-4.4# lsROOT          docs          examples      host-manager  login         managerbash-4.4# echo "<h1>Login Web Page</h1>" >login/index.jspbash-4.4# exit

修改haproxy配置

defaults        log     global        mode    http        option  httplog        option  dontlognull        timeout connect 5000        timeout client  50000        timeout server  50000        errorfile 400 /etc/haproxy/errors/400.http        errorfile 403 /etc/haproxy/errors/403.http        errorfile 408 /etc/haproxy/errors/408.http        errorfile 500 /etc/haproxy/errors/500.http        errorfile 502 /etc/haproxy/errors/502.http        errorfile 503 /etc/haproxy/errors/503.http        errorfile 504 /etc/haproxy/errors/504.httplisten k8s-cluster-01-6443        bind 172.31.7.188:6443        mode tcp        server k8s-master.host.com 172.31.7.2:6443 check inter 3s fall 3 rise 1#增加如下配置,访问80的时候通过vip转到宿主机的80端口listen k8s-linux66-nginx-80        bind 172.31.7.189:80        mode tcp        server k8s-master.host.com 172.31.7.2:30006 check inter 3s fall 3 rise 1

验证结果

修改nginx配置,访问login时跳转到tomcat服务,一般是把这部分配置打到镜像里面。

root@linux66-nginx-deployment-6bb84c7d7b-jxfnc:/# cat /etc/nginx/conf.d/default.conf server {    listen       80;    listen  [::]:80;    server_name  localhost;    #access_log  /var/log/nginx/host.access.log  main;    location / {        root   /usr/share/nginx/html;        index  index.html index.htm;    }#增加如下配置,访问login页面跳车到tomcat,地址通tomcat的svc地址,最好写全称    location /login{        proxy_pass ;    }#nginx -s reload

静态页面直接通过nginx访问

标签: #nginxtomcat502