akerust / blogs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

给 Minikube 用户的 Istio 导引

opened this issue · comments

给 Minikube 用户的 Istio 导引

准备

启动 k8s 集群

请先阅读《给 macOS 用户的 Minikube 导引》一文,了解 Minikube 的安装与配置。

由于本文使用的 Istio v1.1 对 k8s v1.13.0 兼容支持较好,所以先启动指定版本的 k8s 集群:

socks minikube start --memory=8192 --cpus=4 --kubernetes-version=v1.13.0 --docker-env no_proxy=$no_proxy --docker-env http_proxy=$socks_url --docker-env https_proxy=$socks_url

如果是首次启动 k8s v1.13.0,会下载相应的安装镜像,使用socks别名和docker-env参数完成加速。

下载 Istio 安装包

创建一个实验目录,在该目录下执行:

curl -L https://git.io/getLatestIstio | ISTIO_VERSION=1.1.7 sh -

下载成功后,可以看到如下目录结构:

.
└── istio-1.1.7
    ├── LICENSE
    ├── README.md
    ├── bin
    ├── install
    ├── istio.VERSION
    ├── samples
    └── tools

进入istio-1.1.7目录,后续所有的实验都在该目录下进行:

cd istio-1.1.7

安装

注:下面的步骤请确认在istio-1.1.7目录下进行

添加 CRD(自定义资源定义)

for i in install/kubernetes/helm/istio-init/files/crd*yaml; do kubectl apply -f $i; done

安装 Istio

kubectl apply -f install/kubernetes/istio-demo.yaml

安装过程大约持续 10min-20min,视网络情况而定。

检查安装结果

Services

执行命令查看服务的就绪情况:

kubectl get services -n istio-system

检查输出内容:

NAME                     TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)                                                                                                                                      AGE
grafana                  ClusterIP      10.104.174.221   <none>        3000/TCP                                                                                                                                     36m
istio-citadel            ClusterIP      10.96.108.27     <none>        8060/TCP,15014/TCP                                                                                                                           36m
istio-egressgateway      ClusterIP      10.100.151.111   <none>        80/TCP,443/TCP,15443/TCP                                                                                                                     36m
istio-galley             ClusterIP      10.110.227.101   <none>        443/TCP,15014/TCP,9901/TCP                                                                                                                   36m
istio-ingressgateway     LoadBalancer   10.108.119.42    <pending>     15020:32716/TCP,80:31380/TCP,443:31390/TCP,31400:31400/TCP,15029:30008/TCP,15030:32507/TCP,15031:30568/TCP,15032:30749/TCP,15443:31875/TCP   36m
istio-pilot              ClusterIP      10.105.96.117    <none>        15010/TCP,15011/TCP,8080/TCP,15014/TCP                                                                                                       36m
istio-policy             ClusterIP      10.98.239.221    <none>        9091/TCP,15004/TCP,15014/TCP                                                                                                                 36m
istio-sidecar-injector   ClusterIP      10.102.186.28    <none>        443/TCP                                                                                                                                      36m
istio-telemetry          ClusterIP      10.111.110.10    <none>        9091/TCP,15004/TCP,15014/TCP,42422/TCP                                                                                                       36m
jaeger-agent             ClusterIP      None             <none>        5775/UDP,6831/UDP,6832/UDP                                                                                                                   36m
jaeger-collector         ClusterIP      10.110.36.113    <none>        14267/TCP,14268/TCP                                                                                                                          36m
jaeger-query             ClusterIP      10.97.210.125    <none>        16686/TCP                                                                                                                                    36m
kiali                    ClusterIP      10.99.166.70     <none>        20001/TCP                                                                                                                                    36m
prometheus               ClusterIP      10.98.15.254     <none>        9090/TCP                                                                                                                                     36m
tracing                  ClusterIP      10.96.42.77      <none>        80/TCP                                                                                                                                       36m
zipkin                   ClusterIP      10.100.80.107    <none>        9411/TCP                                                                                                                                     36m

确保每个 k8s 服务都分配了CLUSTER-IP

由于我们的 k8s 集群运行在不提供外部负载均衡器支持的 Minikube 上,所以istio-ingressgatewayEXTERNAL-IP<pending>,如果要访问这个网关,需要通过服务的NodePort或使用端口转发才能实现。

Pods

执行命令查看 Pods 的就绪情况:

kubectl get pods -n istio-system

检查输出内容:

