One place for hosting & domains

      Kubernetes

      Des conteneurs à Kubernetes avec Node.js eBook


      Télécharger le eBook complet

      Des conteneurs à Kubernetes avec Node.js eBook sous format EPUB

      Des conteneurs à Kubernetes avec Node.js eBook sous format PDF

      Présentation de l’eBook

      Ce livre a été conçu pour vous expliquer de quelle manière on peut utiliser des conteneurs et Kubernetes pour développer une pile complète. Vous apprendrez à développer une application de pile complète à l’aide de Node.js et MongoDB et à les gérer – d’abord avec Docker, puis avec Docker Compose et, pour finir, avec Kubernetes.

      Ce livre est basé sur la série de tutoriels Des conteneurs à Kubernetes avec Node.js disponible sur DigitalOcean Community. Les sujets traités sont les suivants :

      1. Créer une application Node.js à l’aide de Docker pour le développement

      2. Intégrer une base de données NoSQL à votre application Node.js à l’aide de MongoDB

      3. Gérer votre environnement de développement avec Docker Compose

      4. Migrer votre flux de travail Docker Compose vers Kubernetes

      5. Faire évoluer vos applications Node.js et MongoDB à l’aide de Helm et Kubernetes

      6. Sécuriser votre application Node.js conteneurisée avec Nginx, Let’s Encrypt et Docker Compose

      Chaque chapitre est conçu pour que vous puissiez progressivement faire votre construction à partir du début. Cependant, si vous connaissez un sujet ou si vous êtes plus intéressé par une section particulière, n’hésitez pas à passer au chapitre qui répond au mieux à votre objectif.

      Télécharger le eBook

      Vous pouvez télécharger le eBook au format EPUB ou PDF en suivant les liens ci-dessous.

      Télécharger le eBook complet

      Des conteneurs à Kubernetes avec Node.js eBook sous format EPUB

      Des conteneurs à Kubernetes avec Node.js eBook sous format PDF

      Si vous souhaitez en savoir plus sur le développement d’applications à l’aide de Node.js, consultez la section Node.js de la communauté DigitalOcean. Ou si vous souhaitez continuer à en apprendre plus sur les conteneurs, Docker et Kubernetes, nous vous conseillons de suivre le cours autoguidé Kubernetes pour les développeurs de pile complète.



      Source link

      How To Deploy a Resilient Node.js Application on Kubernetes from Scratch


      Description

      You may have heard the buzz around Kubernetes and noticed that many companies have been rapidly adopting it. Due to its many components and vast ecosystem it can be quite confusing to find where the path starts to learn it.

      In this session, you will learn the basics of containers and Kubernetes. Step by step, we will go through the entire process of packaging a Node.js application into a Docker container image and then deploying it on Kubernetes. We will demonstrate scaling to multiple replicas for better performance. The end result will be a resilient and scalable Node.js deployment.

      You will leave this session with sufficient knowledge of containerization, Kubernetes basics, and the ability to deploy highly available, performant, and scalable Node.js applications on Kubernetes.

      💰 Use this free $100 credit to try out Kubernetes on DigitalOcean for free!

      About the Presenter

      Kamal Nasser is a Developer Advocate at DigitalOcean. If not automating and playing with modern software and technologies, you’ll likely find him penning early 17th century calligraphy. You can find Kamal on Twitter at @kamaln7 or on GitHub at @kamaln7.

      Resources

      View the slides for this talk, or watch the recording on YouTube (coming soon).

      Transcript of The Commands and Manifests Used

      Be sure to follow along with the recording for an explanation and replace kamaln7 with your own DockerHub username.

      Node App

      1. Create an empty node package: npm init -y
      2. Install express as a dependency: npm install express
      3. index.js

        const express = require('express')
        const os = require('os')
        
        const app = express()
        app.get('/', (req, res) => {
                res.send(`Hi from ${os.hostname()}!`)
        })
        
        const port = 3000
        app.listen(port, () => console.log(`listening on port ${port}`))
        

      Docker

      1. Dockerfile

        FROM node:13-alpine
        
        WORKDIR /app
        
        COPY package.json package-lock.json ./
        
        RUN npm install --production
        
        COPY . .
        
        EXPOSE 3000
        
        CMD node index.js
        
      2. Build the image: docker build -t kamaln7/node-hello-app .

      3. Edit index.js and replace the word Hi with Hello.

      4. Re-build the image and notice Docker re-using previous layers: docker build -t kamaln7/node-hello-app .

      5. Run a container to test it: docker run --rm -d -p 3000:3000 kamaln7/node-hello-app

      6. Look at the running containers: docker ps

      7. Stop the container: docker stop CONTAINER_ID

      8. Push the image to DockerHub: docker push kamaln7/node-hello-app

      Kubernetes

      1. Get worker nodes: kubectl get nodes
      2. Create a deployment: kubectl create deployment --image kamaln7/node-hello-app node-app
      3. Scale up to 3 replicas: kubectl scale deployment node-app --replicas 3
      4. Expose the deployment as a NodePort replica: kubectl expose deployment node-app --port 3000
      5. Look at the newly created service (and the assigned port): kubectl get services
      6. Grab the public IP of one of the worker nodes: kubectl get nodes -o wide
      7. Browse to IP:port to test the service
      8. Edit the service: kubectl edit service node-app
        1. Replace port: 3000 with port: 80
        2. Replace type: NodePort with type: LoadBalancer
      9. Verify that the service was updated: kubectl get service
      10. Run the above command every few seconds until you get the external IP address of the Load Balancer
      11. Browse to the IP of the Load Balancer





      Source link

      How To Develop Applications on Kubernetes with Okteto


      The author selected Girls Who Code to receive a donation as part of the Write for DOnations program.

      Introduction

      The Okteto CLI is an open-source project that provides a local development experience for applications running on Kubernetes. With it you can write your code on your local IDE and as soon as you save a file, the changes can be pushed to your Kubernetes cluster and your app will immediately update. This whole process happens without the need to build Docker images or apply Kubernetes manifests, which can take considerable time.

      In this tutorial, you’ll use Okteto to improve your productivity when developing a Kubernetes-native application. First, you’ll create a Kubernetes cluster and use it to run a standard “Hello World” application. Then you’ll use Okteto to develop and automatically update your application without having to install anything locally.

      Prerequisites

      Before you begin this tutorial, you’ll need the following:

      Step 1 — Creating the Hello World Application

      The “Hello World” program is a time-honored tradition in web development. In this case, it is a simple web service that responds “Hello World” to every request. Now that you’ve created your Kubernetes cluster, let’s create a “Hello World” app in Golang and the manifests that you’ll use to deploy it on Kubernetes.

      First change to your home directory:

      Now make a new directory called hello_world and move inside it:

      • mkdir hello_world
      • cd hello_world

      Create and open a new file under the name main.go with your favorite IDE or text editor:

      main.go will be a Golang web server that returns the message Hello world!. So, let’s use the following code:

      main.go

      package main
      
      import (
          "fmt"
          "net/http"
      )
      
      func main() {
          fmt.Println("Starting hello-world server...")
          http.HandleFunc("/", helloServer)
          if err := http.ListenAndServe(":8080", nil); err != nil {
              panic(err)
          }
      }
      
      func helloServer(w http.ResponseWriter, r *http.Request) {
          fmt.Fprint(w, "Hello world!")
      }
      

      The code in main.go does the following:

      • The first statement in a Go source file must be the package name. Executable commands must always use package main.
      • The import section indicates which packages the code depends on. In this case it uses fmt for string manipulation, and net/http for the HTTP server.
      • The main function is the entry point to your binary. The http.HandleFunc method is used to configure the server to call the helloServer function when a request to the / path is received. http.ListenAndServe starts an HTTP server that listens on all network interfaces on port 8080.
      • The helloServer function contains the logic of your request handler. In this case, it will write Hello world! as the response to the request.

      You need to create a Docker image and push it to your Docker registry so that Kubernetes can pull it and then run the application.

      Open a new file under the name Dockerfile with your favorite IDE or text editor:

      The Dockerfile will contain the commands required to build your application’s Docker container. Let’s use the following code:

      Dockerfile

      FROM golang:alpine as builder
      RUN apk --update --no-cache add bash
      WORKDIR /app
      ADD . .
      RUN go build -o app
      
      FROM alpine as prod
      WORKDIR /app
      COPY --from=builder /app/app /app/app
      EXPOSE 8080
      CMD ["./app"]
      

      The Dockerfile contains two stages, builder and prod:

      • The builder stage contains the Go build tools. It’s responsible for copying the files and building the Go binary.
      • The prod stage is the final image. It will contain only a stripped down OS and the application binary.

      This is a good practice to follow. It makes your production containers smaller and safer since they only contain your application and exactly what is needed to run it.

      Build the container image (replace your_DockerHub_username with your Docker Hub username):

      • docker build -t your_DockerHub_username/hello-world:latest

      Now push it to Docker Hub:

      • docker push your_DockerHub_username/hello-world:latest

      Next, create a new folder for the Kubernetes manifests:

      When you use a Kubernetes manifest, you tell Kubernetes how you want your application to run. This time, you’ll create a deployment object. So, create a new file deployment.yaml with your favorite IDE or text editor:

      The following content describes a Kubernetes deployment object that runs the okteto/hello-world:latest Docker image. Add this content to your new file, but in your case replace okteto listed after the image label with your_DockerHub_username:

      ~/hello_world/k8s/deployment.yaml

      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: hello-world
      spec:
        selector:
          matchLabels:
            app: hello-world
        replicas: 1
        template:
          metadata:
            labels:
              app: hello-world
          spec:
            containers:
            - name: hello-world
              image: your_DockerHub_username/hello-world:latest
              ports:
              - containerPort: 8080
      

      The deployment manifest has three main sections:

      • metadata defines the name for your deployment.
      • replicas defines how many copies of it you want running.
      • template tells Kubernetes what to deploy, and what labels to add. In this case, a single container, with the okteto/hello-world:latest image, listening on port 8080, and with the app: hello-world label. Note that this label is the same used in the selector section.

      You’ll now need a way to access your application. You can expose an application on Kubernetes by creating a service object. Let’s continue using manifests to do that. Create a new file called service.yaml with your favorite IDE or text editor:

      The following content describes a service that exposes the hello-world deployment object, which under the hood will use a DigitalOcean Load Balancer:

      k8s/service.yaml

      apiVersion: v1
      kind: Service
      metadata:
        name: hello-world
      spec:
        type: LoadBalancer
        ports:
          - protocol: TCP
            port: 80
            targetPort: 8080
            name: http
        selector:
          app: hello-world
      

      The service manifest has four main sections:

      • metadata tells Kubernetes how to name your service.
      • type tells Kubernetes how you want to expose your service. In this case, it will expose it externally through a Digital Ocean Load Balancer.
      • The ports label tells Kubernetes which ports you want to expose, and how to map them to your deployment. In this case, you will expose port 80 externally and direct it to port 8080 in your deployment.
      • selector tells Kubernetes how to direct traffic. In this case, any pod with the app: hello-world label will receive traffic.

      You now have everything ready to deploy your “Hello World” application on Kubernetes. We will do this next.

      Step 2 — Deploying Your Hello World Application

      In this step you’ll deploy your “Hello World” application on Kubernetes, and then you’ll validate that it is working correctly.

      Start by deploying your application on Kubernetes:

      You’ll see the following output:

      Output

      deployment.apps "hello-world" created service "hello-world" created

      After about one minute or so, you will be able to retrieve your application’s IP. Use this kubectl command to check your service:

      • kubectl get service hello-world

      You’ll see an output like this listing your Kubernetes service objects. Note your application’s IP in the the EXTERNAL-IP column:

      Output

      NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE hello-world ClusterIP your_cluster_ip your_external_ip 8080/TCP 37s

      Open your browser and go to your_external_ip listed for your “Hello World” application. Confirm that your application is up and running before continuing with the next step.

      Hello World Okteto

      Until this moment, you’ve followed a fairly traditional pathway for developing applications with Kubernetes. Moving forward, whenever you want to change the code in your application, you’ll have to build and push a new Docker image, and then pull that image from Kubernetes. This process can take quite some time. Okteto was designed to streamline this development inner-loop. Let’s look at the Okteto CLI and see just how it can help.

      Step 3 — Installing the Okteto CLI

      You will now improve your Kubernetes development productivity by installing the Okteto CLI. The Okteto command line interface is an open-source project that lets you synchronize application code changes to an application running on Kubernetes. You can continue using your favorite IDE, debuggers, or compilers without having to commit, build, push, or redeploy containers to test your application–as you did in the previous steps.

      To install the Okteto CLI on a macOS or Linux machine, run the following command:

      • curl https://get.okteto.com -sSfL | sh

      Let’s take a closer look at this command:

      • The curl command is used to transfer data to and from a server.
      • The -s flag suppresses any output.
      • The -S flag shows errors.
      • The -f flag causes the request to fail on HTTP errors.
      • The -L flag makes the request follow redirects.
      • The | operator pipes this output to the sh command, which will download and install the latest okteto binary in your local machine.

      If you are running Windows, you can alternately download the file through your web browser and manually add it to your $PATH.

      Once the Okteto CLI is installed, you are ready to put your “Hello World” application in development mode.

      Step 4 — Putting Your Hello World Application in Development Mode

      The Okteto CLI is designed to swap the application running on a Kubernetes cluster with the code you have in your machine. To do so, Okteto uses the information provided from an Okteto manifest file. This file declares the Kubernetes deployment object that will swap with your local code.

      Create a new file called okteto.yaml with your favorite IDE or text editor:

      Let’s write a basic manifest where you define the deployment object name, the Docker base image to use, and a shell. We will return to this information later. Use the following sample content file:

      okteto.yaml

      name: hello-world
      image: okteto/golang:1
      workdir: /app
      command: ["bash"]
      

      Prepare to put your application in development mode by running the following command:

      Output

      ✓ Development environment activated ✓ Files synchronized Namespace: default Name: hello-world Welcome to your development environment. Happy coding! default:hello-world /app>

      The okteto up command swaps the “Hello World” application into a development environment, which means:

      • The Hello World application container is updated with the docker image okteto/golang:1. This image contains the required dev tools to build, test, debug, and run the “Hello World” application.

      • A file synchronization service is created to keep your changes up-to-date between your local filesystem and your application pods.

      • A remote shell starts in your development environment. Now you can build, test, and run your application as if you were in your local machine.

      • Whatever process you run in the remote shell will get the same incoming traffic, the same environment variables, volumes, or secrets as the original “Hello World” application pods. This, in turn, gives you a highly realistic, production-like development environment.

      In the same console, now run the application as you would typically do (without building and pushing a Docker image), like this:

      Output

      Starting hello-world server...

      The first time you run the application, Go will download your dependencies and compile your application. Wait for this process to finish and test your application by opening your browser and refreshing the page of your application, just as you did previously.

      Now you are ready to begin developing directly on Kubernetes.

      Step 5 — Developing Directly on Kubernetes

      Let’s start making changes to the “Hello World” application and then see how these changes get reflected in Kubernetes.

      Open the main.go file with your favorite IDE or text editor. For example, open a separate console and run the following command:

      Then, change your response message to Hello world from DigitalOcean!:

      main.go

      package main
      
      import (
          "fmt"
          "net/http"
      )
      
      func main() {
          fmt.Println("Starting hello-world server...")
          http.HandleFunc("/", helloServer)
          if err := http.ListenAndServe(":8080", nil); err != nil {
              panic(err)
          }
      }
      
      func helloServer(w http.ResponseWriter, r *http.Request) {
          fmt.Fprint(w, "Hello world from DigitalOcean!")
      }
      

      It is here that your workflow changes. Instead of building images and redeploying containers to update the “Hello World” application, Okteto will synchronize your changes to your development environment on Kubernetes.

      From the console where you executed the okteto up command, cancel the execution of go run main.go by pressing CTRL + C. Now rerun the application:

      • default:hello-world /app> go run main.go

      Output

      Starting hello-world server...

      Go back to the browser and reload the page for your “Hello World” application.

      Hello world DigitalOcean

      Your code changes were applied instantly to Kubernetes, and all without requiring any commits, builds, or pushes.

      Conclusion

      Okteto transforms your Kubernetes cluster into a fully-featured development platform with the click of a button. In this tutorial you installed and configured the Okteto CLI to iterate your code changes directly on Kubernetes as fast as you can type code. Now you can head over to the Okteto samples repository to see how to use Okteto with different programming languages and debuggers.

      Also, if you share a Kubernetes cluster with your team, consider giving each member access to a secure Kubernetes namespace, configured to be isolated from other developers working on the same cluster. This great functionality is also provided by the Okteto App in the DigitalOcean Kubernetes Marketplace.



      Source link