wyh-l6

导航

实战-nginx和tomcat实现动静分离(包含镜像分层构建)

harbor地址:
    harbor.wyh.net
master地址:
    192.168.213.11
node地址:
    192.168.213.21
    192.168.213.22
 
创建tomcat的docker文件并上传到harbor(分层构建镜像,不同的Dockerfile放在不同的目录下):
 
系统镜像构建:
cat <<EOF >Dockerfile
#自定义Centos 基础镜像
FROM centos:7.9.2009
MAINTAINER 956860991@qq.com
ADD filebeat-7.12.1-x86_64.rpm /tmp
RUN yum install -y /tmp/filebeat-7.12.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-7.12.1-x86_64.rpm && ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime  && useradd nginx -u 2088
EOF
#构建镜像的脚本
cat <<EOF >build-command.sh
#!/bin/bash
#docker build -t  harbor.wyh.net/test-sy/wyh-centos-base:7.9.2009 .
#docker push harbor.wyh.net/test-sy/wyh-centos-base:7.9.2009
/usr/local/bin/nerdctl build -t harbor.wyh.net/test-sy/wyh-centos-base:7.9.2009 .
/usr/local/bin/nerdctl push harbor.wyh.net/test-sy/wyh-centos-base:7.9.2009
EOF

chmod +x build-command.sh
bash build-command.sh
 
构建jdk镜像(提前下载好jdk的安装包到服务器上):
#根据刚刚构建好的centos镜像来构建jdk镜像
cat <<EOF >Dockerfile FROM harbor.wyh.net/test-sy/wyh-centos-base:7.9.2009 #FROM centos:7.9.2009 MAINTAINER 956860991@qq.com 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 EOF
 
#构建镜像的脚本
cat <<EOF >build-command.sh
#!/bin/bash
#docker build -t harbor.wyh.net/test-sy/jdk-base:v8.212  .
#sleep 1
#docker push  harbor.wyh.net/test-sy/jdk-base:v8.212
nerdctl build -t  harbor.wyh.net/test-sy/jdk-base:v8.212  .  
nerdctl push  harbor.wyh.net/test-sy/jdk-base:v8.212
EOF
 
chmod +x build-command.sh
bash build-command.sh
 
构建tomcat镜像(提前下载好tomcat安装包到服务器上):
#根据刚刚构建好的jdk镜像来构建tomcat镜像
cat <<EOF >Dockerfile
#Tomcat 8.5.43基础镜像
FROM harbor.wyh.net/test-sy/jdk-base:v8.212
MAINTAINER 956860991@qq.com
RUN mkdir /apps /data/tomcat/webapps /data/tomcat/logs -pv
ADD apache-tomcat-8.5.43.tar.gz  /apps
RUN useradd tomcat -u 2050 && ln -sv /apps/apache-tomcat-8.5.43 /apps/tomcat && chown -R tomcat.tomcat /apps /data -R
EOF
 
#构建镜像的脚本
cat <<EOF >build-command.sh
#!/bin/bash
#docker build -t harbor.wyh.net/test-sy/tomcat-base:v8.5.43  .
#sleep 3
#docker push  harbor.wyh.net/test-sy/tomcat-base:v8.5.43
nerdctl  build -t harbor.wyh.net/test-sy/tomcat-base:v8.5.43  .  
nerdctl  push harbor.wyh.net/test-sy/tomcat-base:v8.5.43
EOF
 
chmod +x build-command.sh
bash build-command.sh
 
构建应用镜像(提前准备些index页面或测试程序):
#使用刚刚构建好的tomcat基础镜像
cat <<EOF >Dockerfile #tomcat web1 FROM harbor.wyh.net/test-sy/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 chown -R nginx.nginx /data/ /apps/ #ADD filebeat-7.5.1-x86_64.rpm /tmp/ #RUN cd /tmp && yum localinstall -y filebeat-7.5.1-amd64.deb EXPOSE 8080 8443 CMD ["/apps/tomcat/bin/run_tomcat.sh"] EOF
 
#CMD指定的脚本
cat <<EOF >run_tomcat.sh
#!/bin/bash
#echo "nameserver 223.6.6.6" > /etc/resolv.conf
#echo "192.168.7.248 k8s-vip.example.com" >> /etc/hosts
#/usr/share/filebeat/bin/filebeat -e -c /etc/filebeat/filebeat.yml -path.home /usr/share/filebeat -path.config /etc/filebeat -path.data /var/lib/filebeat -path.logs /var/log/filebeat &
su - nginx -c "/apps/tomcat/bin/catalina.sh start"
tail -f /etc/hosts
EOF
 
#构建镜像的脚本
cat <<EOF >build-command.sh
#!/bin/bash
TAG=\$1
#docker build -t  harbor.wyh.net/test-sy/tomcat-app1:\${TAG} .
#sleep 3
#docker push  harbor.wyh.net/test-sy/tomcat-app1:\${TAG}
nerdctl build -t  harbor.wyh.net/test-sy/tomcat-app1:\${TAG} .
nerdctl push  harbor.wyh.net/test-sy/tomcat-app1:\${TAG}
EOF
 
chmod +x build-command.sh
bash build-command.sh v20220816
 
创建nginx的基础镜像并上传到harbor(根据刚刚生成的centos镜像制作,提前上传好nginx安装包):
#使用构建好的centos基础镜像
cat <<EOF >Dockerfile #Nginx Base Image FROM harbor.wyh.net/test-sy/wyh-centos-base:7.9.2009 MAINTAINER 956860991@qq.com 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 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 EOF
 
