环境信息

Hyper-V 部署的麒麟 V10 SP1 最小化安装

主机 配置 地址
Master 2C4G 192.168.254.101
Node01 2C4G 192.168.254.102
Node02 2C4G 192.168.254.103

组件清单

主节点

  • docker:也可以用其他容器运行时
  • kubectl:集群命令行交互工具
  • kubeadm:集群初始化工具

    工作节点

  • docker:也可以用其他容器运行时
  • kubelet:管理 Pod 和容器,确保他们健康稳定运行
  • kube-proxy:网络代理,负责网络相关的工作

    创建集群

    https://jimmysong.io/kubernetes-handbook/practice/install-kubernetes-on-centos.html

    初始化系统

    设置主机名

    1
    2
    3
    4
    hostnamectl set-hostname devops
    hostnamectl set-hostname master
    hostnamectl set-hostname node01
    hostnamectl set-hostname node02

    修改 hosts

    1
    2
    3
    4
    5
    6
    7
    cat >> /etc/hosts <<EOF
    192.168.254.100 devops
    192.168.254.101 master
    192.168.254.102 node01
    192.168.254.103 node02
    EOF
    cat /etc/hosts

    关闭 SELinux

    1
    2
    setenforce 0
    sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux

    关闭防火墙

    1
    2
    systemctl stop firewalld
    systemctl disable firewalld

    关闭 SWAP

    1
    2
    3
    4
    5
    6
    #临时关闭
    swapoff -a
    #永久关闭
    vi /etc/fstab
    #注释掉以下字段
    /dev/mapper/centos-swap swap swap defaults 0 0

    时间同步

    1
    2
    3
    yum install -y ntpdate
    crontab -e
    */5 * * * * ntpdate ntp.aliyun.com
    1
    2
    3
    4
    #安装 epel-release 源,需要 RedHat 系
    yum install epel-release
    yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

    添加安装源

    国内镜像站
1
2
https://developer.aliyun.com/mirror/docker-ce/
https://mirrors.tuna.tsinghua.edu.cn/

Docker

1
2
3
4
5
6
7
8
cat << EOF > /etc/yum.repos.d/docker-ce.repo
[docker-ce-stable]
name=Docker CE Stable
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable
enabled=1
gpgcheck=0
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
EOF

Kubenetes

