Linux运维-陈雷雷


k8s_使用kubeadm部署k8s集群

admin 2020-03-18 324浏览 0条评论
首页/正文
分享到: / / / /

使用kubeadm部署k8s集群

环境

IP地址 主机名 节点
10.0.0.63 k8s-master1 master1
10.0.0.63 k8s-master2 master2
10.0.0.65 k8s-node1 node1
10.0.0.66 k8s-node2 node2
k8s瓶颈
节点数不超过5000
pod总数不超过 150000
容器总数不超过300000
每个节点的pod数量不超过100

1. 简要

kubeadm是官方社区推出的快速部署kubernetes集群工具 部署环境适用于学习和使用k8s相关软件和功能

2. 安装要求

3台纯净centos虚拟机,版本为7.x及以上
机器配置 2核4G以上 x3台
服务器网络互通
禁止swap分区

3. 学习目标

学会使用kubeadm来安装一个集群,便于学习k8s相关知识

4. 环境准备

# 1. 关闭防火墙功能
systemctl stop firewalld
systemctl disable firewalld

# 2.关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config 
setenforce 0

# 3. 关闭swap
swapoff -a
#或将命令加入开机启动
echo "swapoff -a" >>/etc/profile

# 4. 服务器规划
cat > /etc/hosts << EOF
10.0.0.63 k8s-master1
10.0.0.64 k8s-master2
10.0.0.65 k8s-node1
10.0.0.66 k8s-node2
EOF

#5. 临时主机名配置方法:
 hostnamectl set-hostname  k8s-master1
 bash

#6. 时间同步配置
yum install -y ntpdate
ntpdate time.windows.com

#开启转发
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system

# 以上可以全部复制粘贴直接运行,但是主机名配置需要重新修改

5. docker安装[所有节点都需要安装]

#源添加
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo

# 安装指定版版本 
yum -y install docker-ce-18.09.9-3.el7

#也可以查看版本安装
yum list docker-ce --showduplicates | sort -r

#启动docker
systemctl start docker

6. docker配置cgroup驱动[所有节点]

