쿠버네티스 secret 파일 저장하기
쿠버네티스의 Secret 은 암호화해서 데이터를 저장하는데, 파일도 저장할 수 있다. 바이너리 파일이던 텍스트 파일이던 모두 base64 인코딩 스트링으로 저장이된다. 이것을 파일로 저장하는 방법에 대해서 간단하게 알아본다.
Prometheus 설정 Secret
Pormetheus 를 오퍼레이터(Operator) 로 설치를 했을 경우에 다음과 같은 Secret 을 볼 수 있다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
$ kubectl get secret prometheus-prometheus-kube-prometheus-prometheus -n monitoring -o yaml apiVersion: v1 data: prometheus.yaml.gz: H4sIAAAAAAAA/+ydz4+jNhTH7/krfNjDzFYZdraXijmttJV66G5X7bGqLMe8JN4Y27JNOqOq/3uFYxJIIBtmWCZp3g0wfjz/4H2+NmAWUs+YTCeEwJrJgnmhFRXKg10zmZIf37kJIY5bZuDgMDx6sIpJKtkMpCuNEGKszsEvoXApybUSXluhFsnu8HRVzGBa299t7hmgFowUnKXkzc2X3z7Szx8+/Xw7sYUEOhcSXDqZkgQ8rxlPylSXtFo/cuFpmS3YnL5L3t49sVxOYqG5VnOxCNf6qmdUsRxS4sCuBYdPm/Ilp5WTSbA+Z4otwCbvJoQstdK2qjwyZ9LBhJAym1XgwVGX7S5PyJRYLSEloDKjhfKb6iodcoZxiPUfj1Q701ojTAjJwVvBHTXML1OSxN0JIRaCH83rOV1YDo3mDbUQtjyzC/CbxJRQ6nNDa21XVVYwxHjZr1KyAjAhc6tlSnPwjNYrYFPPm/MoM+a0M40FB8pvM1hYwGNKbvZbxHnGV412uX3wthjSZwsSWGjXHn7XM1W+79we3EcHck5jN+nn6EHOytvSwwH8rLo6Ndr6qjttr7L03kz/hll3Xz1ikWWZBedo7MYrobLembYOOTDMMq9tSh7qHn7WGTzc3L29jQeNZBxyUD4lb/65/7flPlI6g/Mtzxed9SuO0Vmf0myDWVvN1NJONlj12m3JmjZjch+LRmdd1noWtrTEtfJMKLBdNrcnDFjqKo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Y3FV4WgkvKJgLMzFY0qSzY3f5OwRqndxvY3se2w/oDshzAi6ButCodbvY2kOCt7Kk1ainHhjtNTZ0V9yD+BDB9cOZ3X/CwAA///mZWEHdn0AAA== kind: Secret metadata: annotations: generated: "true" creationTimestamp: "2022-01-29T16:06:35Z" labels: managed-by: prometheus-operator name: prometheus-prometheus-kube-prometheus-prometheus namespace: monitoring ownerReferences: - apiVersion: monitoring.coreos.com/v1 blockOwnerDeletion: true controller: true kind: Prometheus name: prometheus-kube-prometheus-prometheus uid: 6b8415cb-b99a-4ac4-8bc1-a6175265fd1c resourceVersion: "841303" uid: 18933ea2-4dd8-464c-a61b-69513f7a807c type: Opaque |
data 필드를 보면 prometheus.yaml.gz 파일이름이 보이고 내용이 base64 스트링이 보인다. 이 prometheus.yaml.gz 파일을 받기 위해서는 간단히 bas64 스트링을 디코딩하고 나오는 스트링을 그냥 파일명으로 저장하면 된다.
1 2 3 |
$ echo "H4sIAAAAAAAA/+ydz4+jNhTH7/krfNjDzFYZdraXijmttJV66G5X7bGqLMe8JN4Y27JNOqOq/3uFYxJIIBtmWCZp3g0wfjz/4H2+NmAWUs+YTCeEwJrJgnmhFRXKg10zmZIf37kJIY5bZuDgMDx6sIpJKtkMpCuNEGKszsEvoXApybUSXluhFsnu8HRVzGBa299t7hmgFowUnKXkzc2X3z7Szx8+/Xw7sYUEOhcSXDqZkgQ8rxlPylSXtFo/cuFpmS3YnL5L3t49sVxOYqG5VnOxCNf6qmdUsRxS4sCuBYdPm/Ilp5WTSbA+Z4otwCbvJoQstdK2qjwyZ9LBhJAym1XgwVGX7S5PyJRYLSEloDKjhfKb6iodcoZxiPUfj1Q701ojTAjJwVvBHTXML1OSxN0JIRaCH83rOV1YDo3mDbUQtjyzC/CbxJRQ6nNDa21XVVYwxHjZr1KyAjAhc6tlSnPwjNYrYFPPm/MoM+a0M40FB8pvM1hYwGNKbvZbxHnGV412uX3wthjSZwsSWGjXHn7XM1W+79we3EcHck5jN+nn6EHOytvSwwH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bYFwR7gj3HFlrCH47jzzUM2NvNLo/ULovTffjktMfvPpQ7N3jcLU8+UP8hR5emk8HTrAjDXbf9Vcr22WEWQKj2W0fL1fUtYn5y+H9i8bq3e1wZWM0fELzvMrD/Iaed0aQvALzoFIHf4zLFR45hy2D85qBNWQlFndIMfhz7ErY3FV4WgkvKJgLMzFY0qSzY3f5OwRqndxvY3se2w/oDshzAi6ButCodbvY2kOCt7Kk1ainHhjtNTZ0V9yD+BDB9cOZ3X/CwAA///mZWEHdn0AAA==" | base64 -d > prometheus.yaml.gz $ file prometheus.yaml.gz prometheus.yaml.gz: gzip compressed data, original size modulo 2^32 32118 |
간단하게 echo “<encoded-value>” | base64 -d prometheus.yaml.gz 으로 보면 된다.
압축을 해제하고 파일을 수정한 후에 다시 압축을 한다. 그리고 이것을 base64 로 인코딩 스트링을 만들면 되는데 다음과 같이 만들 수 있다.
1 |
$ cat prometheus.yaml.gz | base64 -w 0 |
이렇게 하게 되면 Base64 인코딩 스트링 나오는데, Secret 에 데이터부분에 이 스트링을 넣고 편집하면 된다.