史上最全的k8s在ubuntu18.04上的安装

总览:纵观k8s的整个安装过程,国内受到最大的制约就是“墙”的问题,所以已经实现科学上网的小伙伴应该不会有安装问题,这里主要介绍如果跳过“墙”的限制来实现k8s的安装。

1.按照惯例,首先介绍一下系统配置信息:

root@Virtual-Machine:/home# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.2 LTS
Release:        18.04
Codename:       bionic
root@Virtual-Machine:/home# docker -v
Docker version 19.03.6, build 369ce74a3c

2.安装之前的准备:

关闭防火墙:ufw enable

关闭swap: swapoff -a

                  sed -i 's/.*swap.*/#&/' /etc/fstab

网上有很多说要改host名称的,改不改都无所谓,不影响,只是改了之后知道哪个主机是master,那些是node。

3.安装kubelet,kubectl,kubeadm

打开apt源文件:

vim /etc/apt/sources.list

添加如下:

deb https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial main

然后更新apt源:

apt-get update

安装kube:

apt-get install -y kubelet=1.18.0-00 kubeadm=1.18.0-00 kubectl=1.18.0-00

注意:这里改成1.18.0版本是为kubeadm init做的需求,也可以不加版本号,直接apt-get install -y kubelet kubeadm kubectl,但是由于没有找到当前最新版本-1.18.6的kubeadm init需要的镜像,所以下载1.18.0版本。

4.启动master节点

启动master节点需要执行:kubeadm init --kubernetes-version=v1.18.0 --pod-network-cidr=10.10.0.0/16 --apiserver-advertise-address=192.168.0.188,但是由于“墙”,只能手动下载1.18.0的镜像。

查询需要的镜像:

root@Virtual-Machine:/home# kubeadm config images list --kubernetes-version v1.18.0
k8s.gcr.io/kube-apiserver:v1.18.0
k8s.gcr.io/kube-controller-manager:v1.18.0
k8s.gcr.io/kube-scheduler:v1.18.0
k8s.gcr.io/kube-proxy:v1.18.0
k8s.gcr.io/pause:3.2
k8s.gcr.io/etcd:3.4.3-0
k8s.gcr.io/coredns:1.6.7

拉取镜像:

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.18.0 
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.18.0 
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.18.0 
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.18.0 
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2 
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.3-0 
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.7 

给镜像改名:

docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.18.0 k8s.gcr.io/kube-apiserver:v1.18.0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.18.0 k8s.gcr.io/kube-controller-manager:v1.18.0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.18.0 k8s.gcr.io/kube-scheduler:v1.18.0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.18.0 k8s.gcr.io/kube-proxy:v1.18.0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2 k8s.gcr.io/pause:3.2
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.3-0 k8s.gcr.io/etcd:3.4.3-0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.7 k8s.gcr.io/coredns:1.6.7

删除不需要的镜像(此步骤可有可无):

 docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.18.0
 docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.18.0
 docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.18.0
 docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.18.0
 docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2
 docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.3-0
 docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.7

注意:以上三步关于镜像的操作可以直接写到一个脚本里,for循环来执行,详情可以查询脚本操作。

此时,镜像下载完成,可以启动master节点了:

kubeadm init --kubernetes-version=v1.18.0 --pod-network-cidr=10.10.0.0/16 --apiserver-advertise-address=192.168.0.188

当收到kubeadm join 192.168.0.188:6443 --token hagd9m.ohpm4iyvu4odmcqk  --discovery-token-ca-cert-hash sha256:855cb9d1a3131a18f657259aad123217151a05e5fcca8b894287dc101eae353c,说明成功了,保存这条消息,其他节点加入时需要使用。

设置配置文件如下:

mkdir -p /root/.kube
 
cp -i /etc/kubernetes/admin.conf /root/.kube/config

chown root:root /root/.kube/config

5.主节点生效

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
root@Virtual-Machine:/home# kubectl get nodes
NAME                       STATUS   ROLES    AGE     VERSION
virtual-machine            Ready    master   6h42m   v1.18.0

当看到状态是ready的时候,此时已经安装成功了,finish。

下一节介绍:如何安装k8s的dashboard

https://blog.csdn.net/wangkaizheng123/article/details/107492833

你可能感兴趣的:(自动化部署)