部署istio
tar zxvf istio-1.8.2-linux.tar.gz
cd istio-1.8.2
mv bin/istioctl /usr/bin
查看配置文件的名称,生产环境建议使用default,基本上核心功能都有,minimal是以最小版本去部署的,demo功能多一点,比default多一点。我们这里使用default进行部署安装。
下面是查看有哪些配置文件的名称。
[root@master bin]# ./istioctl profile list
Istio configuration profiles:
default
demo
empty
minimal
openshift
preview
remote
[root@master profiles]# ls
default.yaml demo.yaml empty.yaml minimal.yaml openshift.yaml preview.yaml PROFILES.md remote.yaml
在install不指定profile那么默认就是使用default。安装的时候后面可以添加--set-profile=default -y
部署完查看结果,部署了两个pod,一个istiod,也就是它的控制平面,ingressgateway是用来接受网格流量的,就类似于nginx-ingress。
service的istiod暴露端口,让数据平面的proxy和它去通信,这个是不需要去对外访问的。
ingressgateway就需要对外访问了,它采用的是loadbalance,它是是用于公有云服务类型,它能够在公有云的负载均衡器上面创建负载均衡出来,由公有云的负载均衡器暴露出来。
我们这里没有公有云的环境,所以这里处于pending的状态,我们只能使用nodeport的方式去访问。
[root@master ~]# kubectl get pods -n istio-system
NAME READY STATUS RESTARTS AGE
istio-ingressgateway-5759b6968-nztgj 1/1 Running 0 166m
istiod-597dd8db8-b79zn 1/1 Running 0 167m
[root@master ~]# kubectl get svc -n istio-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
istio-ingressgateway LoadBalancer 10.233.58.97 <pending> 15021:31184/TCP,80:30698/TCP,443:31759/TCP,15012:31608/TCP,15443:31314/TCP 166m
istiod ClusterIP 10.233.37.152 <none> 15010/TCP,15012/TCP,443/TCP,15014/TCP 167m
istioctl manifest generate | kubectl delete -f -
Sidercar注入
sample目录下是官方给你的案例
[root@master istio-1.8.2]# ls samples/
addons certs external helloworld https multicluster rawvm security tcp-echo
bookinfo custom-bootstrap health-check httpbin kubernetes-blog operator README.md sleep websockets
sidecar对istio来说是比较重要的,因为它是数据平面的唯一组件,要想让一个应用被istio接管到,就必须将proxy和应用绑定在一块。
sidecar注入方式有两种,一种是手动注入,一种是自动注入。
[root@master httpbin]# istioctl kube-inject -f httpbin-nodeport.yaml
之前只有这个容器
containers:
- image: docker.io/kennethreitz/httpbin
imagePullPolicy: IfNotPresent
name: httpbin
ports:
- containerPort: 80
现在多了这个容器,在原有的yaml基础上又了istio的proxy,为这个业务pod做流量的代理。
- name: DNS_AGENT
image: docker.io/istio/proxyv2:1.8.2
imagePullPolicy: Always
name: istio-proxy
ports:
- containerPort: 15090
[root@master httpbin]# kubectl apply -f <(istioctl kube-inject -f httpbin-nodeport.yaml)
service/httpbin created
deployment.apps/httpbin created
[root@master httpbin]# kubectl get pod
NAME READY STATUS RESTARTS AGE
httpbin-84ff597c89-g6h56 2/2 Running 0 104s
istio-proxy@httpbin-84ff597c89-g6h56:/$ ps -ef
UID PID PPID C STIME TTY TIME CMD
istio-p+ 1 0 0 17:08 ? 00:00:01 /usr/local/bin/pilot-agent proxy sidecar --domain default.svc.cluster
istio-p+ 12 1 0 17:08 ? 00:00:02 /usr/local/bin/envoy -c etc/istio/proxy/envoy-rev0.json --restart-epo
istio-p+ 27 0 1 17:13 pts/0 00:00:00 bash
istio-p+ 37 27 0 17:13 pts/0 00:00:00 ps -ef
这里面跑了agent,这个pilot-agent主要负责envoy的热更新配置,因为envoy是代理的一个程序。
自动注入(给命名空间打指定标签,启用自动注入)
kubectl label namespace default istio-injection=enabled
上面就实现了自动注入并且将应用部署起来了,然后让其在istio里面去访问。你要创建规则,这个规则和nginx规则是一样的。
[root@master httpbin]# cat httpbin-gateway.yaml
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: httpbin-gateway
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: httpbin
spec:
hosts:
- "*"
gateways:
- httpbin-gateway
http:
- route:
- destination:
host: httpbin
port:
number: 8000
[root@master httpbin]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
httpbin NodePort 10.233.38.15 <none> 8000:31339/TCP 37m
[root@master httpbin]# kubectl apply -f httpbin-gateway.yaml
gateway.networking.istio.io/httpbin-gateway created
virtualservice.networking.istio.io/httpbin created
最后访问IngressGateway NodePort访问地址:http://192.168.111.6:30698/
现在这个应用已经被istio接管了,并且通过istio提供的ingress gateway可以访问到它。流程如下。
Istio与K8s集成流程
通过kubectl创建的istio的相关的资源,很多资源不是k8s自身的资源,这些资源以operator的方式部署。