|
| 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 | +Create a secret using a Personal Access Token (PAT). Ensure the token has at least |
| 21 | +read-only access to private repositories. For Docker Hub replace `<registry server>` |
| 22 | +with `docker.io`. |
| 23 | + |
| 24 | +```console |
| 25 | +$ kubectl create -n <kubernetes namespace> secret docker-registry <secret name> --docker-server=<registry server> \ |
| 26 | + --docker-username=<registry user> --docker-password=<access token> \ |
| 27 | + --docker-email=<registry email> |
| 28 | +``` |
| 29 | + |
| 30 | +To tests the secrets use the following command: |
| 31 | + |
| 32 | +```console |
| 33 | +kubectl apply --wait -f - <<EOF |
| 34 | +apiVersion: v1 |
| 35 | +kind: Pod |
| 36 | +metadata: |
| 37 | + name: dhi-test |
| 38 | + namespace: <kubernetes namespace> |
| 39 | +spec: |
| 40 | + containers: |
| 41 | + - name: test |
| 42 | + image: <your-namespace>/dhi-bash:5 |
| 43 | + command: [ "sh", "-c", "echo 'Hello from DHI in Kubernetes!'" ] |
| 44 | + imagePullSecrets: |
| 45 | + - name: <secret name> |
| 46 | +EOF |
| 47 | +``` |
| 48 | + |
| 49 | +Get the status of the pod by running: |
| 50 | + |
| 51 | +```console |
| 52 | +$ kubectl get -n <kubernetes namespace> pods/dhi-test |
| 53 | +``` |
| 54 | + |
| 55 | +The command should return the following result: |
| 56 | + |
| 57 | +```console |
| 58 | +NAME READY STATUS RESTARTS AGE |
| 59 | +dhi-test 0/1 Completed ... ... |
| 60 | +``` |
| 61 | + |
| 62 | +If instead, the result is the following, there might be an issue with your secret. |
| 63 | + |
| 64 | +```console |
| 65 | +NAME READY STATUS RESTARTS AGE |
| 66 | +dhi-test 0/1 ErrImagePull 0 ... |
| 67 | +``` |
| 68 | + |
| 69 | +Verify the output of the pod by running, which should return `Hello from DHI in Kubernetes!` |
| 70 | + |
| 71 | +```console |
| 72 | +kubectl logs -n <kubernetes namespace> pods/dhi-test |
| 73 | +``` |
| 74 | + |
| 75 | +After a successful test, the test pod can be deleted with the following command: |
| 76 | + |
| 77 | +```console |
| 78 | +$ kubectl delete -n <kubernetes namespace> pods/dhi-test |
| 79 | +``` |
0 commit comments