Skip to content

How to configure our GoLang Server to use any port

Cesar Celis Hernandez edited this page Mar 29, 2022 · 4 revisions

Objective:

Explain initConsoleServer() function in buckets_test.go in order for people to see how to configure the server to select any port as needed. Currently this function is creating confution around the way we handle 9090 port and 9000 port from our docker container. Then, let's clarify this confution in this short document.

Docker container

Let's start by looking and explaining our docker container with the MinIO Server:

docker run \
  -v /data1 \
  -v /data2 \
  -v /data3 \
  -v /data4 \
  --net=mynet123 \
  -d \
  --name minio \
  --rm \
  -p 9000:9000 -p 9001:9001 \
  -e MINIO_KMS_SECRET_KEY=my-minio-key:OSMM+vkKUTCvQs9YL/CVMIMt43HFhkUpqJxTmGl6rYw= \
  "quay.io/minio/minio:latest" server /data{1...4} --address :9000 --console-address :9001

As output you will see this:

Formatting 1st pool, 1 set(s), 4 drives per set.

WARNING: Host local has more than 2 drives of set. A host failure will result in data becoming unavailable.

Automatically configured API requests per node based on available memory on the system: 25

Status:         4 Online, 0 Offline. 

API: http://173.18.0.2:9000  http://127.0.0.1:9000 


Console: http://173.18.0.2:9001 http://127.0.0.1:9001 

Explanation: API is running on port 9000 inside the container and the port is being exposed in same port number 9000

Go Lang Server

File: ~/console/integration/buckets_test.go

func initConsoleServer() (*restapi.Server, error) {

	os.Setenv("CONSOLE_MINIO_SERVER", "http://localhost:9000") // This is the port we are listening on to create the server

	swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON)
	if err != nil {
		return nil, err
	}

	noLog := func(string, ...interface{}) {
		// nothing to log
	}

	// Initialize MinIO loggers
	restapi.LogInfo = noLog
	restapi.LogError = noLog

	api := operations.NewConsoleAPI(swaggerSpec)
	api.Logger = noLog

	server := restapi.NewServer(api)
	// register all APIs
	server.ConfigureAPI()

	//restapi.GlobalRootCAs, restapi.GlobalPublicCerts, restapi.GlobalTLSCertsManager = globalRootCAs, globalPublicCerts, globalTLSCerts

	consolePort, _ := strconv.Atoi("9090") // this is the port we are providing on the service.

	server.Host = "0.0.0.0"
	server.Port = consolePort
	restapi.Port = "9090"
	restapi.Hostname = "0.0.0.0"

	return server, nil
}

Explanation: CONSOLE_MINIO_SERVER is being expected in 9000 and our server is exposing the API in port 9090

How to use different ports in a single container environment

  1. Start by changing the docker container from port 9000 to port 6000
docker run \
  -v /data1 \
  -v /data2 \
  -v /data3 \
  -v /data4 \
  --net=mynet123 \
  -d \
  --name minio \
  --rm \
  -p 6000:6000 -p 6001:6001 \
  -e MINIO_KMS_SECRET_KEY=my-minio-key:OSMM+vkKUTCvQs9YL/CVMIMt43HFhkUpqJxTmGl6rYw= \
  "quay.io/minio/minio:latest" server /data{1...4} --address :6000 --console-address :6001

Notice --address, --console, and -p are changed to 6000 and 6001

  1. Update GoLang Server in buckets_test.go to expect MinIO server in proper port 6000
func initConsoleServer() (*restapi.Server, error) {

	os.Setenv("CONSOLE_MINIO_SERVER", "http://localhost:6000") // This is the port we are listening on to create the server

	swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON)
	if err != nil {
		return nil, err
	}
        ...

Important line is: os.Setenv("CONSOLE_MINIO_SERVER", "http://localhost:6000") since this will overide the default which is 9000 to be 6000

  1. Then, serve the API in 9091 rather than 9090
func initConsoleServer() (*restapi.Server, error) {

        ...

	consolePort, _ := strconv.Atoi("9091") // this is the port we are providing on the service.

	server.Host = "0.0.0.0"
	server.Port = consolePort
	restapi.Port = "9091"
	restapi.Hostname = "0.0.0.0"

	return server, nil
}

Important lines are: consolePort, _ := strconv.Atoi("9091") and restapi.Port = "9091" now the API Server will serve on a different port

  1. You will have to update every GET/POST in the test to call API on 9091
	request, err := http.NewRequest("POST", "http://localhost:9091/api/v1/login", requestDataBody)
	if err != nil {
		log.Println(err)
		return
	}

Notice the POST is being performed against "http://localhost:9091/api/v1/login" not 9090 anymore.

For a multiple container test (multi MinIO Servers)

We can do something like this:

# 1st kill all existing containers
docker stop minio
docker stop minio2
docker stop minio3
docker stop minio4
brew services stop postgresql
docker stop pgsqlcontainer
docker network rm mynet123

# 2nd create multiple minio servers
docker network create --subnet=173.18.0.0/29 mynet123
docker run --net=mynet123 --ip=173.18.0.3 --name pgsqlcontainer --rm -p 5432:5432 -e POSTGRES_PASSWORD=password -d postgres
docker run -v /data1 -v /data2 -v /data3 -v /data4 --net=mynet123 -d --name minio --rm -p 9000:9000 -p 9001:9001 -e MINIO_KMS_SECRET_KEY=my-minio-key:OSMM+vkKUTCvQs9YL/CVMIMt43HFhkUpqJxTmGl6rYw= "quay.io/minio/minio:latest" server /data{1...4} --address :9000 --console-address :9001
docker run -v /data1 -v /data2 -v /data3 -v /data4 --net=mynet123 -d --name minio2 --rm -p 9002:9002 -p 9003:9003 -e MINIO_KMS_SECRET_KEY=my-minio-key:OSMM+vkKUTCvQs9YL/CVMIMt43HFhkUpqJxTmGl6rYw= "quay.io/minio/minio:latest" server /data{1...4} --address :9002 --console-address :9003
docker run -v /data1 -v /data2 -v /data3 -v /data4 --net=mynet123 -d --name minio3 --rm -p 9004:9004 -p 9005:9005 -e MINIO_KMS_SECRET_KEY=my-minio-key:OSMM+vkKUTCvQs9YL/CVMIMt43HFhkUpqJxTmGl6rYw= "quay.io/minio/minio:latest" server /data{1...4} --address :9004 --console-address :9005
docker run -v /data1 -v /data2 -v /data3 -v /data4 --net=mynet123 -d --name minio4 --rm -p 9006:9006 -p 9007:9007 -e MINIO_KMS_SECRET_KEY=my-minio-key:OSMM+vkKUTCvQs9YL/CVMIMt43HFhkUpqJxTmGl6rYw= "quay.io/minio/minio:latest" server /data{1...4} --address :9006 --console-address :9007

# 3rd run the tests
cd ~/console/integration
go test