前提

因为apiserver 是无状态话的,所以可以使用简单的4层负载均衡实现高可用!
下例子使用ha-proxy实现4层负载均衡和故障检查,使用keepalive实现虚拟地址。

创建haproxy的配置文件,文件名为:haproxy.cfg

  1. global
  2. log 127.0.0.1 local2
  3. pidfile /var/run/haproxy.pid
  4. maxconn 4000
  5. daemon
  6. defaults
  7. mode tcp
  8. log global
  9. option abortonclose
  10. option redispatch
  11. retries 3
  12. timeout connect 10s
  13. timeout client 1m
  14. timeout server 1m
  15. maxconn 30000
  16. listen stats
  17. mode http
  18. bind 0.0.0.0:1080
  19. stats enable
  20. stats hide-version
  21. stats uri /haproxyadmin?stats
  22. stats realm Haproxy\ Statistics
  23. stats auth admin:123456
  24. stats admin if TRUE
  25. listen check
  26. mode http
  27. bind 0.0:10200
  28. stats enable
  29. stats hide-version
  30. stats uri /healthz
  31. stats scope security_port
  32. frontend security_port
  33. bind *:8443
  34. mode tcp
  35. log global
  36. default_backend servers
  37. backend servers
  38. mode tcp
  39. balance roundrobin
  40. stick-table type ip size 200k expire 30m
  41. stick on src
  42. server master1 10.10.1.21:6443 weight 1 check inter 1000 rise 2 fall 2
  43. server master2 10.10.1.22:6443 weight 1 check inter 1000 rise 2 fall 2
  44. server master3 10.10.1.23:6443 weight 1 check inter 1000 rise 2 fall 2

创建keepalived 配置文件

  1. ! Configuration File for keepalived
  2. global_defs {
  3. notification_email {
  4. dblvs@jn.idc
  5. }
  6. notification_email_from Alexandre.Cassen@firewall.loc
  7. smtp_server 127.0.0.1
  8. smtp_connect_timeout 30
  9. router_id LVS_DEVEL
  10. }
  11. vrrp_instance VI_1 {
  12. state MASTER
  13. interface ens33
  14. virtual_router_id 51
  15. priority 120
  16. advert_int 1
  17. authentication {
  18. auth_type PASS
  19. auth_pass 1111@2222
  20. }
  21. virtual_ipaddress {
  22. 10.10.10.20/24
  23. }
  24. }
  25. virtual_server 10.10.10.20 8443 {
  26. delay_loop 6
  27. lb_algo wlc
  28. lb_kind DR
  29. nat_mask 255.255.255.0
  30. #persistence_timeout 50
  31. protocol TCP
  32. real_server 10.10.10.21 8443 {
  33. weight 1
  34. TCP_CHECK {
  35. connect_timeout 3
  36. nb_get_retry 3
  37. delay_before_retry 3
  38. connect_port 8443
  39. }
  40. }
  41. }

创建 haproxy 和 keepalived 的staticPod文件 hub.k8s.com/apps/keepalived:1.4.1 这个镜像下载地址为 osixia/keepalived:1.4.1 ha-proxy.yaml

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: haproxy
  5. namespace: kube-system
  6. labels:
  7. name: haproxy
  8. spec:
  9. restartPolicy: Always
  10. hostNetwork: true
  11. containers:
  12. - name: haproxy
  13. image: hub.k8s.com/apps/haproxy:1.8.3
  14. livenessProbe:
  15. httpGet:
  16. scheme: HTTP
  17. host: 127.0.0.1
  18. port: 10200
  19. path: /healthz
  20. initialDelaySeconds: 15
  21. timeoutSeconds: 15
  22. securityContext:
  23. privileged: true
  24. volumeMounts:
  25. - name: lib
  26. mountPath: /var/lib/haproxy
  27. readOnly: false
  28. - name: config
  29. mountPath: /usr/local/etc/haproxy/
  30. readOnly: false
  31. ports:
  32. - name: security-port
  33. containerPort: 8443
  34. protocol: TCP
  35. - name: check-port
  36. containerPort: 1080
  37. protocol: TCP
  38. - name: keepalived
  39. image: hub.k8s.com/apps/keepalived:1.4.1
  40. command:
  41. - '/container/tool/run'
  42. - '--copy-service'
  43. securityContext:
  44. privileged: true
  45. volumeMounts:
  46. - name: modules
  47. mountPath: /lib/modules
  48. readOnly: false
  49. - name: kpconfig
  50. mountPath: /container/service/keepalived/assets/keepalived.conf
  51. readOnly: false
  52. volumes:
  53. - name: modules
  54. hostPath:
  55. path: /lib/modules/
  56. - name: kpconfig
  57. hostPath:
  58. path: /opt/keepalived/keepalived.conf
  59. - name: lib
  60. emptyDir: {}
  61. - name: config
  62. hostPath:
  63. path: /opt/haproxy/