Kubernetes 集群部署
# 一、环境信息
Hyper-V 部署的麒麟 V10 SP1 最小化安装
主机 | 配置 | 地址 |
---|---|---|
Master | 2C4G | 192.168.254.101 |
Node01 | 2C4G | 192.168.254.102 |
Node02 | 2C4G | 192.168.254.103 |
# 二、组件清单
# 1.主节点
docker:也可以用其他容器运行时 kubectl:集群命令行交互工具 kubeadm:集群初始化工具
# 2.工作节点
docker:也可以用其他容器运行时 kubelet:管理 Pod 和容器,确保他们健康稳定运行 kube-proxy:网络代理,负责网络相关的工作
# 三、创建集群
# 1.初始化系统
# 1.1.设置主机名
hostnamectl set-hostname devops
hostnamectl set-hostname master
hostnamectl set-hostname node01
hostnamectl set-hostname node02
1
2
3
4
2
3
4
# 1.2.修改 hosts
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
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 1.3.关闭 SELinux
setenforce 0
sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
1
2
2
# 1.4.关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
1
2
2
# 1.5.关闭 SWAP
#临时关闭
swapoff -a
#永久关闭
vi /etc/fstab
#注释掉以下字段
/dev/mapper/centos-swap swap swap defaults 0 0
1
2
3
4
5
6
2
3
4
5
6
# 1.6.时间同步
yum install -y ntpdate
crontab -e
*/5 * * * * ntpdate ntp.aliyun.com
1
2
3
2
3
#安装 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
3
4
2
3
4
# 2.添加安装源
国内镜像站
https://developer.aliyun.com/mirror/docker-ce/
https://mirrors.tuna.tsinghua.edu.cn/
1
2
2
# 2.1.Docker
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
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 2.2.Kubenetes
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
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 3.安装组件
v1.24 版本及以上的 Kubernetes 放弃了原生 Docker 容器运行时的支持
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
1
2
3
4
5
6
7
2
3
4
5
6
7
# 3.1.修改 Docker 配置
设置为 systemd,确保服务器节点在资源紧张的情况更加稳定
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
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 3.2.kubectl 命令补全
yum install -y bash-completion
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc
1
2
3
4
2
3
4
# 4.初始化集群
PS.如果没有关闭 SWAP,可以通过以下配置忽略 SWAP 报错
vi /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS="--fail-swap-on=false"
1
2
2
# 4.1.Master 节点
#初始化集群控制台 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
1
2
3
4
5
6
7
2
3
4
5
6
7
参数说明:
- --kubernetes-version #指定 Kubernetes 版本
- --apiserver-advertise-address #指定 Master 主机的IP
- --image-repository #指定阿里云镜像仓库地址
- --service-cidr #指定 service 网络段
- --pod-network-cidr #指定 pod 网络段
- --ignore-preflight-errors=Swap #忽略 Swap 报错信息,按需选择
[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
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
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
后续修改网段 (opens new window)
然后等待拉取镜像,初始化完成以后按提示运行
记住 kubeadm jion 192.168.1.101:6443 ... 命令,以便后续 Node 节点加入集群
- 配置 kubectl
#复制授权文件,以便其他节点 kubectl 可以有权限访问集群
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
1
2
3
4
2
3
4
- 添加网络组件 否则 NotReady Flannel (opens new window) 是一个专门为 K8s 定制的网络解决方案,主要解决 Pod 跨主机通信问题
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
3
2
3
# 验证 flannel 网络插件是否部署成功(Running 即为成功)
kubectl get pods -n kube-system | grep flannel
1
2
2
Calico 包括原生 VXLAN 功能,无需 Flannel
curl https://raw.githubusercontent.com/projectcalico/calico/v3.24.5/manifests/canal.yaml -O
kubectl apply -f canal.yaml
1
2
2
# 4.2.Node 节点
- 节点加入集群
kubeadm join 192.168.1.101:6443 \
--token 7wor0d.7i31zm9hw330ay97 \
--discovery-token-ca-cert-hash sha256:70970addce82343177ba403997644a2a38d7b8058724c5c22b8b1d5018c05560
1
2
3
2
3
- 配置 kubectl
#复制授权文件,以便其他节点 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
3
4
5
2
3
4
5
# 4.3.验证集群
kubectl cluster-info
kubectl version --short=true
1
2
2
查看集群客户端和服务端程序版本信息
查看集群信息
# 四、管理集群
# 1.命令行
# 1.1.kubectl
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
5
2
3
4
5
# 1.2.自动补全
yum install -y bash-completion
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc
1
2
3
4
2
3
4
# 2.Web 界面
# 2.1.Dashboard (opens new window)
Angular + Go、单集群、K8s 资源管理
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
1
2
3
2
3
# 2.2.Kuboard (opens new window)
Vue、多集群、K8s 资源管理
# 2.3.Kubevious (opens new window)
React、多集群、K8s 资源管理、全资源检索、资源回滚
# 2.4.KubeSphere (opens new window)
React + Go、多集群、K8s 资源管理、CLI 安装、升级、KubeSphere Mini 入侵、应用商店
# 2.5.KubeOperator (opens new window)
Vue + Go、多集群、K8s 资源管理、Web 安装、升级 K8s
# 五、其他方式
# 1.Sealos (opens new window)
# 1.1.安装 sealos
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
2
# 1.2.创建集群
- 在线安装
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 '密码'
1
2
3
4
2
3
4
- 离线安装 离线环境只需要提前导入镜像,其它步骤与在线安装一致。 首先在有网络的环境中 save 安装包:
sealos pull labring/kubernetes:v1.25.0
sealos save -o kubernetes.tar labring/kubernetes:v1.25.0
1
2
2
拷贝 kubernetes.tar 到离线环境, 使用 load 命令导入镜像即可:
sealos load -i kubernetes.tar
1
剩下的安装方式与在线安装一致。
sealos images # 查看集群镜像是否导入成功
sealos run kuberentes:v1.25.0 --single # 单机安装,集群安装同理
1
2
2
# 2.Kubesphere (opens new window)
网页编辑 (opens new window)
最近提交: 2023/03/22, 11:52:35