Build a Kubernetes cluster based on the calico network plug-in

lab environment

IP Remarks system
192.168.1.10 master centos7
192.168.1.20 node1 centos7
192.168.1.30 node2 centos7

Version Information

  • docker 18.09.0
  • k8s v1.20.2

Change hostname

master

[root@localhost ~]# hostname master
[root@localhost ~]# bash
[root@master ~]#

node1

[root@localhost ~]# hostname node1
[root@localhost ~]# bash
[root@node1 ~]#

node2

[root@localhost ~]# hostname node2
[root@localhost ~]# bash
[root@node2 ~]#

Password-free login

master

[root@master ~]# vi /etc/hosts
#末尾添加
192.168.1.10 master
192.168.1.20 node1
192.168.1.30 node2

[root@master ~]# ssh-keygen  #一直回车
[root@master ~]# ssh-copy-id -i master
[root@master ~]# ssh-copy-id -i node1
[root@master ~]# ssh-copy-id -i node2

#将hosts文件传给node1和node2
[root@master ~]# scp /etc/hosts node1:/etc/hosts
hosts                                        100%  217    15.2KB/s   00:00    
[root@master ~]# scp /etc/hosts node2:/etc/hosts
hosts                                        100%  217    29.7KB/s   00:00  

Update and install dependencies

yum -y update
yum install -y conntrack ipvsadm ipset jq sysstat curl iptables libseccomp

Install docker

Install Docker on each machine, the version is 18.09.0

01 安装必要的依赖
	sudo yum install -y yum-utils device-mapper-persistent-data lvm2
 
02 设置docker仓库
	sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
	
【设置要设置一下阿里云镜像加速器】
登录阿里云 https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors 容器镜像服务中查看,参见下图
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
    
    
 "registry-mirrors": ["https://******.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload


03 安装docker

yum install -y docker-ce-18.09.0 docker-ce-cli-18.09.0 containerd.io


04 启动docker
	sudo systemctl start docker && sudo systemctl enable docker

Insert picture description here

System basic prerequisite configuration

# (1)关闭防火墙
systemctl stop firewalld && systemctl disable firewalld

# (2)关闭selinux
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config

# (3)关闭swap
swapoff -a
sed -i '/swap/s/^\(.*\)$/#\1/g' /etc/fstab

# (4)配置iptables的ACCEPT规则
iptables -F && iptables -X && iptables -F -t nat && iptables -X -t nat && iptables -P FORWARD ACCEPT

# (5)设置系统参数
cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

sysctl --system

Installing beadm, kubelet and kubectl

(1) Configure yum 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

(2) Install kubeadm&kubelet&kubectl

yum install -y kubeadm kubelet kubectl -y

(3) docker and k8s set the same cgroup

# docker
vi /etc/docker/daemon.json
    "exec-opts": ["native.cgroupdriver=systemd"],
    
systemctl restart docker
    
# kubelet,这边如果发现输出directory not exist,也说明是没问题的,大家继续往下进行即可
sed -i "s/cgroup-driver=systemd/cgroup-driver=cgroupfs/g" /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
	
systemctl enable kubelet && systemctl start kubelet

kube init initializes master

# 此操作只在master节点执行 查看日志,记录join信息
kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.20.2 --apiserver-advertise-address 192.168.1.10 --pod-network-cidr=10.244.0.0/16
#输出信息
·····
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 192.168.1.10:6443 --token 1ha7wh.gs6i1z30tpmdm3or \
    --discovery-token-ca-cert-hash sha256:06324a09f5e51f4a012f24041d94e73385fe0ac1a2e98cc114005cdc66c9cae2 
·····
【若要重新初始化集群状态:kubeadm reset,然后再进行上述操作】

Remember to save the last kubeadm join information

(3) According to the log prompt

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

Deploy calico network plug-in

Select network plugin: https://kubernetes.io/docs/concepts/cluster-administration/addons/
calico network plugin: https://docs.projectcalico.org/v3.9/getting-started/kubernetes/

calico, which also operates on the master node

# 在k8s中安装calico
kubectl apply -f https://docs.projectcalico.org/v3.9/manifests/calico.yaml

# 确认一下calico是否安装成功
kubectl get pods --all-namespaces -w

Insert picture description here

kube join

Remember to save the final print information of the initialization master node
(1) node1 and node2 are executed

kubeadm join 192.168.1.10:6443 --token 1ha7wh.gs6i1z30tpmdm3or \
    --discovery-token-ca-cert-hash sha256:06324a09f5e51f4a012f24041d94e73385fe0ac1a2e98cc114005cdc66c9cae2

(2) The master node checks the cluster information

[root@master ~]# kubectl get nodes
NAME     STATUS   ROLES                  AGE     VERSION
master   Ready    control-plane,master   5m35s   v1.20.2
node1    Ready    <none>                 64s     v1.20.2
node2    Ready    <none>                 61s     v1.20.2

Create pod test

cat > pod_nginx_rs.yaml <<EOF
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx
  labels:
    tier: frontend
spec:
  replicas: 3
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      name: nginx
      labels:
        tier: frontend
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
EOF

Deploy the yaml file

kubectl apply -f pod_nginx_rs.yaml

View pod

kubectl get pods
kubectl get pods -o wide
kubectl describe pod nginx

Expand pod through rs

kubectl scale rs nginx --replicas=5
kubectl get pods -o wide

Delete pod

kubectl delete -f pod_nginx_rs.yaml

Guess you like

Origin blog.csdn.net/weixin_46152207/article/details/113713358