Cách thiết lập Kubernetes Cluster trên Ubuntu 22.04

theanh

Administrator
Nhân viên
Kubernetes là một công cụ phối hợp container miễn phí giúp bạn đạt được triển khai, mở rộng và quản lý tự động các ứng dụng được chứa trong container. Một cụm Kubernetes bao gồm một nút Master và một nút Worker. Nút master chịu trách nhiệm quản lý các nút và pod trong cụm. Nút worker được sử dụng để triển khai khối lượng công việc của ứng dụng. Với Kubernetes, bạn có thể triển khai và quản lý các ứng dụng gốc trên đám mây bằng cơ sở hạ tầng tại chỗ hoặc nền tảng đám mây công cộng.

Hướng dẫn này sẽ chỉ cho bạn cách thiết lập cụm Kubernetes trên máy chủ Ubuntu 22.04.

Điều kiện tiên quyết​

  • Một máy chủ chạy Ubuntu 22.04.
  • Mật khẩu gốc được cấu hình trên máy chủ.

Bắt đầu​

Trước tiên, bạn sẽ cần cập nhật và nâng cấp các gói hệ thống của mình lên phiên bản mới nhất. Bạn có thể thực hiện bằng lệnh sau:
Mã:
apt update -y
apt upgrade -y
Sau khi tất cả các gói được cập nhật, bạn cũng sẽ cần phải tắt Swap trên hệ thống của mình. Bạn có thể vô hiệu hóa nó bằng lệnh sau:
Mã:
swapoff -a
Tiếp theo, bạn sẽ cần tải một số mô-đun hạt nhân cần thiết trên tất cả các nút. Để thực hiện, hãy chỉnh sửa tệp /etc/modules-load.d/containerd.conf:
Mã:
nano /etc/modules-load.d/containerd.conf
Thêm các dòng sau:
Mã:
overlay
br_netfilter
Lưu và đóng tệp, sau đó tải các mô-đun bằng lệnh sau lệnh:
Mã:
modprobe overlay
modprobe br_netfilter
Tiếp theo, bạn cũng sẽ cần tạo một tệp tốt và xác định một số tham số hạt nhân cần thiết:
Mã:
nano /etc/sysctl.d/kubernetes.conf
Thêm nội dung sau dòng:
Mã:
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
Lưu và đóng tệp, sau đó chạy lệnh sau để áp dụng các thay đổi:
Mã:
sysctl --system

Cài đặt Containerd​

Tiếp theo, bạn sẽ cần cài đặt Containerd cho cụm Kubernetes. Trước tiên, hãy cài đặt tất cả các phụ thuộc cần thiết bằng lệnh sau:
Mã:
apt install curl gnupg2 software-properties-common apt-transport-https ca-certificates -y
Sau khi cài đặt tất cả các phụ thuộc, hãy thêm kho lưu trữ Docker bằng lệnh sau:
Mã:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmour -o /etc/apt/trusted.gpg.d/docker.gpg
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Tiếp theo, hãy cập nhật kho lưu trữ và cài đặt Containerd bằng lệnh sau lệnh:
Mã:
apt update -y
apt install containerd.io -y
Tiếp theo, bạn sẽ cần cấu hình Container để nó khởi động bằng systemd. Bạn có thể thực hiện bằng lệnh sau:
Mã:
containerd config default | tee /etc/containerd/config.toml >/dev/null 2>&1
sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml
Tiếp theo, khởi động lại dịch vụ Containerd để áp dụng các thay đổi:
Mã:
systemctl restart containerd

Cài đặt các thành phần Kubernetes​

Theo mặc định, các thành phần Kubernetes không được bao gồm trong kho lưu trữ mặc định của Ubuntu. Vì vậy, bạn sẽ cần thêm kho lưu trữ Kubernetes vào hệ thống của mình. Bạn có thể thêm nó bằng lệnh sau:
Mã:
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"
Tiếp theo, hãy cập nhật kho lưu trữ và cài đặt tất cả các thành phần Kubernetes bằng lệnh sau:
Mã:
apt update -y
apt install kubelet kubeadm kubectl -y
Sau khi tất cả các gói được cài đặt, bạn có thể tiến hành bước tiếp theo.

