K8S-入门篇-实战-Nginx+Tomcat+NFS实现动静分离Web站点

Nginx+Tomcat+NFS实现动静分离Web站点

Nginx镜像制作规划:

基于基础的centos/ubuntu/alpine镜像,制作公司内部基础镜像-Nginx基础镜像--Nginx业务镜像

 

Centos基础镜像制作

下载基础镜像

]# dokcer pull centos

Dockerfile

from centos:latest
#MAINTAINER 
ADD filebeat-7.6.1-x86_64.rpm /tmp
RUN yum install -y /tmp/filebeat-7.6.1-x86_64.rpm vim wget tree  lrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop &&  rm -rf /etc/localtime /tmp/filebeat-6.8.3-x86_64.rpm && ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && useradd  nginx -u 2019 && useradd www -u 2020

制作镜像

docker build -t  centos:web .

制作Nginx基础镜像

Dockerfile

#Nginx Base Image
FROM centos:web
#MAINTAINER  
RUN yum install -y vim wget tree  lrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop make
ADD nginx-1.14.2.tar.gz /usr/local/src/
RUN cd /usr/local/src/nginx-1.14.2 && ./configure  && make && make install && ln -sv  /usr/local/nginx/sbin/nginx /usr/sbin/nginx  &&rm -rf /usr/local/src/nginx-1.14.2.tar.gz 

制作镜像

docker build -t nginx-base:v1.14.2  .

制作Nginx业务镜像

Dockerfile

FROM nginx-base:v1.14.2

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

#静态资源挂载路径
RUN mkdir -p /usr/local/nginx/html/webapp/static  /usr/local/nginx/html/webapp/images
RUN chown  -R nginx.nginx /usr/local/nginx

EXPOSE 80 443

CMD ["nginx"]

制作镜像

docker build -t nginx-web1:v1

Nginx 业务yaml文件详解

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    app: magedu-nginx-deployment-label
  name: magedu-nginx-deployment
  namespace: linux40
spec:
  replicas: 1
  selector:
    matchLabels:
      app: magedu-nginx-selector
  template:
    metadata:
      labels:
        app: magedu-nginx-selector
    spec:
      containers:
      - name: magedu-nginx-container
        image: nginx-web1:v2
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
          protocol: TCP
          name: http
        - containerPort: 443
          protocol: TCP
          name: https
        env:
        - name: "password"
          value: "123456"
        - name: "age"
          value: "20"
        resources:
          limits:
            cpu: 1
            memory: 512Mi
          requests:
            cpu: 200m
            memory: 246Mi
        volumeMounts:
        - name: magedu-images
          mountPath: /usr/local/nginx/html/webapp/images
          readOnly: false
        - name: magedu-static
          mountPath: /usr/local/nginx/html/webapp/static
          readOnly: false
      volumes:
      - name: magedu-images
        nfs:
          server: 192.168.64.110
          path: /root/data/nfs1
      - name: magedu-static
        nfs:
          server: 192.168.64.110
          path: /root/data/nfs2
---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: magedu-nginx-service-label
  name: magedu-nginx-service
  namespace: linux40
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30800
  - name: https
    port: 443
    protocol: TCP
    targetPort: 443
    nodePort: 30443
  selector:
    app: magedu-nginx-selector

---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  namespace: linux40
  name: linux40-nginx-web1-podautoscaler
  labels:
    app: magedu-nginx-selector
    version: v2beta1
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: magedu-nginx-deployment
  minReplicas: 1
  maxReplicas: 5
  targetCPUUtilizationPercentage: 30

JDK基础镜像制作

Dockerfile

#JDK Base Image
FROM centos:web

#MAINTAINER 

ADD jdk-8u212-linux-x64.tar.gz /usr/local/src/
RUN ln -sv /usr/local/src/jdk1.8.0_212 /usr/local/jdk
ADD profile /etc/profile


ENV JAVA_HOME /usr/local/jdk
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/lib/:$JRE_HOME/lib/
ENV PATH $PATH:$JAVA_HOME/bin

制作镜像

docker build -t jdk-base:v8.212  .

Tomcat基础镜像制作

Dockerfile

#Tomcat 8.5.43基础镜像
FROM jdk-base:v8.212

#MAINTAINER 
RUN mkdir /apps /data/tomcat/webapps /data/tomcat/logs -pv ADD apache-tomcat-8.5.43.tar.gz /apps RUN ln -sv /apps/apache-tomcat-8.5.43 /apps/tomcat && chown -R nginx.nginx /apps /data -R

制作镜像

