[ELK] Logstash 401 Unauthorized 에러

Kibana에 로그가 전달이 되질 않아Docker Desktop으로 logstash의 로그를 확인해보니 다음과 같은 에러가 발생했다.

[2023-10-04T08:03:23,112][WARN ][logstash.outputs.elasticsearch][main] 
Attempted to resurrect connection to dead ES instance, but got an error 
{:url=>"http://logstash_internal:xxxxxx@elasticsearch:9200/", :exception=>LogStash::Outputs::ElasticSearch::HttpClient::Pool::BadResponseCodeError, :message=>"Got response code '401' contacting Elasticsearch at URL 'http://elasticsearch:9200/'"}

 

이 오류의 발생 원인은 총 2가지가 될 수 있다.

1. 사용자 이름 또는 비밀번호 입력 오류

2. Elasticsearch에서 logstash.conf에 작성된 user(사용자)를 찾을수 없는 경우

 

필자의 경우에는 사용자 이름과 비밀번호를 알맞게 입력해 접속했기 때문에 1번은 해당하지 않았고

의심해볼수 있는건 2번의 경우였다.

 

필자의 ELK 파일은 아래의 git에서 clone받은 elk 파일들로 구성되어 있다.
git clone https://github.com/900gle/docker-elk

 

위 git 파일을 clone 한뒤 logstash.conf 파일을 kafka로 부터 메세지를 받아 Elasticsearch에 전달하도록 다음과 같이 코드를 수정했는데

여기서 output 부분의 user에 작성된 "logstash_internal" 사용자를 찾지못했기 때문에 발생하는 문제였다.

logstash.conf
input {
    kafka {
        bootstrap_servers => "kafka:29092"
        topics => ["ek-log"]
        consumer_threads => 1
        decorate_events => true
    }
}

## Add your filters / logstash plugins configuration here

output {
	elasticsearch {
		hosts => "elasticsearch:9200"
		user => "logstash_internal"
		password => "${LOGSTASH_INTERNAL_PASSWORD}"
		index => "ek-log-%{+YYYY.MM.dd}"
	}
}

 

logstash_internal 사용자 존재 유무 확인

우선 logstash_internal가 정말 존재하지 않는지 아래 명령어를 통해 확인해보자.

curl -u elastic:비밀번호 "http://localhost:9200/_security/user/logstash_internal"

만약 빈 중괄호 {}가 나온다면 해당 사용자가 존재하지 않는 것이다.

 

 

logstash_internal 사용자 생성

아래 명령을 통해 해당 사용자를 생성해주도록하자.

curl -u elastic:비밀번호 -XPOST "http://localhost:9200/_security/user/logstash_internal" -H "Content-Type: application/json" -d "{ \"password\" : \"비밀번호\", \"roles\" : [ \"superuser\" ], \"full_name\" : \"Internal Logstash User\" }"

 

생성 확인
curl -u elastic:비밀번호 "http://localhost:9200/_security/user/logstash_internal"

생성한 logstash_internal 사용자가 보인다면 이제 401에러 문제는 해결된 것이다.