前言
本章主要介绍istio在k8s中的安装和入门使用
一.k8s中安装istio
1.安装k8s
我们选择的测试环境是Docker Desktop,安装和配置方式如下:
K8S环境搭建(使用docker-desktop)_docker和k8s环境搭建及使用_c流火j的博客-CSDN博客
Download Docker Desktop | Docker
安装成功后,可以看到
2.安装istio
下载地址:Releases · istio/istio · GitHub
或者执行命令下载安装包:
curl -L https://istio.io/downloadIstio | ISTIO_VERSION=1.13.0 sh -
解压后:
将bin目录下的istioctl添加到path中,然后检查下确保所有的前置条件已经具备,执行命令:
istioctl x precheck
✔ No issues found when checking the cluster.
➥Istio is safe to install or upgrade!
To get started, check out
➥https://istio.io/latest/docs/setup/getting-started/
进行demo模式安装,执行如下命令,等待一会后
$ istioctl install --set profile=demo -y
✔ Istio core installed
✔ Istiod installed
✔ Ingress gateways installed
✔ Egress gateways installed
✔ Installation complete
如果安装过程中your cluster doesn’t support thirdparty JSON Web Token (JWT) authentication错误提醒,可以根据下面文档进行配置解决
Istioldie 1.11 / Security Best Practices
安装完成后,执行下述命令,在istio-system空间中可以看到新增了三个pod在运行,分别是control plane,ingressgateway,egressgateway
kubectl get pod -n istio-system
最后,校验下安装是否正确,执行
istioctl verify-install
✔ Istio is installed and verified successfully
demo模式安装下,还可以安装一些支撑的组件,例如prometheus,jaeger,grafana等
kubectl apply -f ./samples/addons
二.control plane相关介绍
istio提供如下功能:
1.提供APIs让操作者指定需要的路由/弹性策略
2.提供APIs让data plane消费配置
3.抽象化data plane服务发现
4.提供APIs指定使用策略
5.证书颁发
6.工作负载身份分配
7.统一监控数据采集
8.代理注入
9.指定网络边界和如何进行访问
具体实现上述功能的大部分由istiod实现,具体的组件图如下:
其中,istiod从k8s的api中获取到配置信息,然后下发到data plane中,下发的协议是xDS,支持动态的下发配置到envoy,如下图
上面的配置流程是将配置意图在yaml中并提交给k8s api,后续istiod监听到目标资源变更后会拉取配置转换,并下发到data plane中。istio的配置资源是使用k8s的custom resource definitions (CRDs),CRDs是用来扩展k8s api新增一些功能的。
二.部署第一个应用到service mesh
1.创建一个namespace,并切换当前工作空间
kubectl create namespace istioinaction
kubectl config set-context $(kubectl config current-context) --namespace=istioinaction
2.配置在此namespace的pod都注入一个sidecar
kubectl label namespace istioinaction istio-injection=enabled
3.部署catalog.yaml文件
kubectl apply -f catalog.yaml
文件catalog.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: catalog
---
apiVersion: v1
kind: Service
metadata:
labels:
app: catalog
name: catalog
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 3000
selector:
app: catalog
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: catalog
version: v1
name: catalog
spec:
replicas: 1
selector:
matchLabels:
app: catalog
version: v1
template:
metadata:
labels:
app: catalog
version: v1
spec:
serviceAccountName: catalog
containers:
- env:
- name: KUBERNETES_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
image: istioinaction/catalog:latest
imagePullPolicy: IfNotPresent
name: catalog
ports:
- containerPort: 3000
name: http
protocol: TCP
securityContext:
privileged: false
执行成功后,生成了对应的deployment,service和pod,可以查询到生成的pod
其中这个pod包含两个container,一个是应用容器,另一个是sidecar,可以通过访问service命令判断是否启动成功:
kubectl run -i -n default --rm --restart=Never dummy --image=curlimages/curl --command -- sh -c 'curl -s http://catalog.istioinaction/items/1'
输出为:
{
"id": 1,
"color": "amber",
"department": "Eyewear",
"name": "Elinor Glasses",
"price": "282.00"
}
4.部署webapp应用,作为catalog的前端程序
kubectl apply -f webapp.yaml
文件webapp.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: webapp
---
apiVersion: v1
kind: Service
metadata:
labels:
app: webapp
name: webapp
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 8080
selector:
app: webapp
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: webapp
name: webapp
spec:
replicas: 1
selector:
matchLabels:
app: webapp
template:
metadata:
labels:
app: webapp
spec:
serviceAccountName: webapp
containers:
- env:
- name: KUBERNETES_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: CATALOG_SERVICE_HOST
value: catalog.istioinaction
- name: CATALOG_SERVICE_PORT
value: "80"
- name: FORUM_SERVICE_HOST
value: forum.istioinaction
- name: FORUM_SERVICE_PORT
value: "80"
image: istioinaction/webapp:latest
imagePullPolicy: IfNotPresent
name: webapp
ports:
- containerPort: 8080
name: http
protocol: TCP
securityContext:
privileged: false
执行成功后,和之前catalog一样,也有两个container,最后执行访问命令:
kubectl run -i -n default --rm --restart=Never dummy --image=curlimages/curl --command -- sh -c 'curl -s http://webapp.istioinaction/api/catalog/items/1'
和之前命令执行后获得一样的json数据,接下来我们可以在宿主机上暴露端口直接通过浏览器访问
kubectl port-forward deploy/webapp 8080:8080
5.访问的流程图如下
总结
本文介绍了demo模式下istio的安装以及如何运行一个简单的伴生sidecar的应用程序,最后通过pod和pod之间和宿主机和pod之间访问了应用服务
参考:
《istio in action》