k8s[kubernetes] cluster construction (0>1)

Article Directory
1. Planning for building a K8S environment platform
2. Server hardware configuration requirements
3. Set up k8s cluster deployment method
4. Deployment in kubeadm mode – system initialization operation
5. Deployment in kubeadm mode – deploy master node
1Install Docker on all nodes
2Configure Alibaba Cloud Docker and kubernetes images on all nodes
on all nodes
3Install kubelet kubeadm kubectl on all nodes
4Deploy Kubernetes Master node
6. Deployment using kubeadm – deploying node nodes
5Add the Node node to the Kubernetes Master node
6Deploy CNI network plug-in
7Test kubernetes cluster
7. Commonly used commands of kubeadm

1. Build K8S environment platform planning


2. Server hardware configuration requirements
Test environment configuration requirements

Node Number of CPU cores Memory size Hard disk size
master 2 cores and above 4G and above 20GB and above
node 4 core and above 8G and above 40GB and above
Production environment configuration requirements

The configuration requirements are higher.

3. Set up k8s cluster deployment method
(1)kubeadm

It is a K8S deployment tool that provides kubeadm init and kubeadm join for rapid deployment of K8S clusters.

Reference link: Boot the cluster using kubeadm | Kubernetes

(2) Binary package

4. Deployment in kubeadm mode – system initialization operation
kubeadm is a tool launched by the official community for rapid deployment of kubernetes clusters. This tool can complete the deployment of a kubernetes cluster through two instructions:

First, create a Master node kubeadm init

Second, add the Node node to the current cluster kubeadm join

Environmental requirements

Worker node host name IP address system version memory CPU disk
master k8s-master 192.168.200.31 CentOS 7.9 2GB 2 core 30GB
node1 k8s-node1 192.168.200.32 CentOS 7.9 8GB 2 core 30GB
node2 k8s-node2 192.168.200.33 CentOS 7.9 8GB 2 core 30GB
Prepare CentOS 7.9 image to install three virtual machines
Hardware configuration requirements are as above
Three virtual machines can access each other
The virtual machine can access the external network and pull the image
Disable swap partition
Preparations before installation

1. Modify the host name
hostnamectl set-hostname k8s-master

2. Add host name
cat >> /etc/hosts << EOF
192.168.200.31 k8s-master
192.168.200.32 k8s-node1
192.168.200.33 k8s-node2
EOF

3. Turn off the firewall
systemctl stop firewalld & amp; & amp; systemctl disable firewalld
systemctl status firewalld

4. Close selinux
# Temporarily allowed
setenforce 0
getenforce

#Allow permanently
sed -i "s/SELINUX=enforcing/SELINUX=permissive/g" /etc/selinux/config
cat /etc/selinux/config

5. Close the swap partition
# Reference link: https://www.cnblogs.com/architectforest/p/12982886.html
# Check the version of swapoff
swapoff --version

# Temporarily closed?
swapoff -a

# Close permanently?
sed -ri 's/.*swap.*/# & amp;/' /etc/fstab # Restart to take effect

# Check using swapon
swapon -v #The output is empty, indicating that swap is closed

# Use free command to check
free -m

# Restart the swap partition
swapon-a

6. Configure network card for networking
cat /etc/sysconfig/network-scripts/ifcfg-ens32

7. Configure Alibaba Cloud image
cd /etc/yum.repos.d/ & amp; & amp; mkdir bak & amp; & amp; mv CentOS-* bak/
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

8. Generate local cache
yum makecache fast

9. Update YUM source software package
yum update -y

10. Pass bridged IPv4 traffic to the iptables chain
cat >> /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
# cat /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
[root@k8s-master ~]#

11. Set time synchronization between hosts
yum install -y ntpdate
ntpdate time.windows.com

5. Deployment in kubeadm mode – deploy master node
1Install Docker on all nodes

#yum installs gcc related environment (you need to ensure that the virtual machine can access the external network.)
yum install -y gcc & amp; & amp; yum install -y gcc-c + +

1. Uninstall the old version of docker
yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

2. Install the required dependency packages
yum install -y yum-utils


3. Set up Alibaba Cloud docker image
yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo # The default image source is foreign and is not recommended.

yum-config-manager \
    --add-repo \
     https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # It is recommended to use domestic mirrors


4. Install docker docker-ce ee enterprise version
yum install -y docker-ce docker-ce-cli containerd.io


5. Start Docker
systemctl start docker & amp; & amp; systemctl enable docker & amp; & amp; systemctl status docker


6. Check docker version information
docker version

2Configure Alibaba Cloud Docker and kubernetes images on all nodes

