之前我们在windows机器上用Minikube安装了一个单节点Kubernetes集群,这个只能当做了解k8s的练手,本篇文章我们安装一个拥有一个Master,两个Worker节点的k8s集群,作为熟悉Kubernetes的测试集群。
配置要求
3台8核16G的机器,一个Master,两个Worker
RedHat7.2
- Kubernetes v1.15.1
- Docker 18.09.7
安装Docker
1.卸载旧版本
代码语言:javascript复制 $ sudo yum remove docker
docker-client
docker-client-latest
docker-common
docker-latest
docker-latest-logrotate
docker-logrotate
docker-engine
2.安装依赖包,相关驱动
代码语言:javascript复制sudo yum install -y yum-utils
device-mapper-persistent-data
lvm2
3.添加repo
代码语言:javascript复制yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
5.安装docker
安装指定版docker
代码语言:javascript复制 yum install docker-ce-18.09.9 docker-ce-cli-18.09.9 containerd.io -y
启动
代码语言:javascript复制systemctl start docker
systemctl enable docker
检查 docker 版本
代码语言:javascript复制# 在 master 节点和 worker 节点都要执行
[root@cdh1 data]# docker --version
Docker version 18.09.9, build 039a7df9ba
安装docker遇到的报错
代码语言:javascript复制Error: Package: docker-ce-18.09.9.ce-1.el7.centos.x86_64 (docker-ce-edge)
Requires: container-selinux >= 2.9
You could try using --skip-broken to work around the problem
You could try running: rpm -Va --nofiles --nodigest
报错是container-selinux版本低或者是没安装的原因,yum 安装container-selinux 一般的yum源又找不到这个包,需要安装epel源 才能yum安装container-selinux 解决办法:下载阿里云的repo并安装所需的rpm,方法如下
代码语言:javascript复制wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum install epel-release
yum install container-selinux
这时遇到另一个报错
代码语言:javascript复制http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/x86_64/repodata/repomd.xml: [Errno 14] HTTP Error 404 - Not Found
这是因为 $releasever
变量没有被系统识别,查看 rpm-q centos-release
,结果 packagecentos-releaseisnotinstalled
,此包没有安装。
修改 /etc/yum.repos.d/CentOS-Base.repo
中 $releasever
为对应的系统版本路径,比如 centos7
就改为7
安装 nfs-utils
执行安装命令
代码语言:javascript复制# 在 master 节点和 worker 节点都要执行
sudo yum install -y nfs-utils
必须先安装 nfs-utils 才能挂载 nfs 网络存储
K8S基本配置
配置K8S的yum源
代码语言:javascript复制# 在 master 节点和 worker 节点都要执行
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
关闭 防火墙、SeLinux和swap
代码语言:javascript复制# 在 master 节点和 worker 节点都要执行
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
swapoff -a
yes | cp /etc/fstab /etc/fstab_bak
cat /etc/fstab_bak |grep -v swap > /etc/fstab
修改 /etc/sysctl.conf
代码语言:javascript复制# 在 master 节点和 worker 节点都要执行
vim /etc/sysctl.conf
向其中添加
代码语言:javascript复制net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
执行命令以应用
代码语言:javascript复制# 在 master 节点和 worker 节点都要执行
sysctl -p
安装kubelet、kubeadm、kubectl
代码语言:javascript复制# 在 master 节点和 worker 节点都要执行
yum install -y kubelet-1.15.1 kubeadm-1.15.1 kubectl-1.15.1
修改docker Cgroup Driver为systemd
如果不修改,在添加 worker 节点时可能会碰到如下错误
代码语言:javascript复制[WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd".
Please follow the guide at https://kubernetes.io/docs/setup/cri/
我们需要修改配置
代码语言:javascript复制# 在 master 节点和 worker 节点都要执行
vim /usr/lib/systemd/system/docker.service
向其中添加
代码语言:javascript复制--exec-opt native.cgroupdriver=systemd
如下图所示
设置 docker 镜像加速
通过修改daemon配置文件/etc/docker/daemon.json来使用阿里云的加速器
代码语言:javascript复制sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://2u0tz1b9.mirror.aliyuncs.com"]
}
EOF
重启Docker
代码语言:javascript复制sudo systemctl daemon-reload
sudo systemctl restart docker
初始化Master节点
以 root 身份在 master 机器上执行
配置域名
代码语言:javascript复制# 只在 master 节点执行
echo "192.168.0.171 cdh1.macro.com" >> /etc/hosts
创建 ./kubeadm-config.yaml
代码语言:javascript复制# 只在 master 节点执行
cat <<EOF > ./kubeadm-config.yaml
apiVersion: kubeadm.k8s.io/v1beta1
kind: ClusterConfiguration
kubernetesVersion: v1.15.1
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
controlPlaneEndpoint: "cdh1.macro.com:6443"
networking:
podSubnet: "10.100.0.1/20"
EOF
podSubnet 所使用的网段不能与节点所在的网段重叠
初始化 apiserver
执行结果如下:
代码语言:javascript复制[root@cdh1 data]# kubeadm init --config=kubeadm-config.yaml --upload-certs
[init] Using Kubernetes version: v1.15.1
[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'
[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] Activating the kubelet service
[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 [cdh1.macro.com kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local cdh1.macro.com] and IPs [10.96.0.1 192.168.0.171]
[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/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [cdh1.macro.com localhost] and IPs [192.168.0.171 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [cdh1.macro.com localhost] and IPs [192.168.0.171 127.0.0.1 ::1]
[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
[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 38.502925 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.15" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Storing the certificates in Secret "kubeadm-certs" in the "kube-system" Namespace
[upload-certs] Using certificate key:
879945914e72f4da0726dbf3cdbe31107b6e2c5b7ca8228914d07c32be75a3f3
[mark-control-plane] Marking the node cdh1.macro.com as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node cdh1.macro.com as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: cqfgz4.pmg04pko9wueg2kw
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[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-check] Initial timeout of 40s passed.
[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
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/
You can now join any number of the control-plane node running the following command on each as root:
kubeadm join cdh1.macro.com:6443 --token cqfgz4.pmg04pko9wueg2kw
--discovery-token-ca-cert-hash sha256:88c715302430cbe25bc903a03f9d355117f53b3a38fcf5599829438dc3d36331
--control-plane --certificate-key 879945914e72f4da0726dbf3cdbe31107b6e2c5b7ca8228914d07c32be75a3f3
Please note that the certificate-key gives access to cluster sensitive data, keep it secret!
As a safeguard, uploaded-certs will be deleted in two hours; If necessary, you can use
"kubeadm init phase upload-certs --upload-certs" to reload certs afterward.
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join cdh1.macro.com:6443 --token cqfgz4.pmg04pko9wueg2kw
--discovery-token-ca-cert-hash sha256:88c715302430cbe25bc903a03f9d355117f53b3a38fcf5599829438dc3d36331
初始化 root 用户的 kubectl 配置
代码语言:javascript复制# 只在 master 节点执行
[root@cdh1 data]# rm -rf /root/.kube/
[root@cdh1 data]# mkdir /root/.kube/
[root@cdh1 data]# cp -i /etc/kubernetes/admin.conf /root/.kube/config
安装 calico
代码语言:javascript复制[root@cdh1 data]# kubectl apply -f https://docs.projectcalico.org/v3.6/getting-started/kubernetes/installation/hosted/kubernetes-datastore/calico-networking/1.7/calico.yaml
configmap/calico-config created
customresourcedefinition.apiextensions.k8s.io/felixconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamblocks.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/blockaffinities.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamhandles.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamconfigs.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/bgppeers.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/bgpconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ippools.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/hostendpoints.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/clusterinformations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/globalnetworkpolicies.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/globalnetworksets.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/networkpolicies.crd.projectcalico.org created
clusterrole.rbac.authorization.k8s.io/calico-kube-controllers created
clusterrolebinding.rbac.authorization.k8s.io/calico-kube-controllers created
clusterrole.rbac.authorization.k8s.io/calico-node created
clusterrolebinding.rbac.authorization.k8s.io/calico-node created
daemonset.extensions/calico-node created
serviceaccount/calico-node created
deployment.extensions/calico-kube-controllers created
serviceaccount/calico-kube-controllers created
执行如下命令,直到所有的容器组处于 Running 状态
代码语言:javascript复制# 只在 master 节点执行
watch kubectl get pod -n kube-system
检查 master 初始化结果
在 master 节点 cdh1.macro.com上执行
代码语言:javascript复制[root@cdh1 data]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
cdh1.macro.com Ready master 2m19s v1.15.1
初始化Worker节点
获得join命令参数
在 master 节点 cdh1.macro.com 节点执行
代码语言:javascript复制[root@cdh1 data]# kubeadm token create --print-join-command
kubeadm join cdh1.macro.com:6443 --token fbujx7.lzp8ftdzv6n6j0td --discovery-token-ca-cert-hash sha256:88c715302430cbe25bc903a03f9d355117f53b3a38fcf5599829438dc3d36331
初始化worker
针对所有的 worker 节点执行
先将master主机和域名添加进worker
代码语言:javascript复制echo "192.168.0.171 cdh1.macro.com" >> /etc/hosts
worker1:
代码语言:javascript复制[root@cdh2 ~]# kubeadm join cdh1.macro.com:6443 --token fbujx7.lzp8ftdzv6n6j0td --discovery-token-ca-cert-hash sha256:88c715302430cbe25bc903a03f9d355117f53b3a38fcf5599829438dc3d36331
[preflight] Running pre-flight checks
[WARNING Service-Docker]: docker service is not enabled, please run 'systemctl enable docker.service'
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.15" ConfigMap in the kube-system namespace
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Activating the kubelet service
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.
Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
worker2:
代码语言:javascript复制[root@cdh3 ~]# kubeadm join cdh1.macro.com:6443 --token fbujx7.lzp8ftdzv6n6j0td --discovery-token-ca-cert-hash sha256:88c715302430cbe25bc903a03f9d355117f53b3a38fcf5599829438dc3d36331
[preflight] Running pre-flight checks
[WARNING Service-Docker]: docker service is not enabled, please run 'systemctl enable docker.service'
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.15" ConfigMap in the kube-system namespace
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Activating the kubelet service
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.
Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
检查初始化结果
在 master 节点 cdh1.macro.com 上执行
代码语言:javascript复制[root@cdh1 data]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
cdh1.macro.com Ready master 4m55s v1.15.1
cdh2.macro.com NotReady <none> 22s v1.15.1
cdh3.macro.com NotReady <none> 5s v1.15.1
到这里kubernetes的安装已经完毕
安装Kuboard
这里我们没有选用kubernetes dashboard,而选用了国产的kuboard
获取kuboard
代码语言:javascript复制[root@cdh1 data]# kubectl apply -f https://kuboard.cn/install-script/kuboard.yaml
deployment.apps/kuboard created
service/kuboard created
serviceaccount/kuboard-user created
clusterrolebinding.rbac.authorization.k8s.io/kuboard-user created
serviceaccount/kuboard-viewer created
clusterrolebinding.rbac.authorization.k8s.io/kuboard-viewer created
代码语言:javascript复制[root@cdh1 data]# kubectl apply -f https://addons.kuboard.cn/metrics-server/0.3.7/metrics-server.yaml
clusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader created
clusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator created
rolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader created
clusterrole.rbac.authorization.k8s.io/system:metrics-server created
clusterrolebinding.rbac.authorization.k8s.io/system:metrics-server created
apiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io created
serviceaccount/metrics-server created
deployment.apps/metrics-server created
service/metrics-server created
查看 Kuboard 运行状态:
代码语言:javascript复制[root@cdh1 data]# kubectl get pods -l k8s.kuboard.cn/name=kuboard -n kube-system
NAME READY STATUS RESTARTS AGE
kuboard-864ff95474-8vwkr 1/1 Running 0 17s
获取token,管理员用户
- 此Token拥有 ClusterAdmin 的权限,可以执行所有操作
执行命令
代码语言:javascript复制[root@cdh1 data]# echo $(kubectl -n kube-system get secret $(kubectl -n kube-system get secret | grep kuboard-user | awk '{print $1}') -o go-template='{{.data.token}}' | base64 -d)
eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJrdWJvYXJkLXVzZXItdG9rZW4taDh0czQiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoia3Vib2FyZC11c2VyIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiNTQwMDg1YmUtYzRmMS00Y2Q1LWFjZDUtNTBiMDMzYTk2MTZmIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmt1Ym9hcmQtdXNlciJ9.CNhY-pX7Ao4kLu3xnrtyzlpR-DXnfDW3-FjC4bs9q1HPGxU-SLWsMK9X-EL0Qo1Abu-djDU0XFOWi6TNEDBcstk72wB3rGVu5VP8hh1w4Efc2NuqUPM4U2i3ZgIsagcHS9kbx3EIGvqNHINc-HETo9J6Mak18o5RTgaUGp7GQQuO-GoD9te4r1u-db5BsFN2NGz6frsVreb1PpN_gzjkK5dAcMFf9k838aMg0yiUw0hFV3q54_n0orKnLKsPKV_dtaGPenJolj-VQ7xsbANjMu0vKVPLvq7k6RCoXdyv1AY2lmSKdJwhW-oSoHKsYWAlJBalQScyDSOdO-PmwVid4w
访问kuboard
通过NodePort方式访问
Kuboard Service 使用了 NodePort 的方式暴露服务,NodePort 为 32567;您可以按如下方式访问 Kuboard。
代码语言:javascript复制http://任意一个Worker节点的IP地址:32567/
输入前一步骤中获得的 token,可进入Kuboard 集群概览页面