1. 为什么选择CentOS 7作为Istio的部署平台?
CentOS 7作为企业级Linux发行版,至今仍是许多传统企业的首选操作系统。尽管CentOS 8已停止维护,但CentOS 7凭借其长期支持周期(维护至2024年6月30日)和极高的稳定性,仍然是生产环境中的主力军。特别是在金融、电信等行业,系统升级周期长,CentOS 7的市场占有率依然可观。
选择CentOS 7部署Kubernetes和Istio组合主要基于以下考虑:
- 内核兼容性:CentOS 7默认搭载3.10内核,虽较旧但完全满足Kubernetes的最低要求(1.14+版本需内核3.10+)
- 软件包成熟度:yum仓库中的Docker、kubelet等关键组件版本经过充分测试
- 企业运维惯性:许多企业的自动化运维体系都是基于CentOS 7构建
- 硬件兼容性:对老旧服务器硬件的驱动支持更好
提示:虽然CentOS 7可以运行最新版Kubernetes和Istio,但建议生产环境至少升级内核到4.x版本以获得更好的容器支持。可通过
yum install kernel-lt安装长期支持版内核。
需要模型API调用? 免费领10W Token,多模型网关一键接入 Claude、DeepSeek 等主流模型。
2. 基础环境准备与Kubernetes集群搭建
2.1 系统初始化配置
在开始前,需要确保所有节点(建议至少3节点:1 master + 2 worker)完成以下准备:
bash复制# 关闭SELinux(Istio组件需要)
setenforce 0
sed -i 's/^SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/config
# 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
# 加载br_netfilter模块
modprobe br_netfilter
echo 'br_netfilter' > /etc/modules-load.d/br_netfilter.conf
# 配置内核参数
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sysctl --system
2.2 Docker安装与配置
虽然Kubernetes已开始转向containerd,但在CentOS 7上Docker仍是更稳定的选择:
bash复制# 安装旧版docker(兼容性更好)
yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install -y docker-ce-19.03.15 docker-ce-cli-19.03.15 containerd.io
# 配置cgroup驱动为systemd
mkdir -p /etc/docker
cat <<EOF > /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
]
}
EOF
systemctl enable --now docker
2.3 Kubernetes集群部署
使用kubeadm部署集群时需特别注意版本兼容性:
bash复制# 配置k8s源
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
EOF
# 安装指定版本(1.20.x是CentOS 7上最稳定的版本)
yum install -y kubelet-1.20.15 kubeadm-1.20.15 kubectl-1.20.15
systemctl enable --now kubelet
# master节点初始化(注意替换apiserver-advertise-address)
kubeadm init \
--apiserver-advertise-address=192.168.1.100 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.20.15 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16
# 配置kubectl
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
# 安装Flannel网络插件
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
注意:如果遇到"kubelet isn't running"错误,通常是因为cgroup驱动不匹配,检查docker和kubelet的cgroup配置是否一致。
3. Istio服务网格部署与验证
3.1 Istio 1.12定制化安装
选择Istio 1.12.x版本因其对旧版Kubernetes更好的兼容性:
bash复制# 下载指定版本
curl -L https://istio.io/downloadIstio | ISTIO_VERSION=1.12.8 sh -
cd istio-1.12.8
export PATH=$PWD/bin:$PATH
# 生成定制化配置(启用ingressgateway和监控组件)
cat <<EOF > custom-config.yaml
apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
spec:
profile: default
components:
ingressGateways:
- name: istio-ingressgateway
enabled: true
k8s:
service:
type: NodePort
egressGateways:
- name: istio-egressgateway
enabled: false
values:
global:
proxy:
autoInject: disabled
pilot:
traceSampling: 10.0
telemetry:
enabled: true
prometheus:
enabled: true
grafana:
enabled: true
kiali:
enabled: true
EOF
# 执行安装
istioctl install -f custom-config.yaml
3.2 关键组件验证
安装完成后需要检查各组件状态:
bash复制# 检查控制平面
kubectl -n istio-system get pods
# 预期输出应包含以下Running状态的pod:
# istiod-xxxxx 1/1 Running
# istio-ingressgateway-xxxxx 1/1 Running
# prometheus-xxxxx 2/2 Running
# grafana-xxxxx 1/1 Running
# kiali-xxxxx 1/1 Running
# 验证注入功能
kubectl create ns test
kubectl label ns test istio-injection=enabled
kubectl -n test apply -f samples/sleep/sleep.yaml
kubectl -n test get pods -l app=sleep -o jsonpath='{.items[0].spec.containers[*].name}'
# 正确输出应包含"istio-proxy"
3.3 常见问题排查
在CentOS 7环境中可能遇到的典型问题:
-
Pod启动失败报"conntrack缺失"错误:
bash复制
yum install -y conntrack-tools -
IngressGateway无法获取外部IP:
bash复制# 修改服务类型为NodePort kubectl -n istio-system patch svc istio-ingressgateway -p '{"spec":{"type":"NodePort"}}' -
Prometheus监控数据不显示:
bash复制# 检查scrape配置 kubectl -n istio-system get prometheus -o yaml | grep targets # 必要时重启prometheus pod
4. 流量控制实战:金丝雀发布与故障注入
4.1 部署示例应用
我们使用Bookinfo应用作为演示案例:
bash复制kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml
kubectl apply -f samples/bookinfo/networking/bookinfo-gateway.yaml
# 验证访问
export INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].nodePort}')
curl -s "http://$NODE_IP:$INGRESS_PORT/productpage" | grep -o "<title>.*</title>"
4.2 基于权重的流量路由
实现v1和v3版本按7:3比例分流:
yaml复制apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v1
weight: 70
- destination:
host: reviews
subset: v3
weight: 30
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: reviews
spec:
host: reviews
subsets:
- name: v1
labels:
version: v1
- name: v3
labels:
version: v3
应用配置后,多次访问productpage页面,可以看到70%的请求会显示不带评分的界面(v1),30%显示带红色星标的评分(v3)。
4.3 故障注入测试
模拟reviews服务v3版本延迟:
yaml复制apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- fault:
delay:
percentage:
value: 100
fixedDelay: 7s
route:
- destination:
host: reviews
subset: v3
这个配置会导致所有访问v3版本的请求都会延迟7秒响应,可以用来测试前端服务的超时重试机制。
4.4 生产环境最佳实践
-
渐进式交付策略:
- 初始阶段:1%流量导向新版本
- 监控关键指标(错误率、延迟等)至少30分钟
- 每阶段流量增幅不超过10%
-
熔断配置示例:
yaml复制apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: ratings-cb spec: host: ratings trafficPolicy: connectionPool: tcp: maxConnections: 100 http: http2MaxRequests: 1000 maxRequestsPerConnection: 10 outlierDetection: consecutive5xxErrors: 5 interval: 5s baseEjectionTime: 30s maxEjectionPercent: 50 -
监控看板集成:
bash复制# 暴露Kiali控制台 kubectl -n istio-system patch svc kiali -p '{"spec":{"type":"NodePort"}}' # 获取访问端口 kubectl -n istio-system get svc kiali -o jsonpath='{.spec.ports[0].nodePort}'
5. 性能调优与运维技巧
5.1 资源限制配置
Istio组件默认资源请求较低,生产环境需要调整:
yaml复制# istio-operator.yaml调整示例
spec:
components:
pilot:
k8s:
resources:
requests:
cpu: 500m
memory: 1024Mi
ingressGateways:
- name: istio-ingressgateway
k8s:
resources:
requests:
cpu: 1000m
memory: 1024Mi
5.2 内核参数优化
针对高负载场景的内核调优:
bash复制# /etc/sysctl.conf追加
net.core.somaxconn = 32768
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 1024 65000
# 针对Sidecar注入的pod
annotations:
proxy.istio.io/config: |
concurrency: 4
defaultConfig:
holdApplicationUntilProxyStarts: true
5.3 诊断工具集
-
Envoy调试端点:
bash复制# 获取pod中envoy的管理端口 kubectl exec -it productpage-v1-xxxxx -c istio-proxy -- netstat -ltnp # 访问调试接口 kubectl exec -it productpage-v1-xxxxx -c istio-proxy -- curl localhost:15000/config_dump -
性能分析工具:
bash复制# 采集CPU profile kubectl exec -it istiod-xxxxx -n istio-system -- curl localhost:15014/debug/pprof/profile -o /tmp/profile.out kubectl cp istio-system/istiod-xxxxx:/tmp/profile.out ./profile.out go tool pprof -http=:8080 ./profile.out -
网络抓包技巧:
bash复制# 在sidecar容器中抓包 kubectl exec -it productpage-v1-xxxxx -c istio-proxy -- tcpdump -i eth0 -w /tmp/dump.pcap kubectl cp default/productpage-v1-xxxxx:/tmp/dump.pcap ./dump.pcap
在实际生产环境中,我们通常会遇到CentOS 7特有的磁盘空间监控问题。由于Istio组件和Kubernetes日志会持续产生数据,建议设置日志轮转:
bash复制# 配置journal日志限制
cat <<EOF > /etc/systemd/journald.conf
[Journal]
SystemMaxUse=1G
RuntimeMaxUse=500M
EOF
systemctl restart systemd-journald
# Docker日志限制
cat <<EOF > /etc/docker/daemon.json
{
"log-driver": "json-file",
"log-opts": {
"max-size": "50m",
"max-file": "3"
}
}
EOF
systemctl restart docker
经过多次生产实践,我发现CentOS 7上最稳定的组合是:Kubernetes 1.20.x + Istio 1.12.x + Docker 19.03.x。新版本虽然功能更多,但在老旧内核上容易出现兼容性问题。对于关键业务系统,建议先在测试环境充分验证各组件版本组合,再逐步推进到生产环境。
