k8s端口问题

在service中

port:8199   //svc端口

targetport:8199    //容器端口

在ingress中

servicePort:8199   //svc端口要与port端口一致

headless_serivce(无头服务)

在svc资源中增加如下表示启动无头服务,不走clusterip,默认不设置走的就是clusterip模式。

apiVersion: v1
kind: Service
metadata:
  name: main
  labels:
    helm.sh/chart: main
spec:
  type: ClusterIP
  clusterIP: None   //启动无头服务
  ports:
    - port: 8199
      targetPort: 8199
      protocol: TCP
      name: http
  selector:
    app.kubernetes.io/name: main
    app.kubernetes.io/instance: main

headless_serivce(无头服务)在deployment和StatefulSet都能使用

StatefulSet

使用StatefulSet资源时候service资源标准规范设置是加无头服务的,也就是说在端口设置上要保持和容器的端口一致,无头服务了所以没有分配IP地址所以无法做转发不走svcip,添加配置clusterIP: None ,像开源组件zookeeper官方给出的k8s.yaml文件,配置了2个service资源对象,一个是headless,一个是clusterIP。

例子如下:

---
apiVersion: v1
kind: Service
metadata:
  namespace: sgm-middleware
  name: zookeeper-svc
  labels:
    app: zookeeper
spec:
  ports:
    - port: 2181
      name: client
  selector:
    app: zookeeper
---
apiVersion: v1
kind: Service
metadata:
  namespace: sgm-middleware
  name: zookeeper-svc-headless
spec:
  ports:
    - port: 2888
      name: server
    - port: 3888
      name: leader-election
  clusterIP: None
  selector:
    app: zookeeper
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  namespace: sgm-middleware
  name: zookeeper
  labels:
    app: zookeeper
spec:
  serviceName: zookeeper-svc-headless
  replicas: 3
  updateStrategy:
    type: OnDelete
  selector:
    matchLabels:
      app: zookeeper
  template:
    metadata:
      labels:
        app: zookeeper
    spec:
      containers:
        - name: zookeeper-server
          imagePullPolicy: Always
          image: test-zk.com/paas/zookeeper:3.4.6
          resources:
            limits:
              cpu: "2"
              memory: 4Gi
            requests:
              cpu: "1"
              memory: 2Gi
          ports:
            - containerPort: 2181
              name: client
            - containerPort: 2888
              name: server
            - containerPort: 3888
              name: leader-election
          env:
            - name: TZ
              value: "Asia/Shanghai"
            - name: sleep_seconds
              value: "120"
            - name : ZK_REPLICAS
              value: "3"
            - name : ZK_HEAP_SIZE
              value: "1G"
            - name : ZK_TICK_TIME
              value: "2000"
            - name : ZK_INIT_LIMIT
              value: "10"
            - name : ZK_SYNC_LIMIT
              value: "5"
            - name : ZK_MAX_CLIENT_CNXNS
              value: "60"
            - name: ZK_SNAP_RETAIN_COUNT
              value: "3"
            - name: ZK_PURGE_INTERVAL
              value: "1"
            - name: ZK_LOG_LEVEL
              value: INFO
            - name: ZK_CLIENT_PORT
              value: "2181"
            - name: ZK_SERVER_PORT
              value: "2888"
            - name: ZK_ELECTION_PORT
              value: "3888"
          volumeMounts:
            - name: datadir
              mountPath: /var/lib/zookeeper
              subPath: data
  volumeClaimTemplates:
    - metadata:
        name: datadir
      spec:
        accessModes: [ "ReadWriteOnce" ]
        storageClassName: nfs-rd
        resources:
          requests:
            storage: 100Gi

其中把集群内部通信的端口2888 和 集群选举端口3888 设置成了 headless方式,端口2181就是zk服务端暴露出来供zk客户端连接的端口,之后zk客户端(ssh终端或者java代码客户端)发送命令到服务端都会经过这个端口设置成了clusterIP,2181这么设置是为了集群可以在k8s外访问,当暴漏NodePort(公网或集团内公网)的时候就会用上,而headless方式是无法使用NodePort的。

posted @ 2021-12-27 11:32  a戴姆勒  阅读(639)  评论(0编辑  收藏  举报