Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the posture of the simplest K8S deployment file?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

What is the most simple K8S deployment file writing posture? in order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

1. Headache writing K8S deployment file?

There are many parameters of K8S yaml, which need to be checked while writing.

How to set the number of rollback versions?

How to detect the success of start-up, how to detect life?

How to allocate and limit resources?

How do I set the time zone? Otherwise, printing the log is GMT standard time

How do I expose a service for other services to invoke?

How to configure horizontal scaling based on CPU and memory usage?

First of all, you need to know that there are these knowledge points, secondly, it is not easy to understand these knowledge points, and third, it is still easy to make mistakes every time!

two。 Create a service image

The previous article explained how to quickly create your own service image, but for demonstration purposes, we'll take the redis:6-alpine image as an example.

3. Complete K8S deployment file writing process

Install the goctl tool first

GO111MODULE=on GOPROXY= https://goproxy.cn/,direct go get-u github.com/tal-tech/go-zero/tools/goctl

Generate K8S deployment file with one click

Goctl kube deploy-name redis-namespace adhoc-image redis:6-alpine-o redis.yaml-port 6379

The generated yaml file is as follows:

ApiVersion: apps/v1kind: Deploymentmetadata: name: redis namespace: adhoc labels: app: redisspec: 3 revisionHistoryLimit: 5 selector: matchLabels: app: redis template: metadata: labels: app: redisspec: containers:-name: redis image: redis:6-alpine lifecycle: preStop: exec: command: ["sh", "- c" "sleep 5"] ports:-containerPort: 6379 readinessProbe: tcpSocket: port: 6379 initialDelaySeconds: 5 periodSeconds: 10 livenessProbe: tcpSocket: port: 6379 initialDelaySeconds: 15 periodSeconds: 20 resources: requests: cpu: 500m memory: 512Mi Limits: cpu: 1000m memory: 1024Mi volumeMounts:-name: timezone mountPath: / etc/localtime volumes:-name: timezone hostPath: path: / usr/share/zoneinfo/Asia/Shanghai---apiVersion: v1kind: Servicemetadata: name: redis-svc namespace: adhocspec: ports:-port: 6379 selector: app: redis ApiVersion: autoscaling/v2beta1kind: HorizontalPodAutoscalermetadata: name: redis-hpa-c namespace: adhoc labels: app: redis-hpa-cspec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: redis minReplicas: 3 maxReplicas: 10 metrics:-type: Resource resource: name: cpu targetAverageUtilization: 80---apiVersion: autoscaling/v2beta1kind: HorizontalPodAutoscalermetadata: name: redis-hpa-m namespace: adhoc labels: app: redis-hpa-mspec: scaleTargetRef: apiVersion: apps/v1 Kind: Deployment name: redis minReplicas: 3 maxReplicas: 10 metrics:-type: Resource resource: name: memory targetAverageUtilization: 80

Deployment service. If adhoc namespace does not exist, create it through kubectl create namespace adhoc first.

$kubectl apply-f redis.yamldeployment.apps/redis createdservice/redis-svc createdhorizontalpodautoscaler.autoscaling/redis-hpa-c createdhorizontalpodautoscaler.autoscaling/redis-hpa-m created

View service permission status

$kubectl get all-n adhocNAME READY STATUS RESTARTS AGEpod/redis-585bc66876-5ph36 1 6m5spod/redis-585bc66876-bfqxz 1 Running 0 6m5spod/redis-585bc66876-vvfc9 1 Running 0 6m5spod/redis-585bc66876-vvfc9 1 Running 0 6m5sNAME TYPE CLUSTER-IP EXTERNAL-IP PORT (S) AGEservice/redis-svc ClusterIP 172 .24.15.8 6379/TCP 6m5sNAME READY UP-TO-DATE AVAILABLE AGEdeployment.apps/redis 3/3 3 3 6m6sNAME DESIRED CURRENT READY AGEreplicaset.apps/redis-585bc66876 3 3 3 6m6sNAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGEhorizontalpodautoscaler.autoscaling/redis-hpa-c Deployment/redis 0% 6m6shorizontalpodautoscaler.autoscaling/redis-hpa-m Deployment/redis 80% 3 10 3 6m6s 0%

Test service

$kubectl run-I-tty-rm cli-image=redis:6-alpine-n adhoc-sh/data # redis-cli-h redis-svcredis-svc:6379 > set go-zero greatOKredis-svc:6379 > get go-zero "great"

The goctl tool greatly simplifies the writing of K8S yaml files, provides best practices out of the box, and supports template customization.

The answer to the question about the simplest K8S deployment document writing posture is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report