docker build -t  tomcat-base:v8.5.43  .

Tomcat业务镜像制作

#tomcat web1
#tomcat 业务基础镜像

FROM tomcat-base:v8.5.43

ADD catalina.sh /apps/tomcat/bin/catalina.sh
ADD server.xml /apps/tomcat/conf/server.xml
#ADD myapp/* /data/tomcat/webapps/myapp/
ADD app1.tar.gz /data/tomcat/webapps/myapp/
ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
ADD filebeat.yml /etc/filebeat/filebeat.yml

RUN mkdir -p /usr/local/nginx/html/webapp/images /usr/local/nginx/html/webapp/static
RUN chown  -R nginx.nginx /data/ /apps/
RUN chown  -R nginx.nginx  /usr/local/nginx/

EXPOSE 8080 8443

CMD ["/apps/tomcat/bin/run_tomcat.sh"]

制作镜像

docker build -t  tomcat-app1:v1  .

Tomcat业务镜像yaml

kind: Deployment
#apiVersion: extensions/v1beta1
apiVersion: apps/v1
metadata:
  labels:
    app: magedu-tomcat-app1-deployment-label
  name: magedu-tomcat-app1-deployment
  namespace: linux40
spec:
  replicas: 1
  selector:
    matchLabels:
      app: magedu-tomcat-app1-selector
  template:
    metadata:
      labels:
        app: magedu-tomcat-app1-selector
    spec:
      containers:
      - name: magedu-tomcat-app1-container
        image: tomcat-app1:v3
        #command: ["/apps/tomcat/bin/run_tomcat.sh"]
        imagePullPolicy: IfNotPresent
        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"
        volumeMounts:
        - name: magedu-images
          mountPath: /data/tomcat/webapps/myapp/images
          readOnly: false
        - name: magedu-static
          mountPath: /data/tomcat/webapps/myapp/static
          readOnly: false
      volumes:
      - name: magedu-images
        nfs:
          server: 192.168.64.110
          path: /root/data/nfs1
      - name: magedu-static
        nfs:
          server: 192.168.64.110
          path: /root/data/nfs2

---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: magedu-tomcat-app1-service-label
  name: magedu-tomcat-app1-service
  namespace: linux40
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 8080
    nodePort: 30033
  selector:
    app: magedu-tomcat-app1-selector

---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  namespace: linux40
  name: magedu-tomcat-app1-podautoscaler
  labels:
    app: magedu-tomcat-app1
    version: v2beta1
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    #apiVersion: extensions/v1beta1
    kind: Deployment
    name: magedu-tomcat-app1-deployment
  minReplicas: 2
  maxReplicas: 20
  targetCPUUtilizationPercentage: 60

k8s中nginx+tomcat实现动静分离

实现一个通用的nginx+tomcat动静分离web架构,即用户访问的静态页面和图片在由nginx直接响应,而动态请求
则基于location转发至tomcat。
重点:Nginx基于tomcat的service name转发用户请求到tomcat业务app

查看tomcat service name

]# kubectl   get svc -A
NAMESPACE              NAME                         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
default                kubernetes                   ClusterIP   10.96.0.1        <none>        443/TCP                      326d
kube-system            kube-dns                     ClusterIP   10.96.0.10       <none>        53/UDP,53/TCP,9153/TCP       326d
kube-system            metrics-server               ClusterIP   10.101.130.120   <none>        443/TCP                      44h
kubernetes-dashboard   dashboard-metrics-scraper    ClusterIP   10.109.95.129    <none>        8000/TCP                     105d
kubernetes-dashboard   kubernetes-dashboard         NodePort    10.98.147.122    <none>        443:30002/TCP                105d
linux40                magedu-nginx-service         NodePort    10.106.18.229    <none>        80:30800/TCP,443:30443/TCP   45h
linux40                magedu-tomcat-app1-service   NodePort    10.100.71.240    <none>        80:30033/TCP                 45m

nginx配置文件

upstream  tomcat_webserver {
        server   magedu-tomcat-app1-service.linux40.svc.cluster.local:80;
}

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

 

上传数据到NFS

 data]# tree .
.
├── nfs1
│   ├── 1.jpg
│   └── index.html
└── nfs2
    └── index.html

2 directories, 3 files

在浏览器测试访问

http://192.168.64.110:30800/myapp/images/1.jpg
http://192.168.64.110:30800/myapp/static/
http://192.168.64.110:30800/myapp

 

posted @ 2021-12-05 19:07  不会跳舞的胖子  阅读(326)  评论(1编辑  收藏  举报