一、阐明
本文介绍基于 Kubernetes(k8s)
环境集成阿里云 公有镜像仓库 来部署一套 Dubbo
+ Nacos
的微服务零碎,并应用 Kubernetes DNS
以及 port-forward
的形式来买通网络拜访。
- k8s 环境搭建参考文章《基于 minikube 疾速搭建 kubernetes 单节点环境》中在阿里云镜像仓库构建的镜像来进行部署。
- 基于《基于 jib-maven-plugin 疾速构建微服务 docker 镜像》中在阿里云镜像仓库构建的镜像来进行部署。
二、部署 MySQL
创立配置文件 mysql-local.yaml
内容如下:
apiVersion: v1
kind: ReplicationController
metadata:
name: mysql
labels:
name: mysql
spec:
replicas: 1
selector:
name: mysql
template:
metadata:
labels:
name: mysql
spec:
containers:
- name: mysql
image: nacos/nacos-mysql:5.7
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: "root"
- name: MYSQL_DATABASE
value: "nacos_devtest"
- name: MYSQL_USER
value: "nacos"
- name: MYSQL_PASSWORD
value: "nacos"
volumes:
- name: mysql-data
hostPath:
path: /var/lib/mysql
---
apiVersion: v1
kind: Service
metadata:
name: mysql
labels:
name: mysql
spec:
ports:
- port: 3306
targetPort: 3306
selector:
name: mysql
ReplicationController 简称 RC 能够保障在任意工夫运行 Pod 的正本数量,可能保障 Pod 总是可用的。
执行以下命令,部署 MySQL 5.7:
kubectl apply -f mysql-local.yaml
三、部署 Nacos
创立配置文件 nacos-standalone-start.yaml
内容如下:
---
apiVersion: v1
kind: Service
metadata:
name: nacos-standalone
labels:
app: nacos-standalone
spec:
type: ClusterIP
clusterIP: None
ports:
- port: 8848
name: server
targetPort: 8848
- port: 9848
name: client-rpc
targetPort: 9848
- port: 9849
name: raft-rpc
targetPort: 9849
## 兼容 1.4.x 版本的选举端口
- port: 7848
name: old-raft-rpc
targetPort: 7848
selector:
app: nacos
---
apiVersion: v1
kind: ConfigMap
metadata:
name: nacos-cm
data:
mysql.host: "mysql"
mysql.db.name: "nacos_devtest"
mysql.port: "3306"
mysql.user: "nacos"
mysql.password: "nacos"
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nacos
spec:
replicas: 1
template:
metadata:
labels:
app: nacos
annotations:
pod.alpha.kubernetes.io/initialized: "true"
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: "app"
operator: In
values:
- nacos
topologyKey: "kubernetes.io/hostname"
containers:
- name: nacos
imagePullPolicy: Always
image: nacos/nacos-server:latest
resources:
requests:
memory: "1Gi"
cpu: "500m"
ports:
- containerPort: 8848
name: client
- containerPort: 9848
name: client-rpc
- containerPort: 9849
name: raft-rpc
- containerPort: 7848
name: old-raft-rpc
env:
- name: SPRING_DATASOURCE_PLATFORM
value: "mysql"
- name: MYSQL_SERVICE_HOST
valueFrom:
configMapKeyRef:
name: nacos-cm
key: mysql.host
- name: MYSQL_SERVICE_DB_NAME
valueFrom:
configMapKeyRef:
name: nacos-cm
key: mysql.db.name
- name: MYSQL_SERVICE_PORT
valueFrom:
configMapKeyRef:
name: nacos-cm
key: mysql.port
- name: MYSQL_SERVICE_USER
valueFrom:
configMapKeyRef:
name: nacos-cm
key: mysql.user
- name: MYSQL_SERVICE_PASSWORD
valueFrom:
configMapKeyRef:
name: nacos-cm
key: mysql.password
- name: MODE
value: "standalone"
- name: NACOS_SERVER_PORT
value: "8848"
- name: PREFER_HOST_MODE
value: "hostname"
selector:
matchLabels:
app: nacos
应用
ConfigMap
对象来配置 MySQL 的参数;Nacos
通过DNS
来拜访数据库的Service
。
执行以下命令,部署 Nacos 最新版本:
kubectl apply -f nacos-standalone-start.yaml
执行以下命令,裸露 Nacos 的端口到宿主机中给内部拜访:
nohup kubectl port-forward svc/nacos-standalone 8848:8848 9848:9848 9849:9849 7848:7848 --address='0.0.0.0' &
kubectl port-forward 通过端口转发映射本地端口到指定的利用端口,它将在您的计算机和 kubernetes 之间创立一条隧道;个别用于测试、实验室、故障排除,而不是长期的解决方案。
四、部署 Dubbo 服务
4.1. 创立镜像仓库的密钥
因为拉取阿里云仓库的公有镜像时须要输出账户和明码,须要用到 k8s 的 Secret
对象来治理明码秘钥等敏感信息。
执行以下命令,创立 Secret 对象:
kubectl create secret docker-registry aliyuncs \
--docker-server=registry.cn-guangzhou.aliyuncs.com \
--docker-username=zltdiablo@163.com \
--docker-password=xxxxxx
- docker-registry 指定 secret 的名称
- docker-server 仓库地址
- docker-username 仓库账号
- docker-password 仓库明码
创立胜利后,能够应用以下命令查看密钥信息:
kubectl get secret aliyuncs --output=yaml
4.2. 部署 provider 服务
创立配置文件 provider.yaml
内容如下:
---
apiVersion: v1
kind: Service
metadata:
name: zlt-nacos-provider
spec:
clusterIP: None
selector:
app: zlt-nacos-provider
ports:
- protocol: TCP
port: 20880
targetPort: 20880
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: zlt-nacos-provider
spec:
replicas: 1
selector:
matchLabels:
app: zlt-nacos-provider
template:
metadata:
labels:
app: zlt-nacos-provider
spec:
imagePullSecrets:
- name: aliyuncs
containers:
- name: server
image: registry.cn-guangzhou.aliyuncs.com/zlt-test/nacos-provider:1.0-SNAPSHOT
imagePullPolicy: IfNotPresent
ports:
- containerPort: 20880
env:
- name: DUBBO_REGISTRY_ADDRESS
value: "nacos://nacos-standalone:8848"
- name: DUBBO_IP_TO_REGISTRY
value: "zlt-nacos-provider"
DUBBO_REGISTRY_ADDRESS 参数指定注册核心地址,应用
DNS
来拜访Nacos
DUBBO_IP_TO_REGISTRY 参数指定服务注册的 IP 地址,配置本人
Service
的名称通过
imagePullSecrets
参数来绑定登录镜像仓库所应用的secret
名称。
执行以下命令,部署 provider 最新版本:
kubectl apply -f provider.yaml
4.3. 部署 consumer 服务
创立配置文件 consumer.yaml
内容如下:
---
apiVersion: v1
kind: Service
metadata:
name: zlt-nacos-consumer
spec:
clusterIP: None
selector:
app: zlt-nacos-consumer
ports:
- name: web
port: 8080
targetPort: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: zlt-nacos-consumer
spec:
replicas: 1
selector:
matchLabels:
app: zlt-nacos-consumer
template:
metadata:
labels:
app: zlt-nacos-consumer
spec:
imagePullSecrets:
- name: aliyuncs
containers:
- name: server
image: registry.cn-guangzhou.aliyuncs.com/zlt-test/nacos-consumer:1.0-SNAPSHOT
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
env:
- name: DUBBO_REGISTRY_ADDRESS
value: "nacos://nacos-standalone:8848"
- name: DUBBO_IP_TO_REGISTRY
value: "zlt-nacos-consumer"
执行以下命令,部署 consumer 最新版本:
kubectl apply -f consumer.yaml
五、测试
通过命令 kubectl get pod
查看所有创立的 pods 确保所有的状态都为 Running
执行以下命令,裸露 consumer 服务的 web 端口到宿主机中给内部拜访:
nohup kubectl port-forward svc/zlt-nacos-consumer 8080:8080 --address='0.0.0.0' &
在浏览器输出以下地址进行拜访:
http:// 宿主机 IP:8080/test?name=123
六、样例工程
集成 jib-maven-plugin
插件的样例 Spring Boot
工程:
- https://gitee.com/zlt2000_admin/zlt-dubbo-samples/tree/master…
- https://github.com/zlt2000/zlt-dubbo-samples/tree/master/dubbo-nacos
扫码关注有惊喜!