service mesh istio-1.0 快速安裝體驗

簡介

istio是一個service mesh開源實現,由Google/IBM/Lyft共同開發。架構圖以下: html

istio-arch

安裝

安裝k8s集羣

參考文章node

安裝istioctl

# 去下面的地址下載壓縮包
# https://github.com/istio/istio/releases
wget https://github.com/istio/istio/releases/download/1.0.0/istio-1.0.0-linux.tar.gz
tar xf istio-1.0.0-linux.tar.gz

# 安裝配置環境變量
mv istio-1.0.0 /usr/local/
ln -sv /usr/local/istio-1.0.0 /usr/local/istio
echo 'export PATH=/usr/local/istio/bin:$PATH' > /etc/profile.d/istio.sh
source /etc/profile.d/istio.sh
istioctl version
複製代碼

在k8s集羣中安裝istio

# 若是環境不是雲環境,不支持LoadBalancer
# 做以下修改,使得 ingressgateway 監聽在80和443端口
# 修改使用主機端口映射
# 使用此修改版本以後,每臺機器只能運行單個實例
# 大概在3027行左右
cd /usr/local/istio
sudo cp install/kubernetes/istio-demo.yaml install/kubernetes/istio-demo.yaml.ori
sudo vim install/kubernetes/istio-demo.yaml
...
apiVersion: extensions/v1beta1
# kind: Deployment
# 使用DaemonSet部署方式
kind: DaemonSet
metadata:
  name: istio-ingressgateway
  namespace: istio-system
  labels:
    app: ingressgateway
    chart: gateways-1.0.0
    release: RELEASE-NAME
    heritage: Tiller
    app: istio-ingressgateway
    istio: ingressgateway
spec:
  # DaemonSet不支持replicas
  # replicas: 1
  template:
    metadata:
      labels:
        app: istio-ingressgateway
        istio: ingressgateway
      annotations:
        sidecar.istio.io/inject: "false"
        scheduler.alpha.kubernetes.io/critical-pod: ""
    spec:
      serviceAccountName: istio-ingressgateway-service-account
      containers:
        - name: ingressgateway
          image: "gcr.io/istio-release/proxyv2:1.0.0"
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 80
              # 主機80端口映射
              hostPort: 80
            - containerPort: 443
              # 主機443端口映射
              hostPort: 443
...

# 替換鏡像地址
sudo sed -i 's@gcr.io/istio-release@docker.io/istio@g' install/kubernetes/istio-demo.yaml
sudo sed -i 's@quay.io/coreos/hyperkube:v1.7.6_coreos.0@registry.cn-shanghai.aliyuncs.com/gcr-k8s/hyperkube:v1.7.6_coreos.0@g' install/kubernetes/istio-demo.yaml

# 查看鏡像地址
grep 'image:' install/kubernetes/istio-demo.yaml

# 安裝 CRDs
# 等待數秒
kubectl apply -f install/kubernetes/helm/istio/templates/crds.yaml -n istio-system
kubectl get crd

# 安裝不使用認證(不使用tls)
# 若是機器內存太小會沒法成功啓動
# 實驗使用3臺虛擬機每臺3G內存
kubectl apply -f install/kubernetes/istio-demo.yaml

# 查看狀態
kubectl get svc -n istio-system
kubectl get pods -n istio-system
複製代碼

注意linux

istio-1.0.0 默認已經開啓了自動注入功能以及其餘日誌監控和追蹤的相關組件如git

  • istio-tracing
  • istio-telemetry
  • grafana
  • prometheus
  • servicegraph

啓用自動注入 sidecar

  • 不開啓自動注入部署應用須要使用以下方式的命令 kubectl apply -f <(istioctl kube-inject -f samples/bookinfo/kube/bookinfo.yaml)github

  • 開啓自動注入後,使用正常命令便可部署應用 kubectl apply -f samples/bookinfo/kube/bookinfo.yamldocker

# istio-1.0.0 默認已經開啓了自動注入功能

# k8s 1.9 及以後的版本才能使用自動注入功能
# 查看是否支持
kubectl api-versions | grep admissionregistration

