Docker安装Kubernetes搭建集群

环境:Centeros7 jdk8 Docker1.7或1.8

1.关闭不必要的设置及一些基本源安装(先在第一台机器操作然后再复制一台机器)

$ setenforce 0

$ sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux

$ systemctl stop firewalld

$ systemctl disable firewalld

$ swapoff -a

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

$ vi  /etc/sysctl.d/k8s.conf

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

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

$ sysctl --system

$ yum install -y epel-release

$ yum install -y net-tools wget vim  ntpdate

$ vi /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
enabled=1

$yum makecache fast

安装kubeadm、kubectl、kubelet
$ yum install -y kubelet-1.12.1 kubeadm-1.12.1 kubectl-1.12.1 kubernetes-cni-0.6.0

$ systemctl enable kubelet && systemctl start kubelet
打开docker
systemctl start docker

拉取k8s需要的镜像

docker pull mirrorgooglecontainers/kube-apiserver:v1.12.1
docker pull mirrorgooglecontainers/kube-controller-manager:v1.12.1
docker pull mirrorgooglecontainers/kube-scheduler:v1.12.1
docker pull mirrorgooglecontainers/kube-proxy:v1.12.1
docker pull mirrorgooglecontainers/etcd-amd64:3.2.24
docker pull mirrorgooglecontainers/pause-amd64:3.1
docker pull mirrorgooglecontainers/etcd:3.2.24
docker pull mirrorgooglecontainers/pause:3.1
docker pull coredns/coredns:1.2.2
docker pull registry.cn-shanghai.aliyuncs.com/gcr-k8s/flannel:v0.10.0-amd64

docker tag mirrorgooglecontainers/kube-apiserver:v1.12.1 k8s.gcr.io/kube-apiserver:v1.12.1
docker tag mirrorgooglecontainers/kube-controller-manager:v1.12.1 k8s.gcr.io/kube-controller-manager:v1.12.1
docker tag mirrorgooglecontainers/kube-scheduler:v1.12.1 k8s.gcr.io/kube-scheduler:v1.12.1
docker tag mirrorgooglecontainers/kube-proxy:v1.12.1 k8s.gcr.io/kube-proxy:v1.12.1
docker tag mirrorgooglecontainers/etcd-amd64:3.2.24 k8s.gcr.io/etcd-amd64:3.2.24
docker tag mirrorgooglecontainers/pause-amd64:3.1 k8s.gcr.io/pause-amd64:3.1
docker tag mirrorgooglecontainers/etcd:3.2.24 k8s.gcr.io/etcd:3.2.24
docker tag mirrorgooglecontainers/pause:3.1 k8s.gcr.io/pause:3.1
docker tag coredns/coredns:1.2.2 k8s.gcr.io/coredns:1.2.2
docker tag registry.cn-shanghai.aliyuncs.com/gcr-k8s/flannel:v0.10.0-amd64 quay.io/coreos/flannel:v0.10.0-amd64

查看镜像结果
docker images | grep k8s
Docker安装Kubernetes搭建集群_第1张图片
image.png

注意: 这个时候需要关闭机器然后clone一台这样的主机(你可以clone2台1台留着备用,毕竟前期的安装花费了好长时间以后用的时候直接clone)

修改两台的主机名

#第一台主机名
hostnamectl set-hostname node5

#第二台主机名
hostnamectl set-hostname k8s2 
存储两台机器的ip和主机名对应
vi /etc/hosts
#第一台主机IP
192.168.229.141   node5
#第一台主机IP
192.168.229.142   k8s2
在第一台主机上进行操作(开启k8s)
kubeadm init --kubernetes-version=v1.12.1 --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.229.141
Docker安装Kubernetes搭建集群_第2张图片
image.png
执行第一个方框的命令(注意第二个框下面会有用)
  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config
创建 flannel 网络
sysctl net.bridge.bridge-nf-call-iptables=1
 wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
查看namespaces
kubectl get pod --all-namespaces
Docker安装Kubernetes搭建集群_第3张图片
image.png
查看节点
kubectl get node
image.png
可以看到只有一个节点
在第二台主机上进行操作(从节点加入到主节点构建集群)

还记得第一个框上的内容嘛!!!按照下面的命令开始操作

ip token sha256hash码
kubeadm join 192.168.229.141:6443 --token f7o0in.3wbmorzz5xowqijy --discovery-token-ca-cert-hash sha256:c6035a597546d445a25734eae0edeecf6124bbc25fad557097be39e55c01ebfd

在第一台主机上进行操作(查看节点是否加入)
kubectl get node
Docker安装Kubernetes搭建集群_第4张图片
image.png
可以看到状态改变了,从节点也加入了!至此k8s集群搭建完毕!!
(说实话刚开始心里也没底安装了好多东西~~~)
附录:
master节点是NotReady
kubectl create -f https://raw.githubusercontent.com/coreos/flannel/v0.9.1/Documentation/kube-flannel.yml
token会过期所以需要执行下面的命令(拿到token和shahash码)
kubeadm token list

kubeadm token create

openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'


springcloud in k8s kubeadm安装k8s -
kubeadm 生成的token过期后,集群增加节点 - discsthnew的博客 - CSDN博客
https://blog.csdn.net/u013355826/article/details/82786649

你可能感兴趣的:(Docker安装Kubernetes搭建集群)