一 查看tomcat app的server name
root@k8s-master-01:~# kubectl get svc -n wgs
NAME                      TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
wgs-tomcat-app1-service   NodePort   10.100.39.62   <none>        80:40003/TCP   60m
二 查看集群domain
root@k8s-master-01:~# kubectl exec  wgs-tomcat-app1-deployment-764458b74f-xbd7r -n wgs -- cat /etc/resolv.conf
nameserver 10.100.0.2
search wgs.svc.wgs.local svc.wgs.local wgs.local  
options ndots:5
三 nginx配置
3.1 工作目录
root@ceph-node04:/data/k8s/dockerfile/web/nginx/nginx-app01# pwd
/data/k8s/dockerfile/web/nginx/nginx-app01
3.2 修改nginx.conf文件
查看代码
root@ceph-node04:/data/k8s/dockerfile/web/nginx/nginx-tomcat# cat nginx.conf 
user  nginx nginx;
worker_processes  auto;
daemon off;   
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format log_json  '{'
                        '"@timestamp":"$time_iso8601",'
                        '"@source":"$server_addr",'
                        '"@nginx_fields":{'
                            '"x_forwarded_for":"$http_x_forwarded_for",'
                            '"request":"$request",'
                            '"status":"$status",'
                            '"size":"$body_bytes_sent",'
                            '"http_referer":"$http_referer",'
                            '"client":"$remote_addr",'
                            '"responsetime":"$request_time",'
                            '"upstreamtime":"$upstream_response_time",'
                            '"upstreamaddr":"$upstream_addr",'
                            '"request_method":"$request_method",'
                            '"domain":"$host",'
                            '"url":"$uri",'
                            '"request_body":"$request_body",'
                            '"http_user_agent":"$http_user_agent"'
                        '}'
                    '}';
    access_log  logs/access.log  log_json;
    sendfile        on;
   
    keepalive_timeout  65;
    upstream  tomcat_webserver {
        server   wgs-tomcat-app1-service.wgs.svc.wgs.local:80;
    }
    server {
        listen       80;
        server_name  localhost;
        location / {
            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   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
 
3.3 重新构建镜像
root@ceph-node04:/data/k8s/dockerfile/web/nginx/nginx-app01# ./build-command.sh v1.1
Sending build context to Docker daemon  8.192kB
Step 1/6 : FROM 192.168.174.120/baseimages/nginx:1.20.2
 ---> 9e15403c2475
Step 2/6 : ADD nginx.conf /usr/local/nginx/conf/nginx.conf
 ---> Using cache
 ---> 00c339b1c62d
Step 3/6 : ADD webapp  /usr/local/nginx/html/webapp/
 ---> a3db10cedd5d
Step 4/6 : ADD index.html  /usr/local/nginx/html/index.html
 ---> 23191a7a8545
Step 5/6 : EXPOSE 80 443
 ---> Running in d65afdeeae25
Removing intermediate container d65afdeeae25
 ---> 4aef837a8fe6
Step 6/6 : CMD ["nginx"]
 ---> Running in 29536957fb3f
Removing intermediate container 29536957fb3f
 ---> b43430ba271f
Successfully built b43430ba271f
Successfully tagged 192.168.174.120/baseimages/nginx-web1:v1.1
3.4 上传镜像到harbor
root@ceph-node04:/data/k8s/dockerfile/web/nginx/nginx-app01# docker push 192.168.174.120/baseimages/nginx-web1:v1.1
四 更新nginx pod
4.1 修改nginx.yaml
root@k8s-master-01:/data/k8s/yaml# sed -i 's@192.168.174.120/baseimages/nginx-web1:v1.0@192.168.174.120/baseimages/nginx-web1:v1.1@g' nginx/nginx.yaml 
4.2 更新nginx pod
root@k8s-master-01:/data/k8s/yaml# kubectl apply -f nginx/nginx.yaml 
deployment.apps/wgs-nginx-deployment configured
service/wgs-nginx-service unchanged
4.3 查看当前pod
root@k8s-master-01:/data/k8s/yaml# kubectl get pods -n wgs 
NAME                                          READY   STATUS    RESTARTS   AGE
wgs-nginx-deployment-b4475bcf5-k29nv          1/1     Running   0          2m46s
wgs-tomcat-app1-deployment-764458b74f-xbd7r   1/1     Running   0          88m
4.4 验证nginx配置文件
查看代码
root@k8s-master-01:/data/k8s/yaml# kubectl exec wgs-nginx-deployment-b4475bcf5-k29nv  -n wgs -- cat /usr/local/nginx/conf/nginx.conf
user  nginx nginx;
worker_processes  auto;
daemon off;   
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format log_json  '{'
                        '"@timestamp":"$time_iso8601",'
                        '"@source":"$server_addr",'
                        '"@nginx_fields":{'
                            '"x_forwarded_for":"$http_x_forwarded_for",'
                            '"request":"$request",'
                            '"status":"$status",'
                            '"size":"$body_bytes_sent",'
                            '"http_referer":"$http_referer",'
                            '"client":"$remote_addr",'
                            '"responsetime":"$request_time",'
                            '"upstreamtime":"$upstream_response_time",'
                            '"upstreamaddr":"$upstream_addr",'
                            '"request_method":"$request_method",'
                            '"domain":"$host",'
                            '"url":"$uri",'
                            '"request_body":"$request_body",'
                            '"http_user_agent":"$http_user_agent"'
                        '}'
                    '}';
    access_log  logs/access.log  log_json;
    sendfile        on;
   
    keepalive_timeout  65;
    upstream  tomcat_webserver {
        server   wgs-tomcat-app1-service.wgs.svc.wgs.local:80;
    }
    server {
        listen       80;
        server_name  localhost;
        location / {
            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   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
 
4.5 查看当前svc
root@k8s-master-01:/data/k8s/yaml# kubectl get svc -n wgs 
NAME                      TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
wgs-nginx-service         NodePort   10.100.114.185   <none>        80:40002/TCP,443:40443/TCP   14m
wgs-tomcat-app1-service   NodePort   10.100.39.62     <none>        80:40003/TCP                 89m
五 web访问测试
root@k8s-master-01:~# curl http://192.168.174.108:40002/
hello nginx
root@k8s-master-01:~# curl http://192.168.174.108:40002/myapp/
tomcat docker image test