Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create kubernetes docker-registry secret from yaml file?

I can run this command to create a docker registry secret for a kubernetes cluster:

kubectl create secret docker-registry regsecret \ --docker-server=docker.example.com \ --docker-username=kube \ --docker-password=PW_STRING \ [email protected] \ --namespace mynamespace  

I would like to create the same secret from a YAML file. Does anyone know how this can be set in a YAML file?

I need this as a YAML file so that it can be used as a Helm template, which allows for a Helm install command such as this (simplified) one:

helm install ... --set docker.user=peter,docker.pw=foobar,docker.email=... 
like image 338
Rotareti Avatar asked Apr 03 '18 11:04

Rotareti


People also ask

How do I create a secret in Kubernetes for Docker registry?

Create a Secret by providing credentials on the command line <your-registry-server> is your Private Docker Registry FQDN. Use https://index.docker.io/v1/ for DockerHub. <your-name> is your Docker username. <your-pword> is your Docker password.

How do you create a Kubernetes secret from a file?

To create a Kubernetes secret, apply one of the following methods: Use kubectl for a command-line based approach. Create a configuration file for the secret. Use a generator, such as Kustomize to generate the secret.

How to create a Kubernetes secret from existing Docker credentials?

Create a Secret based on existing Docker credentials. A Kubernetes cluster uses the Secret of kubernetes.io/dockerconfigjson type to authenticate with a container registry to pull a private image. If you already ran docker login, you can copy that credential into Kubernetes: kubectl create secret generic regcred --from-file=.

How do I create a docker registry secret?

When you do not have a Docker config file, or you want to use kubectl to create a Docker registry Secret, you can do: kubectl create secret docker-registry secret-tiger-docker --docker-username=tiger --docker-password=pass113 [email protected] This command creates a Secret of type kubernetes.io/dockerconfigjson.

How to post a YAML file to Kubernetes CLI?

When using the CLI format basically you're using a generator of the yaml before posting it to the server-side. Since Kubernetes is client-server app with REST API in between, and the actions need to be atomic, the posted YAML needs to contain the content of the file, and best way to do that is by embedding it as a base64 format in-line.

How are secrets read in Kubernetes containers?

The program in a container is responsible for reading the secrets from the files. When a secret currently consumed in a volume is updated, projected keys are eventually updated as well. The kubelet checks whether the mounted secret is fresh on every periodic sync.


2 Answers

You can write that yaml by yourself, but it will be faster to create it in 2 steps using kubectl:

  1. Generate a 'yaml' file. You can use the same command but in dry-run mode and output mode yaml.

Here is an example of a command that will save a secret into a 'docker-secret.yaml' file for kubectl version < 1.18 (check the version by kubectl version --short|grep Client):

kubectl create secret docker-registry --dry-run=true $secret_name \   --docker-server=<DOCKER_REGISTRY_SERVER> \   --docker-username=<DOCKER_USER> \   --docker-password=<DOCKER_PASSWORD> \   --docker-email=<DOCKER_EMAIL> -o yaml > docker-secret.yaml 

For kubectl version >= 1.18:

kubectl create secret docker-registry --dry-run=client $secret_name \   --docker-server=<DOCKER_REGISTRY_SERVER> \   --docker-username=<DOCKER_USER> \   --docker-password=<DOCKER_PASSWORD> \   --docker-email=<DOCKER_EMAIL> -o yaml > docker-secret.yaml 
  1. You can apply the file like any other Kubernetes 'yaml':

    kubectl apply -f docker-secret.yaml

UPD, as a question has been updated.

If you are using Helm, here is an official documentation about how to create an ImagePullSecret.

From a doc:

  1. First, assume that the credentials are defined in the values.yaml file like so:
imageCredentials:   registry: quay.io   username: someone   password: sillyness 
  1. We then define our helper template as follows:
{{- define "imagePullSecret" }} {{- printf "{\"auths\": {\"%s\": {\"auth\": \"%s\"}}}" .Values.imageCredentials.registry (printf "%s:%s" .Values.imageCredentials.username .Values.imageCredentials.password | b64enc) | b64enc }} {{- end }} 
  1. Finally, we use the helper template in a larger template to create the Secret manifest:
apiVersion: v1 kind: Secret metadata:   name: myregistrykey type: kubernetes.io/dockerconfigjson data:   .dockerconfigjson: {{ template "imagePullSecret" . }} 
like image 55
Anton Kostenko Avatar answered Oct 05 '22 03:10

Anton Kostenko


You can kubectl apply the output of an imperative command in one line:

kubectl create secret docker-registry --dry-run=true $secret_name \   --docker-server=<DOCKER_REGISTRY_SERVER> \   --docker-username=<DOCKER_USER> \   --docker-password=<DOCKER_PASSWORD> \   --docker-email=<DOCKER_EMAIL> -o yaml | kubectl apply -f - 
like image 30
Sébastien Dan Avatar answered Oct 05 '22 02:10

Sébastien Dan