Chuyển đến nội dung chính

Kubernetes: Deployment - Day 5

Kubernetes Deployment là gì?

Một định nghĩa từ trang vmware

A Kubernetes Deployment tells Kubernetes how to create or modify instances of the pods that hold a containerized application. Deployments can help to efficiently scale the number of replica pods, enable the rollout of updated code in a controlled manner, or roll back to an earlier deployment version if necessary. Kubernetes deployments are completed using kubectl, the command-line tool that can be installed on various platforms, including Linux, macOS, and Windows.

Tại sao phải dùng deployment?

Trong bài viết Kubernetes: Setup project ASP.NET core trên minikube - Day 4 , chúng ta đã xây dựng 1 kubernetes bao gồm 1 Pod và 1 NodePort. Pod là đơn vị nhỏ nhất của kubernetes, nhưng nó không có khả năng tự phục hồi khi bị lỗi hoặc bị xóa. Deployment cho phép bạn cập nhật, quay lại hoặc mở rộng các phiên bản ứng dụng một cách tự động. Ngoài ra, deployment trong kubernetes có tác dụng quản lý các replicaset và các pod để chạy các ứng dụng một cách ổn định và linh hoạt

Ví dụ

Quay lại ví dụ bài trước, chúng ta có cấu hình file Pod như sau
apiVersion: v1
kind: Pod
metadata:
  name: client-pod
  labels:
    component: web
spec:
  containers:
    - image: anbinhtrong/hostingmvc
      name: client
      imagePullPolicy: Always
      ports:
        - containerPort: 80

Để chuyển từ Pod sang Deployment, bạn cần thực hiện các bước sau:

  • Tạo một file deployment mới. File deployment sẽ mô tả các thông số của Pod, vd như số lượng Pod (replicas), image của container, label của Pod,...
  • Xóa Pod hiện tại.
  • Áp dụng deployment mới.
Chúng ta sẽ viết như sau
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    component: web
  name: client-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: web
  template:
    <pod-configuration>
Dựa vào cấu hình trên, chúng ta có file deployment như sau:
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    component: web
  name: client-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: web
  template:
    metadata:
      labels:
        component: web
    spec:
      containers:
      - image: anbinhtrong/hostingmvc
        name: client
        imagePullPolicy: Always
        ports:
          - containerPort: 80
Apply k8s
kubectl apply -f deployment.yml 
kubectl apply -f service.yml
Get log pod. Example: pod name = client-deployment-dbbdcd4f4-n42ms
kubectl get pod 
kubectl logs client-deployment-dbbdcd4f4-n42ms
Lấy thông tin service và địa chỉ cluster IP, port
minikube service client-node-port
Dựa trên kết quả xuất ra, chúng ta kiểm tra website có hoạt động hay không
curl http://192.168.49.2:31515
Tham khảo source code: Github

Tham khảo

Kubernetes Series - Bài 5 - Deployment: cập nhật ứng dụng bằng Deployment 

Deploy ASP.NET Core App on Kubernetes 

Nhận xét