k8s部署nginx访问Tomcat的实现示例

 更新时间:2023年08月08日 08:38:59   作者:大虾别跑  
本文介绍了如何使用Kubernetes部署Nginx,并通过Nginx访问Tomcat,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1.nginx打包镜像

#1、编写DockerFile
mkdir /opt/my_nginx_dockerfile
cd /opt/my_nginx_dockerfile
cat >default.conf<<'EOF' 
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;
    }
    location /tomcat/ {
        proxy_pass http://tomcat-web-service;
        proxy_set_header   Host    $host;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
    }
    #error_page  404              /404.html;
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
EOF
cat  >/opt/my_nginx_dockerfile/Dockerfile << 'EOF'
FROM nginx
WORKDIR /etc/nginx/conf.d
RUN echo "nginx v1 version" >/usr/share/nginx/html/index.html
ADD default.conf /etc/nginx/conf.d
EOF
cd /opt/my_nginx_dockerfile
#2、编译镜像
docker build -t 192.168.1:30012/k8s/my_nginx:v1 .
#3、登陆镜像
docker login -u admin -p Harbor12345 192.168.1:30012
#4、推送至仓库
docker push 192.168.1:30012/k8s/my_nginx:v1

2.Tomcat打包镜像

#编写DockerFile
mkdir -p /opt/my_tomcat_dockerfile
cat  >/opt/my_tomcat_dockerfile/Dockerfile << 'EOF'
FROM tomcat:latest
RUN mkdir webapps/ROOT/tomcat -p && echo "My Tomcat v1 version">webapps/ROOT/tomcat/index.html
EOF
cd /opt/my_tomcat_dockerfile
#编译镜像
docker build -t 192.168.1:30012/k8s/my_tomcat:v1 .
#登陆镜像
docker login -u admin -p Harbor12345 192.168.1:30012
#推送至仓库
docker push 192.168.1:30012/k8s/my_tomcat:v1

3.部署nginx

cat >nginx-proxy.yml<<'EOF'
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: 192.168.1:30012/k8s/my_nginx:v1
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-web-service
  labels:
    app: nginx-web-service
spec:
  type: NodePort
  selector:
    app: nginx
  ports:
  - protocol: TCP
    name: http
    port: 80
    targetPort: 80
    nodePort: 30086
EOF
kubectl apply -f nginx-proxy.yml

4.部署Tomcat

cat >tomcat-proxy.yml<<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
 name: tomcat-deployment
 labels:
   app: tomcat
spec:
 replicas: 1
 selector:
   matchLabels:
     app: tomcat
 template:
   metadata:
     labels:
       app: tomcat
   spec:
     containers:
     - name: tomcat
       image: 192.168.1:30012/k8s/my_tomcat:v1
       ports:
       - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
 name: tomcat-web-service
 labels:
   app: tomcat-web-service
spec:
 type: NodePort
 selector:
   app: tomcat
 ports:
   - protocol: TCP
     name: http
     port: 80
     targetPort: 8080
     nodePort: 30087
EOF
kubectl apply -f tomcat-proxy.yml

测试

curl http://192.168.1:30086/tomcat/

到此这篇关于k8s部署nginx访问Tomcat的实现示例的文章就介绍到这了,更多相关k8s部署nginx访问Tomcat内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Nginx中报错:Permission denied与Connection refused的解决

    Nginx中报错:Permission denied与Connection refused的解决

    这篇文章主要给大家介绍了在Nginx中报错:13: Permission denied与111: Connection refused的解决方法,文中介绍的非常详细,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
    2017-04-04
  • nginx如何配置参数以及变量

    nginx如何配置参数以及变量

    这篇文章主要介绍了nginx如何配置参数以及变量问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • CentOS 7下安装Nginx服务器

    CentOS 7下安装Nginx服务器

    这篇文章主要为大家详细介绍了CentOS 7下安装Nginx服务器的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • Nginx使用mirror指令实现接口复制

    Nginx使用mirror指令实现接口复制

    Nginx中使用mirro指令可以方便地实现接口请求的复制,这个功能非常适合用于流量监控、数据收集或负载均衡,下面我们就来看看具体的用法吧
    2024-10-10
  • nginx反向代理用做内网域名转发

    nginx反向代理用做内网域名转发

    这篇文章主要为大家详细介绍了nginx反向代理用做内网域名转发,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • Nginx+uwsgi+ssl配置https的详细步骤

    Nginx+uwsgi+ssl配置https的详细步骤

    nginx是一个轻量级的web服务器,在处理静态资源和高并发有优势,uwsgi是一个基于python的高效率的协议,处理后端和动态网页有优势,我这里使用的是Ubuntu18.04版本,服务器在阿里云,感兴趣的朋友跟随小编一起看看吧
    2023-10-10
  • Nginx中配置防盗链的方法实现

    Nginx中配置防盗链的方法实现

    在数字化时代,保护网站内容免受盗链至关重要,Nginx防盗链通过检查请求头Referer字段来拒绝非法来源请求,本文就来详细的介绍一下,感兴趣的可以了解一下
    2024-10-10
  • nginx配置完rewrite浏览器提示将您重定向的次数过多的解决方法

    nginx配置完rewrite浏览器提示将您重定向的次数过多的解决方法

    本文主要介绍了nginx配置完rewrite浏览器提示将您重定向的次数过多的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • Nginx支持websocket的配置详解

    Nginx支持websocket的配置详解

    本文主要介绍了Nginx支持websocket的配置,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • Nginx SSI指令配置详解

    Nginx SSI指令配置详解

    这篇文章主要介绍了Nginx SSI指令配置详解,本文讲解了什么是SSI、为什么要用SSI、nginx配置SSI、页面上配置、配置示例等内容,需要的朋友可以参考下
    2015-04-04

最新评论