nginx对K8s集群中的Service做负载均衡

nginx对K8s集群中的Service做负载均衡,第1张

nginx对K8s集群中的Service做负载均衡 利用k8s部署Java项目
[root@master ~]# git clone  https://gitee.com/hadeian/tomcat-java-demo-master.git
正克隆到 'tomcat-java-demo-master'...
remote: Enumerating objects: 182, done.
remote: Counting objects: 100% (182/182), done.
remote: Compressing objects: 100% (165/165), done.
remote: Total 182 (delta 6), reused 178 (delta 4), pack-reused 0
接收对象中: 100% (182/182), 1.12 MiB | 338.00 KiB/s, 完成.
处理 delta 中: 100% (6/6), 完成.
[root@master ~]# ls
anaconda-ks.cfg    tomcat-java-demo-master

//安装环境
[root@master ~]# yum -y install java-1.8.0-openjdk maven

//编译构建
如果maven构建慢的话,默认是官方源,可以换阿里云的源vim /etc/maven/settings.xml,大概在(159-164行),更换为如下代码
[root@master ~]# vim /etc/maven/settings.xml


 
      central
      central
      aliyun maven
      https://maven.aliyun.com/repository/public
    
  


[root@master tomcat-java-demo-master]# mvn clean install package '-Dmaven.test.skip=true'


在tomcat目录下构建镜像

[root@master tomcat-java-demo-master]#  cd target/
[root@master target]# ls
classes            ly-simple-tomcat-0.0.1-SNAPSHOT      maven-archiver
generated-sources  ly-simple-tomcat-0.0.1-SNAPSHOT.war  maven-status

[root@master tomcat-java-demo-maste]# cat Dockerfile 
FROM lizhenliang/tomcat 
LABEL maintainer www.ctnrs.com
RUN rm -rf /usr/local/tomcat/webapps/
ADD target/ly-simple-tomcat-0.0.1-SNAPSHOT.war /usr/local/tomcat/webapps/ROOT.war


[root@master tomcat-java-demo-master]# docker build -t java-demo:latest .
Sending build context to Docker daemon  45.34MB
Step 1/4 : FROM lizhenliang/tomcat
 ---> 143035d83fdc
Step 2/4 : LABEL maintainer www.ctnrs.com
 ---> Using cache
 ---> c7b67ab30e68
Step 3/4 : RUN rm -rf /usr/local/tomcat/webapps/
 ---> Using cache
 ---> 40884143fd9c
Step 4/4 : ADD target/ly-simple-tomcat-0.0.1-SNAPSHOT.war /usr/local/tomcat/webapps/ROOT.war
 ---> 0c0011c17dca
Successfully built 0c0011c17dca
Successfully tagged java-demo:latest

[root@master ~]# docker tag       0c0011c17dca   bravealove1/java-demo:latest
[root@master ~]# docker push  bravealove1/java-demo:latest
The push refers to repository [docker.io/bravealove1/java-demo]
bbc8874d72c7: Pushed 
99229ce5b652: Pushed 
ceead5ca823f: Mounted from lizhenliang/tomcat 
2353c173a26a: Mounted from lizhenliang/tomcat 
071d8bd76517: Mounted from lizhenliang/tomcat 
latest: digest: sha256:1441380bf704f4b2de3bf650f48dcee239ac70c87bea7c5bc3318d46f533407a size: 1371

创建控制器管理pod

[root@master ~]# vim deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-java-demo
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      project: www
      app: java-demo
  template:
    metadata:
      labels:
        project: www
        app: java-demo
    spec:
      imagePullSecrets:
      - name: registry-pull-secret
      containers:
      - name: tomcat
        image: bravealove1/java-demo:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 8080
          name: web
          protocol: TCP
        resources:
          requests:
            cpu: 0.25
            memory: 1Gi
          limits:
            cpu: 1
            memory: 2Gi
        livenessProbe:
          httpGet:
            path: /
            port: 8080
          initialDelaySeconds: 60
          timeoutSeconds: 20
        readinessProbe:
          httpGet:
            path: /
            port: 8080
          initialDelaySeconds: 60
[root@master ~]# kubectl adpply -f deployment.yaml
deployment.apps/tomcat-java-demo configured
[root@master ~]# kubectl get pods 
NAME                                READY   STATUS    RESTARTS   AGE
NAME                                    READY   STATUS    RESTARTS   AGE
pod/tomcat-java-demo-6d798c6996-fjjvk   1/1     Running   0          1m
pod/tomcat-java-demo-6d798c6996-lbklf   1/1     Running   0          1m
pod/tomcat-java-demo-6d798c6996-strth   1/1     Running   0          1m


暴露应用

[root@master ~]#  cat service.yaml
apiVersion: v1
kind: Service
metadata:
  name: tomcat-java-demo
  namespace: default
spec:
  selector:
    project: www
    app: java-demo
  ports:
  - name: web
    port: 80
    targetPort: 8080
  type: NodePort
[root@master ~]# kubectl  apply -f service.yaml 
service/tomcat-java-demo created

[root@k8s-master java-demo]# kubectl get pod,svc 
NAME                                    READY   STATUS    RESTARTS   AGE
pod/tomcat-java-demo-6d798c6996-fjjvk   1/1     Running   0          3m
pod/tomcat-java-demo-6d798c6996-lbklf   1/1     Running   0          3m
pod/tomcat-java-demo-6d798c6996-strth   1/1     Running   0          3m

NAME                       TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
service/tomcat-java-demo   NodePort   10.1.175.191           80:30187/TCP    19s

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5680740.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存