K8S Installation Tutorial on Ubuntu
[TOC]
1. Update System Sources
If the system's default mirror address is located abroad, downloads may be slow. You can open /etc/apt/sources.list
and replace it with domestic mirror sources.
apt upgrade
2. Update Software Packages
Update the system's software components to the latest stable version.
apt update
3. Install Docker
You can also refer to other methods for installation
apt-get install docker.io
If you need to configure it to start on boot, execute the following commands:
systemcd enable docker
systemcd start docker
To configure Docker image acceleration, open the /etc/docker/daemon.json
file, modify or add registry-mirrors
by including https://registry.docker-cn.com
, or you can fill in addresses from Alibaba Cloud or Tencent Cloud for image acceleration.
Example:
{
"registry-mirrors": [
"https://registry.docker-cn.com"
]
}
Restart Docker to apply the configuration:
sudo systemctl daemon-reload
sudo systemctl restart docker
4. Install K8S
Execute the following commands to install the https tools and k8s.
apt-get update && apt-get install -y apt-transport-https curl
apt-get install -y kubelet kubeadm kubectl --allow-unauthenticated
Run the following command to test if everything is normal:
kubeadm init
If there are messages like below during the installation, it indicates that the k8s packages cannot be found in the system's mirror source.
No apt package "kubeadm", but there is a snap with that name.
Try "snap install kubeadm"
No apt package "kubectl", but there is a snap with that name.
Try "snap install kubectl"
No apt package "kubelet", but there is a snap with that name.
Try "snap install kubelet"
You can open the /etc/apt/sources.list
file and add a line:
deb https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial main
Execute the k8s installation command again.
If you see:
The following signatures couldn't be verified because the public key is not available
Then execute the following command to add the key.
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -
The above command installs kubelet
, kubeadm
, and kubectl
. kubelet
is related to k8s services, kubectl
is the k8s management client, and kubeadm
is the deployment tool.
5. Initialization
Run the following command to initialize, which will automatically download the required Docker images from the network.
This command is used to deploy the master node.
Execute kubeadm version
to check the version; the version number is in GitVersion:"v1.17.2"
.
Initialize with the following command:
kubeadm init --pod-network-cidr=10.244.0.0/16 --ignore-preflight-errors=NumCPU
--ignore-preflight-errors=NumCPU
is used when there is only one CPU, for example, on a 1G1M student server.
However, since it needs to connect to Google, it may not be able to download content.
You can use the kubeadm config images list
command to list the images that need to be pulled. We can manually pull them using Docker. This process can be cumbersome and may require manually modifying the image names.
Pulling method: docker pull {image-name}
.
Although Google is not accessible, DockerHub has already backed up the required images.
The repository mirrorgooglecontainers
has the corresponding backup images. Unfortunately, the images are not guaranteed to be the latest backups. The google_containers
repository on Alibaba Cloud should have the latest backups.
For example, the following images are needed:
k8s.gcr.io/kube-apiserver:v1.17.2
k8s.gcr.io/kube-controller-manager:v1.17.2
k8s.gcr.io/kube-scheduler:v1.17.2
k8s.gcr.io/kube-proxy:v1.17.2
k8s.gcr.io/pause:3.1
k8s.gcr.io/etcd:3.4.3-0
k8s.gcr.io/coredns:1.6.5
Then you can pull the corresponding images:
docker pull mirrorgooglecontainers/kube-apiserver:v1.17.2
docker pull mirrorgooglecontainers/kube-controller-manager:v1.17.2
docker pull mirrorgooglecontainers/kube-scheduler:v1.15.0
docker pull mirrorgooglecontainers/kube-proxy:v1.17.2
docker pull mirrorgooglecontainers/pause:3.1
docker pull mirrorgooglecontainers/etcd:3.4.3-0
docker pull coredns/coredns:1.6.5
Use docker tag {old-name:version}:{new-name:version}
to rename the images.
Considering various situations that may arise, the author provides a one-click script written by someone else to complete this step directly.
touch pullk8s.sh # Create script file
nano pullk8s.sh # Edit the script
Then copy the following content into it:
for i in `kubeadm config images list`; do
imageName=${i#k8s.gcr.io/}
docker pull registry.aliyuncs.com/google_containers/$imageName
docker tag registry.aliyuncs.com/google_containers/$imageName k8s.gcr.io/$imageName
docker rmi registry.aliyuncs.com/google_containers/$imageName
done;
Save the file:
Ctrl + O
Enter key
Ctrl + x
Grant permissions to the script file:
chmod +x pullk8s.sh
Execute the script:
sh pullk8s.sh
Then run the docker images
command to check if all required images are prepared:
root@instance-wxxixh4k:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
k8s.gcr.io/kube-proxy v1.17.2 cba2a99699bd 2 weeks ago 116MB
k8s.gcr.io/kube-apiserver v1.17.2 41ef50a5f06a 2 weeks ago 171MB
k8s.gcr.io/kube-controller-manager v1.17.2 da5fd66c4068 2 weeks ago 161MB
k8s.gcr.io/kube-scheduler v1.17.2 f52d4c527ef2 2 weeks ago 94.4MB
k8s.gcr.io/coredns 1.6.5 70f311871ae1 3 months ago 41.6MB
k8s.gcr.io/etcd 3.4.3-0 303ce5db0e90 3 months ago 288MB
k8s.gcr.io/pause 3.1 da86e6ba6ca1 2 years ago 742kB
Finally, execute the initialization command at the start.
If all else fails, you can try the installation tutorial at https://learnku.com/articles/29209.
6. Make Master Node Effective
Add the environment variable:
export KUBECONFIG=/etc/kubernetes/admin.conf
Add the network plugin:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
Run the pre-written yaml configuration file:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
You can also write your own yaml for this step.
After executing, you may see:
podsecuritypolicy.policy/psp.flannel.unprivileged created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.apps/kube-flannel-ds-amd64 created
daemonset.apps/kube-flannel-ds-arm64 created
daemonset.apps/kube-flannel-ds-arm created
daemonset.apps/kube-flannel-ds-ppc64le created
daemonset.apps/kube-flannel-ds-s390x created
Save and use configuration
kubeadm join
7. View Nodes
Execute the following command to查看 View Nodes
kubectl get nodes
Example result:
root@instance-wxxixh4k:~# kubectl get nodes
NAME STATUS ROLES AGE VERSION
instance-wxxixh4k Ready master 9m23s v1.17.2
This indicates that the setup is successful, and nodes can be added or removed.
8. Install Dashboard
Download yaml configuration file
wget https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/alternative/kubernetes-dashboard.yaml
Use cat kubernetes-dashboard.yaml
command to view the contents of the yaml file and note the version number.
The file contains statements like the following, where the number behind indicates the version:
image: k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1
Pull the kubernetes-dashboard
image (note to modify the version number afterward):
docker pull registry.aliyuncs.com/google_containers/kubernetes-dashboard-amd64:v1.10.1
docker tag registry.aliyuncs.com/google_containers/kubernetes-dashboard-amd64:v1.10.1 k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1
docker rmi registry.aliyuncs.com/google_containers/kubernetes-dashboard-amd64:v1.10.1
Install kubernetes-dashboard
kubectl create -f kubernetes-dashboard.yaml
Check installation results
kubectl get pod --namespace=kube-system
Configure the dashboard and add Admin account and permissions
Copy and paste the following content into the terminal and press Enter:
cat <<EOF > dashboard-admin.yaml
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: kubernetes-dashboard
labels:
k8s-app: kubernetes-dashboard
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: kubernetes-dashboard
namespace: kube-system
EOF
The above code creates a dashboard-admin.yaml
file.
Execute the installation
kubectl create -f dashboard-admin.yaml
View service ports
kubectl get svc --namespace=kube-system
Find the node named kubernetes-dashboard
and record the port.
View pod names
kubectl get pod --namespace=kube-system
Among them, there is one starting with kubernetes-dashboard
, for example, kubernetes-dashboard-6bf999dbcc-nc4hq
, record the name.
9. Access Dashboard
If the following methods do not work, please refer to
https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/
Method 1: kubectl proxy
Execute:
kubectl proxy
Within the internal network, you can access it via the following address:
http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/
Method 2: API Server
Access method:
This will not be elaborated further; please refer to online materials for details.
Method 3: Directly expose port (NodePort)
Not recommended.
Execute:
kubectl -n kube-system edit service kubernetes-dashboard
Find type: ClusterIP
and change it to type: NodePort
.
Additionally, you may change the port
as well.
Tip:
Use the arrow keys to move to the appropriate position; use the Delete
key to delete characters.
Then press Esc
, followed by i
to enter editing mode.
Then press Esc
and Shift + q
, it will prompt for input; enter wq!
to save and exit.
Execute kubectl -n kube-system get service kubernetes-dashboard
to see the port mapping.
For example:
80:31901/TCP
Then the access method is https://{ip}:31901
Method 4:
kubectl port-forward kubernetes-dashboard-6bf999dbcc-nc4hq 8080:80 --namespace=kube-system &
10. Add Nodes for Testing
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=8090 --type=NodePort
You can access Nginx via the public network at port 8090.
文章评论