1.操作系统初始化配置【所有节点】

# 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld

# 关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config  # 永久
setenforce 0  # 临时

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

# 根据规划设置主机名
hostnamectl set-hostname <hostname>
hostnamectl set-hostname k8s-master
hostnamectl set-hostname k8s-node1
hostnamectl set-hostname k8s-node2

# 在master添加hosts
cat >> /etc/hosts << EOF
192.168.59.208 k8s-master
192.168.59.210 k8s-node1
192.168.59.198 k8s-node2
EOF

# 将桥接的IPv4流量传递到iptables的链
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system  # 生效

# 时间同步
yum install ntpdate -y
ntpdate time.windows.com

2.安装Docker/kubeadm/kubelet【所有节点】

2.1安装docker

#配置yum源
sudo yum install -y yum-utils
sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo


yum install -y docker-ce-20.10.7
systemctl enable docker && systemctl start docker
#配置镜像加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://82m9ar63.mirror.aliyuncs.com"],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2"
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

2.2 安装cri-dockerd

Kubernetes v1.24移除docker-shim的支持,而Docker Engine默认又不支持CRI标准,因此二者默认无法再直接集成。为此,Mirantis和Docker联合创建了cri-dockerd项目,用于为Docker Engine提供一个能够支持到CRI规范的桥梁,从而能够让Docker作为Kubernetes容器引擎。

wget https://github.com/Mirantis/cri-dockerd/releases/download/v0.3.2/cri-dockerd-0.3.2-3.el7.x86_64.rpm

rpm -ivh cri-dockerd-0.3.2-3.el7.x86_64.rpm

指定依赖镜像地址

vi /usr/lib/systemd/system/cri-docker.service
ExecStart=/usr/bin/cri-dockerd --container-runtime-endpoint fd:// --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.9

systemctl daemon-reload 
systemctl enable cri-docker && systemctl start cri-docker

2.3 添加阿里云YUM软件源

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=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

2.4 安装kubeadm,kubelet和kubectl

yum install -y kubelet-1.28.0 kubeadm-1.28.0 kubectl-1.28.0
systemctl enable kubelet

3. 部署Kubernetes Master

#在192.168.59.208(Master)执行。
kubeadm init \
  --apiserver-advertise-address=192.168.59.208 \
  --image-repository registry.aliyuncs.com/google_containers \
  --kubernetes-version v1.28.0 \
  --service-cidr=10.96.0.0/12 \
  --pod-network-cidr=10.244.0.0/16 \
  --cri-socket=unix:///var/run/cri-dockerd.sock \
  --ignore-preflight-errors=all
  • --apiserver-advertise-address 集群通告地址
  • --image-repository 由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里指定阿里云镜像仓库地址
  • --kubernetes-version K8s版本,与上面安装的一致
  • --service-cidr 集群内部虚拟网络,Pod统一访问入口
  • --pod-network-cidr Pod网络,与下面部署的CNI网络组件yaml中保持一致
  • --cri-socket 指定cri-dockerd接口,如果是containerd则使用--cri-socket unix:///run/containerd/containerd.sock

初始化完成后,最后会输出一个join命令,先记住,下面加入节点会用到。

接着,拷贝kubectl使用的连接k8s认证文件到默认路径:

[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy

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.59.208:6443 --token 4lgxst.wp898nsxtpif2xuj \
	--discovery-token-ca-cert-hash sha256:3e64921dc88fdfc1add72bdf7da3a380b7b2fec6a47e8e7e0dce0950f9a2d932 

4. 加入Kubernetes Node

在192.168.59.210/192.168.59.198(Node)执行。

向集群添加新节点,执行在kubeadm init输出的kubeadm join命令并手动加上--cri-socket=unix:///var/run/cri-dockerd.sock:

kubeadm join 192.168.59.208:6443 --token 4lgxst.wp898nsxtpif2xuj \
	--discovery-token-ca-cert-hash sha256:3e64921dc88fdfc1add72bdf7da3a380b7b2fec6a47e8e7e0dce0950f9a2d932 --cri-socket=unix:///var/run/cri-dockerd.sock

为了安全性,上述命令中的token,默认有效期为24小时,当过期后,就无法使用该命令再加入节点了。需要重新生成加入节点命令:

kubeadm token create --print-join-command

查看工作节点

[root@k8s-master ~]# kubectl get nodes
NAME         STATUS     ROLES           AGE     VERSION
k8s-master   NotReady   control-plane   4m37s   v1.28.0
k8s-node1    NotReady   <none>          26s     v1.28.0
k8s-node2    NotReady   <none>          21s     v1.28.0

由于网络插件还没有部署,节点会处于NotReady状态

5.部署网络插件

kubectl create -f tigera-operator.yaml 
kubectl create -f custom-resources.yaml

 应用完成后等待几分钟,查看Pod运行状态

[root@k8s-master ~]# kubectl get pods -A
NAMESPACE          NAME                                       READY   STATUS    RESTARTS   AGE
calico-apiserver   calico-apiserver-6dddfdd797-v7jx6          1/1     Running   0          100s
calico-apiserver   calico-apiserver-6dddfdd797-xkc8b          1/1     Running   0          100s
calico-system      calico-kube-controllers-85955d4f5b-xrgsr   1/1     Running   0          5m54s
calico-system      calico-node-9mlvr                          1/1     Running   0          5m54s
calico-system      calico-node-r6tzz                          1/1     Running   0          5m54s
calico-system      calico-node-t2qrh                          1/1     Running   0          5m54s
calico-system      calico-typha-5547dfb9-52wn7                1/1     Running   0          5m46s
calico-system      calico-typha-5547dfb9-5tj4p                1/1     Running   0          5m54s
calico-system      csi-node-driver-bc6tj                      2/2     Running   0          5m54s
calico-system      csi-node-driver-hpxfw                      2/2     Running   0          5m54s
calico-system      csi-node-driver-tqvvx                      2/2     Running   0          5m54s
kube-system        coredns-66f779496c-jpgjx                   1/1     Running   0          26m
kube-system        coredns-66f779496c-t2jjn                   1/1     Running   0          26m
kube-system        etcd-k8s-master                            1/1     Running   0          27m
kube-system        kube-apiserver-k8s-master                  1/1     Running   0          27m
kube-system        kube-controller-manager-k8s-master         1/1     Running   0          27m
kube-system        kube-proxy-q2mbw                           1/1     Running   0          22m
kube-system        kube-proxy-rz9h9                           1/1     Running   0          23m
kube-system        kube-proxy-s2qqq                           1/1     Running   0          26m
kube-system        kube-scheduler-k8s-master                  1/1     Running   0          27m
tigera-operator    tigera-operator-774b5dd976-kqrlr           1/1     Running   0          5m58s

6. 部署 Dashboard

Dashboard是官方提供的一个UI,可用于基本管理K8s资源。

YAML下载地址:

https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml

默认Dashboard只能集群内部访问,修改Service为NodePort类型,暴露到外部: 

nodePort: 30001
type: NodePort

kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
spec:
  ports:
    - port: 443
      targetPort: 8443
      nodePort: 30001
  selector:
    k8s-app: kubernetes-dashboard
  type: NodePort

登录地址https://NodeIP:30001

https://192.168.59.210:30001/#/login

创建用户并授权

# 创建用户
kubectl create serviceaccount dashboard-admin -n kubernetes-dashboard
# 用户授权
kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kubernetes-dashboard:dashboard-admin
# 获取用户Token
kubectl create token dashboard-admin -n kubernetes-dashboard

进入页面

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