Kubernetes使用资源定义haproxy

使用资源定义方式创建haproxy的pod进行负载均衡

准备工作

先创建好两个httpd镜像,设置他们的 index.html文件不一样

#apache1
[root@master ~]# vim /opt/Dockerfile
FROM busybox

RUN mkdir /data && echo 'hello amu,this is v1 > /data/index.html

CMD ["/bin/httpd","-f","-h","/data"]

[root@master ~]# docker build -t gaofan1225/httpd:v1 /opt
Successfully built 3ae78ac98950
Successfully tagged gaofan1225/httpd:v1

#apache2
[root@master ~]# vim /opt/Dockerfile
FROM busybox

RUN mkdir /data && echo 'hello amu,this is v2 > /data/index.html

CMD ["/bin/httpd","-f","-h","/data"]

[root@master ~]# docker build -t gaofan1225/httpd:v2 /opt
Successfully built 31dcde996d51
Successfully tagged gaofan1225/httpd:v2

用上面两个httpd镜像创建rs1、rs2容器

// 创建目录
[root@master ~]# cd /opt
[root@master opt]# mkdir manifest

root@master opt]# cd manifest

[root@master manifest]#  vim web.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: rs1
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: amu1
  template: 
    metadata:
      labels:
        app: amu1 
    spec:
      containers:
        - image: gaofan1225/httpd:v1
          imagePullPolicy: IfNotPresent
          name: rs1
          
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: rs2
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: amu2
  template:
    metadata:
      labels:
        app: amu2
    spec:
      containers:
        - image: bgaofan1225/httpd:v2  
          imagePullPolicy: IfNotPresent
          name: rs2

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


---
apiVersion: v1
kind: Service
metadata:
  name: rs2
  namespace: default
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: amu2
  type: NodePort
  
  
  
[root@master manifest]# kubectl apply -f web.yml 
deployment.apps/rs1 created
deployment.apps/rs2 created
service/rs1 created
service/rs2 created

查看创建好的 rs1、rs2容器的 pod、svc类型

[root@master manifest]# kubectl get pod,svc
NAME                         READY   STATUS    RESTARTS   AGE
pod/rs1-6f8b6577bd-f8t6g    1/1     Running   0          48s
pod/rs2-5cff4c7479-kz5wb     1/1     Running   0          1m

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        4m
service/rs1          NodePort    10.105.78.131   <none>        80:31286/TCP   1m
service/rs2          NodePort    10.102.240.88   <none>        80:31286/TCP   1m

写资源清单 定义haproxy

[root@master manifest]#  vim haproxy.yml
---
apiVersion: apps/v1
kind: Pod
metadata:
  name: haproxy
  namespace: default
  labels:
    app: test
spec:
  restartPolicy: OnFailure 	// 健康检查出问题就重启容器
  initContainers:
  - name: cfgfile
    volumeMounts:
    - name: haproxyconfigfile
      mountPath: /tmp
  containers:
  - image: gaofan1225/haproxy:latest
    imagePullPolicy: IfNotPresent
    name: haproxy
    env:
      - name: RSIP
        value: "rs1 rs2"
    livenessProbe:		 // 检查80端口是否存在
      tcpSocket:
        port: 80
      initialDelaySeconds: 60
      periodSeconds: 20
    readinessProbe:
      tcpSocket:
        port: 80
      initialDelaySeconds: 60
      periodSeconds: 20
          
---
apiVersion: v1
kind: Service
metadata:
  name: haproxy
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: test
  type: NodePort

[root@master manifest]# kubectl apply -f haproxy.yml 
deployment.apps/haproxy created
service/haproxy created

[root@master manifest]# kubectl get pods,svc
NAME                           READY   STATUS    RESTARTS   AGE
pod/haproxy-56948856b7-hhcbp   1/1     Running   0          15m
pod/rs1-6f8b6577bd-f8t6g       1/1     Running   0          5m
pod/rs2-5cff4c7479-kz5wb       1/1     Running   0          5m

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/haproxy      NodePort    10.96.97.77    <none>         80:33896/TCP   15s
service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        10m
service/rs1          NodePort    10.105.78.131   <none>        80:31286/TCP   7m
service/rs2          NodePort    10.102.240.88   <none>        80:31286/TCP   7m

访问测试

// 单独访问
#rs1
[root@master manifest]# curl 10.105.78.131 
hello amu,this is v1 

#rs2
[root@master manifest]# curl 10.102.240.88
hello amu,this is v2


// 通过负载均衡访问
[root@master manifest]# curl 10.96.97.77
hello amu,this is v1

[root@master manifest]# curl 10.96.97.77
hello amu,this is v2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值