1. What is Kubernetes

  • Kubernetes is seen as a platform for deploying images
  • It can be used to schedule deployment images on multiple machines
  • In Kubernetes, you can use clusters to organize servers. One will exist in the clusterMasterNode, which is the control node of the Kubernetes cluster and is responsible for scheduling resources for other servers in the cluster. Other nodes are called nodes

2. Basic installation

  • Both Master and Node nodes need to be installed

2.1 Install required Components

  • Vim is a file editor for Linux
  • Wget can be used as a file download
  • Ntpdate can be used to synchronize time zones
yum install vim wget ntpdate -y
Copy the code

2.2 Disabling the Firewall

  • Kubernetes will create firewall rules, first closedfirewalld
systemctl stop firewalld & systemctl disable firewalld
Copy the code

2.3 Disabling a Swap Partition

  • Swap is a Linux Swap partition. When system resources are insufficient, Swap will be enabled, which is unnecessary
  • Instead of using Swap partitions, the newly created service should be automatically scheduled to other nodes in the cluster
# temporarily shut down Swapoff-aCopy the code

2.4 close the Selinux

  • Selinux is turned off to support container access to the host file system
# Temporarily disable Selinux SetenForce0/etc/sysconfig/selinux: disable selinux =disabledCopy the code

2.5 Unify our system time and time zone

  • usentpdateTo unify our system time and time zone, and align the server time with Ali Cloud server
# unified time zones for the Shanghai time zone ln - SNF/usr/share/zoneinfo/Asia/Shanghai/etc/localtime bash - c"echo 'Asia/Shanghai' > /etc/timezone"# Use ali server for time update ntpdate ntp1.aliyun.comCopy the code

2.6 installation Docker

  • In Kubernetes components, services can be Docker image deployment, so you need to install Docker
  • Device-mapper-persistent-data: Storage driver, many advanced volume management techniques on Linux
  • LVM: logical volume manager used to create logical disk partitions
yum install -y yum-utils device-mapper-persistent-data lvm2


sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install docker-ce -y
systemctl start docker
systemctl enable docker

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

sudo systemctl daemon-reload
sudo systemctl restart docker.service
Copy the code

2.7 Installing Kubernetes Components

2.7.1 Switching over Ali Cloud Source

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

2.7.2 Installing Kubernetes Components

  • Kubelet is the core component of Kubernetes. It runs on all nodes of the cluster and is responsible for creating the startup service container
  • Kubectl is the command line tool of Kubernetes. Can be used to manage, delete, create resources
  • Kubeadm is a tool used to initialize the cluster and add child nodes
Yum install -y kubelet kubeadm kubectl # yum install -y kubelet kubeadm kubectl #Copy the code

2.8 set up the bridge – nf – call – iptables

  • Configure kernel parameters to pass the bridged IPV4 browsing to the IPtables chain
  • Open the bridge – nf – call – iptables
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
Copy the code

3. Master

  • The Master node is the scheduling and primary node in the cluster

3.1 Changing the host name to master

hostnamectl set-hostname  master
Copy the code

3.2 configure hosts

ip addr
vim /etc/hosts

172.31178.169.  master  master
Copy the code

3.3 Configuring Kubernetes Initialization Files

  • init-defaultsOutput a default initialization configuration file
kubeadm config print init-defaults > init-kubeadm.conf
vim init-kubeadm.conf
Copy the code
  • Replace Kubernetes mirror warehouse for Ali cloud mirror warehouse, accelerate component pull
  • Replace the IP address with the host IP address
  • Set the POD network toflannelNetwork segment
  • To enable clusters to communicate with each other, you need to configure sub-networks, which will be used in the flannel network
    • 10.96.0.0/12 is the network required by Kubernetes internal network Pods
    • 10.244.0.0/16 is the network required by Kubernetes internal services
- imageRepository: k8s.gcr. IO replace k8s imageRepository
+ imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
- localAPIEndpointc, advertiseAddress is the master IP address. Port is not changed by default
localAPIEndpoint:
+ advertiseAddress: 172.31.178.169 # Indicates the IP address of the masterDnsDomain: cluster.local serviceSubnet: 10.96.0.0/12+ podSubnet: 10.244.0.0/16 # Add this
Copy the code

3.3 Pull other components

  • Kubeadm can be used to pull our default component image
  • Kube-apiserver provides interface services that allow external networks to access the cluster
  • Kube-controller-manager internal control instruction tool
  • Kube-scheduler Internal task scheduler
  • Kube-proxy Reverse proxy, load balancer, and traffic forwarding
  • Pause process management tool
  • Etcd maintains data consistency within the cluster
  • Coredns cluster Intranet communication

