文章都可以转载、引用,标明转载出处即可。

K8S中service关联多组pod

Kubernetes Mr.c 89℃ 0评论

  首先service的selector是根据pod的label进行匹配,所以多组pod上只要具有相同的标签即可关联,如下配置所示,deployment定义nginx-test-1 ,需要在spec.template.metadata.labels下增加一个标签就能定义到pod上,这里我们增加定义ka:nginx-demo的标签。

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
  labels:
    app: nginx-test-1
  name: nginx-test-1
  namespace: qa
spec:
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: nginx-test-1
  template:
    metadata:
      labels:
        app: nginx-test-1
        ka: nginx-demo
    spec:
      containers:
        - image: 'nginx:latest'
          imagePullPolicy: Always
          name: nginx-test-1
          resources:
            requests:
              cpu: 250m
              memory: 256Mi
      dnsPolicy: ClusterFirst
      restartPolicy: Always

而相对应的service配置如下:

apiVersion: v1
kind: Service
metadata:
  name: nginx-demo
  namespace: qa
spec:
  ports:
  - name: nginx-demo
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    ka: nginx-demo
  type: ClusterIP

查看是否已经被service管理,可以查看下endpoint状态:

kubectl get ep nginx-demo -n qa

转载请注明:菜鸟运维网 » K8S中service关联多组pod

喜欢 (7)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址