{"id":19989991,"url":"https://github.com/JohannesEH/dotnet-docker-kubernetes-presentation","last_synced_at":"2025-05-04T09:34:07.536Z","repository":{"id":211642415,"uuid":"128393023","full_name":"JohannesEH/dotnet-docker-kubernetes-presentation","owner":"JohannesEH","description":".NET Group Presentation - Intro to .NET Core, Docker \u0026 Kubernetes","archived":true,"fork":false,"pushed_at":"2019-05-28T08:44:53.000Z","size":621,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2023-12-09T22:30:56.742Z","etag":null,"topics":["docker","dotnet-core","kubernetes"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JohannesEH.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2018-04-06T12:39:32.000Z","updated_at":"2023-12-09T22:31:07.297Z","dependencies_parsed_at":"2023-12-09T22:41:08.430Z","dependency_job_id":null,"html_url":"https://github.com/JohannesEH/dotnet-docker-kubernetes-presentation","commit_stats":null,"previous_names":["johanneseh/dotnet-docker-kubernetes-presentation"],"tags_count":0,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohannesEH%2Fdotnet-docker-kubernetes-presentation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohannesEH%2Fdotnet-docker-kubernetes-presentation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohannesEH%2Fdotnet-docker-kubernetes-presentation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohannesEH%2Fdotnet-docker-kubernetes-presentation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JohannesEH","download_url":"https://codeload.github.com/JohannesEH/dotnet-docker-kubernetes-presentation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224391390,"owners_count":17303609,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker","dotnet-core","kubernetes"],"created_at":"2024-11-13T04:50:53.215Z","updated_at":"2024-11-13T04:50:59.794Z","avatar_url":"https://github.com/JohannesEH.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# .NET Group Presentation - Intro to .NET Core, Docker \u0026 Kubernetes\n\nThis presentation/guide is a quick introduction to dotnet core, docker and kubernetes. In this guide we'll create a couple of simple dotnet apps to run in docker containers and as jobs and services in a kuberntes cluster using minikube.\n\nThis guide can be found on GitHub: https://github.com/JohannesEH/dotnet-docker-kubernetes-presentation (or bit.ly/dotnet-docker-k8s)\n\n## Tools\n\nTo follow this guide you have to install these tools:\n\n* [VS Code](https://code.visualstudio.com/) (optional but recommended)\n* [.Net Core SDK](https://www.microsoft.com/net/learn/get-started/windows)\n* [Docker for Windows](https://www.docker.com/docker-windows)\n* [Minikube](https://github.com/kubernetes/minikube)\n* [Kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/)\n\n## Agenda\n\n1. Dotnet\n    1. Console App\n    2. Web App (React)\n2. Docker\n    1. Dockerize console app\n    2. Dockerize web app\n3. Kubernetes \n    1. Start a local minikube \"cluster\"\n    2. Install web app as a service\n    3. Install console app as a cronjob\n\n## .NET Core\n\nFirst we'll create a couple of apps using the `dotnet new` cli command.\n\n### Console App\n\nTo create the console app we'll first call:\n```shell\ndotnet new console -o console-app\n```\n\nAfter app creation we'll go into the app directory, build the app and run it to see the output.\n\n```shell\ncd console-app\ndotnet build\ndotnet run\n```\nThe app should output `Hello World!`.  \nOk, lets head back out to the root dir and move on to the web app.\n\n```shell\ncd ..\n```\n\n### Web App\n\nIn this presentation we'll create a dotnet web app based on react (other templates are available for angular, vue and others). The steps are almost identical to creating the console app.\n\nTo create the web app we'll call:\n\n```shell\ndotnet new react -o web-app\n```\n\nAfter the web app is created lets build and test it to see if it runs.\n\n```shell\ncd web-app\nnpm install\ndotnet build\ndotnet run\n```\n\nOpen the web app in your browser using the default address of http://localhost:5000/ and verify that the sample app is running correctly. After testing the app go back to the console and close the server by pressing Ctrl+C. Then head back out to the root dir.\n\n```shell\ncd ..\n```\n\nThat's it for the dotnet core part. Not much, I know, but enough for us to have something to build and run in docker and kubernetes.\n\n## Docker Intro (https://docs.docker.com/engine/docker-overview/ \u0026 https://hub.docker.com/)\n\nDocker concepts and CLI (`pull`, `push`, `prune`, `run`, `build`) DEMO 🤞🔥\n\nExample docker run command: `docker run --rm -it ubuntu /bin/bash`\n\n### .dockerignore\n\nFor each app we need to specify a .dockerignore file. This file makes docker ignore certain file just like a .gitignore file does for git. This is useul as we don't want to include certain directories (obj, bin, node_modules, etc.). So copy the premade .dockerignore file from this repo into the app directories:\n\n```shell\nxcopy .dockerignore console-app\nxcopy .dockerignore web-app\n```\n\n### Setting up a Dockerfile for the console app\n\nFirst well dockerize the console app to do this well need to create an empty file called \"Dockerfile\" in the console-app dir. Copy-paste the following code into the file:\n\n```dockerfile\n# Restore dependencies and build the app\nFROM microsoft/dotnet:2.0-sdk AS build\nWORKDIR /src\nCOPY *.csproj .\nRUN dotnet restore\nCOPY . .\nRUN dotnet build -c Release\n\n# Publish the app based on the build step\nFROM build as publish\nRUN dotnet publish -c Release -o /app\n\n# Run tests\n# FROM publish AS test\n# RUN dotnet test \u003csome test project\u003e\n\n# Package the published code with the dotnet runtime image\nFROM microsoft/dotnet:2.0-runtime AS final\nWORKDIR /app\nCOPY --from=publish /app .\nENTRYPOINT [\"dotnet\", \"console-app.dll\"]\n```\n\nAfter creating the dockerfile build using:\n\n```shell\ndocker build console-app -t console-app\n```\n\nIf successful this command will output a docker image tagged console-app:latest to your local docker repository. To run the image use the following command:\n\n```shell\ndocker run --rm -it console-app\n```\n\nThis command should output `Hello World!` just as the `dotnet run` command did earlier.\n\n### Setting up a Dockerfile for the web app\n\nNext we'll setup a Dockerfile for the web app. Again create an empty Dockerfile in the web-app directory and fill in the following code:\n\n```dockerfile\n#Configure build envrionment\nFROM microsoft/dotnet:2.0-sdk AS build-env\nWORKDIR /src\n\nENV NODE_VERSION 8.11.1\nENV NODE_DOWNLOAD_SHA 0e20787e2eda4cc31336d8327556ebc7417e8ee0a6ba0de96a09b0ec2b841f60\n\nRUN curl -SL \"http://unencrypted.nodejs.org/download/release/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64.tar.gz\" --output nodejs.tar.gz \\\n    \u0026\u0026 echo \"$NODE_DOWNLOAD_SHA nodejs.tar.gz\" | sha256sum -c - \\\n    \u0026\u0026 tar -xzf \"nodejs.tar.gz\" -C /usr/local --strip-components=1 \\\n    \u0026\u0026 rm nodejs.tar.gz \\\n    \u0026\u0026 ln -s /usr/local/bin/node /usr/local/bin/nodejs \\\n    \u0026\u0026 echo Node Version \u0026\u0026 node -v \\\n    \u0026\u0026 echo NPM Version \u0026\u0026 npm -v\n\n# Restore dependencies and build the app\nFROM build-env AS build\nRUN npm -v\nCOPY *.csproj .\nRUN dotnet restore\nCOPY package.json .\nRUN npm install\nCOPY . .\nRUN dotnet build -c Release\n\nFROM build AS publish\nRUN dotnet publish -c Release -o /app\n\n# Run tests\n# FROM publish AS test\n# RUN dotnet test \u003csome test project\u003e\n\n# Package the published code with the dotnet runtime image\nFROM microsoft/aspnetcore:2.0 AS final\nWORKDIR /app\nCOPY --from=publish /app .\nEXPOSE 80\nENTRYPOINT [\"dotnet\", \"web-app.dll\"]\n```\n\nAfter creating the dockerfile build using:\n\n```shell\ndocker build web-app -t web-app\n```\n\nIf successful this command will output a docker image tagged web-app:latest to your local docker repository. To run the image use the following command:\n\n```shell\ndocker run --rm -it -p 8080:80 web-app\n```\n\nThis will start an interactive session just like running `dotnet run` from the console. Now you can visit the app on the exposed port 80 mapped to port 8080 on the host machine. \nIf you want to run the web app in the background (detached mode), and keep the docker container around for later use, you can run the following command:\n\n```shell\ndocker run --name web-app -d -p 8080:80 web-app\n```\n\nSo now we have two working docker images let us publish them to dockerhub so we can run them in our kubernetes cluster.\n\n## Dockerhub\n\nThis step is fairly easy. First you have to go to hub.docker.com and create an account and 2 repositories called \"console-app\" and \"web-app\". My username on dockerhub is \"johanneseh\" so I'll use that name in the following steps.\n\nFirst you need to login the docker client to dockerhub by calling:\n\n```shell\ndocker login\n```\n\nAfter you've logged in we need to tag our local images so they know what repo they should go to when you push. So lets tag our app images:\n\n```shell\ndocker tag console-app johanneseh/console-app\ndocker tag web-app johanneseh/web-app\n```\n\nNow we're ready to push our images to dockerhub. The default behavior of the docker client is to push images to dockerhub if you don't choose another docker registry. So to push our images to dockerhub we simply use:\n\n```shell\ndocker push johanneseh/console-app\ndocker push johanneseh/web-app\n```\n\nGood. Now our images are online and ready to be used in our kubernetes cluster so lets go ahead and set that up.\n\n## Kubernetes\n\n### Setting up \u0026 running minikube\n\nFirst of all we need to get the cluster up and running. We need minikube to run in hyper-v, which is a little tricky. So first you should follow [this guide](https://medium.com/@JockDaRock/minikube-on-windows-10-with-hyper-v-6ef0f4dc158c) to configure hyper-v correctly, and then finally you should start start minikube with this command from an elevated prompt:\n\n```shell\nminikube start --vm-driver hyperv --hyperv-virtual-switch \"Primary Virtual Switch\"\n```\n\n### Kubernetes Intro (https://kubernetes.io/docs/concepts/)\n\nK8s concepts, CLI and dashboard DEMO 🤞🤞🔥🔥\n\n### Setting up the console app as a cron job\n\nLet's pretend our console app is job we need to run every minute. To do this we create a cronjob with the following command:\n\n```shell\nkubectl run console-app-job --schedule=\"*/1 * * * *\" --restart=OnFailure --image=johanneseh/console-app\n```\n\nCheck that the job is running using the `minkube dashboard` command.\n\n### Setting up the web-app and expose it\n\nNow let's configure our web app to run as a service in our cluster with the following command:\n\n```shell\nkubectl run web-app --image=johanneseh/web-app --port=80 --replicas=3\n```\n\nCheck that the deployment is running in the dashboard.\n\nFinally let's expose the service using a kubernetes LoadBalancer service.\n\n```shell\nkubectl expose deployment web-app --type=LoadBalancer --name=web-app-service\n```\n\nThis should create the LoadBalancer and let us visit the running web app. In a normal kubernetes cluster we would get an external ip to go to or point our dns at, but in minikube this is a bit different. Fortunately minikube has a command to get the address for the service:\n\n```shell\nminikube service web-app-service\n```\n\nThis should open up your browser and let you see the running web page.\n\n## We're done!\nThat's it... We've created to apps, dockerized them and made the run in a kubernetes cluster.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJohannesEH%2Fdotnet-docker-kubernetes-presentation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FJohannesEH%2Fdotnet-docker-kubernetes-presentation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJohannesEH%2Fdotnet-docker-kubernetes-presentation/lists"}