ããã«ã¡ã¯ãã¨ã ã¹ãªã¼ã¨ã³ã¸ãã¢ã°ã«ã¼ãAIã»æ©æ¢°å¦ç¿ãã¼ã ã§2021å¹´æ°åã®åå·(@kitagry)ã§ããæè¿è²·ã£ã¦ããã£ããã®ã¯ä½æ¸©èª¿çå¨ã§ãããªã¢ã¼ãã¯ã¼ã¯ã¨ã®ç¸æ§ãæ群ã§ãã
ä»åã¯åãå ¥ç¤¾1ã¶æã»ã©ã§ä½æãããGKEã®Secret管çåºç¤ã«ã¤ãã¦æ¸ãããã¨æãã¾ãã
ããã¯GKEã®Secretãå®å ¨ã«ãã¤æ軽ã«ä½æã§ããããã«ãããã¼ã«ã«ãªãã¾ãã OSSã«ãªã£ã¦ããã®ã§çããã®ã³ã³ããªãã¥ã¼ã·ã§ã³ããå¾ ã¡ãã¦ãã¾ãã
ãã®è¨äºã¯ï¼æå§ãã«æ¸ããã¨ãã¾ããããæ¸ãå 容ãã©ãããããèãã¦ããå ã«ï¼æãéãã¦ãã¾ã£ã¦ãã¾ãããç¬
- AIã»æ©æ¢°å¦ç¿ãã¼ã ã®ãµã¼ãã¹ã®ç¹å¾´
- Secretã®ç®¡çæ¹æ³ã®æ¤è¨
- berglasã«ã¤ãã¦
- berglas-secret-controller
- éç¨ããã¼
- æå¾ã«
AIã»æ©æ¢°å¦ç¿ãã¼ã ã®ãµã¼ãã¹ã®ç¹å¾´
AIã»æ©æ¢°å¦ç¿ãã¼ã ã¯æ¤ç´¢åºç¤ã»è¨äºã¬ã³ã¡ã³ãã»ã¡ã«ãã¬ã®ã¬ã³ã¡ã³ããªã©æ°å¤ãã®ãããã¯ããããã¾ãã ããããä»ã¾ã§ç§å¯æ å ±ã®ç®¡çæ¹æ³ã¯æ±ºã¾ã£ã¦ãããCDã®ãããã¤æã«ç§å¯æ å ±ãåãè¾¼ãã ããKubernetesã®Secretãªã½ã¼ã¹ãæåã§ä½æãããªã©ããã¦ãã¾ããã ãããã¯ä»ã¾ã§æ§ã ãªåé¡ãããã¾ããã ä¾ãã°CDã§ãããã¤ããéã«ç§å¯æ å ±ãåãè¾¼ãéç¨ã§ã¯ãéçºç¨ã®GKEä¸ã§ããèµ·ãããªãåé¡ããããã°ããæã§ããããã¡ãã¡ã³ããããè¡ããCIã§ãã¹ããéãã¾ã§å¾ ã¡CDã§ãããã¤ããã¦ãã¾ããã ãããã®ãµã¤ã¯ã«ã¯ã¨ã¦ãæéãããããéçºã®ã³ã¹ããä¸ãã¦ãã¾ãã¾ãã ã¾ãããããã®ç®¡çæ¹æ³ã決ã¾ã£ã¦ããªããã¨ã§ãç§å¯æ å ±ãå©ç¨ãããå ´åã«ã©ãã«ãããã®è¦éããæªããªããªã©ã®åé¡ãããã¾ããã
ããã§ä»¥ä¸ã®ï¼ã¤ã®è¦³ç¹ããç§å¯æ å ±ã管çããä»çµã¿ãä½æãã¾ããã
- ç§å¯æ å ±ã¸ã®ã¢ã¯ã»ã¹ã容æã«ãã
- å®å ¨ã«ä¿åãã
Secretã®ç®¡çæ¹æ³ã®æ¤è¨
Kubernetesã§ç§å¯æ å ±ã管çã«ã¯sopsãkubesecãªã©æ§ã ãªãã¼ã«ãããã¾ãã ãããã¯yamlãã¡ã¤ã«ã«æ¸ãããç§å¯æ å ±ãæå·åãã¦ããã®ãã¡ã¤ã«ãã¨ã³ããããã¦ä½¿ãããã«ãã¦ãã¾ãã ããã«ãã£ã¦ããã¡ã¤ã«ãGitã§ç®¡çã§ãããããããã¤ãå®å ¨ãã¤å®¹æã«è¡ããã¨ãåºæ¥ã¾ãã
ãããããã®æ¹æ³ã§ã¯äººãæå·æ å ±ãç¨ãããå ´åã«ã¯ãã®ãªãã¸ããªã¸ç§»åãã¦ãã¡ã¤ã«ã復å·åããªããã°ãªãã¾ããã ã¾ãããµã¼ãã¹ãå¢ãããã³ã«æåã§æå·åããã¦ãã¡ã¤ã«ãã³ãããããå¿ è¦ããããã¨ã¦ãæéããããã¾ãã ããã¯ãããã¯ãã®æ°ãå¤ãAIã»æ©æ¢°å¦ç¿ãã¼ã ã§ã¯æã¾ããã¨ã¯è¨ãã¾ããã§ããã
ã¾ããAIã»æ©æ¢°å¦ç¿ãã¼ã ã§ã¯GKEã®ãµã¼ãã¹ãå¤ãããä¸æ¹ã§Cloud Runãªã©ã®GCPãããã¯ãã使ã£ã¦ãã¾ãã ãããã«ã¯sopsã®ãããªyamlãã¡ã¤ã«ã«ç§å¯æ å ±ãä¿åããæ¹æ³ã使ã£ã¦ãããã¤ããæ¹æ³ã¯æ³å®ããã¦ããªãã¨æãã¾ãã ãã®ãããã¢ã¯ã»ã¹ã容æã«ããã¨ããç¹ã«ããã¦sopsã¯æ±ãã¦ããè¦ç´ ãå°ãæºãããªãã®ã§ã¯ãªããã¨æãã¾ããã
ãããã®åé¡ã解決ããããã«ãç§å¯æ å ±ããã¡ã¤ã«ã¨ãã¦ãã¼ã«ã«ã«ä¿åããã®ã§ã¯ãªããéµãç§å¯æ å ±ãGCPä¸ã§ä¿åããæ¹æ³ã¯ãªããã¨æ¢ãã¾ããã ããã§è¦ã¤ããã®ããGCPä¸ã§ã®ç§å¯æ å ±ã®åå¾ãä¿åã®CLIãã©ã¤ãã©ãªãæä¾ãã¦ããberglasã¨ãããã¼ã«ã§ããã
berglasã«ã¤ãã¦
berglasã¯GCPä¸ã«ç§å¯æ å ±ãä¿åããã使ç¨ãããããããã®æ©è½ãæä¾ããã¦ãã¾ãã æå·ã»å¾©å·éµã¯Cloud KMSãç¨ãã¦ãããç§å¯æ å ±ã®ä¿åã¯Google Cloud Storage(GCS)ã¾ãã¯Secret Manager storageã使ç¨ãã¦ãã¾ãã
éµã®ä½æã«ã¯ä»¥ä¸ã®ã³ãã³ãã§ä½æã§ãã¾ãã
// Secret Manager storage berglas create sm://${PROJECT_ID}/foo my-secret-data // gcs berglas create ${BUCKET_NAME}/foo my-secret-data
ã¾ãã以ä¸ã®ã³ãã³ãã§ã¢ã¯ã»ã¹ã§ãã¾ãã
// Secret Manager storage berglas access sm://${PROJECT_ID}/foo // gcs berglas access ${BUCKET_NAME}/foo
ããã«ãã£ã¦ãç§å¯æ å ±ãå®å ¨ã«ä¿åãã¤ã¤ãã人ãç§å¯æ å ±ã«ã¢ã¯ã»ã¹ããã®ãã¨ã¦ã容æã«ãªãã¾ããã ã¾ããberglasã«ã¯terraformã®providerããããããã使ãã°terraformã§ä½ææã«ç§å¯æ å ±ã人ã®æãä»ãããã¨ãªãberglasã§ä¿åã§ãã¾ãã ããã«ãã£ã¦ãã¥ã¼ãã³ã¨ã©ã¼ã®ç¢ºçãå°ãã§ãä¸ãããã¨ãã§ãã¾ãã
ã¾ããberglasã®ãªãã¸ããªã«ã¯GAEãGKEãªã©ã§åãããµã³ãã«ããããGCPä¸ã®æ§ã ãªããã¸ã§ã¯ãã§berglasã使ããã¨ãã§ãã¾ãã ãã®ããã°ã§ã¯GKEã§berglasãåããæ¹æ³ã«ã¤ãã¦è©³ããæ¸ãã¦ããã¾ãã
berglas on GKE
berglasã«ã¯k8sã§ä½¿ãããã®ãµã³ãã«ãããã¾ãã æåã¯ãã®æé ã使ã£ã¦berglas on k8sãå®è¡ãããã¨ãã¾ããã ããã¯Mutating Webhookã使ç¨ãããã®ã§ã以ä¸ã®ãããªPodãä½æãããå ´åã«berglasã³ãã³ãã使ããããªinit containerãå·®ãè¾¼ãã¨ããç©ã§ãã
apiVersion: v1 kind: Pod metadata: name: myapp-pod labels: app: myapp spec: containers: - name: myapp-container image: busybox command: ['sh', '-c', 'echo $SECRET'] env: - name: SECRET value: berglas://path_to/secret
å·®ãè¾¼ã¾ãããã¨ã¯ãã®ããã«ãªãã¾ãã
apiVersion: v1 kind: Pod metadata: name: myapp-pod labels: app: myapp spec: initContainers: - name: copy-berglas-bin image: us-docker.pkg.dev/berglas/berglas/berglas:latest command: ['sh', '-c', 'cp /bin/berglas /berglas/bin/'] volumeMounts: - name: berglas-bin readOnly: false mountPath: /berglas/bin containers: - name: myapp-container image: busybox command: ['/berglas/bin/berglas'] args: ['exec', '--', 'sh', '-c', 'echo $SECRET'] env: - name: SECRET value: berglas://path_to/secret volumeMounts: - name: berglas-bin readOnly: true mountPath: /berglas/bin volumes: - name: berglas-bin emptyDir: {}
å°ã説æããã¨ãinitContainerã§å ±éã®volumeã«å¯¾ãã¦berglasã®CLIãä¿åãã¾ãã ããã¦ãberglasã®ç°å¢å¤æ°ãæã¤å ¨containerã®commandãvolumeã«ä¿åããberglasã³ãã³ããéãã¦è¡ããã¨ã«ãã£ã¦ãç°å¢å¤æ°ã«berglasã®URIãæã¤ãã®ãããã°æ¸ãæããã¨ããå¦çãè¡ãã¾ãã
ãã®ããã«è¡ããã¨ã«ãã£ã¦ãåPodã¸berglasã¸ã®ã¢ã¯ã»ã¹æ¨©éããä»ä¸ããã°ç°¡åã«ç§å¯æ å ±ã使ããã¨ãã§ãã¾ãã
ãããããã®Mutating Webhookãç¨ããæ¹æ³ã«ã¯ä»¥ä¸ã®ãããªåé¡ç¹ãããã¾ãã
1. Webhookã®å¯ç¨æ§
Webhookãä½ãã®åé¡ã§åããªãå ´åã«ã¯ç°å¢å¤æ°ãèªã¿è¾¼ããã«ãPodãèµ·åã§ããªãã¨ãããã¨ãèããããã¾ãã ã¾ããèµ·åã§ããªãã®ã¯ã¾ã è¯ãã¦ãç°å¢å¤æ°ãæ¸ãæãããªãã¾ã¾åãç¶ããå ´åãèãããã¾ãã ä¸ã®åé¡ã解決ããããã«Webhookã®failure Policyãå³ããè¨å®ããå ´åãberglasã«é¢ä¿ãªãPodã¾ã§èµ·åã妨ããããã¨ãããã¨ãèãããã¾ãã ãã®ãããWebhookã®å¯ç¨æ§ãã»ãã¥ãªãã£ãªã©ã®æ ä¿ãããªãè¦æ±ããã¾ãã
2. IAMããªã·ã¼ã®è¤éã
ãã®æ¹æ³ã使ãããã«ã¯åPodã®IAMããªã·ã¼ã§berglasãåå¾ããããã®æ¨©éãä»ä¸ããªããã°ãªãã¾ããã 権éã®ä»ä¸ã¯ä»¥ä¸ã®ããã«è¡ãã¾ã
berglas grant ${BUCKET_ID}/foo --member user:[email protected]
ãããããããã¯å¼ãã¼ã ã®ãããªãµã¼ãã¹ãæ°å¤ããããã¼ã ã«ã¯ç ©éãªä½æ¥ã«ãªãã¾ãã
berglas-secret-controller
ããã§ãä¸ã®Mutating Webhookã®åé¡ã解決ããããã®K8sã®ã«ã¹ã¿ã ã³ã³ããã¼ã©ã¼ãä½æãã¾ããã
ããã¯ä»¥ä¸ã®ãããªã«ã¹ã¿ã ãªã½ã¼ã¹ãç¨ãã¾ãã
apiVersion: batch.kitagry.github.io/v1alpha1 kind: BerglasSecret metadata: name: berglassecret-sample namespace: default spec: data: username: berglas://bucket/username password: berglas://bucket/password
ãã®ã«ã¹ã¿ã ãªã½ã¼ã¹ãä½æããã¨ãcontrollerãåãååã®ä»¥ä¸ã®ãããªSecretãä½æãã¾ã.
apiVersion: v1 kind: Secret metadata: name: berglassecret-sample namespace: default spec: data: username: <secret> password: <secret>
Podãããã®ç§å¯æ å ±ãç¨ããå ´åã¯ãenvFromã§ãã®Secretãæå®ããã°è¯ãã§ãã ããã«ãã£ã¦å ã»ã©ã®Mutating Webhookã®å¯ç¨æ§ã¨IAMããªã·ã¼ã®ç®¡çã«ã¤ãã¦ã®åé¡ã解決ããã¾ãã. ããããä¸ã¤ãã¤è¦ã¦ããããã¨æãã¾ãã
1. berglas-secret-controllerã®å¯ç¨æ§
berglas-secret-controllerãè½ã¡ã¦ããå ´åã«ã¯Secretãä½æãããªãã®ã§ãPodã¯èµ·åã§ãã¾ããã ãã®ãããPodã誤ã£ãç§å¯æ å ±ãç¨ãã¦åããã¨ãé²ããã¨ãåºæ¥ã¾ãã ã¾ããberglasã®ç§å¯æ å ±ã使ããªãå ´åã¯ãã®berglas-secret-controllerãåããªãã¦ãå ¨ãåé¡ãªãåä½ãã¾ãã
2. IAMããªã·ã¼ã®ç®¡ç
IAMããªã·ã¼ã«ã¤ãã¦ã¯berglas-secret-controllerã«ç§å¯æ å ±ã®ã¢ã¯ã»ã¹æ¨©éãããã°åPodã«æ¨©éãä¸ããªãã¦ããããªãã¾ãã ããã§ç ©éãªããªã·ã¼ã®è¨å®ããéæ¾ããã¾ãã
ãã ããããã¯ç§å¯æ å ±ãberglas-secret-controllerã«éç´ããããã¨ã«ãªãã®ã§k8sã¸ã®ã¢ã¯ã»ã¹æ¨©éãªã©ã¯æ°ã«ããã¹ãã ã¨æãã¾ãã å¼ãã¼ã ã§ãberglas-secret-controllerãå°å ¥ããéã«ã¯ãã®ãã¤ã³ãã«ã¤ãã¦è°è«ãã¾ããã
éç¨ããã¼
ããã§ã¯å®éã®éç¨ããã¼ãç°¡åã«èª¬æãã¦ããããã¨æãã¾ãã
å¼ãã¼ã ã§ã¯terraformã使ã£ã¦ã¤ã³ãã©åºç¤ã®ç®¡çããã¦ãã¾ãã ããã¦ãterraformã§DBã®ãã¹ã¯ã¼ããªã©ãä½æããããã¦ãã¾ãã 以ä¸ã®ããã«æ¸ããã¨ã«ãã£ã¦ããã¹ã¯ã¼ããã©ã³ãã ã«çæããçæãããã¹ã¯ã¼ããberglasã«ãã£ã¦æå·åãã¦GCSã«ä¿åã§ãã¾ãã
resource "random_string" "database_password" { length = 10 special = true } resource "berglas_secret" "db_pass" { bucket = var.bucket_name key = "projects/${var.project}/locations/global/keyRings/berglas/cryptoKeys/berglas-key" name = "DATABASE_PASSWORD" plaintext = random_password.database_password.result }
å®éã«ã¢ããªã±ã¼ã·ã§ã³ãããã¹ã¯ã¼ãã使ç¨ããå ´åã¯berglas-secret-controllerã使ã£ã¦ä»¥ä¸ã®ããã«æå®ãã¾ãã
apiVersion: batch.kitagry.github.io/v1alpha1 kind: BerglasSecret metadata: name: berglassecret-sample spec: data: password: berglas://BUCKET_NAME/DATABASE_PASSWORD --- apiVersion: apps/v1 kind: Deployment metadata: name: sample spec: template: spec: containers: - name: manager envFrom: - secretRef: name: berglassecret-sample
ããã§ç§å¯æ å ±ãã¢ããªã±ã¼ã·ã§ã³ã§ä½¿ç¨ã§ãã¾ããã è¦ã¦ããããã°ãããããã«ãDeploymentã§ãã¹ã¯ã¼ãã使ãããã«äººããã¹ã¯ã¼ããè¦ãå¿ è¦ãããã¾ããã 人ã触ããªããã¨ã«ãã£ã¦ãã¥ã¼ãã³ã¨ã©ã¼ãçºçããã®ãå°ãã§ãé²ããã¨ãã§ããã¨æãã¾ãã
æå¾ã«
å¼ãã¼ã ã§ã¯berglasãå°å ¥ãããã¨ã«ãã£ã¦ä»¥ä¸ã®ãããªå©ç¹ãå¾ããã¨ãã§ãã¾ãã.
- ç§å¯æ
å ±ç®¡çã®ä¸å
å
- ç§å¯æ å ±ãã©ãã«ããã®ãããããããã«ãªã
- ãã¼ã¿ããªãã£ã®åä¸
- sopsã®ããã«ãã¡ã¤ã«ã«ä¿åããããGitlabã«ä¿åããã®ã§ã¯ãªãã¯ã©ã¦ãä¸ã«ä¿åããã
- CLIãk8sããã¢ã¯ã»ã¹ãå¯è½ã«ãªã
- ãã¥ã¼ãã³ã¨ã©ã¼ã®åæ¸
- terraformã§ä½æããç§å¯æ å ±ã®å ´åãå®éã®ç§å¯æ å ±ã人ãè¦ãå¿ è¦ã¯ããã¾ãã
ã¾ããberglas-secret-controllerãä½æãããã¨ã«ãã£ã¦ãk8sä¸ã§ã®ç§å¯æ å ±ç®¡çã容æã«ãããã¨ãåºæ¥ã¾ããã ãã¡ãã¯ã¾ã ã¾ã çºå±éä¸ãªã®ã§ã³ã³ããªãã¥ã¼ã·ã§ã³ãå¾ ã¡ãã¦ã¾ãï¼
åã¯Kuberneteså¨ãã®ãã¼ã«ä½æã«èå³ãããã®ã§ãä»å¾ããã®ãããªä¾¿å©ãªãã¼ã«ãä½ããã°ãããªã¨æã£ã¦ãã¾ãã
We're hiring !!!
ã¨ã ã¹ãªã¼ã§ã¯æ©æ¢°å¦ç¿ãç¨ããæ°å¤ãã®ãããã¯ããçã¿åºããã¦ãã¾ãã ãã®ãããã¯ããæ¯ããã¤ã³ãã©åºç¤ãä¸ç·ã«ä½ã£ã¦ããã仲éãåéãã¦ãã¾ãï¼
ãã¡ãã£ã¨è©±ãèãã¦ã¿ãããããã¨ãã人ã¯ãã¡ãããï¼