Linux运维-陈雷雷


使用kubeadm部署k8s集群环境[k8s 1.16版本]

admin 2019-11-12 120浏览 0条评论
首页/正文
分享到: / / / /

使用kubeadm部署k8s集群

环境

IP地址 主机名 节点
10.0.0.63 k8s-master master
10.0.0.65 k8s-node1 node1
10.0.0.66 k8s-node2 node2

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-master
10.0.0.65 k8s-node1
10.0.0.66 k8s-node2
EOF

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

#开启转发
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驱动

cat > /etc/docker/daemon.json <<EOF
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2"
}
EOF

systemctl enable docker
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

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.16.0 kubeadm-1.16.0 kubectl-1.16.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.16.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 v3p5pj.1wsrd7ybs6z14o9f
--discovery-token-ca-cert-hash sha256:ef5fb837f9b66fa9742b236b373203df63b1d71651dfc7211f40150d20516d85 记住token,后面使用

10.1. kubectl命令工具配置:

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-master ~]#  kubectl get nodes
NAME         STATUS     ROLES    AGE     VERSION
k8s-master   NotReady   master   4m11s   v1.16.0

10.2. 安装网络插件

上传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

将node1 node2 加入master

node1加入配置

kubeadm join 10.0.0.63:6443 --token v3p5pj.1wsrd7ybs6z14o9f \
    --discovery-token-ca-cert-hash sha256:ef5fb837f9b66fa9742b236b373203df63b1d71651dfc7211f40150d20516d85
    
#这个配置在安装master的时候有过提示,请注意





# 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.16.0
k8s-node1    Ready    <none>   2m9s   v1.16.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


#加入成功后,master节点检测:
[root@k8s-master ~]#  kubectl get nodes
NAME         STATUS   ROLES    AGE     VERSION
k8s-master   Ready    master   25m     v1.16.0
k8s-node1    Ready    <none>   4m40s   v1.16.0
k8s-node2    Ready    <none>   52s     v1.16.0

10.3 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登录

登录后界面:

附件下载

kubeadm快速部署一个Kubernetes集群yaml.zip

最后修改:2019-11-12 20:37:07 © 著作权归作者所有
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付

上一篇

发表评论

评论列表

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

博客信息

  • 文章数目 81
  • 标签数目 233
  • 运行天数
  • 最后活动

广告



文章目录