用nginx对k8s集群中的service做负载均衡(优化)

  • Post author:
  • Post category:其他




用nginx对k8s集群中的service做负载均衡


NodePort


会在每台Node上监听端口接收用户流量,在实际情况下,对用户暴露的只会有一个IP和端口,那这么多台Node该使用哪台让用户访问呢?

这时就需要前面加一个公网负载均衡器为项目提供统一访问入口了。

在这里插入图片描述



环境

主机 节点
192.168.129.250 master
192.168.129.135 node1
192.168.129.136 node2
192.168.129.134 nginx



创建deployment

[root@master ~]# cat test/nginx.yml 
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: web
        image: nginx
        imagePullPolicy: IfNotPresent
        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: data
      volumes:
      - name: data
        hostPath:
          path: /var/www/html

---
apiVersion: v1
kind: Service
metadata:
  name: web
  namespace: default
spec:
  ports:
  - port: 80
    nodePort: 30000
    targetPort: 80
  selector:
    app: nginx
  type: NodePort

[root@master ~]# kubectl apply -f test/nginx.yml 
deployment.apps/web created
service/web created

[root@master ~]# kubectl get pod
NAME                   READY   STATUS    RESTARTS   AGE
web-5b56f7dc6d-7ktbk   1/1     Running   0          12m
web-5b56f7dc6d-xxxxg   1/1     Running   0          12m
[root@master ~]# kubectl get pod -o wide
NAME                   READY   STATUS    RESTARTS   AGE   IP            NODE    NOMINATED NODE   READINESS GATES
web-5b56f7dc6d-7ktbk   1/1     Running   0          12m   10.244.1.22   node1   <none>           <none>
web-5b56f7dc6d-xxxxg   1/1     Running   0          12m   10.244.2.64   node2   <none>           <none>

[root@master ~]# curl 192.168.129.135:30000
1314444
[root@master ~]# curl 192.168.129.136:30000
hello world

在这里插入图片描述



安装nginx

[root@nginx ~]# yum -y install nginx
[root@nginx ~]# systemctl start nginx

[root@nginx ~]# vim /etc/nginx/nginx.conf
#在server { 的上面插入以下代码
#添加以下代码
    upstream webservers {
        server 192.168.129.135:30000;
        server 192.168.129.136:30000;
   }
    server{
        listen       8080;
        location / {
            proxy_pass http://webservers;
        }
   }
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;  
.....

[root@nginx~]# systemctl restart nginx
[root@nginx ~]# ss -anlt
State      Recv-Q     Send-Q           Local Address:Port           Peer Address:Port     
LISTEN     0          128                    0.0.0.0:22                  0.0.0.0:*               
LISTEN     0          128                    0.0.0.0:80                  0.0.0.0:*        
LISTEN     0          128                    0.0.0.0:8080                0.0.0.0:*        
LISTEN     0          128                       [::]:22                     [::]:*        
LISTEN     0          128                       [::]:80                     [::]:*  

[root@nginx ~]# curl 192.168.129.134:8080
1314444
[root@nginx ~]# curl 192.168.129.134:8080
hello world
[root@nginx ~]# curl 192.168.129.134:8080
1314444
[root@nginx ~]# curl 192.168.129.134:8080
hello world



测试

用安装nginx的那台主机的ip+8080 访问

在这里插入图片描述