Aller au contenu

Deployment

In this short demo, we will create a nginx container that server a index.html file. Once the container is correctly configured, it will be published on Docker Hub and in the last step it will be deploy on a K8s Cluster.

Step 1. Create container

  • Create a index.html file with “hello world” as content
  • Create the Dockerfile:
    FROM nginx
    COPY index.html /usr/share/nginx/html
    EXPOSE 80
    CMD ["nginx", "-g", "daemon off;"]
    
  • Build the new container:
    docker build -t buntschu/mynginx:v1 .
    

Step 2. Test the new docker image

  • With docker installed on your machine, just run this command to start the container:
    docker run -p 8080:80 mynginx
    
  • Open a browser and check the answer. Use the URL: http://localhost:8080

Step 3. Publish your new image to Docker Hub

  • Push the new image to Docker Hub (You need to create an account):
    docker push buntschu/mynginx:v1
    

Step 4. Deploy it on minikube

Option 1

With kubectl and specifying parameters

  • Deployment:
    kubectl create deploy myweb --image buntschu/mynginx:v1
    
  • Expose the deployment:
    kubectl expose deploy myweb --type=NodePort --port 80 --name=myservice
    

Option 2

By creating deployment and service description in files.

  • Create a file simple-deploy.yaml (in YAML format):
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: web
      labels:
            app: mynginx
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: mynginx
      template:
        metadata:
          labels:
            app: mynginx
        spec:
          containers:
          - name: nginx
            image: buntschu/mynginx:v1
            ports:
            - containerPort: 80
    
  • Create a file simple-service.yaml:

    apiVersion: v1
    kind: Service
    metadata:
      name: myservice
    spec:
      type: NodePort
      selector:
        app: mynginx
      ports:
      - port: 80
        targetPort: 80
    

  • Deploy the whole:

    kubectl apply -f simple-deploy.yaml -f simple-service
    

Step 5. Check

  • Check the deployment: ```bash kubectl get deploy kubectl get pods ````
  • Check the service:

    kubectl get service
    

  • Get the service URL on minikube:

    minikube  service myservice --url 
    

Step 6. Access your web

  • With the URL obtained in step 4
    curl <url>
    
    or with a browser

Dernière mise à jour: 3 October 2023