rm -f /etc/docker/*
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://ajvcw8qn.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

7. 镜像加速[所有节点]

curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io
systemctl restart docker


#如果源太多容易出错. 错了就删除一个.bak源试试看
#保留 curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io

这个是阿里云配置的加速
https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

8.kubernetes源配置[所有节点]

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

9. 安装kubeadm,kubelet和kubectl[所有节点]

yum install -y kubelet-1.17.0 kubeadm-1.17.0 kubectl-1.17.0
systemctl enable kubelet

10. 部署Kubernetes Master [ master 10.0.0.63]

kubeadm init \
  --apiserver-advertise-address=10.0.0.63 \
  --image-repository registry.aliyuncs.com/google_containers \
  --kubernetes-version v1.17.0 \
  --service-cidr=10.1.0.0/16 \
  --pod-network-cidr=10.244.0.0/16

初始化后获取到token:

kubeadm join 10.0.0.63:6443 --token 0dr1pw.ejybkufnjpalb8k6
--discovery-token-ca-cert-hash sha256:ca1aa9cb753a26d0185e3df410cad09d8ec4af4d7432d127f503f41bc2b14f2a 记住token,后面使用

10.1. kubectl命令工具配置[master]:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

#获取节点信息
# kubectl get nodes

[root@k8s-master1 ~]# kubectl get nodes
NAME          STATUS     ROLES    AGE     VERSION
k8s-master1   Ready      master   38m     v1.17.0

10.2. 安装网络插件[master]

上传kube-flannel.yaml,并执行:
kubectl apply -f kube-flannel.yaml
kubectl get pods -n kube-system

[必须全部运行起来,否则有问题.]
[root@k8s-master ~]# kubectl get pods -n kube-system
NAME                                 READY   STATUS    RESTARTS   AGE
coredns-58cc8c89f4-4rl5b             1/1     Running   0          10m
coredns-58cc8c89f4-dwrch             1/1     Running   0          10m
etcd-k8s-master                      1/1     Running   0          9m42s
kube-apiserver-k8s-master            1/1     Running   0          9m17s
kube-controller-manager-k8s-master   1/1     Running   0          9m19s
kube-flannel-ds-amd64-gsv5d          1/1     Running   0          102s
kube-proxy-wjskk                     1/1     Running   0          10m
kube-scheduler-k8s-master            1/1     Running   0          9m38s

11. 将node1 node2 加入master

node1加入配置

在要加入的节点种执行以下命令来加入:
kubeadm join 10.0.0.63:6443 --token fs0uwh.7yuiawec8tov5igh \
    --discovery-token-ca-cert-hash sha256:471442895b5fb77174103553dc13a4b4681203fbff638e055ce244639342701d
    
    


#这个配置在安装master的时候有过提示,请注意首先要配置	cni网络:

# node1加入master:
[root@k8s-node1 ~]# kubeadm join 10.0.0.63:6443 --token v3p5pj.1wsrd7ybs6z14o9f \
>     --discovery-token-ca-cert-hash sha256:ef5fb837f9b66fa9742b236b373203df63b1d71651dfc7211f40150d20516d85
[preflight] Running pre-flight checks
	[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/
[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.16" 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.

#返回值判断对错,0对,其他值均为错
[root@k8s-node1 ~]# echo $?
0

#加入成功后,master节点检测:
[root@k8s-master ~]#  kubectl get nodes
NAME         STATUS   ROLES    AGE    VERSION
k8s-master   Ready    master   22m    v1.17.0
k8s-node1    Ready    <none>   2m9s   v1.17.0    <---- node1 成功加入到集群中




#node2加入master:
[root@k8s-node2 docker]# kubeadm join 10.0.0.63:6443 --token v3p5pj.1wsrd7ybs6z14o9f \
>     --discovery-token-ca-cert-hash sha256:ef5fb837f9b66fa9742b236b373203df63b1d71651dfc7211f40150d20516d85
[preflight] Running pre-flight checks
	[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/
[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.16" 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.

[root@k8s-node2 docker]# echo $?
0
# master2 加入 一样的操作.


#加入成功后,master节点检测:
[root@k8s-master1 docker]#  kubectl get nodes
NAME          STATUS   ROLES    AGE     VERSION
k8s-master1   Ready    master   76m     v1.17.0
k8s-master2   Ready    <none>   79s     v1.17.0
k8s-node1     Ready    <none>   8m23s   v1.17.0
k8s-node2     Ready    <none>   6m      v1.17.0

12 token创建和查询

默认token会保存24消失,过期后就不可用,如果需要重新建立token,可在master节点使用以下命令重新生成:

kubeadm token create
kubeadm token list
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
63bca849e0e01691ae14eab449570284f0c3ddeea590f8da988c07fe2729e924

kubeadm join 10.0.0.63:6443 --discovery-token nuja6n.o3jrhsffiqs9swnu --discovery-token-ca-cert-hash 63bca849e0e01691ae14eab449570284f0c3ddeea590f8da988c07fe2729e924

11. 创建一个nginx镜像

kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort


kubectl get pod,svc

查看状态,nginx已经启动端口为:31907:

此时可以通过 集群的3台服务器期中一台来测试访问:

12.部署dashboard图形界面

上传 kubernetes-dashboard.yaml 

[root@k8s-master ~]# kubectl apply -f dashboard.yaml 
[root@k8s-master ~]# kubectl get pods -n kubernetes-dashboard
[root@k8s-master ~]# kubectl get pods -n kubernetes-dashboard
NAME                                         READY   STATUS    RESTARTS   AGE
dashboard-metrics-scraper-566cddb686-xjgks   1/1     Running   0          39s
kubernetes-dashboard-7b5bf5d559-8ssvj        1/1     Running   0          39s

13. 获取dashboard token, 也就是创建service account并绑定默认cluster-admin管理员集群角色

# kubectl create serviceaccount dashboard-admin -n kubernetes-dashboard
# kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kubernetes-dashboard:dashboard-admin
# kubectl describe secrets -n kubernetes-dashboard $(kubectl -n kubernetes-dashboard get secret | awk '/dashboard-admin/{print $1}')

将复制的token 填写到 上图中的 token选项,并选择token登录

登录后界面:

报错处理:

k8s-node节点加入时报错:
W0315 22:16:20.123204    5795 join.go:346] [preflight] WARNING: JoinControlPane.controlPlane settings will be ignored when control-plane flag is not set.
[preflight] Running pre-flight checks
	[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/
error execution phase preflight: [preflight] Some fatal errors occurred:
	[ERROR FileContent--proc-sys-net-bridge-bridge-nf-call-iptables]: /proc/sys/net/bridge/bridge-nf-call-iptables contents are not set to 1
[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`
To see the stack trace of this error execute with --v=5 or higher

处理办法:
echo "1" >/proc/sys/net/bridge/bridge-nf-call-iptables
增加后重新加入:
kubeadm join 10.0.0.63:6443 --token 0dr1pw.ejybkufnjpalb8k6     --discovery-token-ca-cert-hash sha256:ca1aa9cb753a26d0185e3df410cad09d8ec4af4d7432d127f503f41bc2b14f2a
这里的token由kubadm服务器生成.

YAML附件[请保存为 .yaml 为后缀]

http://www.chenleilei.net/soft/kubeadm快速部署一个Kubernetes集群yaml.zip

最后修改:2020-03-18 13:55:41 © 著作权归作者所有
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付

上一篇

发表评论

评论列表

还没有人评论哦~赶快抢占沙发吧~

博客信息

  • 文章数目 123
  • 标签数目 333
  • 运行天数
  • 最后活动

广告



文章目录