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目录中上传图片,然后访问测试
在这里插入图片描述
在这里插入图片描述

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