Kubernetes 이야기

Pod에서 imagePullSecrets 설정하기 본문

Kubernetes/일반

Pod에서 imagePullSecrets 설정하기

kmaster 2022. 3. 24. 21:48
반응형

개요

Container 테스트 시 Container Image Registry를 dockerhub로 많이 이용하지만,  gchr.io 또한 많이 사용된다. 그런데 gchr.io registry는 이미지 push를 하게 되면 기본적으로 private repository로 설정된다.

 

Kubernetes에서 Pod 또는 Deployment 등에서 image를 배포할때 Private Repository는 imagePullSecrets을 설정해야 한다. 

 

절차는 아래와 같다.

 

1) Secret 생성

# kubectl create secret docker-registry regsecret -n test \
    --docker-server=ghcr.io \
    --docker-username=kmaster8 \
    --docker-password=<access-token> \
    --docker-email=<email>

 

2) deployment yaml 에 설정

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-demo
  namespace: test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports: 80
      imagePullSecrets:
      - name: regsecret

 

이렇게 설정하면 image를 잘 로딩할 것이다. 하지만 해당 namespace에 많은 workload들이 private repository를 호출한다면 매번 설정하기 귀찮을 것이다.

 

그래서 아래와 같이 해당 namespace에 service account에 설정하는 방법을 알아보자.

 

Add image pull secret to service account

아래와 같이 Deployment에서 사용하는 service account (기본값: default) 에 설정하자.

# kubectl patch serviceaccount default -p '{"imagePullSecrets": [{"name": "regsecret"}]}' -n test

 

위와 같이 설정할 수도 있지만 yaml로 직접 ServiceAccount를 편집할 수 있다. ( 맨 아래 imagePullSecrets 추가)

# kubectl get serviceaccounts default -n test -o yaml > ./sa.yaml

# vi sa.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: default
  namespace: test
secrets:
- name: default-token-uudge
imagePullSecrets:
- name: regsecret
# kubectl replace serviceaccount default -f ./sa.yaml -n test

 

이제 pod나 deployment 를 default ServiceAccount를 사용하여 생성 시 자동으로 spec.imagePullSecrets 가 추가된 것을 볼 수 있다.

 

만약 deployment같은 resource에서 이미 imagePullSecrets이 한개 이상 존재하면 추가되지는 않는다. 
반응형

'Kubernetes > 일반' 카테고리의 다른 글

Pod 스케줄링  (0) 2022.04.02
Kubernetes Pod Eviction  (0) 2022.04.02
kubernetes yaml 을 helm chart로 변환하기  (0) 2022.03.15
Nginx Ingress Path 기반 라우팅 설정  (0) 2022.03.15
로컬 Kubernetes 클러스터 - kind 설치  (0) 2022.03.08
Comments