kube-proxy組件是要是爲集羣內pod應用提供endpoint服務,當咱們爲一個pod定義了svc時,kube-proxy會自動生成pod與svc的映射關係,並代理到集羣內部或宿主機上。linux
特別說明:這裏全部的操做都是在devops這臺機器上經過ansible工具執行;kube-proxy 須要使用kubeconfig認證文件安全訪問kube-apiserver:它監聽 apiserver 中 service 和 endpoint 的變化狀況,建立路由規則以提供服務 IP 和負載均衡功能。git
#################### 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=eth1 LISTEN_IP=$(ifconfig | grep -A 1 ${ETH_INTERFACE} |grep inet |awk '{print $2}') CLUSTER_PODS_CIDR=172.16.0.0/20
訪問kubernetes github 官方地址下載穩定的 realease 包至本機;github
wget $DOWNLOAD_URL -P $SOFTWARE
將kubernetes 軟件包分發到各個master節點服務器;shell
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服務器證書以及私鑰:api
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 的訪問。app
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腳本能夠從此處獲取,負載均衡