Kubernetes(k8s)实例部署java项目、部署nginx负载均衡

java部署项目

[root@master manifest]# yum install -y java-11-openjdk
[root@master manifest]# yum -y install maven git
[root@master manifest]# git clone https://gitee.com/jinchenghe92/tomcat-java-demo.git
[root@master manifest]# cd tomcat-java-demo
[root@master tomcat-java-demo]# mvn clean package -Dmaven.test.skip=true
[root@master tomcat-java-demo]# ls target/
classes
generated-sources
ly-simple-tomcat-0.0.1-SNAPSHOT
ly-simple-tomcat-0.0.1-SNAPSHOT.war
maven-archiver
maven-status
[root@master tomcat-java-demo]# yum install -y unzip
[root@master tomcat-java-demo]# unzip target/*.war -d target/ROOT
[root@master tomcat-java-demo]# ls target/
classes                              maven-archiver
generated-sources                    maven-status
ly-simple-tomcat-0.0.1-SNAPSHOT      ROOT
ly-simple-tomcat-0.0.1-SNAPSHOT.war
[root@master tomcat-java-demo]# ls target/ROOT/
META-INF  WEB-INF
[root@master tomcat-java-demo]# > Dockerfile 
[root@master tomcat-java-demo]# vim Dockerfile 
[root@master tomcat-java-demo]# cat Dockerfile 
FROM tomcat
LABEL MANTAINER "hy 1@2.com "
COPY target/ROOT /usr/local/tomcat/webapps/ROOT
[root@master tomcat-java-demo]# docker build -t hyhxy0206/demo.v0.1 .
Sending build context to Docker daemon  70.86MB
Step 1/3 : FROM tomcat
latest: Pulling from library/tomcat
0e29546d541c: Pull complete 
9b829c73b52b: Pull complete 
cb5b7ae36172: Pull complete 
6494e4811622: Pull complete 
668f6fcc5fa5: Pull complete 
dc120c3e0290: Pull complete 
8f7c0eebb7b1: Downloading  54.07MB/55.47MB
77b694f83996: Download complete 
0f611256ec3a: Download complete 
4f25def12f23: Download complete 
[root@master tomcat-java-demo]# docker login
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
[root@master tomcat-java-demo]# docker push hyhxy0206/demo.v0.1
[root@master tomcat-java-demo]# docker run -d --rm --name web1 -p 8080:8080 hyhxy0206/demo.v0.1
ee6481049478796ac0dc621e2c7e7d8a70c9f4b4a97784c56bfff9d36b11f21b
[root@master tomcat-java-demo]# ss -atnl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128    127.0.0.1:10248               *:*                  
LISTEN     0      128    127.0.0.1:10249               *:*                  
LISTEN     0      128    192.168.143.140:2379                *:*                  
LISTEN     0      128    127.0.0.1:2379                *:*                  
LISTEN     0      128    192.168.143.140:2380                *:*                  
LISTEN     0      128    127.0.0.1:2381                *:*                  
LISTEN     0      128     *:111                 *:*                  
LISTEN     0      128     *:8080                *:*                  
LISTEN     0      128    127.0.0.1:10257               *:*                  
LISTEN     0      128    127.0.0.1:10259               *:*                  
LISTEN     0      128     *:22                  *:*                  
LISTEN     0      128    127.0.0.1:35287               *:*                  
LISTEN     0      100    127.0.0.1:25                  *:*                  
LISTEN     0      128    :::10250              :::*                  
LISTEN     0      128    :::6443               :::*                  
LISTEN     0      128    :::111                :::*                  
LISTEN     0      128    :::8080               :::*                  
LISTEN     0      128    :::10256              :::*                  
LISTEN     0      128    :::22                 :::*                  
LISTEN     0      100       ::1:25                 :::*  

nginx负载均衡

//一个pod跑多个nginx
[root@master manifest]# vi nginx.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: web
        image: nginx
apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  selector:
    app: nginx
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    nodePort: 30000
[root@master manifest]# kubectl apply -f nginx.yaml 
deployment.apps/web created
service/web created
//查看pods是否能正常访问
[root@master manifest]# curl  192.168.143.141:30000
<!DOCTYPE html>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@master manifest]# curl  192.168.143.142:30000
<!DOCTYPE html>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
//添加存储卷,查看负载均衡的区别,真实情况不需要区别,只需要只供存储卷内容,做负载均衡
[root@master manifest]# vim nginx.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: web
        image: nginx
        volumeMounts: 
        - mountPath: /usr/share/nginx/html
          name: webstorage
          readOnly: True
      volumes: 
      - name: webstorage
        hostPath: 
          path: /var/www/html
apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  selector:
    app: nginx
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    nodePort: 30000
[root@master manifest]# kubectl apply -f nginx.yaml 
deployment.apps/web configured
service/web unchanged
[root@node1 ~]# echo 'node1' > /var/www/html/index.html
[root@node2 ~]# echo 'node2' > /var/www/html/index.html
[root@master manifest]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        9d
web          NodePort    10.100.92.239   <none>        80:30000/TCP   15m
//mater查看service的ip负载均衡
[root@master manifest]# curl http://10.100.92.239
node2
[root@master manifest]# curl http://10.100.92.239
node1
[root@master manifest]# curl http://10.100.92.239
node1
[root@master manifest]# curl http://10.100.92.239
node2
[root@master manifest]# curl http://10.100.92.239
node2
[root@master manifest]# curl http://10.100.92.239
node2
//真实ip也实现了负载均衡的效果
[root@master manifest]# curl 192.168.143.140:30000
node1
[root@master manifest]# curl 192.168.143.140:30000
node2
[root@master manifest]# curl 192.168.143.140:30000
node2
[root@master manifest]# curl 192.168.143.140:30000
node1
[root@master manifest]# curl 192.168.143.140:30000
node2
[root@master manifest]# curl 192.168.143.140:30000
node2

k8s lngress知识点

一.什么是Ingress(出口)

K8s的Pod和Service需要通过NodePort把服务暴露到外部, 但是随着微服务的增多。 端口会变得不好管理。 所以通常情况下 我们会设计一个Ingress来做路由的转发,方便统一管理。

二.Ingress组成

Ingress 主要分为两部分

第一部分Ingress Controller(控制器) 是流量的入口,是一个实体软件, 一般是Nginx 和 Haproxy 。
第二部分Ingress 描述具体的路由规则。

三.Ingress作用

1 ingress 减少了基础设施费用

使用 ingress,就只需要一个 负载均衡,然后使用 ingress controller 分发 多个服务的流量

2 可伸缩的 LB 架构

3、ingress 对象允许分布式的配置管理

3.1 管理多个小文件,而不是一个巨大的 LB 配置文件。
3.2 可以隔离实验性的配置、生产配置,通过 git 版本管理就能轻松做到
3.3 简化了运维和开发的耦合,运维准备好环境,开发更新即可
3.4 ingress controller 是一个软件定义的 LB,有很多优点

Kubernetes(k8s)实例部署java项目、部署nginx负载均衡java部署项目[root@master manifest]# yum install -y java-11-openjdk[root@master manifest]# yum -y install maven git[root@master manifest]# git clone https://gitee.com/jinchenghe92/tomcat-java-demo.git[root@master manif
最近发现以上线的tomcat web服务静态资源加载比较慢, 而且每次都会重新从服务端拉取, 这里肯定是有缺陷并且需要改进的. 然后决定使用nginx解决, 通过配置静态资源缓存实现动静分离, 同时发现k8s的dns解析果然简单粗暴, 非常实用, 直接无侵入式的实现了nginx负载均衡(通过在server块的地址里直接配置SVC), 搞定! 后面我也会单独写一篇k8s的coredns/kubedns的工作原理, 和大家一起探讨. 下面是详细的配置文件 Nginx.conf # 静态资源加速, 动静分离 实验要求: (1)Kubernetes 区域可采用 Kubeadm 方式进行安装。 (2)要求在 Kubernetes 环境中,通过yaml文件的方式,创建2个Nginx Pod分别放置在两个不同的节点上,Pod使用hostPath类型的存储卷挂载,节点本地目录共享使用 /data,2个Pod副本测试页面二者要不同,以做区分,测试页面可自己定义。 (3)编写service对应的y...
1. 下载最新的稳定版 v10.16.3 到本地 wget https://nodejs.org/dist/v10.16.3/node-v10.16.3-linux-x64.tar.xz 2. 下载完成后, 将其解压 tar xvJf node-v10.16.3-linux-x64.tar.xz 3. 将解压的 Node.js 目录移动到 /usr/local 目录下
要在 Windows Server 上部署 Java 项目并使用 Nginx 作为反向代理,可以按照以下步骤进行操作: 1. 安装 Java 开发环境:在 Windows Server 上安装 Java 开发环境,确保 Java 运行时环境(JRE)或 Java 开发工具包(JDK)已正确安装。 2. 配置 Java 环境变量:将 Java 安装路径添加到系统的环境变量中,以便系统可以找到 Java 的执行文件。 3. 安装 Nginx:下载并安装 Windows 版本的 Nginx,你可以从 Nginx 官方网站或其他可信来源获取安装程序。 4. 配置 Nginx:编辑 Nginx 的配置文件(通常是 `nginx.conf`),添加一个新的 server 配置块来配置反向代理。示例如下: server { listen 80; server_name your_domain.com; location / { proxy_pass http://localhost:8080; // 将请求转发到 Java 项目的地址和端口 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 在上述示例中,将请求转发到本地的 `localhost:8080` 地址和端口上,你需要根据实际情况修改这些配置。 5. 启动 Nginx:启动 Nginx 服务,确保它正常运行。 6. 启动 Java 项目:启动你的 Java 项目,确保它监听在配置文件中指定的端口(上述示例中为 `8080`)。 7. 测试访问:使用浏览器或其他工具,通过访问你的域名或服务器的 IP 地址来测试访问 Java 项目是否正常。 通过以上步骤,你就可以在 Windows Server 上成功部署 Java 项目并使用 Nginx 进行反向代理。