07-3.部署 kube-proxy 组件

kube-proxy 运行在所有 worker 节点上,,它监听 apiserver 中 service 和 Endpoint 的变化情况,创建路由规则来进行服务负载均衡。

本文档讲解部署 kube-proxy 的部署,使用 ipvs 模式。

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

参考 06-0.部署master节点.md

安装依赖包

参考 07-0.部署worker节点.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": "4Paradigm"
  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*pem

创建和分发 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=/opt/k8s/work/kube-proxy.pem \
  10. --client-key=/opt/k8s/work/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
  • --embed-certs=true:将 ca.pem 和 admin.pem 证书内容嵌入到生成的 kubectl-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 配置文件

  1. cd /opt/k8s/work
  2. source /opt/k8s/bin/environment.sh
  3. cat > kube-proxy.config.yaml.template <<EOF
  4. apiVersion: componentconfig/v1alpha1
  5. kind: KubeProxyConfiguration
  6. bindAddress: ##NODE_IP##
  7. clientConnection:
  8. kubeconfig: /etc/kubernetes/kube-proxy.kubeconfig
  9. clusterCIDR: ${CLUSTER_CIDR}
  10. healthzBindAddress: ##NODE_IP##:10256
  11. hostnameOverride: ##NODE_NAME##
  12. metricsBindAddress: ##NODE_IP##:10249
  13. iptables:
  14. masqueradeAll: false
  15. masqueradeBit: 14
  16. minSyncPeriod: 0s
  17. syncPeriod: 30s
  18. mode: "iptables"
  19. EOF
  • bindAddress: 监听地址;
  • clientConnection.kubeconfig: 连接 apiserver 的 kubeconfig 文件;
  • clusterCIDR: 必须与 kube-controller-manager 的 --cluster-cidr 选项值一致;kube-proxy 根据 --cluster-cidr 判断集群内部和外部流量,指定 --cluster-cidr--masquerade-all 选项后 kube-proxy 才会对访问 Service IP 的请求做 SNAT;
  • hostnameOverride: 参数值必须与 kubelet 的值一致,否则 kube-proxy 启动后会找不到该 Node,从而不会创建任何 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-${NODE_NAMES[i]}.config.yaml
  7. scp kube-proxy-${NODE_NAMES[i]}.config.yaml root@${NODE_NAMES[i]}:/etc/kubernetes/kube-proxy.config.yaml
  8. done

替换后的 kube-proxy.config.yaml 文件:kube-proxy.config.yaml

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

创建 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=always
  15. RestartSec=5
  16. StartLimitInterval=0
  17. LimitNOFILE=65536
  18. [Install]
  19. WantedBy=multi-user.target
  20. EOF

分发 kube-proxy unit 文件:

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

替换后的 unit 文件:kube-proxy.service

启动 kube-proxy 服务

  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} "mkdir -p ${K8S_DIR}/kube-proxy"
  6. ssh root@${node_ip} "systemctl daemon-reload && systemctl enable kube-proxy && systemctl restart kube-proxy"
  7. 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

查看监听端口和 metrics

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

查看路由规则

  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/iptables -nL -t nat|grep kubernetes:https"
  6. echo -e "\n\n"
  7. done

预期输出:

  1. >>> 172.27.136.3
  2. KUBE-MARK-MASQ all -- 172.27.136.2 0.0.0.0/0 /* default/kubernetes:https */
  3. DNAT tcp -- 0.0.0.0/0 0.0.0.0/0 /* default/kubernetes:https */ tcp to:172.27.136.2:6443
  4. KUBE-MARK-MASQ all -- 172.27.136.3 0.0.0.0/0 /* default/kubernetes:https */
  5. DNAT tcp -- 0.0.0.0/0 0.0.0.0/0 /* default/kubernetes:https */ tcp to:172.27.136.3:6443
  6. KUBE-MARK-MASQ all -- 172.27.136.1 0.0.0.0/0 /* default/kubernetes:https */
  7. DNAT tcp -- 0.0.0.0/0 0.0.0.0/0 /* default/kubernetes:https */ tcp to:172.27.136.1:6443
  8. KUBE-MARK-MASQ tcp -- !172.30.0.0/16 10.254.0.1 /* default/kubernetes:https cluster IP */ tcp dpt:443
  9. KUBE-SVC-NPX46M4PTMTKRN6Y tcp -- 0.0.0.0/0 10.254.0.1 /* default/kubernetes:https cluster IP */ tcp dpt:443
  10. KUBE-SEP-5NBUVTPI25CJ3LUF all -- 0.0.0.0/0 0.0.0.0/0 /* default/kubernetes:https */ statistic mode random probability 0.33332999982
  11. KUBE-SEP-WJUQKTGTDY252PCA all -- 0.0.0.0/0 0.0.0.0/0 /* default/kubernetes:https */ statistic mode random probability 0.50000000000
  12. KUBE-SEP-WR6LBNIK2S2UCATZ all -- 0.0.0.0/0 0.0.0.0/0 /* default/kubernetes:https */

可见将所有到 kubernetes cluster ip 443 端口的请求都转发到 kube-apiserver 的 6443 端口;