#构建镜像的脚本
cat <<EOF >build-command.sh
#!/bin/bash
#docker build -t harbor.wyh.net/test-sy/nginx-base:v1.18.0  .
#sleep 1
#docker push  harbor.wyh.net/test-sy/nginx-base:v1.18.0
nerdctl build -t  harbor.wyh.net/test-sy/nginx-base:v1.22.0  .
nerdctl push harbor.wyh.net/test-sy/nginx-base:v1.22.0
EOF
 
chmod +x build-command.sh
bash build-command.sh
 
构建nginx实际环境(使用刚刚构建的基础镜像):
#使用刚刚构建好的nginx基础镜像
cat <<EOF >Dockerfile #Nginx 1.22.0 FROM harbor.wyh.net/test-sy/nginx-base:v1.22.0 ##修改修改nginx.conf文件代理tomcat的service 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 EXPOSE 80 443 CMD ["nginx"] EOF
 
#构建镜像的脚本
cat <<EOF >build-command.sh
#!/bin/bash
TAG=\$1
#docker build -t harbor.wyh.net/test-sy/nginx-web1:\${TAG} .
#echo "镜像构建完成,即将上传到harbor"
#sleep 1
#docker push harbor.wyh.net/test-sy/nginx-web1:\${TAG}
#echo "镜像上传到harbor完成"
nerdctl build -t harbor.wyh.net/test-sy/nginx-web1:\${TAG} .
nerdctl push harbor.wyh.net/test-sy/nginx-web1:\${TAG}
EOF
 
chmod +x build-command.sh
bash build-command.sh v20220816
 
在Kubernetes上跑服务:
在nfs服务器创建目录:
mkdir -p /data/kubernetes/wyh/{images,static}
 
使用yaml文件运行tomcat:
cat <<EOF >tomcat-app1.yaml
kind: Deployment
#apiVersion: extensions/v1beta1
apiVersion: apps/v1
metadata:
  labels:
    app: wyh-tomcat-app1-deployment-label
  name: wyh-tomcat-app1-deployment
  namespace: wyh-ns
spec:
  replicas: 1
  selector:
    matchLabels:
      app: wyh-tomcat-app1-selector
  template:
    metadata:
      labels:
        app: wyh-tomcat-app1-selector
    spec:
      containers:
      - name: wyh-tomcat-app1-container
        image: harbor.wyh.net/test-sy/tomcat-app1:v20220816
        #command: ["/apps/tomcat/bin/run_tomcat.sh"]
        imagePullPolicy: IfNotPresent
        #imagePullPolicy: Always
        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: wyh-images
          mountPath: /usr/local/nginx/html/webapp/images
          readOnly: false
        - name: wyh-static
          mountPath: /usr/local/nginx/html/webapp/static
          readOnly: false
      volumes:
      - name: wyh-images
        nfs:
          server: 192.168.213.21
          path: /data/kubernetes/wyh/images
      - name: wyh-static
        nfs:
          server: 192.168.213.21
          path: /data/kubernetes/wyh/static
#      nodeSelector:
#        project: wyh
#        app: tomcat
---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: wyh-tomcat-app1-service-label
  name: wyh-tomcat-app1-service
  namespace: wyh-ns
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 8080
    nodePort: 30092
  selector:
    app: wyh-tomcat-app1-selector
EOF
 
kubectl apply -f tomcat-app1.yaml
 
使用yaml文件运行nginx:
cat <<EOF >nginx-app1.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    app: wyh-nginx-deployment-label
  name: wyh-nginx-deployment
  namespace: wyh-ns
spec:
  replicas: 1
  selector:
    matchLabels:
      app: wyh-nginx-selector
  template:
    metadata:
      labels:
        app: wyh-nginx-selector
    spec:
      containers:
      - name: wyh-nginx-container
        image: harbor.wyh.net/test-sy/nginx-web1:v20220817
        #command: ["/apps/tomcat/bin/run_tomcat.sh"]
        #imagePullPolicy: IfNotPresent
        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: 500m
            memory: 512Mi
          requests:
            cpu: 500m
            memory: 256Mi
 
 
        volumeMounts:
        - name: wyh-images
          mountPath: /usr/local/nginx/html/webapp/images
          readOnly: false
        - name: wyh-static
          mountPath: /usr/local/nginx/html/webapp/static
          readOnly: false
      volumes:
      - name: wyh-images
        nfs:
          server: 192.168.213.21
          path: /data/kubernetes/wyh/images
      - name: wyh-static
        nfs:
          server: 192.168.213.21
          path: /data/kubernetes/wyh/static
      #nodeSelector:
      #  group: wyh
---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: wyh-nginx-service-label
  name: wyh-nginx-service
  namespace: wyh-ns
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30090
  - name: https
    port: 443
    protocol: TCP
    targetPort: 443
    nodePort: 30091
  selector:
    app: wyh-nginx-selector
EOF
 
kubectl apply -f tomcat-app1.yaml
 
测试访问nginx资源(http://192.168.213.21:30090/webapp/)
 
测试访问tomcat资源(http://192.168.213.21:30090/myapp/)
 
测试成功!!!!!!!!!!!!!!!!!!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

posted on 2022-08-17 11:09  IT老六  阅读(123)  评论(0编辑  收藏  举报