編輯 ingress-tomcat.yaml
, 文件內容以下:nginx
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: ingress-tomcat namespace: default annotations: kubernetes.io/ingress.class: "nginx" spec: rules: - host: tomcat.test.com http: paths: - path: backend: serviceName: tomcat servicePort: 8080
tomcat-deploy.yaml
內容以下:後端
apiVersion: v1 kind: Service metadata: name: tomcat namespace: default spec: selector: app: tomcat release: canary ports: - name: http targetPort: 8080 port: 8080 - name: ajp targetPort: 8009 port: 8009 --- apiVersion: apps/v1 kind: Deployment metadata: name: tomcat-deploy namespace: default spec: replicas: 3 selector: matchLabels: app: tomcat release: canary template: metadata: labels: app: tomcat release: canary spec: containers: - name: tomcat image: tomcat:8.5.32-jre8-alpine ports: - name: http containerPort: 8080 - name: ajp containerPort: 8009
按以下命令建立ingress-tomcatapi
[root@master ingress-nginx]# kubectl apply -f tomcat-deploy.yaml service/tomcat created deployment.apps/tomcat-deploy created [root@master ingress-nginx]# kubectl apply -f ingress-tomcat.yaml ingress.extensions/ingress-tomcat created [root@master ingress-nginx]# kubectl get ingress NAME HOSTS ADDRESS PORTS AGE ingress-tomcat tomcat.test.com 80 6s
此時,修改本地hosts解析後,就能夠訪問tomcat.test.com:30080地址了。出來的是tomcat的歡迎頁面tomcat
建立證書和私鑰app
[root@master ingress-nginx]# openssl genrsa -out tls.key 2048 Generating RSA private key, 2048 bit long modulus ......................................+++ ......+++ e is 65537 (0x10001) [root@master ingress-nginx]# openssl req -new -x509 -key tls.key -out tls.crt -subj /C=CN/ST=Hangzhou/L=Hangzhou/O=kubernetes/CN=tomcat.test.com [root@master ingress-nginx]# ls tls* tls.crt tls.key
這個證書不能直接被ingress的nginx使用,須要轉成secret後,才能被注入到nginx中,被ingress使用。網站
[root@master ingress-nginx]# kubectl create secret tls tomcat-ingress-secret --cert tls.crt --key=tls.key secret/tomcat-ingress-secret created [root@master ingress-nginx]# kubectl get secret NAME TYPE DATA AGE default-token-qcfxf kubernetes.io/service-account-token 3 8d tomcat-ingress-secret kubernetes.io/tls 2 7s [root@master ingress-nginx]# kubectl describe secret tomcat-ingress-secret Name: tomcat-ingress-secret Namespace: default Labels: <none> Annotations: <none> Type: kubernetes.io/tls Data ==== tls.crt: 1306 bytes tls.key: 1679 bytes
編輯 ingress-tomcat-tls.yaml
文件,內容以下:spa
在yaml文件中添加了secret的相關信息:code
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: ingress-tomcat-tls namespace: default annotations: kubernetes.io/ingress.class: "nginx" spec: tls: - hosts: - tomcat.test.com secretName: tomcat-ingress-secret rules: - host: tomcat.test.com http: paths: - path: backend: serviceName: tomcat servicePort: 8080
[root@master ingress-nginx]# kubectl apply -f ingress-tomcat-tls.yaml ingress.extensions/ingress-tomcat-tls created [root@master ingress-nginx]# kubectl get ingress NAME HOSTS ADDRESS PORTS AGE ingress-tomcat tomcat.test.com 80 17m ingress-tomcat-tls tomcat.test.com 80, 443 8s
而後去查看ingress-controller上的nginx配置文件token
[root@master ingress-nginx]# kubectl get pods -n ingress-nginx NAME READY STATUS RESTARTS AGE default-http-backend-6586bc58b6-4f6qm 1/1 Running 1 8h nginx-ingress-controller-7675fd6cdb-njvdb 1/1 Running 1 8h [root@master ingress-nginx]# kubectl exec -n ingress-nginx nginx-ingress-controller-7675fd6cdb-njvdb -it -- /bin/sh $ cat nginx.conf|grep secret ssl_certificate /etc/ingress-controller/ssl/default-tomcat-ingress-secret.pem; ssl_certificate_key /etc/ingress-controller/ssl/default-tomcat-ingress-secret.pem; ssl_trusted_certificate /etc/ingress-controller/ssl/default-tomcat-ingress-secret-full-chain.pem; $
發現,secret的相關信息,已經被注入到了ingress-controller的nginx配置之中。ssl
訪問https://tomcat.test.com:80443 發現能夠訪問。