kubernetes,简称K8s,是用8代替8个字符“ubernete”而成的缩写。是一个开源的,用于管理云平台中多个主机上的容器化的应用,Kubernetes的目标是让部署容器化的应用简单并且高效(powerful),Kubernetes提供了应用部署,规划,更新,维护的一种机制。目前网上说的很多安装方案要不就是版本太旧了,要不就是需要翻墙,作者好不容易研究出一套完整的安装方案。废话少说,我们立刻开始吧。
环境:
系统:Centos7.5
内存:2G
硬盘:50G
操作步骤:
1.修改主机名
hostnamectl set-hostname k8s-master
重启动系统以便生效
reboot
2.安装docker
安装policycoreutils-python yum -y install policycoreutils-python*
下载并安装container-selinux-2.68
wget http://mirror.centos.org/centos/7/extras/x86_64/Packages/container-selinux-2.68-1.el7.noarch.rpm rpm -ivh container-selinux-2.68-1.el7.noarch.rpm
安装libltdl.so
yum install -y libltdl.so*
安装pigz
yum install -y pigz*
下载并安装docker1.18安装包
wget https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/Packages/docker-ce-18.03.1.ce-1.el7.centos.x86_64.rpm rpm -ivh docker-ce-18.03.1.ce-1.el7.centos.x86_64.rpm
输入docker -v打印以下信息表示安装成功
[root@k8s-master software]# docker -v Docker version 18.03.1-ce, build 9ee9f40
3.切换 k8s yum 源
vim /etc/yum.repos.d/kubernetes.repo
写入以下内容
[kuberneten] name=Kubernetes baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=0 repo_gpgcheck=0 gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
*因为目前政策原因,我们无法连接k8s.gcr.io去拉取镜像,的以我们只能使用啊里的镜像仓库,当然如果读者有翻墙工具则无需这步操作
缓存生效:
yum makecache
出现以下信息表示仓库设置成功
Loaded plugins: fastestmirror, langpacks Determining fastest mirrors epel | 3.2 kB 00:00:00 extras | 3.4 kB 00:00:00 kuberneten | 1.4 kB 00:00:00 os | 3.6 kB 00:00:00 updates | 3.4 kB 00:00:00 (1/14): epel/7/x86_64/prestodelta | 75 B 00:00:00 (2/14): extras/7/x86_64/prestodelta | 100 kB 00:00:00 (3/14): extras/7/x86_64/other_db | 126 kB 00:00:00 (4/14): extras/7/x86_64/filelists_db | 603 kB 00:00:00 (5/14): epel/7/x86_64/other | 2.2 MB 00:00:00 (6/14): kuberneten/filelists | 14 kB 00:00:00 (7/14): kuberneten/primary | 39 kB 00:00:00 (8/14): kuberneten/other | 27 kB 00:00:00 (9/14): updates/7/x86_64/prestodelta | 679 kB 00:00:00 (10/14): updates/7/x86_64/other_db | 578 kB 00:00:00 (11/14): os/7/x86_64/other_db | 2.5 MB 00:00:00 (12/14): updates/7/x86_64/filelists_db | 3.4 MB 00:00:00 (13/14): epel/7/x86_64/filelists | 10 MB 00:00:00 (14/14): os/7/x86_64/filelists_db | 6.9 MB 00:00:00 epel 12717/12717 epel 12717/12717 epel 12717/12717 kuberneten 284/284 kuberneten 284/284 kuberneten 284/284 Metadata Cache Created
4.设置网桥
sysctl net.bridge.bridge-nf-call-iptables=1
RHEL / CentOS 7系统由于iptables被绕过而导致流量被错误路由的问题。所以应该确保net.bridge.bridge-nf-call-iptables的sysctl配置中被设置为1
5.安装 kubeadm 和相关工具
yum install -y kubelet kubeadm kubectl kubernetes-cni
查看安装版本
[root@k8s-master software]# kubectl version Client Version: version.Info{Major:"1", Minor:"12", GitVersion:"v1.12.3", GitCommit:"435f92c719f279a3a67808c80521ea17d5715c66", GitTreeState:"clean", BuildDate:"2018-11-26T12:57:14Z", GoVersion:"go1.10.4", Compiler:"gc", Platform:"linux/amd64"} The connection to the server localhost:8080 was refused - did you specify the right host or port?
启动docker和kuberctl
systemctl enable docker && systemctl start docker systemctl enable kubelet && systemctl start kubelet
查看 kubeadm 会用到的镜像
[root@k8s-master software]# kubeadm config images list k8s.gcr.io/kube-apiserver:v1.12.3 k8s.gcr.io/kube-controller-manager:v1.12.3 k8s.gcr.io/kube-scheduler:v1.12.3 k8s.gcr.io/kube-proxy:v1.12.3 k8s.gcr.io/pause:3.1 k8s.gcr.io/etcd:3.2.24 k8s.gcr.io/coredns:1.2.2
可以看到目前已经升级到了1.12.3的版本了
拉取镜像并设置tag
docker pull docker.io/mirrorgooglecontainers/kube-apiserver-amd64:v1.12.3 docker tag docker.io/mirrorgooglecontainers/kube-apiserver-amd64:v1.12.3 k8s.gcr.io/kube-apiserver:v1.12.3 docker pull docker.io/mirrorgooglecontainers/kube-controller-manager-amd64:v1.12.3 docker tag docker.io/mirrorgooglecontainers/kube-controller-manager-amd64:v1.12.3 k8s.gcr.io/kube-controller-manager:v1.12.3 docker pull docker.io/mirrorgooglecontainers/kube-scheduler-amd64:v1.12.3 docker tag docker.io/mirrorgooglecontainers/kube-scheduler-amd64:v1.12.3 k8s.gcr.io/kube-scheduler:v1.12.3 docker pull docker.io/mirrorgooglecontainers/kube-proxy-amd64:v1.12.3 docker tag docker.io/mirrorgooglecontainers/kube-proxy-amd64:v1.12.3 k8s.gcr.io/kube-proxy:v1.12.3 docker pull docker.io/mirrorgooglecontainers/pause-amd64:3.1 docker tag docker.io/mirrorgooglecontainers/pause-amd64:3.1 k8s.gcr.io/pause:3.1 docker pull docker.io/mirrorgooglecontainers/etcd-amd64:3.2.24 docker tag docker.io/mirrorgooglecontainers/etcd-amd64:3.2.24 k8s.gcr.io/etcd:3.2.24 docker pull docker.io/coredns/coredns:1.2.2 docker tag docker.io/coredns/coredns:1.2.2 k8s.gcr.io/coredns:1.2.2
对比一下镜像名和版本号是否一致,否则会安装失败(红色背景部分,tag和版本号都要一致)
[root@k8s-master software]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mirrorgooglecontainers/kube-proxy-amd64 v1.12.3 ab97fa69b926 5 days ago 96.5MB k8s.gcr.io/kube-proxy v1.12.3 ab97fa69b926 5 days ago 96.5MB mirrorgooglecontainers/kube-apiserver-amd64 v1.12.3 6b54f7bebd72 5 days ago 194MB k8s.gcr.io/kube-apiserver v1.12.3 6b54f7bebd72 5 days ago 194MB mirrorgooglecontainers/kube-controller-manager-amd64 v1.12.3 c79022eb8bc9 5 days ago 164MB k8s.gcr.io/kube-controller-manager v1.12.3 c79022eb8bc9 5 days ago 164MB mirrorgooglecontainers/kube-scheduler-amd64 v1.12.3 5e75513787b1 5 days ago 58.3MB k8s.gcr.io/kube-scheduler v1.12.3 5e75513787b1 5 days ago 58.3MB mirrorgooglecontainers/etcd-amd64 3.2.24 3cab8e1b9802 2 months ago 220MB k8s.gcr.io/etcd 3.2.24 3cab8e1b9802 2 months ago 220MB coredns/coredns 1.2.2 367cdc8433a4 3 months ago 39.2MB k8s.gcr.io/coredns 1.2.2 367cdc8433a4 3 months ago 39.2MB mirrorgooglecontainers/pause-amd64 3.1 da86e6ba6ca1 11 months ago 742kB k8s.gcr.io/pause 3.1 da86e6ba6ca1 11 months ago 742kB [root@k8s-master software]# [root@k8s-master software]# [root@k8s-master software]# kubeadm config images list I1202 11:29:39.251650 3916 version.go:93] could not fetch a Kubernetes version from the internet: unable to get URL "https://dl.k8s.io/release/stable-1.txt": Get https://storage.googleapis.com/kubernetes-release/release/stable-1.txt: net/http: request canceled (Client.Timeout exceeded while awaiting headers) I1202 11:29:39.251711 3916 version.go:94] falling back to the local client version: v1.12.3 k8s.gcr.io/kube-apiserver:v1.12.3 k8s.gcr.io/kube-controller-manager:v1.12.3 k8s.gcr.io/kube-scheduler:v1.12.3 k8s.gcr.io/kube-proxy:v1.12.3 k8s.gcr.io/pause:3.1 k8s.gcr.io/etcd:3.2.24 k8s.gcr.io/coredns:1.2.2
初始化k8s
kubeadm init --kubernetes-version=1.12.3 --pod-network-cidr=10.244.0.0/16
--pod-network:这个比较重要,接下来的flannel的网络都要使用这个,如果不一致,flannel启动失败。
看到以下信息表示安装成功
Your Kubernetes master has initialized successfully! To start using your cluster, you need to run the following as a regular user: mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config You should now deploy a pod network to the cluster. Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at: https://kubernetes.io/docs/concepts/cluster-administration/addons/ You can now join any number of machines by running the following on each node as root: kubeadm join 10.104.230.95:6443 --token u5k24m.no52g5zx4qtcmzfx --discovery-token-ca-cert-hash sha256:571c3581a2c88eed50156b8c720d179d0499660643907fc0cb2cc5a940292694
执行下面的命令进行配置:
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
Master 节点默认不参与工作负载,可以执行下面的命令来搭建一个 all-in-one 的 kubernetes 环境。
kubectl taint nodes --all node-role.kubernetes.io/master-
查看node运行状态
[root@k8s-master software]# kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-master NotReady master 3m39s v1.12.3
发现是NotReady状态,这是因为cni 网络插件没有安装的原因
安装 cni 网络插件。
docker pull quay.io/coreos/flannel:v0.10.0-amd64 mkdir -p /etc/cni/net.d/ vi /etc/cni/net.d/10-flannel.conf {"name":"cbr0","type":"flannel","delegate": {"isDefaultGateway": true}} mkdir /usr/share/oci-umount/oci-umount.d -p mkdir /run/flannel/ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.9.1/Documentation/kube-flannel.yml
查看系统pod运行状态
[root@k8s-master ~]# kubectl get pods -n kube-system NAME READY STATUS RESTARTS AGE coredns-576cbf47c7-cpd4t 1/1 Running 1 4m22s coredns-576cbf47c7-m46n6 1/1 Running 1 4m22s etcd-k8s-master 1/1 Running 0 3m45s kube-apiserver-k8s-master 1/1 Running 0 3m23s kube-controller-manager-k8s-master 1/1 Running 0 3m37s kube-flannel-ds-bzql2 1/1 Running 0 2m7s kube-proxy-7t97x 1/1 Running 0 4m22s kube-scheduler-k8s-master 1/1 Running 0 3m32s
可以看到所有pod都处于Running表示运行成功
再次查看node运行状态
[root@k8s-master ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-master Ready master 6m20s v1.12.3
发现处于Ready状态,表示kubernetes单机版本部署成功
* 有时候会遇到一些奇怪问题,这时候需要查看详细信息可运行以下命令
journalctl -f -u kubelet
最后希望大家都能顺利安装成功
来源:https://my.oschina.net/u/731542/blog/2966427
配合:https://www.cnblogs.com/huhyoung/p/9739027.html