7. Configure Alibaba Cloud docker image acceleration
sudo mkdir -p /etc/docker

sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://w2kavmmf.mirror.aliyuncs.com"]
}
EOF

sudo systemctl daemon-reload & amp; & amp; systemctl restart docker

8. Configure Alibaba Cloud Kubernetes image
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

8. Configure Alibaba Cloud Kubernetes image


3Install kubelet kubeadm kubectl on all nodes

# Specify the K8S version to install. If you do not specify a version, the latest version will be installed by default.
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
systemctl enable kubelet

4Deploy Kubernetes Master node
The Alibaba Cloud image warehouse address is specified here. The default image address cannot be loaded and accessed.

kubeadm init \
  --apiserver-advertise-address=192.168.200.31 \
  --image-repository registry.aliyuncs.com/google_containers \
  --kubernetes-version v1.18.0 \
  --service-cidr=10.96.0.0/12 \
  --pod-network-cidr=10.244.0.0/16

To start using the cluster, run the following command as a normal user:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

[root@k8s-master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master NotReady master 6m8s v1.18.0
[root@k8s-master ~]#

6. Deployment using kubeadm – deploying node nodes
5Add the Node node to the Kubernetes Master node
Perform operations on the Node side.

Join any number of worker nodes by running the following on each root node:

kubeadm join 192.168.200.31:6443 --token 3myqeb.35plbttpfc0tjlvz \
    --discovery-token-ca-cert-hash sha256:b8378ad91dc3c88577869edd53937f0be1851ae972035b8449e4eae875ef2542

# The cluster status is NotReady, you need to add the CNI network plug-in
[root@k8s-master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master NotReady master 12m v1.18.0
k8s-node01 NotReady <none> 116s v1.18.0
k8s-node02 NotReady <none> 5s v1.18.0

# Check kubernetes version
[root@k8s-master ~]# kubectl version
Client Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.0", GitCommit:"9e991415386e4cf155a24b1da15becaa390438d8", GitTreeState:"clean", BuildDate :"2020-03-25T14:58:59Z", GoVersion:"go1.13.8", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.0", GitCommit:"9e991415386e4cf155a24b1da15becaa390438d8", GitTreeState:"clean", BuildDate :"2020-03-25T14:50:46Z", GoVersion:"go1.13.8", Compiler:"gc", Platform:"linux/amd64"}


The default token validity period is 24 hours. After expiration, the token cannot be used and the token needs to be re-created. The command is as follows:

kubeadm token create --print-join-command

6Deploy CNI network plug-in

wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

[root@k8s-master ~]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-7ff77c879f-9wt65 1/1 Running 0 26m
coredns-7ff77c879f-vf892 1/1 Running 0 26m
etcd-k8s-master 1/1 Running 0 26m
kube-apiserver-k8s-master 1/1 Running 0 26m
kube-controller-manager-k8s-master 1/1 Running 0 26m
kube-flannel-ds-65b8n 1/1 Running 0 4m22s
kube-flannel-ds-nx6gj 1/1 Running 0 4m22s
kube-flannel-ds-r6f25 1/1 Running 0 4m22s
kube-proxy-9mvdl 1/1 Running 0 26m
kube-proxy-pwd2b 1/1 Running 0 14m
kube-proxy-zslgz 1/1 Running 0 16m
kube-scheduler-k8s-master 1/1 Running 0 26m

Check whether the cluster node status is Ready

[root@k8s-master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master Ready master 28m v1.18.0
k8s-node01 Ready <none> 18m v1.18.0
k8s-node02 Ready <none> 16m v1.18.0

7Test the kubernetes cluster
Create a pod in the Kubernetes cluster and verify whether it is running normally:

# Pull nginx image
kubectl create deployment nginx --image=nginx

# nginx startup completed
# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-f89759699-r6j49 1/1 Running 0 88s

# Expose nginx port 80
kubectl expose deployment nginx --port=80 --type=NodePort

# View exposed port information
# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/nginx-f89759699-r6j49 1/1 Running 0 3m6s

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 37m
service/nginx NodePort 10.101.19.205 <none> 80:31814/TCP 26s

Access address: http://NodeIP:Port

kubectl create

The kubectl create command is used to create resources in Kubernetes. It can be used to create various resources including Pods, services, deployments, etc.

For example, to create a new deployment, you can:

kubectl create deployment my-deployment --image=my-image
kubectl create deployment <deployment-name> --image=<image-name>
Create a Deployment object, specify the container image name and deployment name.

kubectl create service <service-name> --tcp=<port>:<target-port>
Create a Service object, expose the container's port to the cluster, and map it to the specified port.

kubectl create configmap <configmap-name> --from-file=<path-to-file>
Create a ConfigMap object to store the application's configuration information.

kubectl create secret generic <secret-name> --from-literal=<key>=<value>
Create a Secret object to store sensitive information such as passwords.

kubectl create namespace <namespace-name>
Create a Namespace object for isolating and managing Kubernetes resources.

kubectl create job <job-name> --image=<image-name>
Create a Job object that runs one or more tasks in a Kubernetes cluster.

kubectl create cronjob <cronjob-name> --image=<image-name> --schedule=<cron-expression>
Create a CronJob object that runs one or more tasks periodically.
kubectl get

Used to retrieve information about Kubernetes resources. It can be used to retrieve information about various resources including Pods, Services, Deployments, etc.

For example, to retrieve information about all Pods in a Kubernetes cluster

kubectl get pods/deployments/svc/configmaps/secrets
kubectl describe

Used to retrieve detailed information about a specific Kubernetes resource. It can be used to retrieve information about various resources including pods, services, deployments, etc.

To retrieve detailed information about a specific container

kubectl describe pod <pod-name>
kubectl delete

Used to delete Kubernetes resources. It can be used to delete various resources including Pods, Services, Deployments

For example, to delete a specific Pod

kubectl delete pod my-pod

kubectl exec
The kubectl exec command is usedto execute commands in a running container. It can be used to execute commands in various containers, including Pods, Services, Deployments, etc.

For example, to execute a command in a running Pod

kubectl exec my-pod -- ls
kubectl exec -it <pod-name> /bin/bash/
kubectl logs

The kubectl logs command is used to retrieve logs from containers. It can be used to retrieve logs from various containers including Pods, Services, Deployments, etc.

kubectl logs my-pod
kubectl port-forward

Used to forward ports from various Pods, including Pods, services, deployments, etc.

For example, to forward port 8080 on the local machine to port 80 on the Pod

kubectl port-forward my-pod 8080:80
kubectl scale

Used to scale up or down Kubernetes resources. It can be used to scale various resources including deployments, replica sets

Used to scale up or down Kubernetes resources. It can be used to scale various resources including deployments, replica sets

kubectl scale deployment my-deployment --replicas=5
kubectl rollout

Used to manage the rollout of Kubernetes resources. It can be used to manage the rollout of various resources including deployments, replica sets

kubectl rollout status deployment/my-deployment
kubectl expose

Used to expose Kubernetes resources as services. It can be used to expose various resources, including Pods, deployments

For example, to expose a deployment as a service

kubectl expose deployment my-deployment --port=80 --target-port=8080
kubectl run

Used to create new Kubernetes resources. It can be used to create various resources, including Pod, deployment

kubectl run my-pod --image=my-image
kubectl config

For managing Kubernetes configuration. It can be used to manage various configurations including contexts, clusters

For example, to view the current context configuration

kubectl config current-context
kubectl cluster-info

The kubectl cluster-info command retrieves information about a Kubernetes cluster.
It can be used to retrieve various information including API server URL, Kubernetes version

For example, to retrieve information about a Kubernetes cluster

kubectl cluster-info
kubectl apply -dry-run

The kubectl apply --dry-run command simulates the application of changes to Kubernetes resources. It can be used to simulate changes to a variety of resources including Pods, Services, Deployments, and more.

kubectl apply -f deployment.yaml - dry-run
kubectl rollout undo

The kubectl rollout undo command undoes the rollout of Kubernetes resources. It can be used to undo rollouts of various resources including deployments, replica sets

kubectl rollout undo deployment/my-deployment
kubectl auth

The kubectl auth command manages Kubernetes authentication. It can be used to manage various authentication settings including roles, role bindings

kubectl auth can-i get pods --as my-user

kubectl top

The kubectl top command is used to retrieve resource usage metrics from Kubernetes resources. It can be used to retrieve metrics from various resources including nodes, pods, etc.

kubectl top pod my-pod
kubectl set

This command is used to update or modify the status of Kubernetes resources. This is an imperative command, meaning it directly instructs Kubernetes to perform an action rather than declaring a desired state.

kubectl set image: This subcommand is used to update the container image used by a deployment or Pod.
kubectl set env: This subcommand is used to update the environment variables of a Pod or deployment.
kubectl set resources: This subcommand is used to update resource requests and limits for a Pod or deployment.
kubectl set replicas: This subcommand is used to update the number of deployed replicas.

kubectl set image deployment/my-deployment my-container=new-image:latest

The knowledge points of the article match the official knowledge archives, and you can further learn relevant knowledge. Cloud native entry-level skills treeContainer orchestration (production environment k8s)kubelet, kubectl, kubeadm three-piece set 17031 people Currently studying the system