NAME                                      READY   STATUS      RESTARTS   AGE
grafana-77b49c55db-lhdv9                  1/1     Running     0          30m
istio-citadel-66d49b64fc-mdf7f            1/1     Running     0          30m
istio-cleanup-secrets-1.1.7-xp9ft         0/1     Completed   0          30m
istio-egressgateway-68d9cfdd4-52824       1/1     Running     0          30m
istio-galley-676599ffb4-ttvb6             1/1     Running     0          30m
istio-grafana-post-install-1.1.7-gt7js    0/1     Completed   0          30m
istio-ingressgateway-7fbf7bcf45-pk5ct     1/1     Running     0          30m
istio-pilot-56b4dd7bd7-jhnqc              2/2     Running     0          30m
istio-policy-7bcc6d45df-jdj5v             2/2     Running     8          30m
istio-security-post-install-1.1.7-nrvst   0/1     Completed   0          30m
istio-sidecar-injector-779544894b-d5n6g   1/1     Running     0          30m
istio-telemetry-d6f5cd5d9-rlr55           2/2     Running     8          30m
istio-tracing-595796cf54-fb7h9            1/1     Running     0          30m
kiali-5c584d45f6-rqc4v                    1/1     Running     0          30m
prometheus-5fffdf8848-52pd7               1/1     Running     0          30m

确保每个 Pod 都已进入RunningCompleted状态。

体验

注:下面的步骤请确认在istio-1.1.7目录下进行

部署 Bookinfo 应用

启用 Sidecar 自动注入

Istio 默认启用Sidecar 自动注入,只需为default命名空间打上标签istio-injection=enabled

kubectl label namespace default istio-injection=enabled
部署应用

执行命令部署 Bookinfo 应用:

kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml
检查部署结果

执行命令查看服务状态:

kubectl get services

检查输出内容:

NAME          TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
details       ClusterIP   10.102.227.44    <none>        9080/TCP   20h
kubernetes    ClusterIP   10.96.0.1        <none>        443/TCP    22h
productpage   ClusterIP   10.100.152.61    <none>        9080/TCP   20h
ratings       ClusterIP   10.105.130.154   <none>        9080/TCP   20h
reviews       ClusterIP   10.108.230.68    <none>        9080/TCP   20h

执行命令查看 Pod 状态:

kubectl get pods

检查输出内容:

NAME                              READY   STATUS    RESTARTS   AGE
details-v1-5df567f4f6-bxpgk       2/2     Running   4          20h
productpage-v1-5df8c67478-bxmjv   2/2     Running   4          20h
ratings-v1-7fb4544f5b-vbfhp       2/2     Running   4          20h
reviews-v1-7df6c8697-k7cvg        2/2     Running   4          20h
reviews-v2-7f4845bd8f-mtlnh       2/2     Running   4          20h
reviews-v3-fc5847d9c-kz6bp        2/2     Running   4          20h

创建应用网关

为了使应用可以被 k8s 集群外部的程序访问,需要创建一个入口网关

kubectl apply -f samples/bookinfo/networking/bookinfo-gateway.yaml

执行命令查看网关创建结果:

kubectl get gateway

检查输出内容:

NAME               AGE
bookinfo-gateway   12s

确定网关地址

执行下列命令获取入口网关的地址:

export INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].nodePort}')
export INGRESS_HOST=$(minikube ip)
export GATEWAY_URL=$INGRESS_HOST:$INGRESS_PORT

GATEWAY_URL环境变量的值便是入口网关的地址。

访问应用页面

可以先通过命令行检查应用是否正确提供了服务页面:

curl -s http://$GATEWAY_URL/productpage | grep -o "<title>.*</title>"

如果一切顺利,可以得到输出:

<title>Simple Bookstore App</title>

当然,我们也可以打开浏览器访问这串地址,你会注意到多次刷新页面,界面右侧的书评区将得到不一样的渲染结果,这是正常现象。

清理

清理应用资源

执行命令删除路由规则、停止应用 Pod:

./samples/bookinfo/platform/kube/cleanup.sh

确认相关资源已被全部清理:

kubectl get virtualservices # 输出:No resources found.
kubectl get destinationrules # 输出:No resources found.
kubectl get gateway # 输出:No resources found.
kubectl get pods # 输出:No resources found.

清理 Istio 资源

kubectl delete -f install/kubernetes/istio-demo.yaml

因为有些资源会被级联删除,所以会出现一些无法找到资源的提示,可以忽略。

确认相关资源已被全部清理:

kubectl get services -n istio-system # 输出:No resources found.
kubectl get pods -n istio-system # 输出:No resources found.

停止 k8s 集群

minikube stop

删除 k8s 集群

minikube delete

参考