您好,登录后才能下订单哦!
kube-proxy组件是要是为集群内pod应用提供endpoint服务,当我们为一个pod定义了svc时,kube-proxy会自动生成pod与svc的映射关系,并代理到集群内部或宿主机上。
特别说明:这里所有的操作都是在devops这台机器上通过ansible工具执行;kube-proxy 需要使用kubeconfig认证文件安全访问kube-apiserver:它监听 apiserver 中 service 和 endpoint 的变化情况,创建路由规则以提供服务 IP 和负载均衡功能。
#################### Variable parameter setting ######################
KUBE_NAME=kube-proxy
K8S_INSTALL_PATH=/data/apps/k8s/kubernetes
K8S_BIN_PATH=${K8S_INSTALL_PATH}/sbin
K8S_LOG_DIR=${K8S_INSTALL_PATH}/logs
K8S_CONF_PATH=/etc/k8s/kubernetes
KUBE_CONFIG_PATH=/etc/k8s/kubeconfig
CA_DIR=/etc/k8s/ssl
SOFTWARE=/root/software
HOSTNAME=`hostname`
VERSION=v1.14.2
PACKAGE=kubernetes-server-${VERSION}-linux-amd64.tar.gz
DOWNLOAD_URL=https://github.com/devops-apps/download/raw/master/kubernetes/$PACKAGE
ETH_INTERFACE=eth2
LISTEN_IP=$(ifconfig | grep -A 1 ${ETH_INTERFACE} |grep inet |awk '{print $2}')
CLUSTER_PODS_CIDR=172.16.0.0/20
访问kubernetes github 官方地址下载稳定的 realease 包至本机;
wget $DOWNLOAD_URL -P $SOFTWARE
将kubernetes 软件包分发到各个master节点服务器;
sudo ansible master_k8s_vgs -m copy -a "src=${SOFTWARE}/$PACKAGE dest=${SOFTWARE}/" -b
### 1.Check if the install directory exists.
if [ ! -d "$K8S_BIN_PATH" ]; then
mkdir -p $K8S_BIN_PATH
fi
if [ ! -d "$K8S_LOG_DIR/$KUBE_NAME" ]; then
mkdir -p $K8S_LOG_DIR/$KUBE_NAME
fi
if [ ! -d "$K8S_CONF_PATH" ]; then
mkdir -p $K8S_CONF_PATH
fi
if [ ! -d "$KUBE_CONFIG_PATH" ]; then
mkdir -p $KUBE_CONFIG_PATH
fi
### 2.Install kube-proxy binary of kubernetes.
if [ ! -f "$SOFTWARE/kubernetes-server-${VERSION}-linux-amd64.tar.gz" ]; then
wget $DOWNLOAD_URL -P $SOFTWARE >>/tmp/install.log 2>&1
fi
cd $SOFTWARE && tar -xzf kubernetes-server-${VERSION}-linux-amd64.tar.gz -C ./
cp -fp kubernetes/server/bin/$KUBE_NAME $K8S_BIN_PATH
ln -sf $K8S_BIN_PATH/${KUBE_NAME} /usr/local/bin
chmod -R 755 $K8S_INSTALL_PATH
cd $CA_DIR
ansible worker_k8s_vgs -m copy -a "src=ca.pem dest=$CA_DIR" -b
kube-proxy使用 kubeconfig文件连接访问 apiserver服务,该文件提供了 apiserver 地址、嵌入的 CA 证书和 kube-proxy服务器证书以及私钥:
cd $KUBE_CONFIG_PATH
ansible worker_k8s_vgs -m copy -a "src= kube-proxy.kubeconfig dest=$KUBE_CONFIG_PATH" -b
备注: 如果在前面小节已经同步过各组件kubeconfig和证书文件,此处可以不必执行此操作;
cat >${K8S_CONF_PATH}/kube-proxy-config.yaml<<EOF
kind: KubeProxyConfiguration
apiVersion: kubeproxy.config.k8s.io/v1alpha1
clientConnection:
burst: 200
kubeconfig: "${KUBE_CONFIG_PATH}/kube-proxy.kubeconfig"
qps: 100
bindAddress: ${LISTEN_IP}
healthzBindAddress: ${LISTEN_IP}:10256
metricsBindAddress: ${LISTEN_IP}:10249
clusterCIDR: ${CLUSTER_PODS_CIDR}
hostnameOverride: ${HOSTNAME}
mode: "ipvs"
portRange: ""
kubeProxyIPTablesConfiguration:
masqueradeAll: false
kubeProxyIPVSConfiguration:
scheduler: rr
excludeCIDRs: []
EOF
cat >/usr/lib/systemd/system/${KUBE_NAME}.service <<EOF
[Unit]
Description=Kubernetes Kube-Proxy Server
Documentation=https://github.com/GoogleCloudPlatform/kubernetes
After=network.target
[Service]
WorkingDirectory=${K8S_INSTALL_PATH}
ExecStart=${K8S_BIN_PATH}/${KUBE_NAME} \\
--config=${K8S_CONF_PATH}/kube-proxy-config.yaml \\
--alsologtostderr=true \\
--logtostderr=false \\
--log-dir=${K8S_LOG_DIR}/${KUBE_NAME} \\
--v=2
Restart=on-failure
RestartSec=5
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
systemctl status kube-proxy|grep Active
确保状态为 active (running),否则查看日志,确认原因:
sudo journalctl -u kube-proxy
注意:以下命令在 kube-scheduler 节点上执行。kube-proxy 监听 10249 和 10256 端口:两个接口都对外提供 /metrics 和 /healthz 的访问。
sudo netstat -ntlp | grep kube-proxy
tcp 0 0 10.10.10.40:10249 0.0.0.0:* LISTEN 22604/kube-proxy
tcp 0 0 10.10.10.40:10256 0.0.0.0:* LISTEN 22604/kube-proxy
sudo ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.20.40:8400 rr
-> 172.16.3.2:8080 Masq 1 0 0
-> 172.16.3.3:8080 Masq 1 0 0
-> 172.16.3.4:8080 Masq 1 0 0
TCP 192.168.20.40:8497 rr
-> 172.16.3.2:8500 Masq 1 0 0
-> 172.16.3.3:8500 Masq 1 0 0
-> 172.16.3.4:8500 Masq 1 0 0
TCP 10.10.10.40:8400 rr
-> 172.16.3.2:8080 Masq 1 0 0
-> 172.16.3.3:8080 Masq 1 0 0
-> 172.16.3.4:8080 Masq 1 0 0
至此整个集群基本部署完成,关于kubernetes集群监控请参考:kubernetes集群安装指南:kubernetes集群插件部署。kube-proxy脚本可以从此处获取,
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。