一、基本概念
二、前期准备
三、初始化两个节点
192.168.105.6 k8s-master
192.168.105.7 node
192.168.105.7 node
192.168.105.6 k8s-master
root@k8s-master:~# ufw disable //关闭防火墙
root@k8s-master:~# ufw status //查看防火墙状态
apt-get install chrony -y
vim /etc/chrony/chrony.conf
#修改三项
server 127.127.1.0 iburst
allow 192.168.105.0/24
local stratum 10
systemctl start chronyd
systemctl enable chronyd #开机自启动
(2) node节点
apt-get install chrony -y
vim /etc/chrony/chrony.conf
#修改一项
server 192.168.105.6 iburst
systemctl start chronyd
systemctl enable chronyd #开机自启动
(3) 可以在两节点运行,chronyc sources查看同步信息
四、使用kubeadm安装(k8s-master节点)
apt-get update && apt-get install -y apt-transport-https
curl -fsSL https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
#新增源
add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main"
apt-get update
apt install -y kubelet=1.15.4-00 kubectl=1.15.4-00 kubeadm=1.15.4-00 //安装指定的版本
tee /etc/default/kubelet <<-'EOF'
KUBELET_EXTRA_ARGS="--fail-swap-on=false"
EOF
kubeadm init \
--kubernetes-version=v1.15.4 \
--image-repository registry.aliyuncs.com/google_containers \
--pod-network-cidr=10.244.0.0/16 \
--ignore-preflight-errors=Swap
执行成功会显示initialized sucessfully!,之前安装忘了截图,在网上找了一张,可以参考一下。
上图显示,在开始使用集群前,还要进行以下配置:
root@k8s-master:~# mkdir -p $HOME/.kube
root@k8s-master:~# cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
root@k8s-master:~# chown $(id -u):$(id -g) $HOME/.kube/config
进行网络插件安装前,先配置名称解析,防止访问不了:
vim /etc/hosts,添加
199.232.68.133 raw.githubusercontent.com
安装 pod 网络插件 flannel(可用 wget 把 github 把文件下载到本地)
root@k8s-master:~# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
podsecuritypolicy.policy/psp.flannel.unprivileged created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.apps/kube-flannel-ds-amd64 created
daemonset.apps/kube-flannel-ds-arm64 created
daemonset.apps/kube-flannel-ds-arm created
daemonset.apps/kube-flannel-ds-ppc64le created
daemonset.apps/kube-flannel-ds-s390x created
root@k8s-master:~#
root@k8s-master:~# kubectl get pods -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-bccdc95cf-lnbl9 0/1 Pending 0 2m52s
kube-system coredns-bccdc95cf-zr8bk 0/1 Pending 0 2m52s
kube-system etcd-k8s-master 1/1 Running 0 2m8s
kube-system kube-apiserver-k8s-master 1/1 Running 0 2m10s
kube-system kube-controller-manager-k8s-master 1/1 Running 0 2m7s
kube-system kube-flannel-ds-amd64-wpj2p 0/1 Init:0/1 0 12s //如果拉取不下来,自己pull就可以
kube-system kube-proxy-dt9p4 1/1 Running 0 2m51s
kube-system kube-scheduler-k8s-master 1/1 Running 0 2m12s
(安装时需要下载镜像 quay.io/coreos/flannel:v0.11.0-amd64,具体的镜像见 kube-flannel.yml 文件,如果下载失败则到 quay-mirror.qiniu.com 下载,并重新打回 tag)
docker pull quay.io/coreos/flannel:v0.11.0-amd64
查看节点状态,为 ready,查看集群状态,各服务正常
kubectl get cs
kubectl taint nodes --all node-role.kubernetes.io/master-
查看系统状态:kubectl get pods -n kube-system
五、node节点部署并加入
apt-get update && apt-get install -y apt-transport-https
curl -fsSL https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
#新增源
add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main"
apt-get update
apt install -y kubelet=1.15.4-00 kubectl=1.15.4-00 kubeadm=1.15.4-00 //安装指定的版本
tee /etc/default/kubelet <<-'EOF'
KUBELET_EXTRA_ARGS="--fail-swap-on=false"
EOF
kubeadm join 192.168.105.6:6443 --token pk2rf8.neqrlzq4ls4kxcji \
--discovery-token-ca-cert-hash sha256:c3722c537d6e7e3774cc8e817ad1462da7e9e62eb83656afd8efa7ed4a2b02c0
kubeadm token list # 查看 token
kubeadm token create --print-join-command # 创建 token 并打印 join 命令
或通过 openssl 获得证书的 sha256:
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
加入到集群:
kubeadm join 192.168.105.6:6443 --token \
--discovery-token-ca-cert-hash sha256:<证书sha256>
scp /etc/kubernetes/admin.conf bryou@192.168.105.7:/etc/kubernetes //在master节点执行
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile //在node节点执行
source ~/.bash_profile //在node节点执行,使之立即生效
再运行kubectl命令就成功了:
六、部署WebUI — kuboard
docker load -i kuboard-1.tar.gz
4. 把下载的 start_kuboard.yaml 上传到master节点,编辑yaml配置文件,修改一行,指定希望kuboard运行在哪个节点:nodeName: node
执行yaml对资源进行配置: kubectl apply -f start_kuboard.yaml
查看一下状态:kubectl get pods -n kube-system
查看一下kuboard的暴露的端口:kubectl get svc -n kube-system
发现Kuboard端口为32567
5. 在浏览器登录:http://192.168.105.7:32567/,注:地址为部署kuboard组件的节点地址,这里相应的为node节点IP,发现需要输入token,在master节点运行以下命令获取token:
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
输入token登录,web界面如下:
在kubernetes-ui上执行yaml文件,简单快捷,历经坎坷算是简陋搭建完了,下一步准备搭建高可用k8s集群
六、小结 && 参考资料
本文快速地介绍了一下Kubernetes的核心构成组件及其作用,然后通过在两台真实服务器上通过Kubeadm搭建了一个Master节点一个Node节点的集群,最后验证了一下集群是否可用。