May 16, 2025

[Home-K8S] #4 code-server 구성

[Home-K8S] #4  code-server 구성

code-server - web vscode

언제 어디서나 어떤 디바이스로도 개발을 진행하고 싶을 때, 웹으로 개발환경을 구성하고 싶었습니다. 그리고 code-server가 있다는 것을 알았죠.

code-server는 웹에서 브라우저를 이용해서 사용할 수 있는 VSCode 입니다. VSCode의 거의 모든 기능을 지원하지만, 확장 프로그램에서 가끔 지원하지 않는 게 있습니다.
(물론 extension을 파일로 다운받아서 code-server에서 적용하면 되긴 합니다.)

code-server를 이용하는 이유 중에 하나는 터미널이 있는 것입니다. k8s도 관리를 해야 하기 때문에 기본적으로 터미널을 편하게 쓸 수 있게 하는 게 좋아요.

Kubeconfig to Secret

code-server에서 사용할 kubeconfig를 지금 k8s를 사용하는 kubeconfig에서 가져옵니다. 그래야 kubectl을 사용할 수 있겠죠?

kubectl create secret generic kubeconfig \
  --from-file=/home/user/.kube/config \
  -n code-server

kubeconfig to k8s Secret

Code-server k8s 구축 (with kubectl,helm)

Code-server Docker Image: https://hub.docker.com/r/codercom/code-server

아무래도 code-server는 설정할 config가 별로 없어서 작성하기는 편합니다.
그냥 code-server 이미지를 이용해서 구축을 하면 되지만, 우리는 kubectl을 사용해야 하니까, kubectl과 helm을 적용하고 kubeconfig까지 적용하는 것으로 진행합니다.

initcontainer로 kubectl, helm을 가지고 와서 code-server의 경로에 저장해 줍니다.
이런 식이면 다른 패키지도 쉽게 적용할 수 있게죠?

apiVersion: v1
kind: ConfigMap
metadata:
  name: code-server-config
  namespace: code-server
data:
  config.yaml: |
    bind-addr: 127.0.0.1:8080
    auth: none
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: code-server
  name: code-server
  namespace: code-server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: code-server
  template:
    metadata:
      labels:
        app: code-server
    spec:
      initContainers:                    # kubectl,helm 설치
        - name: kubectl-copy
          image: bitnami/kubectl
          command: ["cp","/opt/bitnami/kubectl/bin/kubectl","/mnt/bin/kubectl"]
          volumeMounts:
            - name: bin-volume
              mountPath: /mnt/bin
        - name: helm-copy
          image: alpine/helm
          command: ["cp","/usr/bin/helm","/mnt/bin/helm"]
          volumeMounts:
            - name: bin-volume
              mountPath: /mnt/bin
      containers:
        - image: codercom/code-server:latest
          name: code-server
          ports:
            - containerPort: 8080
          env:
            - name: KUBECONFIG
              value: /home/coder/.kube/config
          volumeMounts:
            - name: bin-volume
              mountPath: /usr/local/bin/kubectl
              subPath: kubectl
            - name: bin-volume
              mountPath: /usr/local/bin/helm
              subPath: helm
            - name: kubeconfig
              mountPath: /home/coder/.kube
              readOnly: true
            - name: code-config
              mountPath: /home/coder/.config/code-server
      volumes:
        - name: bin-volume
          emptyDir: {}
        - name: kubeconfig
          secret:
            secretname: kubeconfig
        - name: code-config
          configMap:
            name: code-server-config

Authelia 로그인 적용

kubeconfig와 kubectl까지 구성했으니까 이제 외부인이 여기 접속하게 되면 모든 것이 털리게 됩니다. 그래서 기본적인 인증 서비스를 달아줍니다. 물론 기본적으로 제공되는 것이 있습니다.

  1. Code-server의 기본 로그인 서비스
    1. ConfigMap에서 auth: none 으로 되어있는 것을 auth:password 로 하고 password: (비밀번호) 로 설정하시면 됩니다.
apiVersion: v1
kind: ConfigMap
metadata:
  name: code-server-config
  namespace: code-server
data:
  config.yaml: |
    bind-addr: 127.0.0.1:8080
    auth: password
    password: password1234
  1. nginx base-auth를 통한 기본 인증
    1. 전에 포스팅한 nginx 인증을 nginx-ingress에서도 적용할 수 있습니다. base-auth로 적용하면 되는데, 아래 포스팅을 보고 따라한 로그인 정보를 secret으로 만들어 줍니다.
    2. 그러고 ingress에 base-auth를 적용해 줍니다.
Nginx https 인증 (feat. Code-server)
CertBot을 이용해서 인증서를 발급받는다. 80번 포트가 비어있어야 하므로 nginx 서비스를 끈다. sudo snap install certbot --classic sudo systemctl stop nginx.service sudo certbot certonly --standalone # Email / Y / N / domain 하면 다음과 같은 로그가 나온다. Successfully received certificate. Certificate is saved at: /etc/letsencrypt/live/<domain>/fullchain.pem Key is saved at:</domain>
kubectl create secret generic basic-auth \
  --from-file=./auth \
  -n code-server
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  namespace: default
  annotations:
    nginx.ingress.kubernetes.io/auth-type: basic
    nginx.ingress.kubernetes.io/auth-secret: basic-auth
  1. Authelia 연동
    1. 하지만 저는 Authelia를 구성해 놨으니까, 사용해 줍니다.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: code-server
  namespace: code-server
  annotations:
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    nginx.ingress.kubernetes.io/auth-url: "http://authelia.authelia.svc.cluster.local:9091/api/verify"
    nginx.ingress.kubernetes.io/auth-response-headers: "Remote-User, Remote-Name, Remote-Email, Remote-Groups"
    nginx.ingress.kubernetes.io/auth-signin: "https://authelia.dogring.kr/"
    kubernetes.io/tls-acme: "true"
    cert-manager.io/cluster-issuer: letsencrypt-prod
spec:

마무리

원래는 무료 도메인을 사용하고 각 서비스 별로 path만 다르게 해서 사용하려고 했습니다. 근데 최근 code-server가 base-path config를 제공하지 않아요. 그래서 어쩔 수 없이 도메인을 구매할 수 밖에 없었어요.

base-path를 제공하지 않으니까 path로 처음에 code-server에 연결을 해도 /path를 빼고 리디렉션을 해서 정상적으로 작동하지 않습니다.

확실히 code-server를 이용해서 브라우저만 있으면 어디서든 제 환경에 접속이 가능하니까 편하더라구요.
태블릿과 키보드만 들고 다니면서 작업하기도 하고, pod의 log를 볼 때도 터미널 틀어놓고 과정을 보기도 하고, 잘 쓰고 있어요.

다음에는 code-server에서 python을 위한 Jupyter 서버 구성 방법을 작성하겠습니다.

Comments