06-5. 部署 kube-proxy 组件

kube-proxy 运行在所有 worker 节点上,它监听 apiserver 中 service 和 endpoint 的变化情况,创建路由规则以提供服务 IP 和负载均衡功能。

本文档讲解部署 ipvs 模式的 kube-proxy 过程。

注意:如果没有特殊指明,本文档的所有操作均在 zhangjun-k8s-01 节点上执行,然后远程分发文件和执行命令。

下载和分发 kube-proxy 二进制文件

参考 05-1.部署master节点.md

创建 kube-proxy 证书

创建证书签名请求:

  1. cd /opt/k8s/work
  2. cat > kube-proxy-csr.json <<EOF
  3. {
  4. "CN": "system:kube-proxy",
  5. "key": {
  6. "algo": "rsa",
  7. "size": 2048
  8. },
  9. "names": [
  10. {
  11. "C": "CN",
  12. "ST": "BeiJing",
  13. "L": "BeiJing",
  14. "O": "k8s",
  15. "OU": "opsnull"
  16. }
  17. ]
  18. }
  19. EOF
  • CN:指定该证书的 User 为 system:kube-proxy
  • 预定义的 RoleBinding system:node-proxier 将User system:kube-proxy 与 Role system:node-proxier 绑定,该 Role 授予了调用 kube-apiserver Proxy 相关 API 的权限;
  • 该证书只会被 kube-proxy 当做 client 证书使用,所以 hosts 字段为空;

生成证书和私钥:

  1. cd /opt/k8s/work
  2. cfssl gencert -ca=/opt/k8s/work/ca.pem \
  3. -ca-key=/opt/k8s/work/ca-key.pem \
  4. -config=/opt/k8s/work/ca-config.json \
  5. -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxy
  6. ls kube-proxy*

创建和分发 kubeconfig 文件

  1. cd /opt/k8s/work
  2. source /opt/k8s/bin/environment.sh
  3. kubectl config set-cluster kubernetes \
  4. --certificate-authority=/opt/k8s/work/ca.pem \
  5. --embed-certs=true \
  6. --server=${KUBE_APISERVER} \
  7. --kubeconfig=kube-proxy.kubeconfig
  8. kubectl config set-credentials kube-proxy \
  9. --client-certificate=kube-proxy.pem \
  10. --client-key=kube-proxy-key.pem \
  11. --embed-certs=true \
  12. --kubeconfig=kube-proxy.kubeconfig
  13. kubectl config set-context default \
  14. --cluster=kubernetes \
  15. --user=kube-proxy \
  16. --kubeconfig=kube-proxy.kubeconfig
  17. kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig

分发 kubeconfig 文件:

  1. cd /opt/k8s/work
  2. source /opt/k8s/bin/environment.sh
  3. for node_name in ${NODE_NAMES[@]}
  4. do
  5. echo ">>> ${node_name}"
  6. scp kube-proxy.kubeconfig root@${node_name}:/etc/kubernetes/
  7. done

创建 kube-proxy 配置文件

从 v1.10 开始,kube-proxy 部分参数可以配置文件中配置。可以使用 --write-config-to 选项生成该配置文件,或者参考 源代码的注释

创建 kube-proxy config 文件模板:

  1. cd /opt/k8s/work
  2. source /opt/k8s/bin/environment.sh
  3. cat > kube-proxy-config.yaml.template <<EOF
  4. kind: KubeProxyConfiguration
  5. apiVersion: kubeproxy.config.k8s.io/v1alpha1
  6. clientConnection:
  7. burst: 200
  8. kubeconfig: "/etc/kubernetes/kube-proxy.kubeconfig"
  9. qps: 100
  10. bindAddress: ##NODE_IP##
  11. healthzBindAddress: ##NODE_IP##:10256
  12. metricsBindAddress: ##NODE_IP##:10249
  13. enableProfiling: true
  14. clusterCIDR: ${CLUSTER_CIDR}
  15. hostnameOverride: ##NODE_NAME##
  16. mode: "ipvs"
  17. portRange: ""
  18. iptables:
  19. masqueradeAll: false
  20. ipvs:
  21. scheduler: rr
  22. excludeCIDRs: []
  23. EOF
  • bindAddress: 监听地址;
  • clientConnection.kubeconfig: 连接 apiserver 的 kubeconfig 文件;
  • clusterCIDR: kube-proxy 根据 --cluster-cidr 判断集群内部和外部流量,指定 --cluster-cidr--masquerade-all 选项后 kube-proxy 才会对访问 Service IP 的请求做 SNAT;
  • hostnameOverride: 参数值必须与 kubelet 的值一致,否则 kube-proxy 启动后会找不到该 Node,从而不会创建任何 ipvs 规则;
  • mode: 使用 ipvs 模式;

