1. 程式人生 > 其它 >K8S-入門篇-實戰-Nginx+Tomcat+NFS實現動靜分離Web站點

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: /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-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
越學越感到自己的無知