k8s实践案例-nginx+tomcat+nfs实现动静分离

nginx镜像构建

构建nginx基础镜像
#ngix 1.22.0 base image
FROM harbor-server.linux.io/base-images/ubuntu:20.04

ADD nginx-1.22.0.tar.gz /usr/local/src

RUN cd /usr/local/src/nginx-1.22.0 && ./configure  && make && make install && ln -sv  /usr/local/nginx/sbin/nginx /usr/sbin/nginx  &&rm -rf /usr/local/src/nginx-1.22.0.tar.gz

执行构建,上传镜像

nerdctl build -t harbor-server.linux.io/pub-images/nginx-base:1.22.0 .
nerdctl  push harbor-server.linux.io/pub-images/nginx-base:1.22.0
构建nginx业务镜像
FROM harbor-server.linux.io/pub-images/nginx-base:1.22.0

ADD nginx.conf /usr/local/nginx/conf/
ADD index.html /usr/local/nginx/html/
ADD webapp.tar.gz /usr/local/nginx/html/webapp/

RUN mkdir -p /usr/local/nginx/html/webapp/images /usr/local/nginx/html/webapp/static
RUN useradd nginx -u 2000

EXPOSE 80 443

CMD ["nginx"]

nginx.conf内容如下

user  nginx;
worker_processes auto;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;
daemon off;

events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    upstream tomcat-webserver {
        server tomcat-myapp-service.default.svc.cluster.local;	#指定tomcat service的域名
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location /webapp {
            root   html;
            index  index.html index.htm;
        }

        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;
        }
        #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   html;
        }
    }
}

执行构建上传镜像

nerdctl build -t harbor-server.linux.io/n70/nginx-web:v1 .
nerdctl push harbor-server.linux.io/n70/nginx-web:v1

部署tomcat

tomcat的业务镜像在https://blog.csdn.net/weixin_43266367/article/details/126761930?spm=1001.2014.3001.5501中完成

tomcat的部署yaml文件如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-myapp
spec:
  replicas: 2
  selector:
    matchLabels:
       app: tomcat-myapp
  template:
    metadata:
      labels:
        app: tomcat-myapp
    spec:
     containers:
     - name: myapp
       image: harbor-server.linux.io/n70/tomcat-myapp:v1
       imagePullPolicy: IfNotPresent
       ports:
       - name: http
         containerPort: 8080
       - name: https
         containerPort: 8443
       volumeMounts:
       - name: images-file
         mountPath: /usr/local/nginx/html/webapp/images
         readOnly: false
       - name: static-file
         mountPath: /usr/local/nginx/html/webapp/static
         readOnly: false
     volumes:
     - name: images-file
       nfs:
         server: 192.168.122.1
         path: /data/k8s/n70/images
     - name: static-file
       nfs:
         server: 192.168.122.1
         path: /data/k8s/n70/static

---
apiVersion: v1
kind: Service
metadata:
  name: tomcat-myapp-service
spec:
  selector:
    app: tomcat-myapp
  ports:
  - name: http
    port: 80
    targetPort: 8080
    protocol: TCP
  - name: https
    port: 443
    targetPort: 8443
    protocol: TCP

创建之后查看pod状态,并通过service访问tomcat测试
在这里插入图片描述

部署nginx

nginx部署文件如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-web-frontend
spec:
  replicas: 1
  selector:
    matchExpressions:
    - {key: app, operator: In, values: ["nginx-web-frontend"]}
  template:
    metadata:
      labels:
        app: nginx-web-frontend
    spec:
      containers:
      - name: nginx-frontend
        image: harbor-server.linux.io/n70/nginx-web:v1
        imagePullPolicy: Always
        ports:
        - name: http
          containerPort: 80
        - name: https
          containerPort: 443
        volumeMounts:
        - name: images-file
          mountPath: /usr/local/nginx/html/webapp/images
          readOnly: false
        - name: static-file
          mountPath: /usr/local/nginx/html/webapp/static
          readOnly: false
      volumes:
      - name: images-file
        nfs:
          server: 192.168.122.1
          path: /data/k8s/n70/images
      - name: static-file
        nfs:
          server: 192.168.122.1
          path: /data/k8s/n70/static

---
apiVersion: v1
kind: Service
metadata:
  name: nginx-frontend-service
spec:
  type: NodePort
  selector:
    app: nginx-web-frontend
  ports:
  - name: http
    port: 80
    targetPort: 80
    nodePort: 30010
    protocol: TCP
  - name: https
    port: 443
    targetPort: 443
    nodePort: 30011
    protocol: TCP

部署后查看pod状态
在这里插入图片描述

访问测试

nginx默认页面
在这里插入图片描述
nginx webapp路径默认页面
在这里插入图片描述
访问/myapp/ 转到tomcat页面
在这里插入图片描述
在nfs目录中上传图片,然后访问测试
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一种常见的架构组合,用于构建高可用、负载均衡和容灾的Web应用系统。下面是每个组件的简要介绍: LVS(Linux Virtual Server):LVS是一个在Linux内核中实现的负载均衡工具。它通过将输入流量分发到多个后端服务器,以提高系统的性能和可靠性。 keepalived:keepalived是一个基于VRRP(Virtual Router Redundancy Protocol)的高可用性解决方案。它可以监控服务器的健康状态,并在主服务器故障时自动切换到备份服务器。 nginxnginx是一个高性能的HTTP和反向代理服务器。它可以同时处理静态和动态内容,并提供负载均衡和高可用性功能。 tomcattomcat是一个开源的Java Servlet容器,用于运行Java Web应用程序。它可以与nginx配合使用,处理动态内容。 mysql:mysql是一个流行的关系型数据库管理系统,常用于存储应用程序的数据。 MHA(MySQL Master High Availability):MHA是一个用于MySQL主从复制环境的高可用性解决方案。它可以自动监控主服务器的健康状态,并在主服务器故障时自动切换到备份服务器。 NFS(Network File System):NFS是一种分布式文件系统协议,允许远程服务器通过网络访问共享文件。在这种架构中,NFS可以用于共享静态文件或其他数据,以提供一致的内容访问。 这种架构组合可以提供高可用性、负载均衡和容灾能力,适用于大型Web应用系统。但是具体的实施和配置需要根据具体需求和环境来确定。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值