lngress实现虚拟主机的方案
首先确定要运行ingress-controller服务。
将ingress-nginx-controller暴露为一个Service资源对象。
[root@master ingress]# mkdir vhost/
[root@master ingress]# cd vhost/
查看运行环境:
[root@master vhost]# kubectl get pod -n ingress-nginx
NAME READY STATUS RESTARTS AGE
nginx-ingress-controller-5954d475b6-72kz5 1/1 Running 0 40h
[root@master ingress]# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx NodePort 10.97.160.233 <none> 80:32284/TCP,443:30742/TCP 39h
创建一个Deployment资源和Service资源相互关联
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: deploy1
spec:
replicas: 2
template:
metadata:
labels:
app: nginx1
spec:
containers:
- name: nginx1
image: nginx
---
apiVersion: v1
kind: Service
metadata:
name: svc-1
spec:
selector:
app: nginx1
ports:
- port: 80
targetPort: 80
查看一下
[root@master ingress]# kubectl get deployments.
NAME READY UP-TO-DATE AVAILABLE AGE
deploy1 2/2 2 2 3m47s
[root@master ingress]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deploy1-7df6778547-7vh5v 1/1 Running 0 4m28s
deploy1-7df6778547-whwlf 1/1 Running 0 4m28s
[root@master ingress]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc-1 ClusterIP 10.106.240.73 <none> 80/TCP 7m19s
[root@master ingress]# curl 10.106.240.73
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
然后复制资源创建,另外”一对“服务。
[root@master ingress]# cp deploy1.yaml deploy2.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: deploy2
spec:
replicas: 2
template:
metadata:
labels:
app: nginx2
spec:
containers:
- name: nginx2
image: nginx
---
apiVersion: v1
kind: Service
metadata:
name: svc-2
spec:
selector:
app: nginx2
ports:
- port: 80
targetPort: 80
查看一下
[root@master ingress]# kubectl get deployments.
NAME READY UP-TO-DATE AVAILABLE AGE
deploy2 2/2 2 2 13s
[root@master ingress]# kubectl get pods
NAME READY STATUS RESTARTS AGE
deploy2-7b6786d8bf-557g8 1/1 Running 0 44s
deploy2-7b6786d8bf-svvd7 1/1 Running 0 44s
[root@master ingress]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc-2 ClusterIP 10.101.208.93 <none> 80/TCP 84s
创建ingress规则
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-1
spec:
rules:
- host: www1.bdqn.com
http:
paths:
- path: /
backend:
serviceName: svc-1
servicePort: 80
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-2
spec:
rules:
- host: www2.bdqn.com
http:
paths:
- path: /
backend:
serviceName: svc-2
servicePort: 80
查看一下
[root@master vhost]# kubectl get ingresses.
NAME HOSTS ADDRESS PORTS AGE
ingress-1 www1.bdqn.com 10.97.160.233 80 19s
ingress-2 www2.bdqn.com 10.97.160.233 80 19s
实验环境限制(这个域名是假的),所有自己用来模拟一个域名。
更改windows里的hosts文件,添加一下内容
192.168.2.10 www1.bdqn.com
192.168.2.10 www2.bdqn.com
查看一下暴露端口
[root@master vhost]# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx NodePort 10.97.160.233 <none> 80:32284/TCP,443:30742/TCP 40h
访问
http://www1.bdqn.com:32284/
http://www2.bdqn.com:32284/
ingress资源实现https代理安全访问。
生成证书
[root@master https]# openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=testsvc /O=testsvc"
查看
[root@master https]# ls
tls.crt tls.key
创建secret资源,保存证书
[root@master https]# kubectl create secret tls tls-secret --key=tls.key --cert tls.crt
创建deploy3.yaml,模拟web服务
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: deploy3
spec:
replicas: 2
template:
metadata:
labels:
app: nginx3
spec:
containers:
- name: nginx3
image: nginx
---
apiVersion: v1
kind: Service
metadata:
name: svc-3
spec:
selector:
app: nginx3
ports:
- port: 80
targetPort: 80
查看一下
[root@master https]# kubectl get pods
NAME READY STATUS RESTARTS AGE
deploy3-5c545fcc5f-tsscz 1/1 Running 0 25m
deploy3-5c545fcc5f-z6rf7 1/1 Running 0 25m
创建对于的ingress规则。
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-3
spec:
tls:
- hosts:
- www3.bdqn.com
secretName: tls-secret
rules:
- host: www3.bdqn.com
http:
paths:
- path: /
backend:
serviceName: svc-3
servicePort: 80
查找对应service-nodePort的443端口映射的端口,浏览器访问即可:
https://www3.bdqn.com:30742/