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

环境

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

创建deployment

[root@master manifest]# cat web.yml 
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web1
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web1
  template:  
    metadata:
      labels:
        app: web1
    spec:
      containers:
      - name: web1
        image: 1314444/httpd:v0.1
        imagePullPolicy: IfNotPresent

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

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web2
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web2
  template:  
    metadata:
      labels:
        app: web2
    spec:
      containers:
      - name: httpd
        image: 1314444/httpd:v0.2
        imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
  name: web2
  namespace: default
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 31000
  selector:
    app: web2
  type: NodePort

[root@master manifest]# kubectl apply -f web.yml 
deployment.apps/web1 unchanged
service/web1 unchanged
deployment.apps/web2 unchanged
service/web2 created

[root@master manifest]# kubectl get pod
NAME                    READY   STATUS    RESTARTS   AGE
web1-855b788957-4fvrq   1/1     Running   0          64m
web2-5f7456967b-nkhpf   1/1     Running   0          64m

[root@master manifest]# kubectl get node -o wide
NAME     STATUS     ROLES                  AGE   VERSION   INTERNAL-IP       EXTERNAL-IP   OS-IMAGE                               KERNEL-VERSION          CONTAINER-RUNTIME
master   Ready      control-plane,master   8d    v1.20.0   192.168.129.250   <none>        Red Hat Enterprise Linux 8.2 (Ootpa)   4.18.0-193.el8.x86_64   docker://20.10.12
node1    Ready      <none>                 8d    v1.20.0   192.168.129.135   <none>        Red Hat Enterprise Linux 8.2 (Ootpa)   4.18.0-193.el8.x86_64   docker://20.10.12
node2    Ready      <none>                 8d    v1.20.0   192.168.129.136   <none>        Red Hat Enterprise Linux 8.2 (Ootpa)   4.18.0-193.el8.x86_64   docker://20.10.12

[root@master manifest]# curl 192.168.129.135:30000
test page on jjyy
[root@master manifest]# curl 192.168.129.136:31000
test page on 666

安装nginx

[root@nginx ~]# yum -y install nginx
[root@nginx ~]# systemctl start nginx
[root@nginx ~]# vim /etc/nginx/nginx.conf
#在httpd{ 的下面插入以下代码
#添加以下代码
    upstream webservers {
        server 192.168.129.135:30000;
        server 192.168.129.136:31000;
   }
    server{
        listen       81;
        location / {
            proxy_pass http://webservers;
        }
   }
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;  
.....
[root@nginx~]# systemctl restart nginx

测试

[root@nginx ~]# curl 192.168.129.134:81
test page on jjyy
[root@nginx ~]# curl 192.168.129.134:81
test page on 666
[root@nginx ~]# curl 192.168.129.134:81
test page on jjyy
[root@nginx ~]# curl 192.168.129.134:81
test page on 666

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值