k8s部署nginx访问Tomcat

1.nginx打包镜像

bash 复制代码
#1、编写DockerFile

mkdir /opt/my_nginx_dockerfile

cd /opt/my_nginx_dockerfile
cat >default.conf<<'EOF' 
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    location /tomcat/ {
        proxy_pass http://tomcat-web-service;
        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   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
EOF



cat  >/opt/my_nginx_dockerfile/Dockerfile << 'EOF'
FROM nginx
WORKDIR /etc/nginx/conf.d
RUN echo "nginx v1 version" >/usr/share/nginx/html/index.html
ADD default.conf /etc/nginx/conf.d
EOF


cd /opt/my_nginx_dockerfile
#2、编译镜像

docker build -t 192.168.1:30012/k8s/my_nginx:v1 .

#3、登陆镜像

docker login -u admin -p Harbor12345 192.168.1:30012

#4、推送至仓库

docker push 192.168.1:30012/k8s/my_nginx:v1

2.Tomcat打包镜像

bash 复制代码
#编写DockerFile

mkdir -p /opt/my_tomcat_dockerfile
cat  >/opt/my_tomcat_dockerfile/Dockerfile << 'EOF'
FROM tomcat:latest
RUN mkdir webapps/ROOT/tomcat -p && echo "My Tomcat v1 version">webapps/ROOT/tomcat/index.html
EOF
cd /opt/my_tomcat_dockerfile

#编译镜像

docker build -t 192.168.1:30012/k8s/my_tomcat:v1 .

#登陆镜像

docker login -u admin -p Harbor12345 192.168.1:30012

#推送至仓库

docker push 192.168.1:30012/k8s/my_tomcat:v1

3.部署nginx

bash 复制代码
cat >nginx-proxy.yml<<'EOF'
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: 192.168.1:30012/k8s/my_nginx:v1
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-web-service
  labels:
    app: nginx-web-service
spec:
  type: NodePort
  selector:
    app: nginx
  ports:
  - protocol: TCP
    name: http
    port: 80
    targetPort: 80
    nodePort: 30086
EOF


kubectl apply -f nginx-proxy.yml

4.部署Tomcat

bash 复制代码
cat >tomcat-proxy.yml<<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
 name: tomcat-deployment
 labels:
   app: tomcat
spec:
 replicas: 1
 selector:
   matchLabels:
     app: tomcat
 template:
   metadata:
     labels:
       app: tomcat
   spec:
     containers:
     - name: tomcat
       image: 192.168.1:30012/k8s/my_tomcat:v1
       ports:
       - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
 name: tomcat-web-service
 labels:
   app: tomcat-web-service
spec:
 type: NodePort
 selector:
   app: tomcat
 ports:
   - protocol: TCP
     name: http
     port: 80
     targetPort: 8080
     nodePort: 30087
EOF

kubectl apply -f tomcat-proxy.yml

#测试

curl http://192.168.1:30086/tomcat/

相关推荐
ajsbxi8 小时前
苍穹外卖学习记录
java·笔记·后端·学习·nginx·spring·servlet
景天科技苑8 小时前
【云原生开发】K8S多集群资源管理平台架构设计
云原生·容器·kubernetes·k8s·云原生开发·k8s管理系统
wclass-zhengge9 小时前
K8S篇(基本介绍)
云原生·容器·kubernetes
颜淡慕潇9 小时前
【K8S问题系列 |1 】Kubernetes 中 NodePort 类型的 Service 无法访问【已解决】
后端·云原生·容器·kubernetes·问题解决
昌sit!17 小时前
K8S node节点没有相应的pod镜像运行故障处理办法
云原生·容器·kubernetes
A ?Charis20 小时前
Gitlab-runner running on Kubernetes - hostAliases
容器·kubernetes·gitlab
尢词20 小时前
SpringMVC
java·spring·java-ee·tomcat·maven
清风百草20 小时前
【04】【Maven项目热部署】将Maven项目热部署到远程tomcat服务器上
tomcat·maven项目热部署
北漂IT民工_程序员_ZG21 小时前
k8s集群安装(minikube)
云原生·容器·kubernetes
JustCouvrir1 天前
macOS|前端工程部署到Nginx服务器
服务器·前端·nginx