# Deploying Ambassador to Kubernetes
在本教程中,我們將介紹在Kubernetes中部署Ambassador以進行入口路由的過程。Ambassador提供傳統入口控制器的所有功能(即基于路徑的路由),同時暴露許多其他功能,如身份驗證,URL重寫,CORS,速率限制和自動度量收集。
ambassador被設計允許服務所有者控制怎么樣暴露他們的服務到公網上。我們通過允許大量的服務注釋來實現這一點,ambassador讀取這些注釋以配置其Envoy代理。下面,我們將使用服務注釋來配置ambassador映射`/httpbin/`到`httpbin.org`。
## 1.部署ambassador
要在您的默認命名空間中部署Ambassador,首先需要檢查Kubernetes是否啟用了RBAC:
```bash
kubectl cluster-info dump --namespace kube-system | grep authorization-mode
```
如果您`--authorization-mode=Node,RBAC`在輸出中看到類似的內容,則啟用RBAC。大多數當前托管的Kubernetes提供程序(例如GKE)創建默認情況下啟用了RBAC的集群,遺憾的是上述命令可能不會返回任何指示此情況的信息。
注意:如果您使用帶有RBAC的Google Kubernetes引擎,則需要向將要設置大使的帳戶授予權限。為此,請獲取您的官方GKE用戶名,然后cluster-admin為該用戶名授予角色權限:
```
$ kubectl create clusterrolebinding my-cluster-admin-binding --clusterrole=cluster-admin --user=$(gcloud info --format="value(config.account)")
```
如果啟用了RBAC:
```
kubectl apply -f https://getambassador.io/yaml/ambassador/ambassador-rbac.yaml
```
沒有RBAC,您可以使用:
```
kubectl apply -f https://getambassador.io/yaml/ambassador/ambassador-no-rbac.yaml
```
我們建議您下載YAML文件并瀏覽內容。您將看到`ambassador-admin`創建了NodePort服務(提供了 ambassador 診斷Web UI),以及ambassador ClusterRole,ServiceAccount和ClusterRoleBinding(如果啟用了RBAC)。還創建了 ambassador 部署。
對于生產配置,我們建議您下載這些YAML文件作為起點,并相應地自定義它們(例如,您的命名空間)。
## 2. Defining the Ambassador Service
ambassador 部署為Kubernetes service,引用您之前部署的ambassador deployment。創建以下YAML并將其放在一個名為`ambassador-service.yaml`的文件中。
```yaml
---
apiVersion: v1
kind: Service
metadata:
name: ambassador
spec:
type: LoadBalancer
ports:
- port: 80
selector:
service: ambassador
```
部署此服務kubectl:
```
$ kubectl apply -f ambassador-service.yaml
```
上面的YAML為 ambassador 創建了Kubernetes服務LoadBalancer。將根據您創建的路由規則評估所有HTTP流量。請注意,如果您未在LoadBalancer支持類型的環境(例如minikube)中進行部署,則需要將其更改為其他類型的服務,例如NodePort。
暴漏服務有多種方式:LoadBalancer、NodePort、Ingress
如果這里我們使用NodePort暴漏服務,k8s默認的服務暴漏端口范圍是30000~32767,當然這個端口的范圍可以在啟動apiserver的時候進行修改,指定--service-node-port-range=1-65535,修改為需要的端口范圍,最好是不要將常見服務的端口包含在內,否則容易沖突。
```yaml
---
apiVersion: v1
kind: Service
metadata:
labels:
service: ambassador
name: ambssador
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 30009
selector:
service: ambassador
```
這里采用NodePort方式暴漏到服務器的30009端口。可以根據需要自己制定。
## 3. Creating your first route
Create the following YAML and put it in a file called httpbin.yaml.
```yaml
---
apiVersion: v1
kind: Service
metadata:
name: httpbin
annotations:
getambassador.io/config: |
---
apiVersion: ambassador/v0
kind: Mapping
name: httpbin_mapping
prefix: /httpbin/
service: httpbin.org:80
host_rewrite: httpbin.org
spec:
ports:
- name: httpbin
port: 80
```
Then, apply it to the Kubernetes with kubectl:
```bash
$ kubectl apply -f httpbin.yaml
```
部署服務時,Ambassador將注意到getambassador.io/config服務上的注釋,并使用其中Mapping包含的注釋來配置路由。(對于注釋中可以使用哪種ambassador配置沒有限制,但重要的是要注意 ambassador 只關注Kubernetes Service的注釋。)
在這種情況下,映射會創建一條路由,將路由從`/httpbin/`端點路由到公共`httpbin.org`服務。請注意,我們使用的`host_rewrite`屬性`httpbin_mapping-` 這會強制HTTP Host標頭,并且在映射到外部服務時通常是個好主意。
## 4.測試映射
為了測試一下,我們需要 ambassador 的外部IP(可能需要一些時間才能使用):
```
kubectl get svc -o wide ambssador
```
Eventually, this should give you something like:
```
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ambassador 10.11.12.13 35.36.37.38 80:31656/TCP 1m
```
You should now be able to use curl to httpbin (don't forget the trailing /):
如果是LoadBalancer
```
$ curl 35.36.37.38/httpbin/
```
如果是NodePort訪問的url:http://ip:30009/httpbin/,ip為kubernetes服務器的ip
## 5. Adding a Service
您可以通過使用適當的Ambassador注釋部署它來添加服務路由。例如,我們可以在此群集中本地部署QoTM服務,并通過qotm.yaml使用以下配置創建,通過Ambassador自動映射它:
```yaml
---
apiVersion: v1
kind: Service
metadata:
name: qotm
annotations:
getambassador.io/config: |
---
apiVersion: ambassador/v0
kind: Mapping
name: qotm_mapping
prefix: /qotm/
service: qotm
spec:
selector:
app: qotm
ports:
- port: 80
name: http-qotm
targetPort: http-api
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: qotm
spec:
replicas: 1
strategy:
type: RollingUpdate
template:
metadata:
labels:
app: qotm
spec:
containers:
- name: qotm
image: datawire/qotm:1.1
ports:
- name: http-api
containerPort: 5000
resources:
limits:
cpu: "0.1"
memory: 100Mi
```
然后應用它:
```bash
kubectl apply -f qotm.yaml
```
在QoTM服務運行幾秒鐘后,應該為它配置大使。嘗試一下
```bash
$ curl http://${AMBASSADOR_IP}/qotm/
```
## 6. Kubernetes的診斷服務
Ambassador包括一個集成的診斷服務,以幫助進行故障排除 默認情況下,這不會暴露給Internet。要查看它,我們需要獲得其中一個ambassador pod的名稱:
```bash
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
ambassador-3655608000-43x86 1/1 Running 0 2m
ambassador-3655608000-w63zf 1/1 Running 0 2m
```
將本地端口8877轉發到其中一個pod:
```bash
kubectl port-forward ambassador-3655608000-43x86 8877
```
然后讓我們在http//localhost8877/ambassador/v0/diag/查看診斷信息。