使用 kubeadm 搭建 Kubernetes 高可用集群
本文记录了如何使用 kubeadm 工具搭建高可用 Kubernetes 集群,主要通过 keepalived+haproxy 组合实现 master 节点的高可用架构,整个部署基于 CentOS 7 系统,Kubernetes 版本为 1.16.3。
kubeadm 是 Kubernetes 官方推出的集群部署工具,通过简单的命令即可快速搭建生产级别的 Kubernetes 集群。
1. 环境要求
- 操作系统: CentOS 7.x-86_x64
- 硬件配置: 2GB+ RAM,2+ CPU,30GB+ 磁盘
- 网络要求: 能够访问外网拉取镜像
- 其他要求: 禁用 swap 分区
2. 集群规划
角色 | IP 地址 | 备注 |
---|---|---|
master1 | 192.168.44.155 | 主控制节点 |
master2 | 192.168.44.156 | 备用控制节点 |
node1 | 192.168.44.157 | 工作节点 |
VIP | 192.168.44.158 | 虚拟 IP |
3. 基础环境配置
在所有节点执行以下操作:
bash
# 关闭防火墙和 SELinux
systemctl stop firewalld && systemctl disable firewalld
sed -i 's/enforcing/disabled/' /etc/selinux/config && setenforce 0
# 关闭 swap
swapoff -a && sed -ri 's/.*swap.*/#&/' /etc/fstab
# 配置主机名和 hosts 文件
hostnamectl set-hostname <对应主机名>
cat >> /etc/hosts << EOF
192.168.44.158 master.k8s.io
192.168.44.155 master01.k8s.io
192.168.44.156 master02.k8s.io
192.168.44.157 node01.k8s.io
EOF
# 配置内核参数
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
# 关闭防火墙和 SELinux
systemctl stop firewalld && systemctl disable firewalld
sed -i 's/enforcing/disabled/' /etc/selinux/config && setenforce 0
# 关闭 swap
swapoff -a && sed -ri 's/.*swap.*/#&/' /etc/fstab
# 配置主机名和 hosts 文件
hostnamectl set-hostname <对应主机名>
cat >> /etc/hosts << EOF
192.168.44.158 master.k8s.io
192.168.44.155 master01.k8s.io
192.168.44.156 master02.k8s.io
192.168.44.157 node01.k8s.io
EOF
# 配置内核参数
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
4. 配置高可用负载均衡
4.1 安装 keepalived
在两台 master 节点安装 keepalived:
bash
yum install -y keepalived
yum install -y keepalived
master1 配置 (/etc/keepalived/keepalived.conf
):
bash
cat > /etc/keepalived/keepalived.conf <<EOF
global_defs {
router_id k8s
}
vrrp_script check_haproxy {
script "killall -0 haproxy"
interval 3
weight -2
fall 10
rise 2
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 250
advert_int 1
authentication {
auth_type PASS
auth_pass ceb1b3ec013d66163d6ab
}
virtual_ipaddress {
192.168.44.158
}
track_script {
check_haproxy
}
}
EOF
cat > /etc/keepalived/keepalived.conf <<EOF
global_defs {
router_id k8s
}
vrrp_script check_haproxy {
script "killall -0 haproxy"
interval 3
weight -2
fall 10
rise 2
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 250
advert_int 1
authentication {
auth_type PASS
auth_pass ceb1b3ec013d66163d6ab
}
virtual_ipaddress {
192.168.44.158
}
track_script {
check_haproxy
}
}
EOF
master2 配置:同 master1,但修改 state BACKUP
和 priority 200
。
启动 keepalived:
bash
systemctl enable keepalived && systemctl start keepalived
systemctl enable keepalived && systemctl start keepalived
4.2 安装 HAProxy
在两台 master 节点安装并配置 HAProxy:
bash
yum install -y haproxy
cat > /etc/haproxy/haproxy.cfg << EOF
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
frontend kubernetes-apiserver
mode tcp
bind *:16443
option tcplog
default_backend kubernetes-apiserver
backend kubernetes-apiserver
mode tcp
balance roundrobin
server master01.k8s.io 192.168.44.155:6443 check
server master02.k8s.io 192.168.44.156:6443 check
listen stats
bind *:1080
stats auth admin:awesomePassword
stats refresh 5s
stats realm HAProxy\ Statistics
stats uri /admin?stats
EOF
systemctl enable haproxy && systemctl start haproxy
yum install -y haproxy
cat > /etc/haproxy/haproxy.cfg << EOF
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
frontend kubernetes-apiserver
mode tcp
bind *:16443
option tcplog
default_backend kubernetes-apiserver
backend kubernetes-apiserver
mode tcp
balance roundrobin
server master01.k8s.io 192.168.44.155:6443 check
server master02.k8s.io 192.168.44.156:6443 check
listen stats
bind *:1080
stats auth admin:awesomePassword
stats refresh 5s
stats realm HAProxy\ Statistics
stats uri /admin?stats
EOF
systemctl enable haproxy && systemctl start haproxy
5. 安装容器运行时和 Kubernetes 组件
在所有节点执行:
5.1 安装 Docker
bash
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum install -y docker-ce-18.06.1.ce-3.el7
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF
systemctl enable docker && systemctl start docker
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum install -y docker-ce-18.06.1.ce-3.el7
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF
systemctl enable docker && systemctl start docker
5.2 安装 kubeadm、kubelet、kubectl
bash
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
yum install -y kubelet-1.16.3 kubeadm-1.16.3 kubectl-1.16.3
systemctl enable kubelet
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
yum install -y kubelet-1.16.3 kubeadm-1.16.3 kubectl-1.16.3
systemctl enable kubelet
6. 初始化 Kubernetes 集群
6.1 创建集群配置文件
在 master1 节点创建 kubeadm 配置文件:
bash
mkdir -p /usr/local/kubernetes/manifests && cd /usr/local/kubernetes/manifests
cat > kubeadm-config.yaml << EOF
apiServer:
certSANs:
- master1
- master2
- master.k8s.io
- 192.168.44.158
- 192.168.44.155
- 192.168.44.156
- 127.0.0.1
extraArgs:
authorization-mode: Node,RBAC
timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta1
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controlPlaneEndpoint: "master.k8s.io:16443"
controllerManager: {}
dns:
type: CoreDNS
etcd:
local:
dataDir: /var/lib/etcd
imageRepository: registry.aliyuncs.com/google_containers
kind: ClusterConfiguration
kubernetesVersion: v1.16.3
networking:
dnsDomain: cluster.local
podSubnet: 10.244.0.0/16
serviceSubnet: 10.1.0.0/16
scheduler: {}
EOF
mkdir -p /usr/local/kubernetes/manifests && cd /usr/local/kubernetes/manifests
cat > kubeadm-config.yaml << EOF
apiServer:
certSANs:
- master1
- master2
- master.k8s.io
- 192.168.44.158
- 192.168.44.155
- 192.168.44.156
- 127.0.0.1
extraArgs:
authorization-mode: Node,RBAC
timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta1
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controlPlaneEndpoint: "master.k8s.io:16443"
controllerManager: {}
dns:
type: CoreDNS
etcd:
local:
dataDir: /var/lib/etcd
imageRepository: registry.aliyuncs.com/google_containers
kind: ClusterConfiguration
kubernetesVersion: v1.16.3
networking:
dnsDomain: cluster.local
podSubnet: 10.244.0.0/16
serviceSubnet: 10.1.0.0/16
scheduler: {}
EOF
6.2 初始化第一个 master 节点
bash
kubeadm init --config kubeadm-config.yaml
# 配置 kubectl
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
kubeadm init --config kubeadm-config.yaml
# 配置 kubectl
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
记录输出的 join 命令,后续加入节点时使用。
6.3 安装网络插件
bash
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f kube-flannel.yml
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f kube-flannel.yml
7. 添加第二个 master 节点
7.1 复制证书文件
从 master1 复制必要文件到 master2:
bash
ssh root@192.168.44.156 mkdir -p /etc/kubernetes/pki/etcd
scp /etc/kubernetes/admin.conf root@192.168.44.156:/etc/kubernetes
scp /etc/kubernetes/pki/{ca.*,sa.*,front-proxy-ca.*} root@192.168.44.156:/etc/kubernetes/pki
scp /etc/kubernetes/pki/etcd/ca.* root@192.168.44.156:/etc/kubernetes/pki/etcd
ssh root@192.168.44.156 mkdir -p /etc/kubernetes/pki/etcd
scp /etc/kubernetes/admin.conf root@192.168.44.156:/etc/kubernetes
scp /etc/kubernetes/pki/{ca.*,sa.*,front-proxy-ca.*} root@192.168.44.156:/etc/kubernetes/pki
scp /etc/kubernetes/pki/etcd/ca.* root@192.168.44.156:/etc/kubernetes/pki/etcd
7.2 加入集群
在 master2 节点执行 join 命令(添加 --control-plane
参数):
bash
kubeadm join master.k8s.io:16443 --token <token> \
--discovery-token-ca-cert-hash sha256:<hash> \
--control-plane
kubeadm join master.k8s.io:16443 --token <token> \
--discovery-token-ca-cert-hash sha256:<hash> \
--control-plane
8. 添加工作节点
在 node1 节点执行 join 命令(不需要 --control-plane
参数):
bash
kubeadm join master.k8s.io:16443 --token <token> \
--discovery-token-ca-cert-hash sha256:<hash>
kubeadm join master.k8s.io:16443 --token <token> \
--discovery-token-ca-cert-hash sha256:<hash>
9. 验证集群
创建测试应用验证集群功能:
bash
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pods,svc
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pods,svc
通过 http://<任意节点 IP>:<NodePort>
访问 nginx 服务。
10. 常用维护命令
bash
# 查看集群状态
kubectl get nodes
kubectl get cs
# 查看所有 Pod
kubectl get pods --all-namespaces
# 重新生成 join token
kubeadm token create --print-join-command
# 查看集群信息
kubectl cluster-info
# 查看集群状态
kubectl get nodes
kubectl get cs
# 查看所有 Pod
kubectl get pods --all-namespaces
# 重新生成 join token
kubeadm token create --print-join-command
# 查看集群信息
kubectl cluster-info
评论
暂无评论,来发表第一条评论吧