Khởi tạo Kubernetes Cluster​

Tại thời điểm này, tất cả các thành phần Kubernetes đã được cài đặt. Bây giờ, hãy chạy lệnh sau trên nút chính để khởi tạo cụm:
Mã:
kubeadm init --control-plane-endpoint=kubernetes-master-ip
Bạn sẽ nhận được kết quả sau:
Mã:
To start using your cluster, you need to run the following as a regular user: mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
Alternatively, if you are the root user, you can run: export KUBECONFIG=/etc/kubernetes/admin.conf
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at: https://kubernetes.io/docs/concepts/cluster-administration/addons/
You can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root: kubeadm join kubernetes-master-ip:6443 --token chmz7m.fbjgdcqne1q0ff4t \	--discovery-token-ca-cert-hash sha256:c614bf14af27472e470546539a9a2ff63e5d558dbbb3cc06d6f7a030fcb55426 \	--control-plane
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join kubernetes-master-ip:6443 --token chmz7m.fbjgdcqne1q0ff4t \	--discovery-token-ca-cert-hash sha256:c614bf14af27472e470546539a9a2ff63e5d558dbbb3cc06d6f7a030fcb55426
Lưu ý: Sao chép lệnh kubeadm join từ đầu ra ở trên. Bạn sẽ cần chạy lệnh này trên nút Worker để join cụm. Tiếp theo, bạn sẽ cần chạy các lệnh sau để tương tác với cụm Kubernetes:
Mã:
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config

Kết nối nút Worker với cụm​

Tiếp theo, hãy đăng nhập vào nút Worker và chạy lệnh sau để kết nối nút Worker với Kubernetes cụm:
Mã:
kubeadm join kubernetes-master-ip:6443 --token chmz7m.fbjgdcqne1q0ff4t --discovery-token-ca-cert-hash sha256:c614bf14af27472e470546539a9a2ff63e5d558dbbb3cc06d6f7a030fcb55426
Bạn sẽ nhận được kết quả đầu ra sau:
Mã:
[preflight] Running pre-flight checks	[WARNING SystemVerification]: missing optional cgroups: blkio
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.
Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

Cài đặt tiện ích bổ sung Calico Pod Network​

Tiếp theo, bạn sẽ cần cài đặt Calico Pod Network trên nút Kubernetes Master để quản lý mạng.