// View missing components
kubeadm config images list --config init-kubeadm.conf
// Pull the missing component
kubeadm config images pull --config init-kubeadm.conf
Copy the code

3.4 Initializing Kubernetes

kubeadm init --config init-kubeadm.conf
Copy the code
  • Kubeadm Join can quickly add nodes to the Master cluster

  • Initialization command for the Master node

  • Copy the default Kubernetes authentication file into the.kube folder to use the configuration file by default

Your Kubernetes control-plane has initialized successfully!

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/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 172.31178.169.:6443 --token abcdef.0123456789abcdef \
    --discovery-token-ca-cert-hash sha256:8aac19f4dbe68f1e15ba3d80e141acdc912e353f9757ad69187e8fb9780bc975 
Copy the code

3.5 installation Flannel

  • flannelIt is used to create a virtual network so that services on different nodes have globally unique IP addresses and can access and connect to each other.
  • Cluster Intranet communication protocol The Flannel protocol is used
#wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
wget http://img.golderbrother.cn/kube-flannel.yml
docker pull quay.io/coreos/flannel:v013.. 0-rc2
kubectl apply -f kube-flannel.yml
net-conf.json: |
    {
      "Network": "10.244.0.0/16"."Backend": {
        "Type": "vxlan"}}Copy the code

3.6 Viewing the Startup Status

kubectl get nodes

NAME     STATUS   ROLES                  AGE     VERSION
master   Ready    control-plane,master   9m34s   v120.4.
Copy the code

3.7 Node Node configuration

  • The Node Node is responsible for running the service container and receiving scheduling.
  • Perform basic installation first
hostnamectl set-hostname node1
Copy the code

3.8 Copying the Configuration File of the Master Node

  • Copy the configuration file of master node k8S to node1
scp $HOME/.kube/config root@172.31178.170.: ~ /Copy the code
  • Archive the configuration file on node1
mkdir -p $HOME/.kube
sudo mv $HOME/config $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Copy the code

3.9 Adding a Master Node

  • Add nodes to the master cluster
kubeadm join 172.1681.164.:6443 --token abcdef.0123456789abcdef \
    --discovery-token-ca-cert-hash sha256:b4a059eeffa2e52f2eea7a5d592be10c994c7715c17bda57bbc3757d4f13903d
Copy the code
  • If the command is lost, you can use kubeadm token create to generate a new command on the master machine

    kubeadm token create --print-join-command
    Copy the code

3.10. Installation of Flannel

scp ~/ kube - flannel. Yml [email protected]: ~ /
kubectl apply -f kube-flannel.yml
Copy the code

4. Check the status

kubectl get nodes
NAME     STATUS   ROLES                  AGE    VERSION
master   Ready    control-plane,master   24m    v120.4.
node1    Ready    <none>                 101s   v120.4.
Copy the code

5. Deploy nginx directly

kubectl create deployment nginx --image=nginx
[root@master ~]# kubectl expose deployment nginx --port=80 --type=NodePort
service/nginx exposed
kubectl get pod,svc
NAME                            READY   STATUS    RESTARTS   AGE
pod/nginx-6799fc88d8-bt5n6      1/1     Running   0          5m32s

curl 127.0. 01.:32636
// Rapidly expand the number of copies to three
[root@master ~]# kubectl scale deployment nginx --replicas=3
deployment.apps/nginx scaled
Copy the code

6. Deploy mysql through YAML

6.1 Configuration File

apiVersion: v1
kind: ReplicationController                           
metadata:
  name: mysql                                          
spec:
  replicas: 1           # Expected number of Pod copies
  selector:
    app: mysql          Pods that match the target have this tag
  template:             Create a Pod copy (instance) from this template
    metadata:
      labels:
        app: mysql     #Pod copy has a tag corresponding to RC Selector
    spec:
      containers:      # The defining part of the container inside Pod
      - name: mysql            The name of the container
        image: mysql    The Docker image of the container
        ports: 
        - containerPort: 3306       The container application listens to the port number
        env:                        Inject environment variables into the container
        - name: MYSQL_ROOT_PASSWORD 
          value: "123456"
Copy the code

6.2 create a POD

kubectl create -f mysql-rc.yaml
replicationcontroller/mysql created

kubectl get pods
NAME          READY   STATUS    RESTARTS   AGE
mysql         1/1     Running      0      5m56s
Copy the code

6.3 Viewing Status

kubectl describe pod mysql
Copy the code