如何在部署时使用 Kubernetes 为 pod 分配静态 IP [英] How to assign a static IP to a pod using Kubernetes on deployment

查看:94
本文介绍了如何在部署时使用 Kubernetes 为 pod 分配静态 IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在部署时为 pod 分配静态 IP 地址.

I am trying to assign a static IP address to a pod on deployment.

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: aws-test-mysql
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: aws-test-mysql
    spec:
      containers:
      - name: aws-test-mysql
        image: 461677341235123.dkr.ecr.us-east-1.amazonaws.com/aws-test-mysql
        securityContext:
          privileged: true
        ports:
        - containerPort: 3306
          hostIP: 172.20.32.50
          hostPort: 3306
        resources:
          requests:
            cpu: 100m
      imagePullSecrets:
        - name: ecrkey

正如我在描述我的 pod 时所看到的,它是使用另一个 IP 创建的.

As you can see when I described my pod it is created with another IP.

test-mbp1:aws test$ kubectl describe pods | grep IP
IP:     100.96.1.3

我正在尝试在种类:部署"上部署具有静态 IP 的 pod,而不是作为服务.

I'm trying to deploy a pod with a static IP on "kind: Deployment" and not as a service.

这可能吗?

推荐答案

由于 kubernetes IP 层的动态特性,静态 IP 无法分配给 Pod.

A static IP cannot be assigned to a Pod because of the dynamic nature of kubernetes' IP layer.

由于您不想附加服务(恕我直言,这是最好的方法),一个接近的替代方法是将部署转换为 StatefulSet.这将为 Pod 提供一个或多或少满足您要求的静态主机名.

Since you don't want to attach a Service (which is the best way imho), a close alternative is to convert the Deployment to a StatefulSet. This will give the Pod a static hostname which more-or-less fulfils your requirement.

StatefulSet 的第一个副本将被称为 aws-test-mysql-0.<kubernetes.cluster.tld>.

The first replica of the StatefulSet will be called aws-test-mysql-0.<kubernetes.cluster.tld>.

这篇关于如何在部署时使用 Kubernetes 为 pod 分配静态 IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