1
2
3
4
5
6
7
8
9
cat << EOF > /etc/yum.repos.d/kubernetes.repo
[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

安装组件

v1.24 版本及以上的 Kubernetes 放弃了原生 Docker 容器运行时的支持

1
2
3
4
5
6
7
yum list docker-ce.x86_64 --showduplicates | sort -r
yum install docker-ce-20.10.10
systemctl enable docker && systemctl start docker
docker info | grep Cgroup

yum list kube* --showduplicates | sort -r
yum install kubelet-1.23.6 kubectl-1.23.6 kubeadm-1.23.6

修改 Docker 配置

设置为 systemd,确保服务器节点在资源紧张的情况更加稳定

1
2
3
4
5
6
7
8
9
10
11
cat > /etc/docker/daemon.json << EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"registry-mirrors": ["https://ustc-edu-cn.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload & systemctl restart docker
docker info | grep Cgroup

#Kubernetes 官方推荐 Docker 等使用 systemd 作为 cgroupdriver,否则 kubelet 启动不了
systemctl enable kubelet && systemctl start kubelet

kubectl 命令补全

1
2
3
4
yum install -y bash-completion
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc

初始化集群

PS.如果没有关闭 SWAP,可以通过以下配置忽略 SWAP 报错

1
2
vi /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS="--fail-swap-on=false"

Master 节点

1
2
3
4
5
6
7
#初始化集群控制台 control-plane,失败了可以用 kubeadm reset 重置
kubeadm init --kubernetes-version v1.23.6 \
--apiserver-advertise-address=192.168.254.101 \
--image-repository registry.aliyuncs.com/google_containers \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16 \
--ignore-preflight-errors=Swap

参数说明:

  • —kubernetes-version # 指定 Kubernetes 版本
  • —apiserver-advertise-address # 指定 Master 主机的 IP
  • —image-repository # 指定阿里云镜像仓库地址
  • —service-cidr # 指定 service 网络段
  • —pod-network-cidr # 指定 pod 网络段
  • —ignore-preflight-errors=Swap # 忽略 Swap 报错信息,按需选择
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    [init] Using Kubernetes version: v1.23.6
    [preflight] Running pre-flight checks
    [preflight] Pulling images required for setting up a Kubernetes cluster
    [preflight] This might take a minute or two, depending on the speed of your internet connection
    [preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
    [certs] Using certificateDir folder "/etc/kubernetes/pki"
    [certs] Generating "ca" certificate and key
    [certs] Generating "apiserver" certificate and key
    [certs] apiserver serving cert is signed for DNS names [kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local master] and IPs [10.96.0.1 192.168.254.101]
    [certs] Generating "apiserver-kubelet-client" certificate and key
    [certs] Generating "front-proxy-ca" certificate and key
    [certs] Generating "front-proxy-client" certificate and key
    [certs] Generating "etcd/ca" certificate and key
    [certs] Generating "etcd/server" certificate and key
    [certs] etcd/server serving cert is signed for DNS names [localhost master] and IPs [192.168.254.101 127.0.0.1 ::1]
    [certs] Generating "etcd/peer" certificate and key
    [certs] etcd/peer serving cert is signed for DNS names [localhost master] and IPs [192.168.254.101 127.0.0.1 ::1]
    [certs] Generating "etcd/healthcheck-client" certificate and key
    [certs] Generating "apiserver-etcd-client" certificate and key
    [certs] Generating "sa" key and public key
    [kubeconfig] Using kubeconfig folder "/etc/kubernetes"
    [kubeconfig] Writing "admin.conf" kubeconfig file
    [kubeconfig] Writing "kubelet.conf" kubeconfig file
    [kubeconfig] Writing "controller-manager.conf" kubeconfig file
    [kubeconfig] Writing "scheduler.conf" kubeconfig file
    [kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
    [kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
    [kubelet-start] Starting the kubelet
    [control-plane] Using manifest folder "/etc/kubernetes/manifests"
    [control-plane] Creating static Pod manifest for "kube-apiserver"
    [control-plane] Creating static Pod manifest for "kube-controller-manager"
    [control-plane] Creating static Pod manifest for "kube-scheduler"
    [etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
    [wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
    [apiclient] All control plane components are healthy after 22.503431 seconds
    [upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
    [kubelet] Creating a ConfigMap "kubelet-config-1.23" in namespace kube-system with the configuration for the kubelets in the cluster
    NOTE: The "kubelet-config-1.23" naming of the kubelet ConfigMap is deprecated. Once the UnversionedKubeletConfigMap feature gate graduates to Beta the default name will become just "kubelet-config". Kubeadm upgrade will handle this transition transparently.
    [upload-certs] Skipping phase. Please see --upload-certs
    [mark-control-plane] Marking the node master as control-plane by adding the labels: [node-role.kubernetes.io/master(deprecated) node-role.kubernetes.io/control-plane node.kubernetes.io/exclude-from-external-load-balancers]
    [mark-control-plane] Marking the node master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
    [bootstrap-token] Using token: qwrpqf.tb10u9boslu6mzoj
    [bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
    [bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes
    [bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
    [bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
    [bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
    [bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
    [kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
    [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.254.101:6443 --token qwrpqf.tb10u9boslu6mzoj \
    --discovery-token-ca-cert-hash sha256:14f18211150be68eb24efdc0053a6fd9587da1b77ea0050074f0bb87544926a0
    后续修改网段
    然后等待拉取镜像,初始化完成以后按提示运行

    记住 kubeadm jion 192.168.1.101:6443 … 命令,以便后续 Node 节点加入集群
  • 配置 kubectl
    1
    2
    3
    4
    #复制授权文件,以便其他节点 kubectl 可以有权限访问集群
    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config
  • 添加网络组件

否则 NotReady
Flannel 是一个专门为 K8s 定制的网络解决方案,主要解决 Pod 跨主机通信问题

1
2
3
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f kube-flannel.yml
kubectl delete -f kube-flanneld.yaml


1
2
# 验证 flannel 网络插件是否部署成功(Running 即为成功)
kubectl get pods -n kube-system | grep flannel

Calico 包括原生 VXLAN 功能,无需 Flannel
1
2
curl https://raw.githubusercontent.com/projectcalico/calico/v3.24.5/manifests/canal.yaml -O
kubectl apply -f canal.yaml

Node 节点

  • 节点加入集群
    1
    2
    3
    kubeadm join 192.168.1.101:6443 \
    --token 7wor0d.7i31zm9hw330ay97 \
    --discovery-token-ca-cert-hash sha256:70970addce82343177ba403997644a2a38d7b8058724c5c22b8b1d5018c05560
  • 配置 kubectl
    1
    2
    3
    4
    5
    #复制授权文件,以便其他节点 kubectl 可以有权限访问集群
    mkdir -p $HOME/.kube
    #通过 cat /etc/kubernetes/admin.conf 拷贝 Master 节点内容
    vi $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config

    验证集群

    1
    2
    kubectl cluster-info
    kubectl version --short=true
    查看集群客户端和服务端程序版本信息

    查看集群信息

    管理集群

    界面

    Dashboard

    Angular + Go、单集群、K8s 资源管理
    1
    2
    3
    wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
    mv recommended.yaml kubernetes-dashboard.yaml
    kubectl apply -f kubernetes-dashboard.yaml

    Kuboard

    Vue、多集群、K8s 资源管理

    Kubevious

    React、多集群、K8s 资源管理、全资源检索、资源回滚

    KubeSphere

    React + Go、多集群、K8s 资源管理、CLI 安装、升级、KubeSphere Mini 入侵、应用商店

    KubeOperator

    Vue + Go、多集群、K8s 资源管理、Web 安装、升级 K8s

    命令行

    kubectl

    1
    2
    3
    4
    5
    kubectl get node
    kubectl get service -A
    kubectl get service -n kube-system
    kubectl get pod -A
    kubectl get pod -n kube-system

    自动补全

    1
    2
    3
    4
    yum install -y bash-completion
    source /usr/share/bash-completion/bash_completion
    source <(kubectl completion bash)
    echo "source <(kubectl completion bash)" >> ~/.bashrc

    YAML

    基本语法
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    apiVersion: v1 #必选,API 版本号
    kind: Deployment #必选,Pod 类型:
    metadata: #必选,元数据
    name: nginx-deployment #必选,符合 RFC1035 规范的 Pod 名称
    namespace: web #可选,不指定默认为 default,Pod 所在命名空间
    spec: #必选,用于定义详细信息
    selector: #标签选择器
    matchLabels:
    app: nginx
    replicas: 2 #必选,Pod 副本数量
    template:
    metadata: #必选,元数据
    labels: #可选,标签选择器,一般用于 Service Selector
    app: nginx
    spec: #必选,用于定义详细信息
    containers: #必选,定义容器
    - name: nginx
    image: nginx:1.14.2
    ports:
    - containerPort: 80

    管理 Job

    工作负载

  • Deployments:无状态化
  • StatefulSets:有状态化
  • DaemonSets:守护进程

    启动 Job

    1
    2
    3
    kubectl create
    kubectl run
    kubectl run demo --image=nginx:1.7.9 --replicas=2

    访问 Job

    1
    2
    3
    kubectl exec -it pod_name /bin/bash
    kubectl expose
    kubectl edit services

    扩容 Job

    1
    kubectl scale deployments --replicas= demo

    升级 Job

    1
    kubectl set image deployments demo demo=nginx:1.8.1

    其他方式

    Sealos

    安装 sealos

    1
    2
    wget https://github.com/labring/sealos/releases/download/v4.0.0/sealos_4.0.0_linux_amd64.tar.gz
    tar zxvf sealos_4.0.0_linux_amd64.tar.gz sealos && chmod +x sealos && mv sealos /usr/bin

    创建集群

  • 在线安装
    1
    2
    3
    4
    sealos run labring/kubernetes:v1.25.0 labring/calico:v3.24.1 \
    -m 192.168.254.101 \
    -n 192.168.254.102,192.168.254.103 \
    -u app -p '密码'
  • 离线安装

离线环境只需要提前导入镜像,其它步骤与在线安装一致。
首先在有网络的环境中 save 安装包:

1
2
sealos pull labring/kubernetes:v1.25.0
sealos save -o kubernetes.tar labring/kubernetes:v1.25.0

拷贝 kubernetes.tar 到离线环境, 使用 load 命令导入镜像即可:
1
sealos load -i kubernetes.tar

剩下的安装方式与在线安装一致。
1
2
sealos images # 查看集群镜像是否导入成功
sealos run kuberentes:v1.25.0 --single # 单机安装,集群安装同理

Kubesphere