Kubernetes ingress实现了七层负载均衡策略,首先安装这个服务,要下载一个国外的镜像,可能国内访问不到,
如果有需要这个镜像的话可以联系我,我发给你。
1:首先建立ingress服务
1:先下载 mandatory.yaml文件, 文件太长了贴在最后了
kubectl create -f mandatory.yaml
2:查看服务是否正常启动,ingress默认会起一个新的命名空间,ingress-nginx
[ root@k8s-master ingress]
NAME READY STATUS RESTARTS AGE
default-http-backend-6c7b756d58-9bbn2 1/1 Running 2 22h
mydepoy-7b847b9655-4wrdq 1/1 Running 0 6h2m
myhttpd-9b45698fb-2rbmh 1/1 Running 0 5h54m
myhttpd-9b45698fb-fv8qk 1/1 Running 0 5h54m
nginx-ingress-controller-f57d7bd58-q6hdn 1/1 Running 2 22h
3:可以看到 ningx-ingress-controller是正常启动的
4:查看一下ingress所需要的镜像是什么
[ root@k8s-master ingress]
image: registry.cn-qingdao.aliyuncs.com/kubernetes_xingej/defaultbackend-amd64:1.5
image: registry.cn-qingdao.aliyuncs.com/kubernetes_xingej/nginx-ingress-controller:0.20.0
5:这里使用的是aliyun的镜像,如果下载太慢,可以使用其他版本的镜像;
2:建立service-nodeport.yaml文件
1:kubectl create -f service-nodeport.yaml
apiVersion: v1
kind: Service
metadata:
name: ingress-nginx
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
spec:
type: NodePort
ports:
- name: http
port: 80
targetPort: 80
protocol: TCP
nodePort: 31532
- name: https
port: 443
targetPort: 443
protocol: TCP
nodePort: 30443
selector:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
2:查看ingress svc服务
[ root@k8s-master ingress]
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT( S) AGE
default-http-backend ClusterIP 10.1.237.145 < none> 80/TCP 22h
ingress-nginx NodePort 10.1.135.8 < none> 80:31532/TCP,443:30443/TCP 19h
3:建立nginx-deployment.yaml文件
apiVersion: v1
kind: Service
metadata:
name: service-nginx
namespace: ingress-nginx
spec:
selector:
app: mynginx
ports:
- name: http
port: 80
targetPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mydepoy
namespace: ingress-nginx
spec:
replicas: 1
selector:
matchLabels:
app: mynginx
template:
metadata:
labels:
app: mynginx
spec:
containers:
- name: mycontainer
image: nginx:1.7.9
imagePullPolicy: IfNotPresent
3:建立nginx-ingress.yaml文件
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-mynginx
namespace: ingress-nginx
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: www.tree.com
http:
paths:
- path:
backend:
serviceName: service-nginx
servicePort: 80
结果测试
1: 查看hosts文件映射
[ root@k8s-master ingress2]
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.78.137 www.tree.com
2:访问www.treedown.com:31532 ingress svc的端口是31532
[ root@k8s-master ingress2]
< ! 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>
测试结束
(qq:九七二四三九三二九( 972439329) 微信:( liang7890234) ,有哪里不对欢迎指正,大家一起学习交流)
End