kubernetesreplicaset

How can I specify a custom name for a ReplicaSet created by a Kubernetes Deployment?


I’m working with Kubernetes Deployments, and I’d like to control the naming convention for the associated ReplicaSets.

By default, Kubernetes generates a unique name for each ReplicaSet (e.g., nginx-8576854954). However, I want to create a ReplicaSet with a custom name (e.g., nginx-replicaset1).

If you decide to go with a ReplicaSet, here’s an example manifest:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx-replicaset1
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:latest
          ports:
            - containerPort: 80

Save this into a file (e.g., nginx-replicaset.yaml) and apply it using kubectl apply -f nginx-replicaset.yaml.


Solution

  • There's no way to control the name of the ReplicaSets a Deployment creates. Of note, each time you modify the Deployment, it will create a new ReplicaSet, and the Deployment controller will need to choose a new unique name, and it's very possible to have 3 or more ReplicaSets for a single Deployment.

    In a comment you mention a "limitation of 15 symbols". The normal suffixes added by a Deployment to create its ReplicaSets, and then by a ReplicaSet to create its Pods, usually add up to about a dozen characters on their own, so limiting names to 15 characters isn't reasonable for Kubernetes. (There is a more general limit of 63 characters for a name, which comes from DNS's rules, but that's a much more manageable limit.)