一、搭建docker环境

参考:https://blog.peakliu.top/archives/1695043502167

二、搭建K8s

说明:实例使用三台服务器

ip

备注

k8s-master

192.168.3.236

主节点

k8s-node1

192.168.3.237

子节点

k8s-node2

192.168.3.238

子节点

1、环境准备(每台机器都要进行)

#各个机器设置自己的域名
hostnamectl set-hostname xxxx

# 将 SELinux 设置为 permissive 模式(相当于将其禁用)
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config

#关闭swap
swapoff -a  
sed -ri 's/.*swap.*/#&/' /etc/fstab

#允许 iptables 检查桥接流量
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF

cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

sudo sysctl --system

2、安装kubelet、kubeadm、kubectl(每台机器都要进行)

cat <<EOF | sudo tee /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
exclude=kubelet kubeadm kubectl
EOF

sudo yum install -y kubelet-1.20.9 kubeadm-1.20.9 kubectl-1.20.9 --disableexcludes=kubernetes

sudo systemctl enable --now kubelet

3、使用自制脚本批量拉去镜像(每台机器都要执行)

sudo tee ./images.sh <<-'EOF'
#!/bin/bash
images=(
kube-apiserver:v1.20.9
kube-proxy:v1.20.9
kube-controller-manager:v1.20.9
kube-scheduler:v1.20.9
coredns:1.7.0
etcd:3.4.13-0
pause:3.2
)
for imageName in ${images[@]} ; do
docker pull registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/$imageName
done
EOF
   
chmod +x ./images.sh && ./images.sh

4、添加主节点host配置(每台机器都要执行)

#所有机器添加master域名映射,以下需要修改为自己的
echo "192.168.3.236  cluster-endpoint" >> /etc/hosts

5、配置主节点信息(只需master执行)

(1)、执行初始化

kubeadm init \
--apiserver-advertise-address=192.168.3.236 \
--control-plane-endpoint=cluster-endpoint \
--image-repository registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images \
--kubernetes-version v1.20.9 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=172.31.0.0/16

注意:

  1. apiserver-advertise-address:使用master节点ip

  2. pod-network-cidr:设置一个非冲突的网段,如果集群的ip不是192.168开头的,则不需要修改

初始化之后可以看到如下信息

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/

You can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:

  kubeadm join cluster-endpoint:6443 --token exuuii.arnhjlyla4mg7b8r \
    --discovery-token-ca-cert-hash sha256:dbbcd40e4265c66fbcc911f0cc27526414fc0e9f7bdb8374d8ea94c1da4482f7 \
    --control-plane

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

kubeadm join cluster-endpoint:6443 --token exuuii.arnhjlyla4mg7b8r \
    --discovery-token-ca-cert-hash sha256:dbbcd40e4265c66fbcc911f0cc27526414fc0e9f7bdb8374d8ea94c1da4482f7

(2)、初始化配置(只需master执行)

根据上一步的提示,执行命令

 mkdir -p $HOME/.kube

 sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

 sudo chown $(id -u):$(id -g) $HOME/.kube/config

(3)、安装网络组件(只需master执行)

curl https://docs.projectcalico.org/v3.20/manifests/calico.yaml -O

注意:如果集群的ip为192.168开头的则需执行

vim calico.yaml

#通过/查找192.168,并打开注释
- name: CALICO_IPV4POOL_CIDR
  value: "192.168.0.0/16"
#将192.168修改为其他ip段,例如172.31
 value: "172.31.0.0/16"

安装calico.yaml

kubectl apply -f calico.yaml

6、node加入master节点(只需node执行)

根据第四步的内容,执行

kubeadm join cluster-endpoint:6443 --token exuuii.arnhjlyla4mg7b8r \
    --discovery-token-ca-cert-hash sha256:dbbcd40e4265c66fbcc911f0cc27526414fc0e9f7bdb8374d8ea94c1da4482f7

tips:重新生成新令牌kubeadm token create --print-join-command

7、验证集群

#查看所有pod,所有pod都是running才是正常的
kubectl get pods -A

#查看所有节点,所有节点都是ready才是正常的
kube get nodes

tips:如果某些pod一直在拉镜像,始终拉取不下来,可以手动拉取

#查看pod详细信息
kubectl describe pod pod名称 -n kube-system

#查看具体pod在机器编号
kubectl get pods -n kube-system -o wide

#在具体的机器上拉取镜像
docker pull xxx:版本号

再次查看pod是否启动成功

8、部署dashboard

(1)、安装必要组件

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.3.1/aio/deploy/recommended.yaml

(2)、设置类型

kubectl edit svc kubernetes-dashboard -n kubernetes-dashboard

type: ClusterIP 改为 type: NodePort

(3)、查看端口

#查看启动端口
kubectl get svc -A |grep kubernetes-dashboard

访问: https://集群任意IP:端口 https://192.168.3.236:3275

tips:如果提示不安全的访问,可以在空白位置输入thisisunsafe

(4)、创建访问账号

vim dash.yaml

填入如下内容

apiVersion: v1
kind: ServiceAccount
metadata:
  name: admin-user
  namespace: kubernetes-dashboard
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: admin-user
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: admin-user
  namespace: kubernetes-dashboard

执行安装

kubectl apply -f dash.yaml

(5)、生成token令牌

kubectl -n kubernetes-dashboard get secret $(kubectl -n kubernetes-dashboard get sa/admin-user -o jsonpath="{.secrets[0].name}") -o go-template="{{.data.token | base64decode}}"