This article introduces how to quickly create a cluster instance using Kubernetes command line tools and complete a hello world practice.
The previous article discussed setting up a cluster with minikube, while this one will introduce operations using kubeadm.
Command Line Tools
There are three main tools, all prefixed with kube
.
kubeadm
: A command used to initialize the cluster.kubelet
: Used to start Pods and containers on each node in the cluster.kubectl
: The command line tool for communicating with the cluster.
Installing via Software Repository
Method 1
This method installs the toolkit via Google's repository.
Update the apt
package index and install the packages required for using the Kubernetes apt
repository:
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl
Download the Google Cloud public signing key:
sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
Add the Kubernetes apt
repository:
echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
Update the apt
package index, install kubelet, kubeadm, and kubectl, and hold their versions:
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
Method 2
Install the toolkit via Alibaba Cloud's repository.
Directly add the repository:
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -
Then update the software source and install these three tools.
apt-get update && apt-get install -y apt-transport-https curl
apt-get install -y kubelet kubeadm kubectl --allow-unauthenticated
Execute the command to check if it is functioning normally:
kubeadm --help
Installing Binary Files
Check the latest stable version of kubectl:
curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt
The currently queried stable version is v1.20.2.
Download the kubectl executable file, replacing the version number in the URL.
curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.20.2/bin/linux/amd64/kubectl
After downloading, there will be a kubectl file in the directory, approximately 40MB in size. Download speeds may be slower in China, and it may not be possible to download, so prepare accordingly.
You then need to grant executable permissions to it.
sudo chmod +x ./kubectl
To use kubectl directly, copy the file to the bin directory.
sudo mv ./kubectl /usr/local/bin/kubectl
Run the command to output the version number and check if the installation was successful.
kubectl version --client
Output:
Client Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.0", GitCommit:"af46c47ce925f4c4ad5cc8d1fca46c7b77d13b38", GitTreeState:"clean", BuildDate:"2020-12-08T17:59:43Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"linux/amd64"}
Quick Installation for Ubuntu & CentOS
This section introduces different installation methods for Ubuntu and CentOS, which can be ignored if the installation was successfully completed with the previous methods.
For Ubuntu and Debian systems, you can install via the software repository using the following commands:
sudo apt-get update && sudo apt-get install -y apt-transport-https gnupg2 curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
For CentOS, RHEL, and similar systems, you can install via the software repository using:
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
yum install -y kubelet kubeadm kubectl
Creating a Kubernetes Cluster
Having discussed how to install these three tools, if you don't have a server, we can use online servers provided by open-source organizations for testing.
Address: https://katacoda.com/
We will operate on the first node to create a master. Execute hostname -i
to check the IP of this node.
Kubeadm provides kubeadm init
and kubeadm join
, serving as a "shortcut" best practice for creating a Kubernetes cluster.
- Create the Master
We initialize an API Server service, binding the address to 192.168.0.8 (modify according to your IP). This step creates a master node.
Note: You may use kubeadm init
directly, which will automatically use the default network IP.
kubeadm init --apiserver-advertise-address 192.168.0.8
Once completed, you will see a prompt:
kubeadm join 192.168.0.8:6443 --token q25z3f.v5uo5bphvgxkjnmz \
--discovery-token-ca-cert-hash sha256:0496adc212112b5485d0ff12796f66b29237d066fbc1d4d2c5e45e6add501f64
Be sure to save this information for future use.
If prompted Alternatively, if you are the root user, you can run:
, then you will need to execute the following command.
export KUBECONFIG=/etc/kubernetes/admin.conf
- Initialize Cluster Network
Next, execute the initialization command to handle configuration. Note that you need admin.conf
to execute the command; otherwise, you will receive an error: The connection to the server localhost:8080 was refused - did you specify the right host or port?
.
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
Then execute the command to initialize.
kubectl apply -n kube-system -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"
If successful, you will see:
serviceaccount/weave-net created
clusterrole.rbac.authorization.k8s.io/weave-net created
clusterrolebinding.rbac.authorization.k8s.io/weave-net created
role.rbac.authorization.k8s.io/weave-net created
rolebinding.rbac.authorization.k8s.io/weave-net created
daemonset.apps/weave-net created
- Join the Cluster
Create a new node (another server) and execute hostname -i
to check the IP.
On the second node, execute the command to join the cluster. Please replace 192.168.0.8
in the command with the correct master node's IP.
kubeadm join 192.168.0.8:6443 --token q25z3f.v5uo5bphvgxkjnmz \
--discovery-token-ca-cert-hash sha256:0496adc212112b5485d0ff12796f66b29237d066fbc1d4d2c5e45e6add501f64
Then create the second and third nodes and execute the above kubeadm join
commands to join the cluster.
If you encounter failed to parse kernel config: unable to load kernel module
, it indicates that the Docker version is too high and needs to be downgraded.
Check the Docker version with: yum list installed | grep docker
and docker version
.
Downgrade the Docker version.
dnf remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine
dnf -y install dnf-plugins-core
dnf install docker-ce-18.06.3.ce-3.el7 docker-ce-cli containerd.io
If that doesn’t work, follow the instructions at https://docs.docker.com/engine/install/centos/ to install.
Note that docker version
may show a discrepancy between the client and server versions.
Cleaning up the Environment
If you make a mistake during the steps or want to start over, you can execute the kubeadm reset [flags]
command.
Note: Simply executing kubeadm reset
is ineffective.
[flags]
has four types:
preflight Run reset pre-flight checks
update-cluster-status Remove this node from the ClusterStatus object.
remove-etcd-member Remove a local etcd member.
cleanup-node Run cleanup node.
We need to execute:
kubeadm reset cleanup-node
kubeadm reset
文章评论