# 除了要知足以上條件外還須要檢查kube-apiserver啓動的參數
# k8s 1.9 版本要確保 --admission-control 裏有 MutatingAdmissionWebhook,ValidatingAdmissionWebhook
# k8s 1.9 以後的版本要確保 --enable-admission-plugins 裏有MutatingAdmissionWebhook,ValidatingAdmissionWebhook

# 測試自動注入
# 建立
kubectl apply -f samples/sleep/sleep.yaml 
kubectl get deployment -o wide
kubectl get pod

# 設置 default namespace 開啓自動注入
kubectl label namespace default istio-injection=enabled
kubectl get namespace -L istio-injection

# 刪除建立的pod,等待重建
kubectl delete pod $(kubectl get pod | grep sleep | cut -d ' ' -f 1)

# 查看重建後的pod
# 查看是否有istio-proxy容器
kubectl get pod
kubectl describe pod $(kubectl get pod | grep sleep | cut -d ' ' -f 1)

# 清理
kubectl delete -f samples/sleep/sleep.yaml 

# 關閉自動注入
kubectl label namespace default istio-injection-

# 關閉部分pod的自動注入功能
...
  template:
    metadata:
      annotations:
        sidecar.istio.io/inject: "false"
...
複製代碼

部署官方測試用例

# default開啓自動注入
kubectl label namespace default istio-injection=enabled

# 部署 bookinfo
kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml

# 建立 gateway
kubectl apply -f samples/bookinfo/networking/bookinfo-gateway.yaml

# 查看狀態
kubectl get services
kubectl get pods
istioctl get gateway
複製代碼

訪問測試

# 命令行訪問測試
export INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].nodePort}')
NODE_NAME=$(kubectl get no | grep '<none>' | head -1 | awk '{print $1}')
NODE_IP=$(ping -c 1 $NODE_NAME | grep PING | awk '{print $3}' | tr -d '()')
export GATEWAY_URL=$NODE_IP:$INGRESS_PORT
echo $GATEWAY_URL

curl -o /dev/null -s -w "%{http_code}\n" http://${GATEWAY_URL}/productpage

# 瀏覽器訪問測試
echo "http://${GATEWAY_URL}/productpage"

# 使用daemonset方式部署能夠使用以下方式訪問
# 11.11.11.112爲其中一個node節點的ip
curl http://11.11.11.112/productpage

# 清理
samples/bookinfo/platform/kube/cleanup.sh
複製代碼

清理

# 清理istio
kubectl delete -f install/kubernetes/helm/istio/templates/crds.yaml -n istio-system
kubectl delete -f install/kubernetes/istio-demo.yaml

# kubectl delete -f install/kubernetes/istio-demo-auth.yaml
複製代碼

使用helm安裝istio

安裝helm

參考文章json

安裝istio

# 查看配置
cd /usr/local/istio
egrep -v "^$|#" install/kubernetes/helm/istio/values.yaml

# 安裝 CRDs
kubectl apply -f install/kubernetes/helm/istio/templates/crds.yaml
kubectl get crd

# 根據上面查看的配置和需求配置相關參數
# 部署
helm install install/kubernetes/helm/istio --name istio --namespace istio-system \
--set ingress.enabled=false \
--set global.hub="docker.io/istio" \
--set global.hyperkube.hub="registry.cn-shanghai.aliyuncs.com/gcr-k8s" \
--set gateways.istio-ingressgateway.type=NodePort \
--set gateways.istio-egressgateway.type=NodePort

# 查看
helm ls
kubectl get pods -n istio-system
kubectl get svc -n istio-system

# 運行以前的測試

# 清理
helm delete --purge istio
kubectl delete -f install/kubernetes/helm/istio/templates/crds.yaml -n istio-system
複製代碼

參考文檔

  • https://istio.io/docs/setup/kubernetes/quick-start.html
  • https://istio.io/docs/guides/bookinfo.html
  • https://istio.io/docs/setup/kubernetes/sidecar-injection.html#automatic-sidecar-injection
相關文章
相關標籤/搜索