k8s pod hostNetwork网络模式

问题:

往 k8s 搭建的 fastdfs集群上传文件报错、、

################## fastdfs 参数配置 ##################
fdfs:
  so-timeout: 1501
  connect-timeout: 601
  tracker-list: # tracker地址
    - 192.168.153.131:32122

###

[hadoop@k8s-node-01 fastdfs]$ kubectl  get pod -n fastdfs-ns -o wide
NAME        READY   STATUS    RESTARTS   AGE     IP            NODE       NOMINATED NODE   READINESS GATES
storage-0   1/1     Running   0          2m57s   10.244.0.13   hadoop03   <none>           <none>
storage-1   1/1     Running   0          2m54s   10.244.0.14   hadoop03   <none>           <none>
storage-2   1/1     Running   0          2m50s   10.244.0.15   hadoop03   <none>           <none>
tracker-0   1/1     Running   0          5m40s   10.244.0.9    hadoop03   <none>           <none>

###

[hadoop@k8s-node-01 fastdfs]$ kubectl  get svc -n fastdfs-ns -o wide
NAME                   TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)           AGE   SELECTOR
storage                ClusterIP   None           <none>        23000/TCP         32m   app=storage
storage-nodeport-svc   NodePort    10.1.109.246   <none>        8888:32088/TCP    63m   app=storage
tracker                ClusterIP   None           <none>        22122/TCP         51m   app=tracker
tracker-nodeport-svc   NodePort    10.1.73.59     <none>        22122:32122/TCP   51m   app=tracker

k8s pod hostNetwork网络模式_第1张图片
以上看出返回storage地址是 pod ,本地无法连接…

pod 网络

每个Pod都会默认启动一个pod-infrastructure(或pause)的容器,作为共享网络的基准容器。其他业务容器在启动之后,会将自己的网络模式指定为““NetworkMode”: "container:pause_containerID”。这样就能做到Pod中的所有容器网络都是共享的,一个Pod中的所有容器中的网络是一致的,它们能够通过本地地址(localhost)访问其他用户容器的端口。在Kubernetes的网络模型中,每一个Pod都拥有一个扁平化共享网络命名空间的IP,称为PodIP。通过PodIP,Pod就能够跨网络与其他物理机和容器进行通信。
  也可以设置Pod为Host网络模式,即直接使用宿主机的网络,不进行网络虚拟化隔离。这样一来,Pod中的所有容器就直接暴露在宿主机的网络环境中,这时候,Pod的PodIP就是其所在Node的IP。从原理上来说,当设定Pod的网络为Host时,是设定了Pod中pod-infrastructure(或pause)容器的网络为Host,Pod内部其他容器的网络指向该容器。

k8s Fastdfs

tracker-statefulset.yaml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    app: tracker
  name: tracker  #statefulset名为tracker创建的pod名以该名为基础按顺序为tacker-0 1 2 3...
  namespace: flink
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tracker
  serviceName: "tracker"  #serviceName名为tracker不要与statefulset名混淆,pod的解析需要在pod名后加该名例如tracker-0.tracker.default即$pod名.$serviceName.$命名空间
  template:
    metadata:
      labels:
        app: tracker
    spec:
      containers:
      - image: delron/fastdfs
        name: fastdfs
        command: ["/bin/bash", "/usr/bin/start1.sh","tracker"] #容器启动命令本次为使用tracker
        #volumeMounts: #track数据挂载在pvc上默认容器的数据目录为/var/fdfs
        #- name: tracker-persistent-storage
        #  mountPath: /var/fdfs
      hostNetwork: true
      #volumes: #创建挂载pvc claimName对应名称需要与创建pvc配置文件的名称相同
      #- name: tracker-persistent-storage
      #  persistentVolumeClaim:
      #    claimName: tracker-pv-claim

###

apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    app: storage
  name: storage
  namespace: flink
spec:
  replicas: 2
  selector:
    matchLabels:
      app: storage
  serviceName: "storage"
  template:
    metadata:
      labels:
        app: storage
    spec:
      affinity:
        nodeAffinity:          # node亲和 node的label
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: "fdfs"
                operator: In
                values:
                - "storage"
        podAntiAffinity:          # pod反亲和 pod的label
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: "app"
                operator: In
                values:
                - "storage"
            topologyKey: "kubernetes.io/hostname"  #  设置拓扑域

      containers:
      - image: delron/fastdfs
        name: fastdfs
        command: ["/bin/bash", "/usr/bin/start1.sh","storage"] #该容器启动为storage服务
        env:
        - name: TRACKER_SERVER #定义环境变量
          #value: tracker.flink.svc.cluster.local:22122 #tracker服务器地址及IP DNS解析地址为$pod名.$serviceName.$命名空间
          value: 192.168.42.131:22122
        - name: GROUP_NAME #定义存储组名
          value: group1
        #volumeMounts:
        #- name: storage-persistent-storage
        #  mountPath: /var/fdfs
      hostNetwork: yes
      #volumes:
      #- name: storage-persistent-storage

[root@k8s-node-01 fastdfs]# kubectl  get pod -n flink -o wide
NAME                                      READY   STATUS              RESTARTS        AGE   IP               NODE          NOMINATED NODE   READINESS GATES
nfs-client-provisioner-5c65dcd794-nx72x   1/1     Running             34 (4h8m ago)   30h   10.244.1.20      k8s-node-02   <none>           <none>
storage-0                                 1/1     Running             0               20s   192.168.42.132   k8s-node-02   <none>           <none>
storage-1                                 0/1     ContainerCreating   0               14s   192.168.42.133   k8s-node-03   <none>           <none>
tracker-0                                 1/1     Running             0               21m   192.168.42.131   k8s-node-01   <none>           <none>

################## fastdfs 参数配置 ##################
fdfs:
  so-timeout: 1501
  connect-timeout: 601
  tracker-list: # tracker地址
    - 192.168.153.131:32122

k8s pod hostNetwork网络模式_第2张图片

############# 另外 #############

host 模式下:tracker.fastdfs-ns.svc.cluster.local 无效…
k8s pod hostNetwork网络模式_第3张图片
在这里插入图片描述

参考:https://blog.csdn.net/u012516524/article/details/80308493

你可能感兴趣的:(FastDFS,K8S,kubernetes,fastdfs)