k8s使用ingress-nginx负载均衡的示例分析
本篇文章给大家分享的是有关k8s使用ingress-nginx负载均衡的示例分析,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
创新互联建站是一家以网络技术公司,为中小企业提供网站维护、网站建设、成都做网站、网站备案、服务器租用、国际域名空间、软件开发、微信小程序开发等企业互联网相关业务,是一家有着丰富的互联网运营推广经验的科技公司,有着多年的网站建站经验,致力于帮助中小企业在互联网让打出自已的品牌和口碑,让企业在互联网上打开一个面向全国乃至全球的业务窗口:建站联系电话:18980820575
ingress-nginx负载均衡调用顺序:用户--->ingress-nginx(pod)--->ingress-nginx(控制器)--->ingress--->service--->pod
1.下载安装ingress-nginx(七层调度器)
https://kubernetes.github.io/ingress-nginx/deploy/ --详细文档
https://github.com/kubernetes/ingress-nginx/tree/master/deploy --ingress-nginx的yaml文件
[root@k8s1 ~]# kubectl explain ingress --帮助信息
[root@k8s1 ~]# vim /etc/sysconfig/kubelet --启用ipvs功能(service负载均衡有三种userspace,iptables,ipvs)
KUBE_PROXY_MODE=ipvs --添加参数
[root@k8s1 ~]# kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/mandatory.yaml
[root@k8s1 ~]# kubectl get pods -n ingress-nginx
NAME READY STATUS RESTARTS AGE
nginx-ingress-controller-68db76b4db-pqcl7 0/1 ContainerCreating 0 35s
[root@k8s1 ~]# kubectl get pods -n ingress-nginx
NAME READY STATUS RESTARTS AGE
nginx-ingress-controller-68db76b4db-pqcl7 1/1 Running 0 9m20s
[root@k8s1 ~]#
2.创建service控制器和后端三个pod
[root@k8s1 ~]# vim service-pod.yaml
kind: Service
metadata:
name: ingress-nginx
namespace: ingress-nginx
spec:
selector:
app: ingress-nginx --与下面的pod保持一至
release: canary
ports:
- name: http
targetPort: 80
port: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy-ng
namespace: ingress-nginx
spec:
replicas: 3
selector:
matchLabels:
app: ingress-nginx --与上面的service保持一至
release: canary
template:
metadata:
labels:
app: ingress-nginx --与上面的service保持一至
release: canary
spec:
containers:
- name: ingress-nginx --与上面的service保持一至
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
[root@k8s1 ~]# kubectl apply -f service-pod.yaml
service/ingress-nginx created
deployment.apps/myapp-deploy-ng created
[root@k8s1 ~]# kubectl get pods -o wide -n ingress-nginx
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-deploy-ng-65d64df569-5f9l2 1/1 Running 0 9m6s 10.244.2.9 k8s3
myapp-deploy-ng-65d64df569-n288f 1/1 Running 0 9m6s 10.244.1.76 k8s2
myapp-deploy-ng-65d64df569-vnrj5 1/1 Running 0 9m6s 10.244.1.77 k8s2
nginx-ingress-controller-68db76b4db-d2h75 1/1 Running 0 18d 10.244.2.240 k8s3
[root@k8s1 ~]# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx ClusterIP 10.99.27.16
3.创建service-nodeport
[root@k8s1 ~]# wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/provider/baremetal/service-nodeport.yaml
[root@k8s1 ~]# vim service-nodeport.yaml
apiVersion: v1
kind: Service
metadata:
name: ingress-nginx
namespace: ingress-nginx
spec:
type: NodePort
ports:
- name: http
port: 80
targetPort: 80
protocol: TCP
nodePort: 30880
- name: https
port: 443
targetPort: 443
protocol: TCP
nodePort: 30443
selector:
app: ingress-nginx
[root@k8s1 ~]# kubectl apply -f service-nodeport.yaml
service/ingress-nginx configured
[root@k8s1 ~]# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx NodePort 10.99.27.16
[root@k8s1 ~]# kubectl describe svc ingress-nginx -n ingress-nginx
Name: ingress-nginx
Namespace: ingress-nginx
Labels:
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"ingress-nginx","namespace":"ingress-nginx"},"spec":{"ports":[{"na...
Selector: app=ingress-nginx
Type: NodePort
IP: 10.99.27.16
Port: http 80/TCP
TargetPort: 80/TCP
NodePort: http 30880/TCP
Endpoints: 10.244.1.76:80,10.244.1.77:80,10.244.2.9:80 --后端代理的三个pod地址
Port: https 443/TCP
TargetPort: 443/TCP
NodePort: https 30443/TCP
Endpoints: 10.244.1.76:443,10.244.1.77:443,10.244.2.9:443
Session Affinity: None
External Traffic Policy: Cluster
Events:
[root@k8s1 ~]# curl http://172.16.8.12:30880
4.创建ingress控制器
[root@k8s1 ~]# cat ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-myapp
namespace: default
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: myapp.magedu.com --域名
http:
paths:
- path:
backend:
serviceName: ingress-nginx --service名字
servicePort: 80
[root@k8s1 ~]# kubectl apply -f ingress.yaml
ingress.extensions/ingress-myapp created
[root@k8s1 ~]# kubectl get ingress
NAME HOSTS ADDRESS PORTS AGE
ingress-myapp myapp.magedu.com 80 4s
[root@k8s1 ~]# kubectl describe ingress ingress-myapp --查看ingress-myapp详细信息
[root@k8s1 ~]# kubectl get pods -n ingress-nginx
NAME READY STATUS RESTARTS AGE
nginx-ingress-controller-68db76b4db-d2h75 1/1 Running 0 18d
[root@k8s1 ~]# kubectl exec -n ingress-nginx -it nginx-ingress-controller-68db76b4db-d2h75 -- /bin/sh
$
5.访问web站点
[root@k8s1 ~]# curl http://172.16.8.12:30880/hostname.html --返回的结果就是三个pod的主机名
myapp-deploy-ng-65d64df569-5f9l2
[root@k8s1 ~]# curl http://172.16.8.12:30880/hostname.html
myapp-deploy-ng-65d64df569-5f9l2
[root@k8s1 ~]# curl http://172.16.8.12:30880/hostname.html
myapp-deploy-ng-65d64df569-vnrj5
[root@k8s1 ~]# curl http://172.16.8.12:30880/hostname.html
myapp-deploy-ng-65d64df569-vnrj5
[root@k8s1 ~]# curl http://172.16.8.12:30880/hostname.html
myapp-deploy-ng-65d64df569-n288f
[root@k8s1 ~]#
以上就是k8s使用ingress-nginx负载均衡的示例分析,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。
本文名称:k8s使用ingress-nginx负载均衡的示例分析
分享地址:http://scyanting.com/article/jpghhp.html