如果要ingress的域名增加TLS的证书,该怎么办?
那如何将证书和密钥保存到k8s的secret中呢?
如果使用自签名的证书,使用下面的命令创建密钥和证书
[root@nccztsjb-node-23 secrets]# openssl req -x509 \ > -newkey \ > rsa:4096 \ > -nodes \ > -keyout server.key \ > -out server.crt \ > -sha256 \ > -days 3650 \ > -subj "/C=CN/ST=Beijing/L=Beijing/O=Alididi/OU=Ops/CN=www.example.com" Generating a 4096 bit RSA private key ......................................................................................................................................++ ........................................................++ writing new private key to 'server.key' ----- [root@nccztsjb-node-23 secrets]# [root@nccztsjb-node-23 secrets]# [root@nccztsjb-node-23 secrets]# ls server.crt server.key [root@nccztsjb-node-23 secrets]# ls -ltr total 8 -rw-r--r-- 1 root root 3272 Jun 26 14:44 server.key -rw-r--r-- 1 root root 2025 Jun 26 14:44 server.crt
创建好了密钥key和证书之后,通过如下的命令,保存到secret中
kubectl create secret tls myweb-secret --key server.key --cert server.crt
创建好了,之后,可以查看其信息:
[root@nccztsjb-node-23 secrets]# kubectl create secret tls myweb-secret --key server.key --cert server.crt secret/myweb-secret created [root@nccztsjb-node-23 secrets]# kubectl get secret NAME TYPE DATA AGE default-token-ghs97 kubernetes.io/service-account-token 3 118d myweb-secret kubernetes.io/tls 2 5s [root@nccztsjb-node-23 secrets]# kubectl get secret myweb-secret NAME TYPE DATA AGE myweb-secret kubernetes.io/tls 2 14s [root@nccztsjb-node-23 secrets]# kubectl get secret myweb-secret -o yaml apiVersion: v1 data: tls.crt: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUZxVENDQTVHZ0F3SUJBZ0lKQU5UV0sweEZucG9hTUEwR0NTcUdTSWIzRFFFQkN3VUFNR3N4Q3pBSkJnTlYKQkFZVEFrTk9NUkF3RGdZRFZRUUlEQWRDWldscWFXNW5NUkF3RGdZRFZRUUhEQWRDWldscWFXNW5NUkF3RGdZRApWUVFLREFkQmJHbGthV1JwTVF3d0NnWURWUVFMREFOUGNITXhHREFXQmdOVkJBTU1E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Gh6eWpJeXgyWm1ONjZXQW1TYWFKMHJWUk9EMVhGZFVOL2NEcTg0V2g2CnNZUzhqZHZLeUFMQkdQazdyMzYxYU10OEdyVlRpVWZIcDhQK0svVTd4U05tQ2dmTGRkTGw1TWZVYTVyRUlSV0kKUmpJNUNNbTJoZmowV3IrTnpjVk9yU0lWc1hDejhDMUlXL0JBUjNQRlBFbnAyM3ZLZzArZTlWZGFBWWZWRk9veQpwOW0weXZJRzBLMTJrRkRqZytaS2p6eTRxYkZCQkUrQUFBSVB0Q0dqNjFZVDR0L2dmT243U2QrTGZhbXM5Z1FaCm1vZFRTQ2pJTThQZlBYSHRpZmRXYTlmazd6cHI2NHNGSHE5M0tvNFFta01tYVFubGMyQUpEUzlaTWp3ajRPN1QKcWcydDI5SFVUWlNaQ2dQeWJJS3E5WVo1NU1Fb0Z3PT0KLS0tLS1FTkQgUFJJVkFURSBLRVktLS0tLQo= kind: Secret metadata: creationTimestamp: "2023-06-26T06:44:50Z" name: myweb-secret namespace: default resourceVersion: "15234021" uid: ebabbbb5-0801-480f-96fb-9e057ecda0fb type: kubernetes.io/tls [root@nccztsjb-node-23 secrets]#
看到的数据是tls.crt和tls.key
后续,就可以将这个secret挂载到ingress里面了来使用了。
下面是,如何在ingress中,应用这个证书?
在tls段,引用刚刚创建好的secret对象
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: ingress-tls namespace: default spec: tls: - hosts: - mywebsite.com secretName: myweb-secret ingressClassName: nginx rules: - host: mywebsite.com http: paths: - backend: service: name: nginx-deployment port: number: 80 path: / pathType: Prefix [root@nccztsjb-node-23 yamls]# kubectl get ingress ingress-tls NAME CLASS HOSTS ADDRESS PORTS AGE ingress-tls nginx mywebsite.com 172.20.58.65 80, 443 58s [root@nccztsjb-node-23 yamls]#
这里是自动增减了443端口的
通过域名进行访问,主要是https的访问
[root@nccztsjb-node-23 yamls]# curl -H "Host:mywebsite.com" -k https://172.20.58.65 nginx-deployment-6bfdb59f47-4qgr4 [root@nccztsjb-node-23 yamls]# curl -H "Host:mywebsite.com" -k https://172.20.58.65 nginx-deployment-6bfdb59f47-6kjn5 [root@nccztsjb-node-23 yamls]# curl -H "Host:mywebsite.com" -k https://172.20.58.65 nginx-deployment-6bfdb59f47-4qgr4 [root@nccztsjb-node-23 yamls]#
这样就可以进行对ingress的https的域名访问了。
浏览器也可以进行访问(需要配置hosts)
标签:TLS,node,23,证书,tls,secret,nccztsjb,root From: https://www.cnblogs.com/chuanzhang053/p/17505839.html