Bạn có thể tải xuống và cài đặt nó bằng lệnh sau:
Mã:
curl https://projectcalico.docs.tigera.io/manifests/calico.yaml -O calico.yaml
kubectl apply -f calico.yaml
Bạn sẽ nhận được kết quả đầu ra sau:
Mã:
poddisruptionbudget.policy/calico-kube-controllers created
serviceaccount/calico-kube-controllers created
serviceaccount/calico-node created
configmap/calico-config created
customresourcedefinition.apiextensions.k8s.io/bgpconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/bgppeers.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/blockaffinities.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/caliconodestatuses.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/clusterinformations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/felixconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/globalnetworkpolicies.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/globalnetworksets.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/hostendpoints.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamblocks.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamconfigs.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamhandles.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ippools.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipreservations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/kubecontrollersconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/networkpolicies.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/networksets.crd.projectcalico.org created
clusterrole.rbac.authorization.k8s.io/calico-kube-controllers created
clusterrole.rbac.authorization.k8s.io/calico-node created
clusterrolebinding.rbac.authorization.k8s.io/calico-kube-controllers created
clusterrolebinding.rbac.authorization.k8s.io/calico-node created
Tiếp theo, xác minh trạng thái của pod bằng lệnh sau:
Mã:
kubectl get pods -n kube-system
Bạn sẽ nhận được thông báo sau đầu ra:
Mã:
NAME READY STATUS RESTARTS AGE
calico-kube-controllers-58dbc876ff-nh2st 1/1 Running 0 5m58s
calico-node-7cfz7 1/1 Running 0 5m58s
calico-node-lt5cv 1/1 Running 0 5m58s
coredns-565d847f94-dm6qc 1/1 Running 0 21m
coredns-565d847f94-zhng9 1/1 Running 0 21m
etcd-k8smaster.example.net 1/1 Running 0 22m
kube-apiserver-k8smaster.example.net 1/1 Running 0 22m
kube-controller-manager-k8smaster.example.net 1/1 Running 0 22m
kube-proxy-9w2xp 1/1 Running 0 14m
kube-proxy-gdb97 1/1 Running 0 21m
kube-scheduler-k8smaster.example.net 1/1 Running 0 22m
Bây giờ bạn có thể kiểm tra trạng thái của cụm Kubernetes bằng lệnh sau:
Mã:
kubectl get nodes
Bạn sẽ thấy cả hai nút Master và Worker đều ở trạng thái sẵn sàng trạng thái:
Mã:
NAME STATUS ROLES AGE VERSION
k8smaster.example.net Ready control-plane 22m v1.25.0
kubernetes Ready  14m v1.25.0

Triển khai ứng dụng Nginx trên Kubernetes​

Để kiểm tra Kubernetes, chúng tôi sẽ triển khai ứng dụng Nginx trên cụm.

Chạy lệnh sau để triển khai Nginx ứng dụng:
Mã:
kubectl create deployment nginx-app --image=nginx --replicas=2
Bạn có thể xác minh ứng dụng của mình bằng lệnh sau:
Mã:
kubectl get deployment nginx-app
Bạn sẽ nhận được thông tin sau đầu ra:
Mã:
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app 2/2 2 2 13s
Tiếp theo, hãy hiển thị ứng dụng của bạn trên cổng 80 bằng lệnh sau:
Mã:
kubectl expose deployment nginx-app --type=NodePort --port=80
Tiếp theo, hãy xác minh trạng thái dịch vụ Nginx bằng lệnh sau lệnh:
Mã:
kubectl get svc nginx-app
Bạn sẽ nhận được kết quả sau:
Mã:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-app NodePort 10.109.89.196  80:30921/TCP 14s
Bạn cũng có thể xem thông tin chi tiết về ứng dụng Nginx của mình bằng cách sử dụng lệnh sau lệnh:
Mã:
kubectl describe svc nginx-app
Bạn sẽ thấy kết quả sau:
Mã:
Name: nginx-app
Namespace: default
Labels: app=nginx-app
Annotations: Selector: app=nginx-app
Type: NodePort
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.109.89.196
IPs: 10.109.89.196
Port:  80/TCP
TargetPort: 80/TCP
NodePort:  30921/TCP
Endpoints: 192.168.192.129:80,192.168.192.130:80
Session Affinity: None
External Traffic Policy: Cluster
Events:
Bây giờ, hãy ghi lại địa chỉ IP của ứng dụng Nginx từ kết quả ở trên và xác minh ứng dụng Nginx của bạn bằng lệnh curl lệnh:
Mã:
curl http://10.109.89.196
Nếu mọi thứ đều ổn, bạn sẽ nhận được kết quả sau:
Mã:
Welcome to nginx!

html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }



[HEADING=1]Welcome to nginx![/HEADING]

If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.


For online documentation and support please refer to
[URL=http://nginx.org/]nginx.org[/URL].

Commercial support is available at
[URL=http://nginx.com/]nginx.com[/URL].


[I]Thank you for using nginx.[/I]
Xin chúc mừng! Bạn đã triển khai thành công cụm Kubernetes trên máy chủ Ubuntu 22.04.
 
Back
Bên trên