为各节点创建和分发 kube-proxy 配置文件:

  1. cd /opt/k8s/work
  2. source /opt/k8s/bin/environment.sh
  3. for (( i=0; i < 3; i++ ))
  4. do
  5. echo ">>> ${NODE_NAMES[i]}"
  6. sed -e "s/##NODE_NAME##/${NODE_NAMES[i]}/" -e "s/##NODE_IP##/${NODE_IPS[i]}/" kube-proxy-config.yaml.template > kube-proxy-config-${NODE_NAMES[i]}.yaml.template
  7. scp kube-proxy-config-${NODE_NAMES[i]}.yaml.template root@${NODE_NAMES[i]}:/etc/kubernetes/kube-proxy-config.yaml
  8. done

创建和分发 kube-proxy systemd unit 文件

  1. cd /opt/k8s/work
  2. source /opt/k8s/bin/environment.sh
  3. cat > kube-proxy.service <<EOF
  4. [Unit]
  5. Description=Kubernetes Kube-Proxy Server
  6. Documentation=https://github.com/GoogleCloudPlatform/kubernetes
  7. After=network.target
  8. [Service]
  9. WorkingDirectory=${K8S_DIR}/kube-proxy
  10. ExecStart=/opt/k8s/bin/kube-proxy \\
  11. --config=/etc/kubernetes/kube-proxy-config.yaml \\
  12. --logtostderr=true \\
  13. --v=2
  14. Restart=on-failure
  15. RestartSec=5
  16. LimitNOFILE=65536
  17. [Install]
  18. WantedBy=multi-user.target
  19. EOF

分发 kube-proxy systemd unit 文件:

  1. cd /opt/k8s/work
  2. source /opt/k8s/bin/environment.sh
  3. for node_name in ${NODE_NAMES[@]}
  4. do
  5. echo ">>> ${node_name}"
  6. scp kube-proxy.service root@${node_name}:/etc/systemd/system/
  7. done

启动 kube-proxy 服务

  1. cd /opt/k8s/work
  2. source /opt/k8s/bin/environment.sh
  3. for node_ip in ${NODE_IPS[@]}
  4. do
  5. echo ">>> ${node_ip}"
  6. ssh root@${node_ip} "mkdir -p ${K8S_DIR}/kube-proxy"
  7. ssh root@${node_ip} "modprobe ip_vs_rr"
  8. ssh root@${node_ip} "systemctl daemon-reload && systemctl enable kube-proxy && systemctl restart kube-proxy"
  9. done

检查启动结果

  1. source /opt/k8s/bin/environment.sh
  2. for node_ip in ${NODE_IPS[@]}
  3. do
  4. echo ">>> ${node_ip}"
  5. ssh root@${node_ip} "systemctl status kube-proxy|grep Active"
  6. done

确保状态为 active (running),否则查看日志,确认原因:

  1. journalctl -u kube-proxy

查看监听端口

  1. $ sudo netstat -lnpt|grep kube-prox
  2. tcp 0 0 172.27.138.251:10256 0.0.0.0:* LISTEN 30590/kube-proxy
  3. tcp 0 0 172.27.138.251:10249 0.0.0.0:* LISTEN 30590/kube-proxy
  • 10249:http prometheus metrics port;
  • 10256:http healthz port;

查看 ipvs 路由规则

  1. source /opt/k8s/bin/environment.sh
  2. for node_ip in ${NODE_IPS[@]}
  3. do
  4. echo ">>> ${node_ip}"
  5. ssh root@${node_ip} "/usr/sbin/ipvsadm -ln"
  6. done

预期输出:

  1. >>> 172.27.138.251
  2. IP Virtual Server version 1.2.1 (size=4096)
  3. Prot LocalAddress:Port Scheduler Flags
  4. -> RemoteAddress:Port Forward Weight ActiveConn InActConn
  5. TCP 10.254.0.1:443 rr
  6. -> 172.27.137.229:6443 Masq 1 0 0
  7. -> 172.27.138.239:6443 Masq 1 0 0
  8. -> 172.27.138.251:6443 Masq 1 0 0
  9. >>> 172.27.137.229
  10. IP Virtual Server version 1.2.1 (size=4096)
  11. Prot LocalAddress:Port Scheduler Flags
  12. -> RemoteAddress:Port Forward Weight ActiveConn InActConn
  13. TCP 10.254.0.1:443 rr
  14. -> 172.27.137.229:6443 Masq 1 0 0
  15. -> 172.27.138.239:6443 Masq 1 0 0
  16. -> 172.27.138.251:6443 Masq 1 0 0
  17. >>> 172.27.138.239
  18. IP Virtual Server version 1.2.1 (size=4096)
  19. Prot LocalAddress:Port Scheduler Flags
  20. -> RemoteAddress:Port Forward Weight ActiveConn InActConn
  21. TCP 10.254.0.1:443 rr
  22. -> 172.27.137.229:6443 Masq 1 0 0
  23. -> 172.27.138.239:6443 Masq 1 0 0
  24. -> 172.27.138.251:6443 Masq 1 0 0

可见所有通过 https 访问 K8S SVC kubernetes 的请求都转发到 kube-apiserver 节点的 6443 端口;