{"id":749,"date":"2020-08-29T10:05:49","date_gmt":"2020-08-29T02:05:49","guid":{"rendered":"https:\/\/www.linuxdevops.cn\/?p=749"},"modified":"2020-08-29T10:35:12","modified_gmt":"2020-08-29T02:35:12","slug":"configure-mutual-authentication-for-ingress-nginx-mtls","status":"publish","type":"post","link":"https:\/\/www.linuxdevops.cn\/2020\/08\/configure-mutual-authentication-for-ingress-nginx-mtls\/","title":{"rendered":"\u4e3a Ingress-nginx \u914d\u7f6e\u53cc\u5411\u8ba4\u8bc1**mtls**"},"content":{"rendered":"\n
\u9996\u5148\u4ec0\u4e48\u662f mtls \uff08\u53cc\u5411\u8ba4\u8bc1\uff09\uff1f\u5b83\u662f\u4e00\u4e2a\u8fc7\u7a0b\uff0c\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u5ba2\u6237\u673a\u548c\u670d\u52a1\u5668\u90fd\u901a\u8fc7\u8bc1\u4e66\u9881\u53d1\u673a\u6784\u5f7c\u6b64\u9a8c\u8bc1\u8eab\u4efd\u3002 \u76f8\u4fe1 tls \u5927\u5bb6\u90fd\u6bd4\u8f83\u719f\u6089\uff0c\u5c31\u662f server \u7aef\u63d0\u4f9b\u4e00\u4e2a\u6388\u4fe1\u8bc1\u4e66\uff0c\u5f53\u6211\u4eec\u4f7f\u7528 https \u534f\u8bae\u8bbf\u95eeserver\u7aef\u65f6\uff0cclient \u4f1a\u5411 server \u7aef\u7d22\u53d6\u8bc1\u4e66\u5e76\u8ba4\u8bc1\uff08\u6d4f\u89c8\u5668\u4f1a\u4e0e\u81ea\u5df1\u7684\u6388\u4fe1\u57df\u5339\u914d\u6216\u5f39\u51fa\u4e0d\u5b89\u5168\u7684\u9875\u9762\uff09\u3002 mtls \u5219\u662f\u7531\u540c\u4e00\u4e2a root ca \u751f\u6210\u4e24\u5957\u8bc1\u4e66\uff0c\u5373\u5ba2\u6237\u7aef\u8bc1\u4e66\u548c\u670d\u52a1\u7aef\u8bc1\u4e66\u3002\u5ba2\u6237\u7aef\u4f7f\u7528 https \u8bbf\u95ee\u670d\u52a1\u7aef\u65f6\uff0c\u53cc\u65b9\u4f1a\u4ea4\u6362\u8bc1\u4e66\uff0c\u5e76\u8fdb\u884c\u8ba4\u8bc1\uff0c\u8ba4\u8bc1\u901a\u8fc7\u65b9\u53ef\u901a\u4fe1\u3002<\/p>\n\n\n\n
\u8bc1\u4e66\u751f\u6210<\/strong><\/p>\n\n\n\n \u8bc1\u4e66\u4e3b\u8981\u7684\u683c\u5f0f\u6709\u4ee5\u4e0b\u51e0\u79cd<\/p>\n\n\n\n \/\/ \u751f\u6210\u6839\u8bc1\u4e66\uff08ROOT CA\uff09<\/strong><\/p>\n\n\n\n \/\/ \u751f\u6210 \u670d\u52a1\u7aef\u8bc1\u4e66<\/strong> <\/p>\n\n\n\n \/\/ \u751f\u6210 \u5ba2\u6237\u7aef\u8bc1\u4e66<\/strong> <\/p>\n\n\n\n keytool \u662f\u4e2a\u5bc6\u94a5\u548c\u8bc1\u4e66\u7ba1\u7406\u5de5\u5177\u3002\u5b83\u4f7f\u7528\u6237\u80fd\u591f\u7ba1\u7406\u81ea\u5df1\u7684\u516c\u94a5<\/a>\/\u79c1\u94a5\u5bf9\u53ca\u76f8\u5173\u8bc1\u4e66\uff0c\u7528\u4e8e\uff08\u901a\u8fc7\u6570\u5b57\u7b7e\u540d<\/a>\uff09\u81ea\u6211\u8ba4\u8bc1\uff08\u7528\u6237\u5411\u522b\u7684\u7528\u6237\/\u670d\u52a1\u8ba4\u8bc1\u81ea\u5df1\uff09\u6216\u6570\u636e\u5b8c\u6574\u6027<\/a>\u4ee5\u53ca\u8ba4\u8bc1\u670d\u52a1\u3002\u5b83\u8fd8\u5141\u8bb8\u7528\u6237\u50a8\u5b58\u4ed6\u4eec\u7684\u901a\u4fe1\u5bf9\u7b49\u8005\u7684\u516c\u94a5\uff08\u4ee5\u8bc1\u4e66\u5f62\u5f0f\uff09\u3002(\u4e00\u4e0b\u7528\u4e8ejava)<\/p>\n\n\n\n \u9996\u5148\u6211\u4eec\u9700\u8981\u5728ingress \u6240\u5728\u7684 namespace \u4e0b\u521b\u5efa\u5bf9\u5e94\u7684 secret<\/p>\n\n\n\n \u521b\u5efaingress\uff1a<\/p>\n\n\n\n \u9a8c\u8bc1<\/strong>\uff1a<\/p>\n\n\n\n \u8fd9\u91cc\u6211\u4eec\u968f\u4fbf\u8d77\u4e00\u4e2a nginx \u7684\u670d\u52a1:<\/p>\n\n\n\n \u6309\u7167\u4e0a\u8ff0\u914d\u7f6e\uff0c\u521b\u5efa secret\u3001ingress\u3002<\/p>\n\n\n\n \u901a\u8fc7 \u8fd9\u5c31\u8bf4\u660e\u670d\u52a1\u7aef\u8ba4\u4e3a\u6211\u4eec\u662f\u4e0d\u53ef\u4fe1\u7684\uff0c\u56e0\u4e3a\u6ca1\u6709\u643a\u5e26\u8bc1\u4e66\u3002\u5f53\u6211\u4eec\u643a\u5e26\u8bc1\u4e66\u53bb\u8bbf\u95ee\uff0c<\/p>\n\n\n\n \/\u5219\u4f1a\u8fd4\u56de\u6b63\u5e38\u9875\u9762\u3002<\/p>\n\n\n\n\u8bc1\u4e66\u683c\u5f0f\u7c7b\u578b<\/strong><\/h3>\n\n\n\n
keytool<\/h3>\n\n\n\n
K8s ingress \u914d\u7f6e<\/strong><\/h2>\n\n\n\n
kubectl create secret generic ca-secret --from-file=ca.crt=ca.crt
kubectl create secret generic tls-secret --from-file=tls.crt=server.crt --from-file=tls.key=server.key<\/pre>\n\n\n\napiVersion: extensions\/v1beta1\nkind: Ingress\nmetadata:\nannotations:\n\u00a0 kubernetes.io\/ingress.class: nginx\n\u00a0 \u00a0# \u5f00\u542f\u5ba2\u6237\u7aef\u8ba4\u8bc1\n\u00a0 nginx.ingress.kubernetes.io\/auth-tls-verify-client: \"on\"\n\u00a0 \u00a0# \u6307\u5b9aroot ca \u7684 secret\n\u00a0 nginx.ingress.kubernetes.io\/auth-tls-secret: \"default\/ca-secret\"\n\u00a0 \u00a0# \u6307\u5b9a\u9a8c\u8bc1\u8bc1\u4e66\u94fe\u7684\u6df1\u5ea6\n\u00a0 nginx.ingress.kubernetes.io\/auth-tls-verify-depth: \"1\"\n\u00a0 \u00a0# \u662f\u5426\u5c06\u8bc1\u4e66\u4f20\u9012\u7ed9\u540e\u7aef\u7684\u670d\u52a1\n\u00a0 nginx.ingress.kubernetes.io\/auth-tls-pass-certificate-to-upstream: \"true\"\nname: test-server\nnamespace: default\nspec:\nrules:\n- host: test.wangfeng.live\n\u00a0 http:\n\u00a0 \u00a0 paths:\n\u00a0 \u00a0 - backend:\n\u00a0 \u00a0 \u00a0 \u00a0 serviceName: test-server\n\u00a0 \u00a0 \u00a0 \u00a0 servicePort: 8080\n\u00a0 \u00a0 \u00a0 path: \/\ntls:\n- hosts:\n\u00a0 - test.wangfeng.live\n\u00a0 secretName: tls-secret<\/pre>\n\n\n\n
kubectl run my-nginx --image=nginx --replicas=2 --port=80
kubectl expose deployment my-nginx --type=NodePort --port=80 --target-port=80<\/pre>\n\n\n\ncurl<\/code> \u8bbf\u95ee\u6211\u4eec\u7684\u670d\u52a1
curl -k https:\/\/x.x.x.x\/<\/code>\uff0c\u4f1a\u53d1\u73b0\u8fd4\u56de\u4e86\u4e00\u4e2a 400 \u7684response<\/p>\n\n\n\n
[root@my ~]# curl -k https:\/\/10.48.51.222\/
<html>
<head><title>400 No required SSL certificate was sent<\/title><\/head>
<body>
<center><h1>400 Bad Request<\/h1><\/center>
<center>No required SSL certificate was sent<\/center>
<hr><center>openresty\/1.15.8.1<\/center>
<\/body>
<\/html><\/pre>\n\n\n\ncurl -k --cert client.pem --key key.pem https:\/\/x.x.x.x<\/pre>\n\n\n\n