首页 > 云计算 > Docker/K8 > k8s使用ingress-nginx负载均衡
ingress-nginx负载均衡调用顺序:用户--->ingress-nginx(pod)--->ingress-nginx(控制器)--->ingress--->service--->pod
1.下载安装ingress-nginx(七层调度器)
--详细文档
--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
[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 <none> <none>
myapp-deploy-ng-65d64df569-n288f 1/1 Running 0 9m6s 10.244.1.76 k8s2 <none> <none>
myapp-deploy-ng-65d64df569-vnrj5 1/1 Running 0 9m6s 10.244.1.77 k8s2 <none> <none>
nginx-ingress-controller-68db76b4db-d2h65 1/1 Running 0 18d 10.244.2.240 k8s3 <none> <none>
[root@k8s1 ~]# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx ClusterIP 10.99.27.16 <none> 80/TCP 20s
3.创建service-nodeport
[root@k8s1 ~]# wget
[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 <none> 80:30880/TCP,443:30443/TCP 11h
[root@k8s1 ~]# kubectl describe svc ingress-nginx -n ingress-nginx
Name: ingress-nginx
Namespace: ingress-nginx
Labels: <none>
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: <none>
[root@k8s1 ~]# curl
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-d2h65 1/1 Running 0 18d
[root@k8s1 ~]# kubectl exec -n ingress-nginx -it nginx-ingress-controller-68db76b4db-d2h65 -- /bin/sh
$
5.访问web站点
[root@k8s1 ~]# curl /hostname.html --返回的结果就是三个pod的主机名
myapp-deploy-ng-65d64df569-5f9l2
[root@k8s1 ~]# curl /hostname.html
myapp-deploy-ng-65d64df569-5f9l2
[root@k8s1 ~]# curl /hostname.html
myapp-deploy-ng-65d64df569-vnrj5
[root@k8s1 ~]# curl /hostname.html
myapp-deploy-ng-65d64df569-vnrj5
[root@k8s1 ~]# curl /hostname.html
myapp-deploy-ng-65d64df569-n288f
[root@k8s1 ~]#
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/25854343/viewspace-2641755/ ,如需转载,请注明出处,否则将追究法律责任。