对于刚接触到k8s的同学来说可能有熟悉过其基本概念,但是总在安装集群这里卡壳,我写这篇文章的目的是快速的构建一个试验可用的k8s单主集群,并不针对生产; 文章中尽可能详细的向同学们描述安装的过程; 该集群构建在阿里云ECS中,不具备条件的同学可使用虚拟机; 步骤及设置规则基本相同; 特殊之处会有详细的说明;
环境说明
OS system | roles&ipaddress | hardware&configure |
---|---|---|
centos 7.7 | master&172.26.61.54 | cpu: 2 core Memory: 8G |
centos 7.7 | node&172.26.61.55 | cpu: 2 core Memory: 4G |
centos 7.7 | node&172.26.61.56 | cpu: 2 core Memory: 4G |
所有节点系统层面准备工作
# 开启路由转发功能及网桥设置
$ vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
$ sysctl -p
# 加载内核lvs相关内核模块
$ cat >/etc/sysconfig/modules/ipvs.modules<<EOF
#!/bin/bash
modprobe br_netfilter
modprobe ip_vs
modprobe ip_vs_rr
modprobe ip_vs_wrr
modprobe ip_vs_sh
modprobe nf_conntrack_ipv4
EOF
$ chmod +x /etc/sysconfig/modules/ipvs.modules
$ source /etc/sysconfig/modules/ipvs.modules
所有节点安装docker及kubeadm相关软件
# step 1: 安装必要的一些系统工具
$ yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
$ yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3: 更新并安装Docker-CE
$ yum makecache fast
$ yum -y install docker-ce
# Step 4: 开启Docker服务
$ service docker start
$ chkconfig docker on
# Step 5: 开启docker镜像加速
$ tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://aoewjvel.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
$ systemctl daemon-reload && systemctl restart docker
# Step 6: 安装kubeadm工具
$ 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
EOF
$ yum install -y kubelet kubeadm kubectl
$ systemctl enable kubelet && systemctl start kubelet
注意: 根据官方说明, 在未初始化之前kubelet是无限重启的状态,只有初始化后才会真的存活于启动状态; 所以kubelet未启动先不用管!
Master节点: 使用kubeadm初始化
$ kubeadm config print init-defaults >> kubeinit.yml
$ vim kubeinit.yml
localAPIEndpoint:
advertiseAddress: 172.26.61.54 # 本地IP地址
bindPort: 6443
......
......
imageRepository: registry.aliyuncs.com/google_containers # 修改为国内镜像
kind: ClusterConfiguration
kubernetesVersion: v1.17.0 # 可调整安装的版本
networking:
dnsDomain: cluster.local
serviceSubnet: 10.96.0.0/12
podSubnet: 10.244.0.0/16 # 增加pod的节点ip范围
......
......
# 根据配置文件下载所需镜像
$ kubeadm config images pull --config=kubeinit/kubeinit.yml
# 使用配置文件进行初始化(要注意保留token字段值, 待会node加入节点要用到)
$ kubeadm init --config=kubeinit/kubeinit.yml
# 根据初始化后的输出进行的设置
$ mkdir -p $HOME/.kube
$ cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ chown $(id -u):$(id -g) $HOME/.kube/config
# 安装flannel网络插件(加载的时间可能会较长,耐心等待即可)
$ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
# 最后查看所有组件的运行状态
$ kubectl get nodes --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-9d85f5447-4g25h 1/1 Running 0 41m
kube-system coredns-9d85f5447-5xggq 1/1 Running 0 41m
kube-system etcd-kubemaster 1/1 Running 0 42m
kube-system kube-apiserver-kubemaster 1/1 Running 0 42m
kube-system kube-controller-manager-kubemaster 1/1 Running 0 42m
kube-system kube-flannel-ds-amd64-7bqgb 1/1 Running 0 39m
kube-system kube-flannel-ds-amd64-b2ps4 1/1 Running 0 17m
kube-system kube-proxy-946mp 1/1 Running 0 17m
kube-system kube-proxy-b7mmc 1/1 Running 0 41m
kube-system kube-scheduler-kubemaster 1/1 Running 0 42m
Node节点: 加入集群 kubeadm join
$ vim joinmaster.yml:
apiVersion: kubeadm.k8s.io/v1beta1
kind: JoinConfiguration
discovery:
bootstraToken:
apiServerEndpoint: 172.26.61.54:6443
token: abcdef.0123456789abcdef #这就是刚刚保存的token
unsafeSkipCAVerification: true
tlsBootstrapToken: abcdef.0123456789abcdef
$ kubeadm join --config=joinmaster.yml
最后查看集群的运行状态
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
kubemaster Ready master 16h v1.17.3
kubenode Ready <none> 16h v1.17.3
至此集群的安装已经完毕! 下面我们来配置dashboard!
Master节点: 配置Dashboard
# 使用 wget下载dashboard的配置文件到本地更改如下字段
$ wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-rc5/aio/deploy/recommended.yaml
$ vim recommended.yaml
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
spec:
type: NodePort # 使用nodePort方式把pod的端口映射到节点上
ports:
- port: 443
targetPort: 8443
nodePort: 30443 # 节点端口
selector:
k8s-app: kubernetes-dashboard
# 构建dashboard
$ kubectl apply -f recommended.yaml
# 查看dashboard启动情况
$ kubectl get pods --all-namespaces
......
kubernetes-dashboard dashboard-metrics-scraper-7b8b58dc8b-n6x77 1/1 Running 0 90s
kubernetes-dashboard kubernetes-dashboard-866f987876-zr9zf 1/1 Running 0 90s
注意: 确定dashboard为Running状态后,使用火狐浏览器访问时会拒绝连接, 原因是不安全, 我们点击继续访问即可
生成访问用户
$ vim dashboard_config.yml
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin-user
namespace: kube-system
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: kube-system
$ kubectl apply -f dashboard_config.yml
# 获取用户的token用于登录
$ kubectl -n kube-system describe secret admin-user-token-n8w8l
# 将输出的token复制到浏览器中即可访问dashboard了