利用kubeadm快速部署k8s
内外网络互通
[root@k8s-1 ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core)
配置k8syum仓库,及Docker-ceyum仓库
[root@k8s-1 yum.repos.d]# vim /etc/yum.repos.d/kubernetes.repo [kubernetes] name=kubernetes baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/ gpgcheck=0 enable=1 [root@k8s-1 yum.repos.d]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo --2019-05-10 11:03:16-- https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 正在解析主机 mirrors.aliyun.com (mirrors.aliyun.com)... 47.246.0.251, 47.246.0.253, 47.246.0.254, ... 正在连接 mirrors.aliyun.com (mirrors.aliyun.com)|47.246.0.251|:443... 已连接。 已发出 HTTP 请求,正在等待回应... 200 OK 长度:2640 (2.6K) [application/octet-stream] 正在保存至: “docker-ce.repo” 100%[====================================================================================================================================================================================================================================>] 2,640 --.-K/s 用时 0s 2019-05-10 11:03:17 (152 MB/s) - 已保存 “docker-ce.repo” [2640/2640])
安装
[root@k8s-1 yum.repos.d]# yum install docker-ce kubelet kubeadm kubectl kubernetes-cni
设置开机自启启动docker
[root@k8s-1 yum.repos.d]# systemctl enable docker
设置开机自启启动kubelet
[root@k8s-1 yum.repos.d]# systemctl enable kubelet.service
启动docker与kubelet
[root@k8s-1 yum.repos.d]# systemctl start docker [root@k8s-1 yum.repos.d]# systemctl start kubelet
由于国内网络原因,kubernetes的镜像托管在google云上,无法直接下载,所以直接把把镜像搞下来有个技术大牛把gcr.io的镜像每天同步到https://github.com/anjia0532/gcr.io_mirror这个站点,因此,如果需要用到gcr.io的镜像,可以执行如下的脚本进行镜像拉取
[root@k8s-1 yum.repos.d]# vim pullimages.sh #!/bin/bash images=(kube-proxy-amd64:v1.11.1 kube-scheduler-amd64:v1.11.1 kube-controller-manager-amd64:v1.11.1 kube-apiserver-amd64:v1.11.1 etcd-amd64:3.2.18 coredns:1.1.3 pause:3.1 ) for imageName in ${images[@]} ; do docker pull anjia0532/google-containers.$imageName docker tag anjia0532/google-containers.$imageName k8s.gcr.io/$imageName docker rmi anjia0532/google-containers.$imageName done
[root@k8s-1 yum.repos.d]# sh pullimages.sh
kubernetes集群不允许开启swap
[root@k8s-1 yum.repos.d]# vi /etc/fstab # # /etc/fstab # Created by anaconda on Wed May 8 12:34:12 2019 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/centos-root / xfs defaults 0 0 UUID=6c895b50-de9d-4a3e-8a25-3867ac58f21c /boot xfs defaults 0 0 #/dev/mapper/centos-swap swap swap defaults 0 0 永久禁用 [root@k8s-1 yum.repos.d]# swapoff -a 临时关闭,当前生效
编写kubeadm.yaml;kubeadm 1.14 配置文件
[root@k8s-1 yum.repos.d]# cd [root@k8s-1 ~]# vim kubeadm.yaml apiVersion: kubeadm.k8s.io/v1beta1 kind: ClusterConfiguration controllerManager: extraArgs: horizontal-pod-autoscaler-use-rest-clients: "true" horizontal-pod-autoscaler-sync-period: "10s" node-monitor-grace-period: "10s" apiServer: extraArgs: runtime-config: "api/all=true" kubernetesVersion: "stable-1.14"
启动
[root@k8s-1 ~]# kubeadm init --config kubeadm.yaml
这样就可以完成 Kubernetes Master 的部署了,这个过程只需要几分钟,部署完成后,kubeadm 会生成一行指令:
kubeadm join 192.168.183.7:6443 --token 18ov5i.7l91ujayrn6x29d1 \ --discovery-token-ca-cert-hash sha256:5914bfca1399d70664a90ef0d80c8aa776e1b2a5aa203b30613131e47808149c
配置kubectl与apiserver的认证
[root@k8s-1 ~]# mkdir -p $HOME/.kube [root@k8s-1 ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config [root@k8s-1 ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config
检查健康
[root@k8s-1 ~]# kubectl get cs NAME STATUS MESSAGE ERROR scheduler Healthy ok controller-manager Healthy ok etcd-0 Healthy {"health":"true"}
检查状态
[root@k8s-1 ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-1 NotReady master 22m v1.14.1
部署网络插件
[root@k8s-1 ~]# kubectl apply -f https://git.io/weave-kube-1.6 serviceaccount/weave-net created clusterrole.rbac.authorization.k8s.io/weave-net created clusterrolebinding.rbac.authorization.k8s.io/weave-net created role.rbac.authorization.k8s.io/weave-net created rolebinding.rbac.authorization.k8s.io/weave-net created daemonset.extensions/weave-net created
查看状态
[root@k8s-1 ~]# kubectl get pods -n kube-system NAME READY STATUS RESTARTS AGE coredns-fb8b8dccf-bhst6 1/1 Running 0 28m coredns-fb8b8dccf-mtmsl 1/1 Running 0 28m etcd-k8s-1 1/1 Running 0 27m kube-apiserver-k8s-1 1/1 Running 0 27m kube-controller-manager-k8s-1 1/1 Running 0 27m kube-proxy-ww2kz 1/1 Running 0 28m kube-scheduler-k8s-1 1/1 Running 0 27m weave-net-mg6m4 2/2 Running 0 69s
其中,为节点打上“污点”(Taint)的命令是:
[root@k8s-1 ~]# kubectl taint nodes --all node-role.kubernetes.io/master- node/k8s-1 untainted
可视化插件,下载镜像 参考:https://github.com/kubernetes/dashboard
[root@k8s-1 ~]# wget https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml --2019-05-10 11:59:19-- https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml 正在解析主机 raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.0.133, 151.101.64.133, 151.101.128.133, ... 正在连接 raw.githubusercontent.com (raw.githubusercontent.com)|151.101.0.133|:443... 已连接。 已发出 HTTP 请求,正在等待回应... 200 OK 长度:4577 (4.5K) [text/plain] 正在保存至: “kubernetes-dashboard.yaml” 100%[====================================================================================================================================================================================================================================>] 4,577 --.-K/s 用时 0s 2019-05-10 11:59:20 (32.5 MB/s) - 已保存 “kubernetes-dashboard.yaml” [4577/4577])
草都可以从石头缝隙中长出来更可况你呢