1. Kubernetes
本文档提供了在 kubernetes 上运行 micro 的指南.
1.1. 开始
1.2. 依赖
在 kubernetes 我们建议运行 etcd 和 nats.
- etcd 用于高度可扩展的服务发现
- NATS 用于异步消息传递
安装 etcd (指南)
helm install --name my-release --set customResources.createEtcdClusterCRD=true stable/etcd-operator
安装 nats (指南)
helm install my-release stable/nats
您现在应该具有所需的依赖项.
1.3. 部署
下面是 micro 服务的 k8s 部署示例
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
namespace: default
name: greeter
spec:
replicas: 1
selector:
matchLabels:
name: greeter-srv
micro: service
template:
metadata:
labels:
name: greeter-srv
micro: service
spec:
containers:
- name: greeter
command: [
"/greeter-srv",
]
image: micro/go-micro
imagePullPolicy: Always
ports:
- containerPort: 8080
name: greeter-port
env:
- name: MICRO_SERVER_ADDRESS
value: "0.0.0.0:8080"
- name: MICRO_BROKER
value: "nats"
- name: MICRO_BROKER_ADDRESS
value: "nats-cluster"
- name: MICRO_REGISTRY
value: "etcd"
- name: MICRO_REGISTRY_ADDRESS
value: "etcd-cluster-client"
使用 kubectl 部署
kubectl apply -f greeter.yaml
1.4. Micro API
要部署 micro api, 请使用以下配置. 注意默认情况下 ENABLE_ACME 环境变量需要我们 Let's Encrypt SSL 加密措施.
创建 api 服务
apiVersion: v1
kind: Service
metadata:
name: micro-api
namespace: default
labels:
name: micro-api
micro: service
spec:
ports:
- name: https
port: 443
targetPort: 443
selector:
name: micro-api
micro: service
type: LoadBalancer
创建部署
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: default
name: micro-api
labels:
micro: service
spec:
replicas: 3
selector:
matchLabels:
name: micro-api
micro: service
strategy:
rollingUpdate:
maxSurge: 0
maxUnavailable: 1
template:
metadata:
labels:
name: micro-api
micro: service
spec:
containers:
- name: api
env:
- name: MICRO_ENABLE_STATS
value: "true"
- name: MICRO_BROKER
value: "nats"
- name: MICRO_BROKER_ADDRESS
value: "nats-cluster"
- name: MICRO_REGISTRY
value: "etcd"
- name: MICRO_REGISTRY_ADDRESS
value: "etcd-cluster-client"
- name: MICRO_REGISTER_TTL
value: "60"
- name: MICRO_REGISTER_INTERVAL
value: "30"
- name: MICRO_ENABLE_ACME
value: "true"
args:
- api
image: micro/micro
imagePullPolicy: Always
ports:
- containerPort: 443
name: api-port
1.5. Micro Web
要部署 micro Web, 请使用以下配置 注意默认情况下 ENABLE_ACME 环境变量使用 Let's Encrypt SSL 加密措施.
创建服务
apiVersion: v1
kind: Service
metadata:
name: micro-web
namespace: default
labels:
name: micro-web
micro: service
spec:
ports:
- name: https
port: 443
targetPort: 443
selector:
name: micro-web
micro: service
type: LoadBalancer
创建部署
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: default
name: micro-web
labels:
micro: service
spec:
replicas: 1
selector:
matchLabels:
name: micro-web
micro: service
template:
metadata:
labels:
name: micro-web
micro: service
spec:
containers:
- name: web
env:
- name: MICRO_BROKER
value: "nats"
- name: MICRO_BROKER_ADDRESS
value: "nats-cluster"
- name: MICRO_ENABLE_STATS
value: "true"
- name: MICRO_REGISTRY
value: "etcd"
- name: MICRO_REGISTRY_ADDRESS
value: "etcd-cluster-client"
- name: MICRO_ENABLE_ACME
value: "true"
args:
- web
image: micro/micro
imagePullPolicy: Always
ports:
- containerPort: 443
name: web-port