|
| 1 | +--- |
| 2 | +title: Use a Docker Hardened Image in Kubernetes |
| 3 | +linktitle: Use an image in Kubernetes |
| 4 | +description: Learn how to use Docker Hardened Images in Kubernetes deployments. |
| 5 | +keywords: use hardened image, kubernetes, k8s |
| 6 | +weight: 35 |
| 7 | +--- |
| 8 | + |
| 9 | +{{< summary-bar feature_name="Docker Hardened Images" >}} |
| 10 | + |
| 11 | +## Authentication |
| 12 | + |
| 13 | +To be able to use Docker Hardened Images in Kubernetes, you need to create a |
| 14 | +Kubernetes secret for pulling images from your mirror or internal registry. |
| 15 | + |
| 16 | +> [!NOTE] |
| 17 | +> |
| 18 | +> You need to create this secret in each Kubernetes namespace that uses a DHI. |
| 19 | +
|
| 20 | +To use the credentials from Docker Desktop, run: |
| 21 | + |
| 22 | +```console |
| 23 | +$ kubectl create -n <kubernetes namespace> secret generic <secret name> \ |
| 24 | + --from-file=.dockerconfigjson=$HOME/.docker/config.json \ |
| 25 | + --type=kubernetes.io/dockerconfigjson |
| 26 | +``` |
| 27 | + |
| 28 | +Alternatively, you can create the secret manually using a Personal Access Token (PAT). |
| 29 | +Ensure the token has at least read-only access to private repositories. For Docker Hub |
| 30 | +replace `<registry server>` with `docker.io`. |
| 31 | + |
| 32 | +```console |
| 33 | +$ kubectl create -n <kubernetes namespace> secret docker-registry <secret name> --docker-server=<registry server> \ |
| 34 | + --docker-username=<registry user> --docker-password=<access token> \ |
| 35 | + --docker-email=<registry email> |
| 36 | +``` |
| 37 | + |
| 38 | +To tests the secrets use the following command: |
| 39 | + |
| 40 | +```console |
| 41 | +kubectl apply --wait -f - <<EOF |
| 42 | +apiVersion: v1 |
| 43 | +kind: Pod |
| 44 | +metadata: |
| 45 | + name: dhi-test |
| 46 | + namespace: <kubernetes namespace> |
| 47 | +spec: |
| 48 | + containers: |
| 49 | + - name: test |
| 50 | + image: <your-namespace>/dhi-bash:5 |
| 51 | + command: [ "sh", "-c", "echo 'Hello from DHI in Kubernetes!'" ] |
| 52 | + imagePullSecrets: |
| 53 | + - name: <secret name> |
| 54 | +EOF |
| 55 | +``` |
| 56 | + |
| 57 | +Get the status of the pod by running: |
| 58 | + |
| 59 | +```console |
| 60 | +$ kubectl get -n <kubernetes namespace> pods/dhi-test |
| 61 | +``` |
| 62 | + |
| 63 | +You should be getting the following result: |
| 64 | + |
| 65 | +```console |
| 66 | +NAME READY STATUS RESTARTS AGE |
| 67 | +dhi-test 0/1 Completed ... ... |
| 68 | +``` |
| 69 | + |
| 70 | +If instead, the result is the following, there might be an issue with your secret. |
| 71 | + |
| 72 | +```console |
| 73 | +NAME READY STATUS RESTARTS AGE |
| 74 | +dhi-test 0/1 ErrImagePull 0 ... |
| 75 | +``` |
| 76 | + |
| 77 | +After a successful test, the test pod can be deleted with the following command: |
| 78 | + |
| 79 | +```console |
| 80 | +$ kubectl delete -n <kubernetes namespace> pods/dhi-test |
| 81 | +``` |
0 commit comments