K8S基础-Service代理
系统设置设置sysctl.confsysctl -p|grep net.ipv4.ip_forward如果没有则加入echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf添加k8s所需相关参数cat <<EOF > /etc/sysctl.d/k8s.conf# https://github.com/moby/moby/iss
Service代理
Service存在的意义:
- 防止Pod失联
- 定义一组Pod的访问粗略
Service与Pod的关系:
- 通过label-selector相关联
- 通过Service实现与Pod的负载均衡(TCP/UDP 4层)
四层: OSI中的传输层,TCP/UDP,四元组,只负责数据包转发
七层: OSI中的应用层,HTTP,FTP,SNMP等,可以拿到协议头部信息,可以实现基于协议层面的处理
Service三种常用类型
-
ClusterIP
分配一个内部集群IP地址,只能在集群内部访问(通Namespace内的pod),默认ServiceType.
-
NodePort
分配一个内部集群IP地址,并在每个节点上启用一个端口来暴露服务,可以在集群外部访问
访问地址: :
-
LoadBalancer
分配一个内部集群IP地址,并在每个节点上启用一个端口来暴露服务.
除此之外,Kubernets会请求底层云平台上的负载均衡,将每个Node([NodeIP]:[NodePort])作为后端添加进去
用途:
- 网络代理
- 服务代理
- 服务发现
- 发布服务
YAML配置文件
ClusterIP
获取配置文件命令
kubectl expose deploy/nginx --port=80 --target-port=80 --name=nginx-service --dry-run=client -o yaml > nginx-service.yaml
感觉实际需求来修改
apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: default
labels:
app: nginx
spec:
selector:
app: nginx
ports:
- name: https
port: 443
protocol: TCP
targetPort: 443
- name: http
port: 80
protocol: TCP
targetPort: 80
selector: 选择器,通过标签来匹配后端应用,做负载均衡
ports: 要暴露的后端端口,会分配一个clusterIP,并分配一个port
如果一个svc创建两个端口映射,需要-name来区分
-
apiVersion: 指定版本
-
kind: 类型
-
name: 指定服务名称
-
labels: 标签
-
port: Service 服务暴露的端口
-
targetPort: 容器暴露的端口
-
seletor: 关联的Pod的标签
NodePort
如果需要在宿主机上暴露指定端口,需要加入type: NodePort,并指定nodePort,如果不指定,则会生成一个随机端口
apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: web
labels:
app: nginx
spec:
selector:
app: nginx
ports:
- port: 80
targetPort: 80
# 这个一般不去指定,会生成一个随机端口,端口范围在apiserver配置文件中指定
nodePort: 30008
type: NodePort
通过kubectl get svc 查看,会发现TYPE类型变为NodePort,然后访问node的真实ip加nodePort端口即可访问pod服务
kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-service NodePort 10.0.0.150 <none> 88:30008/TCP 3m20
查看service关联Pod
kubectl get ep
Service-服务发现
-
环境变量
当一个pod运行到Node,kubelet会为每个容器添加一组环境变量,Pod容器中程序就可以使用这些环境变量发现Service.
环境变量格式如下:
(SVCNAME)_SERVICE_HOST
(SVCNAME)_SERVICE_PORT
限制:
1). Pod和Service的创建顺序是有要求的,Service必须在Pod创建之前被创建,否则环境变量不会被设置到Pod中
2). Pod只能获取同Namespace中的Service环境变量
-
DNS
DNS服务监视Kubernetes API,为每一个Service创建DNS记录用于域名解析,这样Pod中就可以通过DNS域名获取Service的访问地址
kube-proxy设置为ipvs模式
系统设置
设置sysctl.conf
sysctl -p|grep net.ipv4.ip_forward
如果没有
则加入
echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf
添加k8s所需相关参数
cat <<EOF > /etc/sysctl.d/k8s.conf
# https://github.com/moby/moby/issues/31208
# ipvsadm -l --timout
# 修复ipvs模式下长连接timeout问题 小于900即可
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 10
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
net.ipv4.neigh.default.gc_stale_time = 120
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2
net.ipv4.ip_forward = 1
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 1024
net.ipv4.tcp_synack_retries = 2
# 要求iptables不对bridge的数据进行处理
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-arptables = 1
net.netfilter.nf_conntrack_max = 2310720
fs.inotify.max_user_watches=89100
fs.may_detach_mounts = 1
fs.file-max = 52706963
fs.nr_open = 52706963
vm.swappiness = 0
vm.overcommit_memory=1
vm.panic_on_oom=0
EOF
重点参数主要三个
net.ipv4.ip_forward=1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
使配置生效
systcl -p
systcl --system
加载ipvs模块
临时生效
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
永久生效
cat > /etc/sysconfig/modules/ipvs.modules <<EOF
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF
安装ipvs ipset
yum -y install ipvsadm ipset conntrack-tools
不通安装方式修改ipvs模式
kubeadm方式安装
kubectl edit cm kube-proxy -n=kube-system
## 修改如下部分,模式修改为ipvs,scheduler默认为rr,根据实际情况选择,其余参数同理
iptables:
masqueradeAll: true
masqueradeBit: null
minSyncPeriod: 0s
syncPeriod: 0s
ipvs:
excludeCIDRs: null
minSyncPeriod: 0s
scheduler: "rr"
strictARP: false
syncPeriod: 0s
tcpFinTimeout: 0s
tcpTimeout: 0s
udpTimeout: 0s
kind: KubeProxyConfiguration
metricsBindAddress: ""
mode: "ipvs"
生效方式:
删除所有kube-proxy的pod,重新生成即可
二进制安装-早期版本
在kube-proxy.conf配置文件中加入
--masquerade-all=true \
--feature-gates=SupportIPVSProxyMode=true \
--proxy-mode=ipvs \
--ipvs-min-sync-period=5s \
--ipvs-sync-period=5s \
--ipvs-scheduler=rr \
生效方法
systemctl restart kube-proxy
二进制安装-1.18版本
先根据config文件找到kube-proxy的参数配置文件
KUBE_PROXY_OPTS="--logtostderr=false \
--v=2 \
--log-dir=/opt/k8s/logs \
--config=/opt/k8s/cfg/kube-proxy-config.yml"
然后配置参数配置文件
vim /opt/k8s/cfg/kube-proxy-config.yml
加入iptables,ipvs,mode这三个配置(iptables可以不加,因为模式选择的ipvs,只有ipvs的参数生效)
kind: KubeProxyConfiguration
apiVersion: kubeproxy.config.k8s.io/v1alpha1
bindAddress: 0.0.0.0
metricsBindAddress: 0.0.0.0:10249
iptables:
masqueradeAll: true
masqueradeBit: null
minSyncPeriod: 0s
syncPeriod: 0s
ipvs:
masqueradeAll: true
excludeCIDRs: null
minSyncPeriod: 0s
scheduler: "rr"
strictARP: false
syncPeriod: 0s
tcpFinTimeout: 0s
tcpTimeout: 0s
udpTimeout: 0s
mode: "ipvs"
clientConnection:
kubeconfig: /opt/k8s/cfg/kube-proxy.kubeconfig
hostnameOverride: k8s-master-02
clusterCIDR: 10.0.0.0/24
生效方法
systemctl restart kube-proxy
验证ipvs
ipvsadm -l
更多推荐
所有评论(0)