In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use Kubernetes". In daily operation, I believe many people have doubts about how to use Kubernetes. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Kubernetes"! Next, please follow the editor to study!
Install Skaffold
Prerequisites:
Make sure Minikube-- is installed (Skaffold can work with any Kubernetes cluster, I chose Minikube for simplicity)
Make sure kubectl is installed
Make sure you have installed Docker
After installing the above dependencies, visit the Skaffold distribution page, download the appropriate version of your system, and then add it to PATH.
Using Skaffold to develop a Node.js application
Let's get started by developing a simple Node.js application-without any magic, just a stupid Express-based HTTP server:
Const express = require ('express') const app = express () app.get (' /', function (req, res) {res.json ({status: 'ok'})}) app.listen (3000, err = > {if (err) {throw err} console.log (' server is listening')})
Next, create a Dockerfile to containerize the application:
FROM node:8.10.0-alpine WORKDIR / usr/src/app COPY package.json. COPY package-lock.json. RUN npm install COPY. . EXPOSE 3000 CMD node index.js
To run the application in Kubernetes, we create a deployment and expose the deployment through a service. To achieve this, I use:
# k8s-app.yml apiVersion: extensions/v1beta1 kind: Deployment metadata: name: node-app spec: replicas: 1 template: metadata: labels: app: node-app spec: containers:-name: node-app image: IMAGE_NAME ports:-containerPort: 3000-apiVersion: V1 kind: Service metadata: name: node-app labels: app: node-app spec: selector: app: node-app ports:-port: 3000 protocol: TCP nodePort: 30003 type: LoadBalancer
Now you can pass docker build. Command to create the containerized application. However, in this way, you will only build the image, which will not run the container in the Kubernetes cluster. This needs the help of Skaffold.
Skaffold uses YAML to describe the workflow. For the above applications, the file is as follows:
# skaffold.yaml apiVersion: skaffold/v1alpha1 kind: Config build: artifacts:-imageName: node-app workspace:. Local: {} deploy: kubectl: manifests:-paths:-k8s-app.yml parameters: IMAGE_NAME: node-app
As you can see, the configuration file has two main sections: the build section and the deployment section. In the build section, we can define the builds we want to create (mostly Docker images), and in the deploy section, we can define the resources we want to see in Kubernetes (such as services or deployments).
The paths array tells Skaffold,Kubernetes manifests the exact location, and with parameters, you can inject variables into manifest. For more details, please refer to the Skaffold annotation example.
Through the above steps, you have completed the configuration of Skaffold. If you have deployed your application using Kubernetes, you can simply reuse your Dockerfile and Kubernetes manifest by writing a yaml file for Skaffold.
Before running Skaffold, you need to start Minikube (using minikube start), and then run Skaffold:
Skaffold dev Starting build... Found minikube or Docker for Desktop context, using local docker daemon. Sending build context to Docker daemon 2.014MB Step 1 usr/src/app 8: FROM node:8.6.0-alpine-- > b7e15c83cdaf Step 2 usr/src/app 8: WORKDIR / usr/src/app-- > Using cache-- > e4cf80f4e3d6 Step 3 Rue 8: COPY package.json. -> Using cache-> 78f285cee4cb Step 4 Using cache 8: COPY package-lock.json. -> Using cache-- > 52c2cc2364fe Step 5 COPY 8: RUN npm install-> Using cache-- > f773a4b93a4b Step 6 8: COPY. . -> b0cc2a87fe89 Step 7 b0cc2a87fe89 Step 8: EXPOSE 3000-> Running in cd4d940ddaff-> e2f558c9f067 Step 8 Accord 8: CMD node index.js-> Running in 4752ba26ff2c-- > 5b62e8667662 Successfully built 5b62e8667662 Successfully tagged 71dba0517e741b4c8a11728cf905fe84:latest Successfully tagged node-app:5b62e86676627e49417af333b8da588b728bd3c9e5d777f6db5565d0e7a91015 Build complete. Starting deploy... Deploying K8sMurapp.yml. Deploy complete. [node-app-5d4df6585b-r87lk node-app] server is listening
Once your file is modified, Skaffold will automatically redeploy it to Kubernetes. You can access your service by executing minikube service [service-name]. In our example, it is minikube service node-app. This will open your default browser and access the Node.js application you just deployed.
At this point, the study on "how to use Kubernetes" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.