本文介紹基於 Kubernetes(k8s)
環境整合阿里雲 私有映象倉庫 來部署一套 Dubbo
+ Nacos
的微服務系統,並使用 Kubernetes DNS
以及 port-forward
的方式來打通網路存取。
- k8s環境搭建參考文章《基於minikube快速搭建kubernetes單節點環境》 中在阿里雲映象倉庫構建的映象來進行部署。
- 基於《基於jib-maven-plugin快速構建微服務docker映象》 中在阿里雲映象倉庫構建的映象來進行部署。
建立組態檔 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-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 之間建立一條隧道;一般用於測試、實驗室、故障排除,而不是長期的解決方案。
由於拉取阿里雲倉庫的私有映象時需要輸入賬戶和密碼,需要用到 k8s 的 Secret
物件來管理密碼祕鑰等敏感資訊。
執行以下命令,建立 Secret 物件:
kubectl create secret docker-registry aliyuncs \
--docker-server=registry.cn-guangzhou.aliyuncs.com \
[email protected] \
--docker-password=xxxxxx
- docker-registry 指定 secret 的名稱
- docker-server 倉庫地址
- docker-username 倉庫賬號
- docker-password 倉庫密碼
建立成功後,可以使用以下命令檢視金鑰資訊:
kubectl get secret aliyuncs --output=yaml
建立組態檔 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
建立組態檔 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
工程:
掃碼關注有驚喜!