Docker安装K8S集群超详细讲解

本次在虚拟机中执行操作

首先第一步:

准备好三台节点,设置好静态ip地址,记住三台ip地址,我这边演示的ip地址分别为:

 192.168.125.101  master

192.168.125.102  node1

192.168.125.103  node2

其中三台名字命名方式为:

hostnamectl set-hostname master  #此命令在master节点中执行,两台node机器同理

Docker安装K8S集群超详细讲解_第1张图片

 接下来正式进行docker安装操作,三台机器同时操作,不要只执行一个节点机器

1:关闭防火墙:

$ systemctl stop firewalld
$ systemctl disable firewalld

2:关闭 selinux:

$ sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久
$ setenforce 0 # 临时

3: 关闭 swap:

$ swapoff -a # 临时
$ vim /etc/fstab # 永久

4: 添加 hosts:

 $ cat >> /etc/hosts << EOF
192.168.125.101  master

192.168.125.102  node1

192.168.125.103  node2
EOF

$ sysctl --system # 生效

5: 时间同步

$ yum install ntpdate -y
$ ntpdate time.windows.com

6:安装Docker

$ yum install wget
$ wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O
/etc/yum.repos.d/docker-ce.repo
$ yum -y install docker-ce-18.06.1.ce-3.el7
$ systemctl enable docker && systemctl start docker
$ docker --version

7:添加阿里云 YUM 软件源,设置仓库地址

# cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF

8:添加 yum 源

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

9:安装指定版本的 kubeadm,kubelet 和 kubectl,为了下面拉取对应版本镜像做准备

(这边如果不安装指定版本的会和下面的拉取安装产生冲突,版本不一致,这里一定安装指定版本)

$  yum install -y kubelet-1.17.0 kubeadm-1.17.0 kubectl-1.17.0

$ systemctl enable kubelet

这里开始只在Master中执行

10:部署 Kubernetes Master(版本和上面的是一致的)

$ kubeadm init \
--apiserver-advertise-address=192.168.125.101 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.17.0 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16

此步运算完之后会出现这样的运行结果--------------------------------------------------
To start using your cluster, you need to run the following as a regular user:
(下面这三行在master运行
  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:
(下面这2行在后面在node节点运行)
kubeadm join 192.168.125.101:6443 --token ag45ch.vb3bogd994laxhx6 \
    --discovery-token-ca-cert-hash sha256:8e221dbd61f7bf4c8498387883d490f3dc00487df88f7d99e52e0ca481760f8a 
-------------------------------------------------------------------------------------

11:在master中执行

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

12:在其他两个node节点中执行,(这里执行的是你自己生成的命令查看你自己的)

kubeadm join 192.168.125.101:6443 --token ag45ch.vb3bogd994laxhx6 \
    --discovery-token-ca-cert-hash sha256:8e221dbd61f7bf4c8498387883d490f3dc00487df88f7d99e52e0ca481760f8a 

13:由于默认拉取镜像地址 k8s.gcr.io 国内无法访问,这里指定阿里云镜像仓库地址。

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kubeflannel.yml

第二种
[root@master ~]# wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
root@master ~]# sed -i 's/quay.io/quay-mirror.qiniu.com/g' kube-flannel.yml
[root@master ~]# kubectl apply -f kube-flannel.yml

拉取完之后执行,kubectl get nodes,就会显示全部就绪状态

Docker安装K8S集群超详细讲解_第2张图片

14:测试 kubernetes 集群,在 Kubernetes 集群中创建一个 pod,验证是否正常运行:

$ kubectl create deployment nginx --image=nginx
$ kubectl expose deployment nginx --port=80 --type=NodePort
$ kubectl get pod,svc

 接着去网站输入暴露的地址,就完成啦

Docker安装K8S集群超详细讲解_第3张图片

 如果有用请多多点赞收藏,谢谢

你可能感兴趣的:(docker,kubernetes,容器)