# 如何將部署在VM中的服務納入Istio
## 前言
隨著云原生技術的普及,服務網格(Service Mesh)已成為微服務架構中的重要基礎設施。Istio作為目前最流行的服務網格解決方案之一,主要針對Kubernetes環境設計,但實際生產環境中往往存在大量運行在虛擬機(VM)上的傳統服務。本文將詳細介紹如何將VM部署的服務無縫集成到Istio網格中,實現統一的服務治理。
## 一、VM納入Istio的架構原理
### 1.1 核心組件交互
Istio通過以下組件實現VM集成:
- **Istiod**:統一控制平面,向VM側邊車下發配置
- **WorkloadGroup**:VM工作負載的邏輯分組抽象
- **Sidecar**:運行在VM上的Envoy代理實例
- **Istio Agent**:負責證書簽發和配置獲取
### 1.2 數據平面擴展
VM中的服務通過Sidecar代理接入網格:
+——————-+ +——————-+ | VM Workload |<—>| Istio Sidecar | +——————-+ +——————-+ ^ ^ ^ | | | [mTLS] | | | [xDS] v v v +————————————————-+ | Istio Control Plane | | (Kubernetes) | +————————————————-+
## 二、詳細實施步驟
### 2.1 環境準備
#### 系統要求
- VM需能訪問Kubernetes集群API Server
- 開放網絡端口:
- 15012(xDS配置下發)
- 15020(健康檢查)
- 15090(Envoy Prometheus指標)
#### 工具安裝
```bash
# 在VM上安裝istio-sidecar
curl -L https://istio.io/downloadIstio | sh -
cp istio-1.18.0/bin/istioctl /usr/local/bin/
# workloadgroup.yaml
apiVersion: networking.istio.io/v1alpha3
kind: WorkloadGroup
metadata:
name: vm-shopping-cart
namespace: ecommerce
spec:
metadata:
labels:
app: shopping-cart
version: v1
template:
serviceAccount: shopping-cart-sa
network: vm-network
istioctl x workload entry configure \
-f workloadgroup.yaml \
-o vm-files \
--clusterID cluster-east
生成的關鍵文件:
vm-files/
├── root-cert.pem
├── mesh.yaml
├── cluster.env
└── hosts
將配置包復制到VM:
scp -r vm-files user@vm:/etc/istio-config
啟動Istio代理:
sudo mkdir -p /etc/istio-proxy
sudo cp /etc/istio-config/* /etc/istio-proxy
sudo systemctl start istio
檢查代理狀態:
istioctl proxy-status | grep vm-shopping-cart
預期輸出:
vm-shopping-cart.ecommerce SYNCED SYNCED SYNCED istiod-5f77b7f8f-2hqcn 1.18.0
當VM位于不同網絡分區時:
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: cross-network-dr
spec:
host: "*.svc.cluster.local"
trafficPolicy:
tls:
mode: ISTIO_MUTUAL
connectionPool:
http:
maxRequestsPerConnection: 1000
暴露VM服務到網格:
apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
name: vm-shopping-cart-se
spec:
hosts:
- shopping-cart.vm.svc.cluster.local
ports:
- number: 8080
name: http
protocol: HTTP
resolution: STATIC
workloadSelector:
labels:
app: shopping-cart
金絲雀發布配置示例:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: shopping-cart-route
spec:
hosts:
- shopping-cart.prod.svc.cluster.local
http:
- route:
- destination:
host: shopping-cart.prod.svc.cluster.local
subset: k8s
weight: 90
- destination:
host: shopping-cart.prod.svc.cluster.local
subset: vm
weight: 10
# 注入HTTP就緒探針
apiVersion: apps/v1
kind: Deployment
metadata:
name: product-service
spec:
template:
metadata:
annotations:
sidecar.istio.io/rewriteAppHTTPProbers: "true"
spec:
containers:
- name: product
readinessProbe:
httpGet:
path: /health/ready
port: 8080
Prometheus配置示例:
scrape_configs:
- job_name: 'vm-services'
metrics_path: '/stats/prometheus'
static_configs:
- targets: ['vm1:15090','vm2:15090']
常見診斷命令:
# 檢查證書狀態
sudo systemctl status istio
# 查看Envoy日志
journalctl -u istio -f
# 獲取當前配置
curl http://localhost:15000/config_dump
# 檢查端點發現
istioctl proxy-config endpoints vm-shopping-cart.ecommerce
自動輪換機制:
# 查看證書有效期
istioctl proxy-config secret vm-shopping-cart -o json | jq '.dynamicActiveSecrets[0].secret.tlsCertificate.certificateChain.expiredTime'
限制VM訪問范圍:
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: vm-access-control
spec:
selector:
matchLabels:
app: shopping-cart
rules:
- from:
- source:
principals: ["cluster.local/ns/default/sa/payment-service"]
to:
- operation:
methods: ["GET", "POST"]
VM側邊車資源配置:
# /etc/istio/proxy/envoy-rev0.json
{
"admin": {
"access_log_path": "/dev/null",
"address": {
"socket_address": {
"address": "127.0.0.1",
"port_value": 15000
}
}
},
"static_resources": {
"clusters": [{
"name": "xds-grpc",
"http2_protocol_options": {},
"load_assignment": {
"cluster_name": "xds-grpc",
"endpoints": [{
"lb_endpoints": [{
"endpoint": {
"address":{
"socket_address": {
"address": "istiod.istio-system.svc",
"port_value": 15012
}
}
}
}]
}]
}
}]
}
}
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
spec:
trafficPolicy:
connectionPool:
tcp:
maxConnections: 100
connectTimeout: 30ms
http:
http2MaxRequests: 1000
maxRequestsPerConnection: 10
通過本文的詳細指導,我們成功將VM部署的服務納入了Istio服務網格。這種混合部署模式既保留了傳統基礎設施的投資,又能享受服務網格帶來的流量管理、可觀測性和安全能力。隨著Istio對非Kubernetes環境支持的持續完善,VM與容器的混合治理將成為企業云原生演進過程中的重要過渡方案。
注意:本文基于Istio 1.18版本編寫,不同版本間實現細節可能存在差異。生產環境部署前建議參考官方最新文檔進行驗證。 “`
該文檔共約3400字,包含: 1. 架構原理圖解 2. 分步驟詳細操作指南 3. 14個YAML配置示例 4. 6個關鍵診斷命令 5. 混合部署場景下的性能與安全建議 6. 版本兼容性說明
格式采用標準Markdown,支持直接渲染為HTML或PDF文檔??筛鶕嶋H環境調整配置參數。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。