Skip to main content

The CRASH course into a fraction of containerization

The world of containerization is massive with lots of learning. This workshop hopes to remove the intimidation factor by letting you work through a meaningful number of pieces but thinly sliced. The pieces:

  1. Getting an application into Docker and seeing it work

Prerequisites

  1. Install Visual Studio (You will need to create a simple test web application)

Create a simple test web application

  1. Launch Visual Studio and create an ASP.NET Core Web Application named MyWebApp

2. Add some code and run the application from Visual Studio

3. Publish the application via the Build menu option

Getting the application into Docker and seeing it work

  1. Launch Visual Studio Code

3. Using the left area — EXPLORER, create a new file named Dockerfile and add the following code (of course save after).

FROM mcr.microsoft.com/dotnet/aspnet:6.0

COPY /bin/Release/net6.0/publish/ App/
WORKDIR /App
ENTRYPOINT ["dotnet", "MyWebApp.dll"]

4. Using the left area — EXPLORER, create a new file named docker-compose.yml and add the following code (of course save after).

version: "3"
services:
mywebapp:
build: .
image: imageorigin:v1
container_name: containerorigin
ports:
- 86:80
networks:
- network

networks:
network: {}

5. Confirm Docker Desktop is running in the System Tray area

6. Start a New Terminal via the Terminal menu option

7. Run the following Docker command

docker-compose up -d

8. Once the command completes. Click on the Docker extension icon.

9. Notice that the container is running. Right-click on the container and select the Open in Brower option. You should now see your application web page at the address: http://localhost:86/. That 86 is from your docker-compose.yml where you specify the external port to expose the stuff running inside the container. The 80 is the default http port that web applications use and is termed the internal port that the container uses to talk to the application.

Using a handful of Docker commands

  1. In the Terminal window. Run the following command
docker images

2. In the Terminal window. Run the following command

docker ps

3. In the Terminal window. Run the following command

docker-compose down

4. Refresh the CONTAINERS area. Notice that the container is gone and if you refresh your web page it no longer works.

5. In the Terminal window. Run the following command

docker-compose up -d

6. Refresh the CONTAINERS area. Refresh the web page.

7. In the Terminal window. Run the following command

docker-compose down

8. The image (of the application) is needed for the next steps.

Getting the application into Kubernetes and seeing it work

  1. Launch Docker Desktop and Enable Kubernetes via Settings

2. Kubernetes requires the use of a registry server. Run the following Docker command

docker run -d -p 5000:5000 --restart=always --name registry registry:2

3. Copy the application image to the registry. Run the following Docker commands

docker tag imageorigin:v1 localhost:5000/imgorigin
docker push localhost:5000/imgorigin

4. With the application image now in the registry. Clean up the other images. Run the following commands

docker image remove imageorigin:v1
docker image remove localhost:5000/imgorigin

(Minor Tangent) Curl for your pleasure. Wait! What?

  • Get the image from the register. Run the following Docker command
docker pull localhost:5000/imgorigin
  • Spin up a container for the application. Run the following Docker command
docker run -d --name testcurl -p 8080:80 localhost:5000/imgorigin
  • Test the web page and note the port number 8080. Run the following command
curl http://localhost:8080/
  • Learn curl
docker stop testcurl 
docker rm testcurl
docker image remove localhost:5000/imgorigin

Back to the business of Kubernetes

5. Using the left area — EXPLORER, create a new folder named k8s

6. Create a new file deploy.yaml under k8s and add the following content

apiVersion: apps/v1
kind: Deployment
metadata:
name: io-deployment
labels:
app: io
spec:
replicas: 2
selector:
matchLabels:
app: io
template:
metadata:
labels:
app: io
spec:
containers:
- name: io
image: localhost:5000/imgorigin
ports:
- containerPort: 80 # this is the port exposed by your application via dockerfile

7. Create a new file service.yaml under k8s and add the following content

apiVersion: v1
kind: Service
metadata:
name: io-service
spec:
selector:
app: io # this need to match spec -> selector -> matchLabels used in the deployment
ports:
- protocol: TCP
port: 8085 # this is the port for others in the cluster to talk to me
targetPort: 80 # this is the port from the deployment and what the pods are exposed on

8. Create a new file ingress.yaml under k8s and add the following content

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: io-ingress
spec:
ingressClassName: nginx
rules:
- host: foo.bar.com # add this entry to your host file - 127.0.0.1 foo.bar.com
http:
paths:
- path: /
pathType: Prefix # https://pet2cattle.com/2021/04/ingress-pathtype
backend:
service:
name: io-service # this is the name of the service
port:
number: 8085 # this is the service's cluster port exposed to be talked to a lot

Using a handful of Kubernetes commands

  1. Deploy the application. Run the following kubectl command
kubectl apply -f .\deploy.yaml

2. Deploy the service to expose the application. Run the following kubectl command

kubectl apply -f .\service.yaml

3. Run the following kubectl command

kubectl get all

4. Click on the Kubernetes extension and expand the Services

5. Right-click on an entry io-deployment... and select Port Forward from the options menu. Use port 8080 for LOCAL.

