k8s service创建之后无法访问集群内本节点的其他pod

你若安好便是晴天

1.问题

deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: service-deployment
  namespace: dev
spec:
  replicas: 3
  selector:
    matchLabels:
          app: nginx-pod
  template:
    metadata:
     labels:
       app: nginx-pod
    spec:
      containers:
      - name: nginx
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: service-type
  namespace: dev
spec:
  selector:
    app: nginx-pod
  clusterIP: 10.1.97.97
  type: ClusterIP
  ports:
  - port: 80 #service端口
    targetPort: 80 #pod端口

创建之后信息

[root@node1 ~]# kubectl describe svc -n dev
Name:              service-type
Namespace:         dev
Labels:            <none>
Annotations:       <none>
Selector:          app=nginx-pod
Type:              ClusterIP
IP Families:       <none>
IP:                10.1.97.97
IPs:               10.1.97.97
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.244.1.44:80,10.244.1.45:80,10.244.2.46:80
Session Affinity:  None
Events:            <none>

pod信息

[root@node1 ~]# kubectl get pod -n dev -o wide
NAME                                 READY   STATUS    RESTARTS   AGE     IP            NODE    NOMINATED NODE   READINESS GATES
service-deployment-848c68b85-8jvcv   1/1     Running   0          90m     10.244.1.44   node2   <none>           <none>
service-deployment-848c68b85-m62qb   1/1     Running   0          90m     10.244.1.45   node2   <none>           <none>
service-deployment-848c68b85-qwz5w   1/1     Running   0          90m     10.244.2.46   node3   <none>           <none>

在node2节点上访问node3上的pod 10.244.2.46:80 不通

image.png

2.解决过程

找到flannel配置名

[root@node2 docker]# kubectl get pod -n kube-system
NAME                            READY   STATUS    RESTARTS   AGE
coredns-7f89b7bc75-jvfhw        1/1     Running   4          6d12h
coredns-7f89b7bc75-x6kqb        1/1     Running   4          6d12h
etcd-node1                      1/1     Running   4          6d12h
kube-apiserver-node1            1/1     Running   4          6d12h
kube-controller-manager-node1   1/1     Running   8          6d12h
--
kube-flannel-ds-bqs55           1/1     Running   0          18h
kube-flannel-ds-czjxv           1/1     Running   5          6d12h
kube-flannel-ds-mbgm4           1/1     Running   4          6d12h
---
kube-proxy-2tn4g                1/1     Running   0          132m
kube-proxy-4tc4f                1/1     Running   0          132m
kube-proxy-bwsgv                1/1     Running   0          132m
kube-scheduler-node1            1/1     Running   8          6d12h

获取flannel的配置名称

kubectl edit pod -n kube-system kube-flannel-ds-bqs55

找到flannel配置名
image.png

修改配置,kube-flannel-ds为flannel配置文件名称

kubectl edit daemonset kube-flannel-ds -n kube-system

指定网卡为第二块网卡,可以通过ifconfig来进行查看
image.png

找到所有kube-flannelpod

[root@node2 docker]# kubectl get pod -n kube-system | grep kube-flan
kube-flannel-ds-b7qfq           1/1     Running    0          29s
kube-flannel-ds-w9hts           1/1     Running    0          17s
kube-flannel-ds-zpzmb           0/1     Init:0/1   0          1s
[root@node2 docker]# kubectl delete pod -n kube-system kube-flannel-ds-b7qfq kube-flannel-ds-w9hts kube-flannel-ds-zpzmb
pod "kube-flannel-ds-b7qfq" deleted
pod "kube-flannel-ds-w9hts" deleted
pod "kube-flannel-ds-zpzmb" deleted

删除之后,重新创建即可正常访问

参考 https://www.jianshu.com/p/ed1...

阅读 182
71 声望
8 粉丝
0 条评论
你知道吗?

71 声望
8 粉丝
宣传栏