k8s实践案例-基于zookeeper实现微服务动态注册和发现
zookeeper的部署在之前的文章中已经实现。
zookeeper的部署在之前的文章https://blog.csdn.net/weixin_43266367/article/details/126856526?spm=1001.2014.3001.5501中已经实现
整体结构图如下:
镜像构建
dubbo provider镜像构建
Dockerfile如下:
#Dubbo provider
FROM harbor-server.linux.io/base-images/ubuntu-jdk-base:8u131
LABEL author="admin@163.com"
RUN mkdir -p /apps/dubbo/provider
ADD dubbo-demo-provider-2.1.5/ /apps/dubbo/provider/
ADD run_java.sh /apps/dubbo/provider/bin/
RUN useradd nginx -u 2022
RUN chown -R nginx.nginx /apps/ && chmod +x /apps/dubbo/provider/bin/*.sh
CMD ["/apps/dubbo/provider/bin/run_java.sh"]
其中dubbo-demo-provider-2.1.5/是dubbo provider的代码目录,需要修改一下里面的配置,指定zookeeper的地址:
cat dubbo-demo-provider-2.1.5/conf/dubbo.properties
#######################
dubbo.container=log4j,spring
dubbo.application.name=demo-provider
dubbo.application.owner=
#修改注册中心的配置,指明zookeeper地址
dubbo.registry.address=zookeeper://zookeeper1.default.svc.cluster.local:2181 | zookeeper://zookeeper2.default.svc.cluster.local:2181 | zookeeper://zookeeper3.default.svc.cluster.local:2181
#dubbo.registry.address=redis://127.0.0.1:6379
#dubbo.registry.address=dubbo://127.0.0.1:9090
dubbo.monitor.protocol=registry
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.log4j.file=logs/dubbo-demo-provider.log
dubbo.log4j.level=WARN
run_java.sh内容如下:
#!/bin/bash
su - nginx -c "/apps/dubbo/provider/bin/start.sh"
tail -f /etc/hosts
执行构建,上传镜像
nerdctl build -t harbor-server.linux.io/n70/dubbo-provider:v1 .
nerdctl push harbor-server.linux.io/n70/dubbo-provider:v1
dubbo cousumer镜像构建
Dockerfile如下:
#Dubbo cousumer
FROM harbor-server.linux.io/base-images/ubuntu-jdk-base:8u131
RUN apt -y install file netcat-traditional net-tools
RUN mkdir -p /apps/dubbo/consumer
ADD dubbo-demo-consumer-2.1.5 /apps/dubbo/consumer/
ADD run_java.sh /apps/dubbo/consumer/bin/
RUN useradd nginx -u 2022
RUN chown -R nginx.nginx /apps/ && chmod +x /apps/dubbo/consumer/bin/*.sh
CMD ["/apps/dubbo/consumer/bin/run_java.sh"]
dubbo-demo-consumer-2.1.5是dubbo consumer的代码,也需要修改下配置,指定zookeeper的地址:
cat dubbo-demo-consumer-2.1.5/conf/dubbo.properties
#################################
dubbo.container=log4j,spring
dubbo.application.name=demo-consumer
dubbo.application.owner=
#修改此处,指定zookeeper地址
dubbo.registry.address=zookeeper://zookeeper1.default.svc.cluster.local:2181 | zookeeper://zookeeper2.default.svc.cluster.local:2181 | zookeeper://zookeeper3.default.svc.cluster.local:2181
#dubbo.registry.address=redis://127.0.0.1:6379
#dubbo.registry.address=dubbo://127.0.0.1:9090
dubbo.monitor.protocol=registry
dubbo.log4j.file=logs/dubbo-demo-consumer.log
dubbo.log4j.level=WARN
run_java.sh内容如下:
#!/bin/bash
su - nginx -c "/apps/dubbo/consumer/bin/start.sh"
tail -f /etc/hosts
执行构建,上传镜像
nerdctl build -t harbor-server.linux.io/n70/dubbo-consumer:v1 .
nerdctl push harbor-server.linux.io/n70/dubbo-consumer:v1
部署应用
dubbo provider部署文件如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: dubbo-provider
spec:
replicas: 2
selector:
matchLabels:
app: dubbo-provider
template:
metadata:
labels:
app: dubbo-provider
spec:
containers:
- name: dubbo-provider
image: harbor-server.linux.io/n70/dubbo-provider:v1
imagePullPolicy: Always
ports:
- name: http
containerPort: 20880
---
apiVersion: v1
kind: Service
metadata:
name: dubbo-provider-svc
spec:
selector:
app: dubbo-provider
type: NodePort
ports:
- name: http
port: 20880
targetPort: 20880
protocol: TCP
创建后查看Pod和Service状态:
dubbo consumer部署文件如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: dubbo-consumer
spec:
replicas: 2
selector:
matchLabels:
app: dubbo-consumer
template:
metadata:
labels:
app: dubbo-consumer
spec:
containers:
- name: dubbo-consumer
image: harbor-server.linux.io/n70/dubbo-consumer:v1
imagePullPolicy: Always
ports:
- name: http
containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: dubbo-consumer-svc
spec:
selector:
app: dubbo-consumer
type: NodePort
ports:
- name: http
port: 80
targetPort: 80
protocol: TCP
创建后查看Pod和Service状态:
测试
查看dubbo consumer的日志,可以获取dubbo provider的地址并访问
扩容一个dubbo provider,验证dubbo consumer能否获取到新的provider地址并访问
查看日志可以访问到新的provider
dubbo admin部署
镜像构建
Dockerfile内容如下:
#Dubbo admin
FROM harbor-server.linux.io/pub-images/tomcat-base:8.5.81
LABEL author="admin@163.com"
RUN apt -y install unzip
ADD server.xml /apps/tomcat/conf/server.xml
ADD logging.properties /apps/tomcat/conf/logging.properties
ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
ADD dubboadmin /data/tomcat/webapps/dubboadmin/
RUN chown -R tomcat.tomcat /apps /data
EXPOSE 8080 8443
CMD ["/apps/tomcat/bin/run_tomcat.sh"]
server.xml中修改了项目路径和项目名称
logging.properties是日志相关配置,内容如下:
handlers = 1catalina.org.apache.juli.AsyncFileHandler, 2localhost.org.apache.juli.AsyncFileHandler, 3manager.org.apache.juli.AsyncFileHandler, 4host-manager.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler
.handlers = 1catalina.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler
############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################
1catalina.org.apache.juli.AsyncFileHandler.level = FINE
1catalina.org.apache.juli.AsyncFileHandler.directory = /data/tomcat/logs
1catalina.org.apache.juli.AsyncFileHandler.prefix = catalina.
2localhost.org.apache.juli.AsyncFileHandler.level = FINE
2localhost.org.apache.juli.AsyncFileHandler.directory = /data/tomcat/logs
2localhost.org.apache.juli.AsyncFileHandler.prefix = localhost.
3manager.org.apache.juli.AsyncFileHandler.level = FINE
3manager.org.apache.juli.AsyncFileHandler.directory = /data/tomcat/logs
3manager.org.apache.juli.AsyncFileHandler.prefix = manager.
4host-manager.org.apache.juli.AsyncFileHandler.level = FINE
4host-manager.org.apache.juli.AsyncFileHandler.directory = /data/tomcat/logs
4host-manager.org.apache.juli.AsyncFileHandler.prefix = host-manager.
java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter
############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.AsyncFileHandler
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers = 3manager.org.apache.juli.AsyncFileHandler
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].handlers = 4host-manager.org.apache.juli.AsyncFileHandler
run_tomcat.sh内容如下:
#!/bin/bash
su - tomcat -c "/apps/tomcat/bin/catalina.sh start"
tail -f /etc/hosts
dubboadmin 目录是dubbo admin代码目录,需要修改里面dubboadmin/WEB-INF/dubbo.properties配置文件致命zookeeper的地址:
cat dubboadmin/WEB-INF/dubbo.properties
################
#修改此处,指定zookeeper地址
dubbo.registry.address=zookeeper://zookeeper1.default.svc.cluster.local:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
执行构建,上传镜像
nerdctl build -t harbor-server.linux.io/n70/dubbo-admin:v1 .
nerdctl push harbor-server.linux.io/n70/dubbo-admin:v1
部署dubbo admin
部署文件如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: dubbo-admin
spec:
replicas: 1
selector:
matchLabels:
app: dubbo-admin
template:
metadata:
labels:
app: dubbo-admin
spec:
containers:
- name: dubbo-admin
image: harbor-server.linux.io/n70/dubbo-admin:v1
imagePullPolicy: Always
ports:
- name: http
containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: dubbo-admin-svc
spec:
selector:
app: dubbo-admin
type: NodePort
ports:
- name: http
port: 80
targetPort: 8080
nodePort: 30005
创建之后查看Pod和Serivice:
通过nodeip:30005 访问dubbo admin页面测试
通过dubbo admin可以看到在注册中心中的服务
更多推荐
所有评论(0)