Cài đặt Kubernetes trên CentOS 7 bằng kubeadm trong môi trường đơn máy chủ

Khởi tạo một máy chủ sạch sẽ

Môi trường chuẩn bị

-----------------------------------------------------------------------------------------

Để đảm bảo an toàn, trước tiên hãy thay đổi cổng SSH từ mặc định 22 thành một cổng khác

vi /etc/ssh/sshd_config

Sửa đổi giá trị Port và thêm cổng đã sửa vào chính sách bảo mật của máy chủ

Khởi động lại dịch vụ SSH

service sshd restart

----------------------------------------------------------------------------------------

Thiết lập tên máy chủ và thêm bản ghi giải quyết tên miền vào file hosts, sau đó khởi động lại để cấu hình có hiệu lực

hostnamectl set-hostname master
hostnamectl status<br></br>vi /etc/hosts<br></br>reboot

-----------------------------------------------------------------------------------------

Tắt tường lửa, SELinux và swap

systemctl stop firewalld
systemctl disable firewalld

sed -i 's/enforcing/disabled/' /etc/selinux/config 
setenforce 0

swapoff -a

------------------------------------------------------------------------------------------

Cấu hình bridge IPv4 truyền tải đến iptables:

cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

sysctl --system<br></br><br></br>yum update -y

------------------------------------------------------------------------------------------

Cài đặt Docker (không chỉ định phiên bản sẽ cài bản mới nhất, hiện tại là 20.10.17)

wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo

yum -y install docker-ce

systemctl enable docker && systemctl start docker

------------------------------------------------------------------------------------------

Cài đặt K8s

Thêm địa chỉ repo của Alibaba Cloud

cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

------------------------------------------------------------------------------------------

Cài đặt Kubeadm, Kubelet và Kubectl

*Lưu ý: do phiên bản cập nhật thường xuyên, nếu không chỉ định phiên bản thì sẽ cài bản mới nhất. Theo hướng dẫn chính thức cho phiên bản 1.24 như sau:

Ở đây, chúng ta dùng phiên bản 1.23.6 (do phiên bản 1.24.1 có nhiều vấn đề sau này)

yum install -y kubelet-1.23.6 kubeadm-1.23.6 kubectl-1.23.6

systemctl enable kubelet<br></br>

Khởi tạo bằng kubeadm

Ghi chú: Không thể truy cập địa chỉ ảnh mặc định k8s.gcr.io ở Trung Quốc nên cần thay thế bằng kho ảnh của Alibaba Cloud (nếu có môi trường ngoài mạng, xin đừng quan tâm đến đoạn này)``` kubeadm init --apiserver-advertise-address=192.168.0.3 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.23.6 --service-cidr=10.1.0.0/16 --pod-network-cidr=10.244.0.0/16


Quá trình chạy có thể gặp lỗi như dưới đây

[kubelet-check] It seems like the kubelet isn't running or healthy. [kubelet-check] The HTTP call equal to 'curl -sSL http://localhost:10248/healthz' failed with error: Get "http://localhost:10248/healthz": dial tcp 192.168.0.3:10248: connect: connection refused. [kubelet-check] The HTTP call equal to 'curl -sSL http://localhost:10248/healthz' failed with error: Get "http://localhost:10248/healthz": dial tcp 192.168.0.3:10248: connect: connection refused.

    Unfortunately, an error has occurred:
            timed out waiting for the condition

    This error is likely caused by:
            - The kubelet is not running
            - The kubelet is unhealthy due to a misconfiguration of the node in some way (required cgroups disabled)

    If you are on a systemd-powered system, you can try to troubleshoot the error with the following commands:
            - 'systemctl status kubelet'
            - 'journalctl -xeu kubelet'

    Additionally, a control plane component may have crashed or exited when started by the container runtime.
    To troubleshoot, list all containers using your preferred container runtimes CLI.

    Here is one example how you may list all Kubernetes containers running in docker:
            - 'docker ps -a | grep kube | grep -v pause'
            Once you have found the failing container, you can inspect its logs with:
            - 'docker logs CONTAINERID'

\------------------------------------------------------------------------------------------------------------------

Reset lại môi trường

kubeadm reset


\------------------------------------------------------------------------------------------------------------------

**Khắc phục sự cố**

Nguyên nhân: Driver cgroup trong Docker và kubelet không khớp nhau

Giải pháp:

1. Cập nhật cấu hình Docker

vi /etc/docker/daemon.json


Thêm nội dung sau

{ "exec-opts": ["native.cgroupdriver=systemd"] }


Khởi động lại Docker

systemctl daemon-reload

systemctl restart docker


2. Cập nhật cấu hình kubelet

vi /etc/systemd/system/kubelet.service.d/10-kubeadm.conf


Thêm dòng sau

Environment="KUBELET_KUBECONFIG_ARGS=--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf --cgroup-driver=cgroupfs"


Khởi động lại kubelet

systemctl daemon-reload

systemctl restart kubelet


Thực thi lại lệnh kubeadm init --XXXXXXXXXXXXXXX sẽ hoạt động tốt~

Khởi tạo thành công

\----------------------------------------------------------------------------------------------------------------------------

Sử dụng kubectl

mkdir -p $HOME/.kube

sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

sudo chown $(id -u):$(id -g) $HOME/.kube/config

kubectl get nodes


Trong môi trường đơn máy chủ, node master mặc định sẽ không cho phép phân bổ pod, cần thực hiện lệnh sau để đánh dấu master có thể nhận pod (xóa taint của master)

kubectl taint nodes --all node-role.kubernetes.io/master-


Kiểm tra trạng thái các pod trong tất cả namespace

kubectl get pods --all-namespaces -o wide


\--------------------------------------------------------------------------------------------------------------------------------------

**Triển khai Dashboard**

Truy cập https://github.com/kubernetes/dashboard/releases để kiểm tra phiên bản dashboard tương thích với phiên bản Kubernetes hiện tại

Kubernetes 1.23.6 tương ứng với phiên bản dashboard 2.5.1

Tải file cấu hình

wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.5.1/aio/deploy/recommended.yaml


Mặc định Dashboard chỉ có thể truy cập từ bên trong cụm, cần chỉnh sửa Service thành kiểu NodePort để truy cập từ bên ngoài (phần được đánh đỏ)

Phạm vi cổng: 30000-32767

kind: Service apiVersion: v1 metadata: labels: k8s-app: kubernetes-dashboard name: kubernetes-dashboard namespace: kube-system spec: type: NodePort ports: - port: 443 targetPort: 8443 nodePort: 32508 selector: k8s-app: kubernetes-dashboard


Triển khai dashboard

kubectl apply -f recommended.yaml


Xem lại danh sách pod

\-----------------------------------------------------------------------------------------------------------------------------------------

Tạo token đăng nhập

Tạo tài khoản admin-user cùng với quyền cấp cho nó

cat&gt;dashboard-adminuser.yml&lt;&lt;EOFapiVersion: v1kind: ServiceAccountmetadata: name: admin-user namespace: kube-system---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata: name: admin-user annotations: rbac.authorization.kubernetes.io/autoupdate: "true"roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-adminsubjects:- kind: ServiceAccount name: admin-user namespace: kube-systemEOFTạo đối tượng

kubectl create -f dashboard-adminuser.yml


Lấy token thông qua câu lệnh

kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')


Truy cập qua URL https://IP:Port

Thẻ: Kubernetes kubeadm CentOS docker dashboard

Đăng vào ngày 9 tháng 7 lúc 08:46