准备阶段

系统环境:CentOS7.2

版本:kubernetes1.10.0

0.修改hostname

如果hostname是localhost,需要修改,比如:master node。

cat << EOF >> /etc/hosts 
172.16.110.124 k8s-master
172.16.110.121 k8s-node-1
172.16.110.122 k8s-node-2
172.16.110.123 k8s-node-2
EOF

1. 禁用防火墙

systemctl stop firewalld
systemctl disable firewalld
2. 禁用SELINUX

执行如下命令:

vi /etc/sysconfig/selinux

修改文件中的SELINUX为:

SELINUX=disabled

3.关闭swap内存

执行命令:

swapoff -a
4.调整内核参数

执行命令

vi /etc/sysctl.d/k8s.conf

在新建的k8s.cof文件中增加:

net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1

执行如下命令使之生效

sudo sysctl --system
4. 配置yum源镜像

#kubernetes yum源
vi /etc/yum.repos.d/kubernetes.repo

[kubernetes] 
name=Kubernetes 
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/ 
enabled=1 
gpgcheck=0

#docker yum源
vi /etc/yum.repos.d/docker.repo

[docker-repo] 
name=Docker 
baseurl=http://mirrors.aliyun.com/docker-engine/yum/repo/main/centos/7 
enabled=1 
gpgcheck=0
5.安装阶段

安装docker

下载kubernetes需要的镜像
指定安装Kubernetes的版本(安装1.15.0版本的)

yum makecache fast && yum install -y docker-ce kubelet-1.15.0  kubeadm-1.15.0 kubectl-1.15.0  kubernetes-cni-0.6.0

#需要的镜像
#下载并且打上指定的标签
docker pull mirrorgooglecontainers/kube-apiserver:v1.15.0
docker tag mirrorgooglecontainers/kube-apiserver:v1.15.0 k8s.gcr.io/kube-apiserver:v1.15.0
docker rmi mirrorgooglecontainers/kube-apiserver:v1.15.0
docker pull mirrorgooglecontainers/kube-controller-manager:v1.15.0
docker tag mirrorgooglecontainers/kube-controller-manager:v1.15.0 k8s.gcr.io/kube-controller-manager:v1.15.0
docker rmi mirrorgooglecontainers/kube-controller-manager:v1.15.0
docker pull mirrorgooglecontainers/kube-scheduler:v1.15.0
docker tag mirrorgooglecontainers/kube-scheduler:v1.15.0 k8s.gcr.io/kube-scheduler:v1.15.0
docker rmi mirrorgooglecontainers/kube-scheduler:v1.15.0
docker pull mirrorgooglecontainers/kube-proxy:v1.15.0
docker tag mirrorgooglecontainers/kube-proxy:v1.15.0 k8s.gcr.io/kube-proxy:v1.15.0
docker rmi mirrorgooglecontainers/kube-proxy:v1.15.0
docker pull mirrorgooglecontainers/pause:3.1
docker tag mirrorgooglecontainers/pause:3.1 k8s.gcr.io/pause:3.1
docker rmi mirrorgooglecontainers/pause:3.1
docker pull mirrorgooglecontainers/etcd:3.3.10
docker tag mirrorgooglecontainers/etcd:3.3.10 k8s.gcr.io/etcd:3.3.10
docker rmi mirrorgooglecontainers/etcd:3.3.10
docker pull coredns/coredns:1.3.1
docker tag coredns/coredns:1.3.1 k8s.gcr.io/coredns:1.3.1
docker rmi coredns/coredns:1.3.1

初始化kubernetes.
下载flannel并安装
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

kubectl apply -f kube-flannel.yml 

然后对组件进行查看,网络组件是成功的处于Running状态

kubectl get pod -n kube-system

在这里插入图片描述

kubeadm init --kubernetes-version=v1.10.0 --pod-network-cidr=10.244.0.0/16
安装成功的话会出现这句话 :

Your Kubernetes control-plane 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/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 172.16.110.124:6443 --token v4lkch.3drpqle91fzz9yvj \
    --discovery-token-ca-cert-hash sha256:3740b79e4e4f76652334c017fd5d706d83a04390290c27b3caf229b4b282c3ab 

配置访问集群的,同时好需要执行如下命令:

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

本文是讨论单机版的kubernetes,集群不做讨论。

单机版kubernetes为了运行Pod.需要删除主机上的Train.允许master执行Pod.
查询组件运行情况:

[root@performancevm-4 ~]# kubectl get pod -n kube-system
NAME                                                READY   STATUS    RESTARTS   AGE
coredns-5c98db65d4-kzzrw                            1/1     Running   0          52m
coredns-5c98db65d4-qdz89                            1/1     Running   0          52m
etcd-performancevm-4.novalocal                      1/1     Running   0          51m
kube-apiserver-performancevm-4.novalocal            1/1     Running   0          51m
kube-controller-manager-performancevm-4.novalocal   1/1     Running   0          51m
kube-flannel-ds-amd64-cprf8                         1/1     Running   0          39m
kube-proxy-bdn52                                    1/1     Running   0          52m
kube-scheduler-performancevm-4.novalocal            1/1     Running   0          51m

当所有的组件处于running状态就是期望的结果,尤其是kube-flannel-ds-amd64-cprf8组件,刚安装组件后需要等待一段时间后查询此内容,因为K8S安装成功后的组件不一定成功,还需要后台下载或运行某些程序。

验证master节点信息以及服务情况

[root@performancevm-4 ~]# kubectl get cs
NAME                 STATUS    MESSAGE             ERROR
controller-manager   Healthy   ok                  
scheduler            Healthy   ok                  
etcd-0               Healthy   {"health":"true"}   
[root@performancevm-4 ~]# kubectl get node
NAME                        STATUS   ROLES    AGE   VERSION
performancevm-4.novalocal   Ready    master   63m   v1.15.0
[root@performancevm-4 ~]# kubectl get service
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   63m

具体出现的错误信息以及处理办法:

出现The connection to the server localhost:8080 was refused - did you specify ...
或者出现The connection to the server 172.16.110.72:6443 was refused - did you specify the right host or port?
需要进行docker与K8S重启
先执行kubeadm reset ,然后执行这个kubeadm init ,执行第二个命令时候会出现错误提示:[ERROR DirAvailable--var-lib-etcd]: /var/lib/etcd is not empty
进入此目录后将其内容删除后再执行命令kubeadm init,生成新的K8S的id需要保存
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