<p>I am trying to have some consumers to process messages from kafka, and I would like to implement kubernetes deployment scalability for elastic message processing capability.</p>
<p>I found this code from sarama official guide <a href="https://pkg.go.dev/github.com/Shopify/sarama#NewConsumerGroup" rel="nofollow noreferrer">https://pkg.go.dev/github.com/Shopify/sarama#NewConsumerGroup</a>:</p>
<pre><code>package main

import (
    &quot;context&quot;
    &quot;fmt&quot;
)

type exampleConsumerGroupHandler struct{}

func (exampleConsumerGroupHandler) Setup(_ ConsumerGroupSession) error   { return nil }
func (exampleConsumerGroupHandler) Cleanup(_ ConsumerGroupSession) error { return nil }
func (h exampleConsumerGroupHandler) ConsumeClaim(sess ConsumerGroupSession, claim ConsumerGroupClaim) error {
    for msg := range claim.Messages() {
        fmt.Printf(&quot;Message topic:%q partition:%d offset:%d\n&quot;, msg.Topic, msg.Partition, msg.Offset)
        sess.MarkMessage(msg, &quot;&quot;)
    }
    return nil
}

func main() {
    config := NewTestConfig()
    config.Version = V2_0_0_0 // specify appropriate version
    config.Consumer.Return.Errors = true

    group, err := NewConsumerGroup([]string{&quot;localhost:9092&quot;}, &quot;my-group&quot;, config)
    if err != nil {
        panic(err)
    }
    defer func() { _ = group.Close() }()

    // Track errors
    go func() {
        for err := range group.Errors() {
            fmt.Println(&quot;ERROR&quot;, err)
        }
    }()

    // Iterate over consumer sessions.
    ctx := context.Background()
    for {
        topics := []string{&quot;my-topic&quot;}
        handler := exampleConsumerGroupHandler{}

        // `Consume` should be called inside an infinite loop, when a
        // server-side rebalance happens, the consumer session will need to be
        // recreated to get the new claims
        err := group.Consume(ctx, topics, handler)
        if err != nil {
            panic(err)
        }
    }
}
</code></pre>
<p>I have some questions:</p>
<ol>
<li>how to set numbers of consumers in a consumer group?</li>
<li>If I deploy this program in a Pod, can I scale it safely? I mean, assume one program is running, and I scale the replicas from 1 to 2, will another <code>NewConsumerGroup</code> call with the same group id works perfectly without conflict?</li>
</ol>
<p>Thank you in advance.</p>
<p>NOTE: I am using Kafka 2.8 and I heard that sarama_cluster package is DEPRECATED.</p>

Li Ziyan
<p>Reminder that groups cannot scale beyond the topic partition count</p>
<p>Scaling the pods is the correct way to use consumer groups, and using the same group name is correct, however I'd recommend extracting that and the broker address to environment variables so they can easily be changed at deploy time</p>
<p>As-is the containerized code would be unable to use localhost as a Kafka connection string as that would be the pod itself</p>
