zoukankan      html  css  js  c++  java
  • Ubuntu16.04安装kubernetes1.13集群

    Ubuntu16.04安装kubernetes1.13集群

    最新的安装可以使用以下方式:https://www.cnrancher.com/docs/rancher/v2.x/cn/overview/quick-start-guide

    方便,快捷!

    以下为正文。

    前言

    Docker容器化,虚拟化技术上的又一个猛将,可以极高提高软件部署的速度。运行一个Docker容器,这个容器作为一个进程分配了计算资源,不同容器之间资源隔离,仿佛每个容器都是一台机器,
    并且通过宿主机网桥,可以模拟一个局域网。可以参考: 新手教程

    Docker-compose可以管理单机上的容器们,但是多机管理还做不到,目前Kubernetes容器编排是最好的解决方案,可实现多机的容器编排。

    Kubernetes简称为k8s,它是 Google 开源的容器集群管理系统。在Docker技术的基础上,为容器化的应用提供部署运行、资源调度、服务发现和动态伸缩等一系列完整功能,提高了大规模容器集群管理的便捷性。

    k8s因为种种原因,在中国很难安装得上,毕竟网不好。通过搭建k8s,我们也算追得上潮流。我们准备安装最新的k8s1.13。

    准备工作

    我们可以通过:VMware Workstation多台虚拟机安装 新建三台Ubuntu16.04机器:

    三台机器的主机名和IP

    ubuntu1:192.168.152.3	4G内存 20G硬盘 必须2CPU核
    ubuntu2:192.168.152.4	2G内存 20G硬盘
    ubuntu3:192.168.152.5	2G内存 20G硬盘
    

    我们必须永久关闭swap空间:

    vim /etc/fstab#注释掉所有swap分区,然后重启三台机器!

    安装k8s集群

    以下的命令都是用root用户进行。

    安装Docker

    在每台机器上运行Docker官方的安装脚本:

    curl -fsSL get.docker.com -o get-docker.sh
    sh get-docker.sh --mirror Aliyun
    

    安装kubelet kubeadm kubectl

    在4G内存的ubuntu1机器上,更换阿里源,并安装kubelet kubeadm kubectl:

    apt-get update && apt-get install -y apt-transport-https
    
    curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
    
    cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
    deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
    EOF
    
    apt-get update
    apt-get install -y kubelet kubeadm kubectl
    

    使用kubeadm创建一个集群

    1.13.0 版本提供了中国特供的一个功能,可以解决以前镜像被墙的问题。

    kubeadm init 
    	--image-repository registry.aliyuncs.com/google_containers 
        --pod-network-cidr=192.167.0.0/16 
        --ignore-preflight-errors=cri 
        --kubernetes-version=1.13.1
    

    镜像从registry.aliyuncs.com/google_containers阿里云处下载,且pod的网段改为192.167.0.0/16(怕虚拟机与外部网络冲突了)。

    耐心等待后出现:

    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 192.168.152.3:6443 --token to697t.fdu5pffmr0862z8g --discovery-token-ca-cert-hash sha256:15da0d9ac768ad5fe546a2b93ed7516222fa043ef9d5e454a72e1f2ca4870862
    
    

    上面的kubeadm join 192.168.152.3:6443 --token to697t.fdu5pffmr0862z8g --discovery-token-ca-cert-hash sha256:15da0d9ac768ad5fe546a2b93ed7516222fa043ef9d5e454a72e1f2ca4870862请记住。

    然后你可以用普通用户,或者root用户:

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

    这样你就可以使用该用户操作k8s集群了。

    我们可以使用kubectl get命令来查看当前唯一一个节点的状态:

    root@ubuntu1:~# kubectl get nodes
    NAME      STATUS     ROLES    AGE     VERSION
    ubuntu1   NotReady   master   2m59s   v1.13.2
    

    我们可以使用kubectl describe查看这个节点(Node)对象的详细信息、状态和事件(Event)信息:

    root@ubuntu1:~# kubectl describe node ubuntu1
    Name:               ubuntu1
    Roles:              master
    Labels:             beta.kubernetes.io/arch=amd64
                        beta.kubernetes.io/os=linux
                        kubernetes.io/hostname=ubuntu1
                        node-role.kubernetes.io/master=
    Annotations:        kubeadm.alpha.kubernetes.io/cri-socket: /var/run/dockershim.sock
                        node.alpha.kubernetes.io/ttl: 0
                        volumes.kubernetes.io/controller-managed-attach-detach: true
    CreationTimestamp:  Fri, 18 Jan 2019 10:34:29 +0800
    Taints:             node-role.kubernetes.io/master:NoSchedule
                        node.kubernetes.io/not-ready:NoSchedule
    Unschedulable:      false
    Conditions:
      Type             Status  LastHeartbeatTime                 LastTransitionTime                Reason                       Message
      ----             ------  -----------------                 ------------------                ------                       -------
      MemoryPressure   False   Fri, 18 Jan 2019 10:39:19 +0800   Fri, 18 Jan 2019 10:34:29 +0800   KubeletHasSufficientMemory   kubelet has sufficient memory available
      DiskPressure     False   Fri, 18 Jan 2019 10:39:19 +0800   Fri, 18 Jan 2019 10:34:29 +0800   KubeletHasNoDiskPressure     kubelet has no disk pressure
      PIDPressure      False   Fri, 18 Jan 2019 10:39:19 +0800   Fri, 18 Jan 2019 10:34:29 +0800   KubeletHasSufficientPID      kubelet has sufficient PID available
      Ready            False   Fri, 18 Jan 2019 10:39:19 +0800   Fri, 18 Jan 2019 10:34:29 +0800   KubeletNotReady              runtime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized
    

    我们知道NotReady的原因是:

      Ready            False   Fri, 18 Jan 2019 10:39:19 +0800   Fri, 18 Jan 2019 10:34:29 +0800   KubeletNotReady              runtime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized
    

    因为我们尚未部署任何网络插件。

    我们还可以通过kubectl检查这个节点上各个系统Pod的状态,其中kube-system是k8s项目预留的系统Pod的工作空间:

    root@ubuntu1:~# kubectl get pods -n kube-system
    NAME                              READY   STATUS              RESTARTS   AGE
    coredns-78d4cf999f-7k9bv          0/1     ContainerCreating   0          7m6s
    coredns-78d4cf999f-mtz4b          0/1     ContainerCreating   0          7m6s
    etcd-ubuntu1                      1/1     Running             0          6m16s
    kube-apiserver-ubuntu1            1/1     Running             0          6m35s
    kube-controller-manager-ubuntu1   1/1     Running             0          6m7s
    kube-proxy-26mzm                  1/1     Running             0          7m6s
    kube-scheduler-ubuntu1            1/1     Running             0          6m31s
    

    有些失败了,因为我们尚未部署任何网络插件。

    默认情况下 Master 节点是不允许运行用户 Pod 的,而 Kubernetes 做到这一点,依靠的是 是 Kubernetes 的 Taint/Toleration 机制。一旦某个节点被加上了一个 Taint,即被“打上了污点”那么所有 Pod 就都不能在这个节点上运行,因为 Kubernetes 的 Pod 都有“洁癖”。

    root@ubuntu1:~# kubectl describe node ubuntu1
    Name:               ubuntu1
    Roles:              master
    Labels:             beta.kubernetes.io/arch=amd64
                        beta.kubernetes.io/os=linux
                        kubernetes.io/hostname=ubuntu1
                        node-role.kubernetes.io/master=
    Annotations:        kubeadm.alpha.kubernetes.io/cri-socket: /var/run/dockershim.sock
                        node.alpha.kubernetes.io/ttl: 0
                        volumes.kubernetes.io/controller-managed-attach-detach: true
    CreationTimestamp:  Fri, 18 Jan 2019 10:34:29 +0800
    Taints:             node-role.kubernetes.io/master:NoSchedule
    

    可以看到Taints: node-role.kubernetes.io/master:NoSchedule,我们去掉这个污点,一个单节点的集群就创建成功了,听起来十分有趣,单节点的集群。

    我们现在执行:

    kubectl taint nodes --all node-role.kubernetes.io/master-
    

    这个步骤的配置最终使Master节点能允许运行用户pod,也是确保下面插件部署能正确运行。

    到了这一步,一个基本完整的 Kubernetes 集群就完毕了!! 撒花,感谢之前写教程的大哥哥~~~~

    部署插件

    我们要安装一些插件来辅助k8s。

    部署网络组件

    直接运行安装命令:

    # 安装
    kubectl apply -f https://git.io/weave-kube-1.6
    
    # 删除
    kubectl delete -f https://git.io/weave-kube-1.6
    

    查看pod是否部署成功:

    kubectl get pods -n kube-system
    

    部署容器存储插件

    # 安装
    kubectl apply -f https://raw.githubusercontent.com/rook/rook/master/cluster/examples/kubernetes/ceph/operator.yaml
    
    kubectl apply -f https://raw.githubusercontent.com/rook/rook/master/cluster/examples/kubernetes/ceph/cluster.yaml
    
    
    # 删除
    kubectl delete -f https://raw.githubusercontent.com/rook/rook/master/cluster/examples/kubernetes/ceph/operator.yaml
    
    kubectl delete -f https://raw.githubusercontent.com/rook/rook/master/cluster/examples/kubernetes/ceph/cluster.yaml
    
    # 查看安装情况
    kubectl get pods -n rook-ceph-system
    
    kubectl get pods -n rook-ceph
    

    工作节点加入master

    在另外两台ubuntu上安装kubeadm, kubelet and kubectl

    apt-get update && apt-get install -y apt-transport-https
    
    curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
    
    cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
    deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
    EOF
    
    apt-get update
    apt-get install -y kubelet kubeadm kubectl
    

    然后输入:

    kubeadm join 192.168.152.3:6443 --token to697t.fdu5pffmr0862z8g --discovery-token-ca-cert-hash sha256:15da0d9ac768ad5fe546a2b93ed7516222fa043ef9d5e454a72e1f2ca4870862
    

    这时候回到master节点服务器,运行下面命令查看节点状态:

    kubectl get nodes
    

    如果发现

    root@ubuntu1:~# kubectl get nodes
    NAME      STATUS     ROLES    AGE   VERSION
    ubuntu1   Ready      master   73m   v1.13.2
    ubuntu2   NotReady   <none>   11m   v1.13.2
    ubuntu3   NotReady   <none>   32s   v1.13.2
    

    可以在NotReady节点用:

    journalctl -f -u kubelet
    

    查看原因,有时候因为网络不稳定,所以会一直重试。

    如果我们忘记了Master节点的加入token,可以使用如下命令来查看:

    kubeadm token list
    

    默认情况下,token的有效期是24小时,如果我们的token已经过期的话,可以使用以下命令重新生成:

    kubeadm token create
    

    如果我们也没有–discovery-token-ca-cert-hash的值,可以使用以下命令生成:

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

    这样集群就搭好了,接下来我们来使用。

    k8s使用

    我们进入实践篇。可学习k8s文档

    1.获取命名空间下的POD:

    kubectl get  pod  -n kube-system
    

    2.获取所有命名空间下的POD

    kubectl get pods --all-namespaces
    

    3.查看POD日志:

    kubectl logs -f --tail=20 kubernetes-dashboard-57df4db6b-m9zbq -n kube-system
    

    查看命名空间kube-system下的kubernetes-dashboard-57df4db6b-m9zbq日志。

    4.查看命名空间下的服务:

    kubectl get svc -n kube-system
    

    此篇参考: 俊先生的文章

  • 相关阅读:
    str_split — 将字符串转换为数组
    str_replace — 子字符串替换
    str_pad — 使用另一个字符串填充字符串为指定长度
    parse_str — 将字符串解析成多个变量
    number_format — 以千位分隔符方式格式化一个数字
    企业邮箱的优势有哪些?使用企业邮箱的好处
    TOM VIP邮箱,化繁为简,在微信里收发邮件
    邮箱办公神操作,让办公更自在,沟通无边界!
    常用的企业邮箱有哪些?企业邮箱有哪几种?
    外贸企业邮箱批发,收费企业邮箱与免费企业邮箱区别
  • 原文地址:https://www.cnblogs.com/nima/p/11751322.html
Copyright © 2011-2022 走看看