6. Navigate to the application web page at the http://localhost:8080

Congrats !!! Your application is working in Kubernetes. But there is more…

We should be accessing our application via a URL. Ingress time…

7. At the time of this writing, the version is 1.5.1

  • please use the latest version if the below does not work
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.5.1/deploy/static/provider/cloud/deploy.yaml

8. Add the following to your system hosts file

127.0.0.1 foo.bar.com

9. Deploy the ingress to allow url navigation to the application. Run the following kubectl command

kubectl apply -f .\ingress.yaml

10. Launch your web browser and navigate to http://foo.bar.com/

11. Keep your web browser open. Click on the Kubernetes extension and expand the Services

12. Right-click on an entry io-deployment... and select Delete Now from the option menu

13. Do this deleting a few times and Refresh your web browser still on http://foo.bar.com/

(cue the dramatic music) THE POWER OF KUBERNETES !!!

14. Keep your web browser open. Run the following kubectl commands. Then refresh your web browser.

kubectl delete -f .\ingress.yaml
kubectl delete -f .\service.yaml
kubectl delete -f .\deploy.yaml

Install Helm

  1. Download the binary from https://github.com/helm/helm/releases

Utilize Helm to deploy into Kubernetes

  1. Using the left area — EXPLORER, create a new folder named helmstuffhere and a subfolder named templates
apiVersion: v2
name: io-release
description: A Helm chart for mywebapp
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
version: 1.0.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application.
appVersion: 2024.1.0

Using a handful of Helm commands

  1. Start a New Terminal via the Terminal menu option
helm template io-release helmstuffhere

4. Run the following helm command to install the application into Kubernetes

helm install io-release helmstuffhere

5. Run the following helm command to see the helm history

helm history io-release

6. Click on the Kubernetes extension and explore what has been installed. Try http://foo.bar.com/ in your web browser or use curl

Why use helm?

  1. Go back to your application source code and edit the /Pages/Index.cshtml.cs file
public void OnGet()
{
ViewData["Name"] = "CRASH COURSE";
}

2. Publish the application via the Build menu option

3. Build a new version of the application image

docker build -t imageorigin:v2 . 

4. Copy the application image to the registry

docker tag imageorigin:v2 localhost:5000/imgoriginv2
docker push localhost:5000/imgoriginv2

5. With the application image now in the registry. Clean up the other images

docker image remove imageorigin:v2
docker image remove localhost:5000/imgoriginv2

6. Edit the deployment file at /helmstuffhere/templates/deploy.yaml to use the new image

image: localhost:5000/imgoriginv2

7. Edit the chart file at /helmstuffhere/Chart.yaml to reflect the new version of our application

apiVersion: v2
name: io-release
description: A Helm chart for mywebapp
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
version: 1.0.1

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application.
appVersion: 2024.1.1

8. Run the following helm command to install the new application version — UPGRADE

helm upgrade io-release helmstuffhere

9. Run the following helm command to see the helm history

helm history io-release

10. Try http://foo.bar.com/ in your web browser and see the CRASH COURSE change

11. Let’s rollback the application to the previous version

helm rollback io-release 1

12. Run the following helm command to see the helm history

helm history io-release

13. Try http://foo.bar.com/ in your web browser

(cue the dramatic music) THE POWER OF HELM !!!

…all the best on your journey into containerization…

…see you out there…

Credit: Brian Singh

Comments

Popular posts from this blog

Certificate of Good Standing SCAM

Yes! SCAM! Their notice is clearly intended to confuse and give the impression of being part of the official government, in the hopes of swindling you out of your hard earned money. Lucky my spidey sense kicked in (helped by the fact that I used a CPA to register my business) and a quick google search provided confirmation of my suspicion. Happy to post this so I can help spread the word about this sleazy tactic. Good luck follow newbie business owners.

Creating my logo

First I have to say. Thank Science. For the great resource that is the internet. After a few google searches and a few very helpful you tube videos. I was able to use a free resource to create my logo for my business. I am sure this achievement is probably trivial to many but it was a small but important step towards hopefully a realized dream. Good luck follow newbie business owners. Resources: https://www.youtube.com/watch?v=wVrEeImyg_E&feature=youtu.be https://logomaker.thehoth.com/

Publish .NET Core Code Coverage in Azure DevOps

  Publishing .NET Core code coverage in Azure Devops with .Net 6 gives very insightful information and does not require a lot of effort. Add the extension for Report Generator from the market place. Add the coverlet.collector NuGet package to (each of) the test project(s) that you run dotnet test against.  For example: <PackageReference Include=”coverlet.collector” Version=”3.1.0"> <IncludeAssets>      runtime;build;native;contentfiles;analyzers;buildtransitive; </IncludeAssets> <PrivateAssets>all</PrivateAssets> </PackageReference> 3. Next, update the pipeline yaml to ensure your dotnet test command looks like below along with the report generator task to combine the generated coverage reports for all the test projects and then publish the report. # This sets the variable to disable automatic generation of default coverage report and will allow us to use the new report that we are generating via Report Generator - task:...