|
| 1 | +# Kubernetes HTTP Fileserver |
| 2 | + |
| 3 | +## Serving static files from a Docker Container |
| 4 | + |
| 5 | +1. Create a Dockerfile to work with the [official NGINX Docker Container](https://hub.docker.com/_/nginx) that will copy all the content from the __download folder__ to the __public directory__ of the NGINX server: |
| 6 | + |
| 7 | + |
| 8 | +```yaml |
| 9 | +FROM nginx |
| 10 | +COPY downloads/ /usr/share/nginx/html |
| 11 | +``` |
| 12 | + |
| 13 | + |
| 14 | +2. Build the image and tag it for your Docker repository: |
| 15 | + |
| 16 | + |
| 17 | +```bash |
| 18 | +docker build -t my-docker-hub-account/http-fileserver-kubernetes . |
| 19 | +``` |
| 20 | + |
| 21 | + |
| 22 | +3. (Optional) Test run your container: |
| 23 | + |
| 24 | + |
| 25 | +```bash |
| 26 | +docker run -d -p 8080:80 my-docker-hub-account/http-fileserver-kubernetes |
| 27 | +``` |
| 28 | + |
| 29 | +You should now be able to access the files you stored inside the __downloads folder__ via `http://localhost:8080/dl/test.txt` (assuming that you used the __downloads__ folder from the repository, that contains a sub directory __dl__ that contains a text file __test.txt__): |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +4. Push the Docker Image to Docker hub |
| 37 | + |
| 38 | + |
| 39 | +```bash |
| 40 | +docker login |
| 41 | +docker push my-docker-hub-account/http-fileserver-kubernetes |
| 42 | +``` |
| 43 | + |
| 44 | + |
| 45 | +Alternatively, do your build on the host system where you want to run the image - so the image is in local storage. This is a __hacky solution__ that is not fully supported - if you don't want to push your image to the Docker Hub, you are supposed to use a local/personal registry for your Docker images. That feels a little bit much for my problem here - so I will proceed with this way. |
| 46 | + |
| 47 | +__Note__: if you want to use the local image, you have to build the image on every Node server that you are using in Kubernetes and that might be used to spawn the pod. You also have to set the `imagePullPolicy= Never` (see YAML file in the next step)! This will make sure that Kubernetes will always use the image from local storage instead of trying to pull it from Docker Hub. |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +5. Creating the Kubernetes Deployment |
| 52 | + |
| 53 | + |
| 54 | +```yaml |
| 55 | +apiVersion: extensions/v1beta1 |
| 56 | +kind: Deployment |
| 57 | +metadata: |
| 58 | + creationTimestamp: null |
| 59 | + labels: |
| 60 | + service: http-fileserver |
| 61 | + name: http-fileserver |
| 62 | +spec: |
| 63 | + replicas: 1 |
| 64 | + strategy: {} |
| 65 | + template: |
| 66 | + metadata: |
| 67 | + creationTimestamp: null |
| 68 | + labels: |
| 69 | + service: http-fileserver |
| 70 | + spec: |
| 71 | + containers: |
| 72 | + - image: my-docker-hub-account/http-fileserver-kubernetes:latest |
| 73 | + imagePullPolicy: Always |
| 74 | + name: http-fileserver |
| 75 | + resources: {} |
| 76 | + restartPolicy: Always |
| 77 | +status: {} |
| 78 | + |
| 79 | +--- |
| 80 | + |
| 81 | +apiVersion: v1 |
| 82 | +kind: Service |
| 83 | +metadata: |
| 84 | + creationTimestamp: null |
| 85 | + labels: |
| 86 | + service: http-fileserver |
| 87 | + name: http-fileserver |
| 88 | +spec: |
| 89 | + ports: |
| 90 | + - name: http |
| 91 | + port: 80 |
| 92 | + selector: |
| 93 | + service: http-fileserver |
| 94 | +status: |
| 95 | + loadBalancer: {} |
| 96 | +``` |
| 97 | +
|
| 98 | +
|
| 99 | +Alternatively use a simple pod, instead of a deployment: |
| 100 | +
|
| 101 | +
|
| 102 | +
|
| 103 | +```yaml |
| 104 | +kind: Pod |
| 105 | +apiVersion: v1 |
| 106 | +metadata: |
| 107 | + name: http-fileserver |
| 108 | + labels: |
| 109 | + app: fileserver |
| 110 | +spec: |
| 111 | + containers: |
| 112 | + - name: http-fileserver |
| 113 | + image: my-docker-hub-account/http-fileserver-kubernetes:latest |
| 114 | + imagePullPolicy: Always |
| 115 | + ports: |
| 116 | + - containerPort: 80 |
| 117 | + |
| 118 | +--- |
| 119 | + |
| 120 | +kind: Service |
| 121 | +apiVersion: v1 |
| 122 | +metadata: |
| 123 | + name: http-fileserver |
| 124 | +spec: |
| 125 | + selector: |
| 126 | + app: fileserver |
| 127 | + ports: |
| 128 | + - port: 80 |
| 129 | +``` |
| 130 | +
|
| 131 | +
|
| 132 | +
|
| 133 | +6. You can run the deployment (or pod) by the Kubernetes command - see [Kubernetes Ingress](https://mpolinowski.github.io/kubernetes-nginx-ingress/): |
| 134 | +
|
| 135 | +
|
| 136 | +
|
| 137 | +```bash |
| 138 | +kubectl create -f http-fileserver.yaml |
| 139 | +``` |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
0 commit comments