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

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 [email protected] "

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



Welcome to nginx!



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

[root@master manifest]# curl 192.168.143.142:30000 Welcome to nginx!

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

//添加存储卷,查看负载均衡的区别,真实情况不需要区别,只需要只供存储卷内容,做负载均衡 [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 443/TCP 9d web NodePort 10.100.92.239 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,有很多优点

你可能感兴趣的:(k8s,kubernetes)