本小节展示了如何使用简单的或双向的TLS来暴露一个安全HTTPS服务。
1. 准备工作
- 部署httpbin服务
# 开启自动sidecar注入的话,执行如下命令$ kubectl apply -f samples/httpbin/httpbin.yaml# 如果没有开启的话,需要手工注入的话执行如下$ kubectl apply -f <(istioctl kube-inject -f samples/httpbin/httpbin.yaml)
[warning] 注意,使用curl进行模拟的时候,应该有LibreSSL库的支持。要不然后面做双向TLS时会报错。
2. 确定Ingress IP和 ports
$ kubectl get svc istio-ingressgateway -n istio-system$ export INGRESS_HOST=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].ip}')$ export INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].port}')$ export SECURE_INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="https")].port}')$ export TCP_INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="tcp")].port}')
3. 产生客户端和服务器的证书及私钥
- 创建一个根证书和私有key, 用它来对服务签署证书
$ openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -subj '/O=example Inc./CN=example.com' -keyout example.com.key -out example.com.crt
- 针对
httpbin.example.com创建证书和私有key.
$ openssl req -out httpbin.example.com.csr -newkey rsa:2048 -nodes -keyout httpbin.example.com.key -subj "/CN=httpbin.example.com/O=httpbin organization"$ openssl x509 -req -days 365 -CA example.com.crt -CAkey example.com.key -set_serial 0 -in httpbin.example.com.csr -out httpbin.example.com.crt
4. 针对单一的主机配置TLS ingress gateway
- 针对ingress gateway创建一个secret
$ kubectl create -n istio-system secret tls httpbin-credential --key=httpbin.example.com.key --cert=httpbin.example.com.crt
- 针对servers定义gateway,指定端口为443, 并且针对
credentialName的值指向httpbin-credential. 这个值和secret's的名称是一样的。 TLS的模式是SIMPLE
$ cat <<EOF | kubectl apply -f -apiVersion: networking.istio.io/v1alpha3kind: Gatewaymetadata:name: mygatewayspec:selector:istio: ingressgateway # use istio default ingress gatewayservers:- port:number: 443name: httpsprotocol: HTTPStls:mode: SIMPLEcredentialName: httpbin-credential # must be the same as secrethosts:- httpbin.example.comEOF
- 配置gateway’s的ingress traffic路由。 定义相对应的虚拟服务
$ cat <<EOF | kubectl apply -f -apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata:name: httpbinspec:hosts:- "httpbin.example.com"gateways:- mygatewayhttp:- match:- uri:prefix: /status- uri:prefix: /delayroute:- destination:port:number: 8000host: httpbinEOF
- 发送HTTPS请求,通过HTTPS来访问httpbin服务
$ curl -v -HHost:httpbin.example.com --resolve "httpbin.example.com:$SECURE_INGRESS_PORT:$INGRESS_HOST" \--cacert example.com.crt "https://httpbin.example.com:$SECURE_INGRESS_PORT/status/418"
- 删除gateway’s的密钥,和创建一个新的去改变ingress gateway’s的凭据。
$ kubectl -n istio-system delete secret httpbin-credential
$ mkdir new_certificates$ openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -subj '/O=example Inc./CN=example.com' -keyout new_certificates/example.com.key -out new_certificates/example.com.crt$ openssl req -out new_certificates/httpbin.example.com.csr -newkey rsa:2048 -nodes -keyout new_certificates/httpbin.example.com.key -subj "/CN=httpbin.example.com/O=httpbin organization"$ openssl x509 -req -days 365 -CA new_certificates/example.com.crt -CAkey new_certificates/example.com.key -set_serial 0 -in new_certificates/httpbin.example.com.csr -out new_certificates/httpbin.example.com.crt$ kubectl create -n istio-system secret tls httpbin-credential \--key=new_certificates/httpbin.example.com.key \--cert=new_certificates/httpbin.example.com.crt
- 通过curl来使用新的证书链访问httpbin服务
curl -v -HHost:httpbin.example.com --resolve "httpbin.example.com:$SECURE_INGRESS_PORT:$INGRESS_HOST" \--cacert new_certificates/example.com.crt "https://httpbin.example.com:$SECURE_INGRESS_PORT/status/418"
- 假如你尝试使用先前的证书去访问,那么将会失败。
$ curl -v -HHost:httpbin.example.com --resolve "httpbin.example.com:$SECURE_INGRESS_PORT:$INGRESS_HOST" \--cacert example.com.crt "https://httpbin.example.com:$SECURE_INGRESS_PORT/status/418"
5. 针对多个主机配置TLS ingress gateway
你可以针对多个主机配置ingress gateway,比如针对httpbin.example.com和helloworld-v1.example.com两个主机。ingress gateway根据相对应的credentialName检索唯一的凭据名称。
- 为了还原针对httpbin的凭据,删除它的密钥,然后再创建它。
$ kubectl -n istio-system delete secret httpbin-credential$ kubectl create -n istio-system secret tls httpbin-credential \--key=httpbin.example.com.key \--cert=httpbin.example.com.crt
- 启动helloworld-v1案例
cat <<EOF | kubectl apply -f -apiVersion: v1kind: Servicemetadata:name: helloworld-v1labels:app: helloworld-v1spec:ports:- name: httpport: 5000selector:app: helloworld-v1---apiVersion: apps/v1kind: Deploymentmetadata:name: helloworld-v1spec:replicas: 1selector:matchLabels:app: helloworld-v1version: v1template:metadata:labels:app: helloworld-v1version: v1spec:containers:- name: helloworldimage: istio/examples-helloworld-v1resources:requests:cpu: "100m"imagePullPolicy: IfNotPresent #Alwaysports:- containerPort: 5000EOF
- 针对
helloworld-v1.example.com产生证书和私有key。
$ openssl req -out helloworld-v1.example.com.csr -newkey rsa:2048 -nodes -keyout helloworld-v1.example.com.key -subj "/CN=helloworld-v1.example.com/O=helloworld organization"$ openssl x509 -req -days 365 -CA example.com.crt -CAkey example.com.key -set_serial 1 -in helloworld-v1.example.com.csr -out helloworld-v1.example.com.crt
- 创建helloworld-credential 密钥
$ kubectl create -n istio-system secret tls helloworld-credential --key=helloworld-v1.example.com.key --cert=helloworld-v1.example.com.crt
- 定义gateway如下 所示:
$ cat <<EOF | kubectl apply -f -apiVersion: networking.istio.io/v1alpha3kind: Gatewaymetadata:name: mygatewayspec:selector:istio: ingressgateway # use istio default ingress gatewayservers:- port:number: 443name: https-httpbinprotocol: HTTPStls:mode: SIMPLEcredentialName: httpbin-credentialhosts:- httpbin.example.com- port:number: 443name: https-helloworldprotocol: HTTPStls:mode: SIMPLEcredentialName: helloworld-credentialhosts:- helloworld-v1.example.comEOF
- 定义gateway的流量路由,定义相对应的虚拟服务
$ cat <<EOF | kubectl apply -f -apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata:name: helloworld-v1spec:hosts:- helloworld-v1.example.comgateways:- mygatewayhttp:- match:- uri:exact: /helloroute:- destination:host: helloworld-v1port:number: 5000EOF
- 发送https的请求到
helloworld-v1.example.com
$ curl -v -HHost:helloworld-v1.example.com --resolve "helloworld-v1.example.com:$SECURE_INGRESS_PORT:$INGRESS_HOST" \--cacert example.com.crt "https://helloworld-v1.example.com:$SECURE_INGRESS_PORT/hello"
- 发送https请求到
httpbin.example.com去测试。
curl -v -HHost:httpbin.example.com --resolve "httpbin.example.com:$SECURE_INGRESS_PORT:$INGRESS_HOST" \--cacert example.com.crt "https://httpbin.example.com:$SECURE_INGRESS_PORT/status/418"
6. 配置双向的TLS ingress gateway
可以扩展你gateway’s的定义去支持双向的TLS. 改变ingress gateway可以通过删除它的secret,再重新创建一个来实现。服务器使用CA证书去校验它的客户端,因此我们还要加载CA证书。
$ kubectl -n istio-system delete secret httpbin-credential$ kubectl create -n istio-system secret generic httpbin-credential --from-file=tls.key=httpbin.example.com.key \--from-file=tls.crt=httpbin.example.com.crt --from-file=ca.crt=example.com.crt
- 改变gateway的定义,设置TLS的模式为MUTUAL.
$ cat <<EOF | kubectl apply -f -apiVersion: networking.istio.io/v1alpha3kind: Gatewaymetadata:name: mygatewayspec:selector:istio: ingressgateway # use istio default ingress gatewayservers:- port:number: 443name: httpsprotocol: HTTPStls:mode: MUTUALcredentialName: httpbin-credential # must be the same as secrethosts:- httpbin.example.comEOF
- 尝试使用先前的方法去发送HTTPS请求,来看它是如何失败的
$ curl -v -HHost:httpbin.example.com --resolve "httpbin.example.com:$SECURE_INGRESS_PORT:$INGRESS_HOST" \--cacert example.com.crt "https://httpbin.example.com:$SECURE_INGRESS_PORT/status/418"
- 产生客户端的证书和私有key
$ openssl req -out client.example.com.csr -newkey rsa:2048 -nodes -keyout client.example.com.key -subj "/CN=client.example.com/O=client organization"$ openssl x509 -req -days 365 -CA example.com.crt -CAkey example.com.key -set_serial 1 -in client.example.com.csr -out client.example.com.crt
- 传送client的证书和key给curl和重新发送请求。
$ curl -v -HHost:httpbin.example.com --resolve "httpbin.example.com:$SECURE_INGRESS_PORT:$INGRESS_HOST" \--cacert ./example.com.crt --cert ./client.example.com.crt --key ./client.example.com.key \"https://httpbin.example.com:$SECURE_INGRESS_PORT/status/418"
7. 排错
- 检查INGRESS_HOST和SECURE_INGRESS_PORT的环境变量值。
$ kubectl get svc -n istio-system$ echo "INGRESS_HOST=$INGRESS_HOST, SECURE_INGRESS_PORT=$SECURE_INGRESS_PORT"
- 检查
istio-ingressgatewayu控制器的错误消息日志
$ kubectl logs -n istio-system "$(kubectl get pod -l istio=ingressgateway \-n istio-system -o jsonpath='{.items[0].metadata.name}')"
- 校验你的secret是否成功的创建在了
istio-system名称空间。
$ kubectl -n istio-system get secrets
- 检查
ingress gateway agent的日志是否推送key/certificate密钥对到ingress gateway.
$ kubectl logs -n istio-system "$(kubectl get pod -l istio=ingressgateway \-n istio-system -o jsonpath='{.items[0].metadata.name}')"
8. 清空本课实验
- 删除gateway的配置,虚拟服务的定义和secrets.
$ kubectl delete gateway mygateway$ kubectl delete virtualservice httpbin$ kubectl delete --ignore-not-found=true -n istio-system secret httpbin-credential \helloworld-credential$ kubectl delete --ignore-not-found=true virtualservice helloworld-v1
- 删除证书和keys
$ rm -rf example.com.crt example.com.key httpbin.example.com.crt httpbin.example.com.key httpbin.example.com.csr helloworld-v1.example.com.crt helloworld-v1.example.com.key helloworld-v1.example.com.csr client.example.com.crt client.example.com.csr client.example.com.key ./new_certificates
- 关闭
httpbin和helloworld-v1服务
$ kubectl delete deployment --ignore-not-found=true httpbin helloworld-v1$ kubectl delete service --ignore-not-found=true httpbin helloworld-v1
