最简单的kubernetes入门实验教程

实验环境:国外云服务器

  • 使用国外服务器的优点:
    • 不需要挂代理去下载k8s
    • 费用便宜且按小时算(0.007美元一个小时)
      • 做完整个实验最多花几毛人民币
    • 支持支付宝支付
  • 推荐国外服务器:https://www.vultr.com/?ref=7320847

安装三个云主机的k8s环境

安装加速(可选)

  • wget --no-check-certificate https://github.com/teddysun/across/raw/master/bbr.sh && chmod +x bbr.sh && ./bbr.sh

安装docker

  • 运行命令:curl -fsSL https://get.docker.com | bash -s docker

安装k8s三个基本程序

https://kubernetes.io/docs/setup/independent/install-kubeadm/

  • 运行下面命令:
apt-get update && apt-get install -y apt-transport-https curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat </etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y kubelet kubeadm kubectl
apt-mark hold kubelet kubeadm kubectl

初始化master/node主机

master主机安装5大基本组件

master节点位于新加波

  • 由kubeadm帮我们自动安装

    • kubeadm init --apiserver-advertise-address 0.0.0.0 --pod-network-cidr=10.244.0.0/16
      • --pod-network-cidr : flannel网络要求设置
  • kubeadm初始化完成后,根据提示操作

    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 178.128.214.74:6443 --token 3agf5r.okfloxs1qz0kjpyd --discovery-token-ca-cert-hash sha256:e34ef064bd33fcf951a7c6a7b934f18334a169db300dba1e71d06e42d3db1c8a
    
  • root用户还需运行命令export KUBECONFIG=/etc/kubernetes/admin.conf

  • 运行kubectl命令查看是否安装成功

    kubectl get pod --all-namespaces
    

安装网络插件

https://kubernetes.io/docs/setup/independent/create-cluster-kubeadm/#pod-network

  • 运行下面命令
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.10.0/Documentation/kube-flannel.yml

节点node1加入到集群

node1位于美国纽约

  • 运行命令:
kubeadm join 178.128.214.74:6443 --token 3agf5r.okfloxs1qz0kjpyd --discovery-token-ca-cert-hash sha256:e34ef064bd33fcf951a7c6a7b934f18334a169db300dba1e71d06e42d3db1c8a

节点node1加入到集群

node1位于美国旧金山

  • 运行命令:
kubeadm join 178.128.214.74:6443 --token 3agf5r.okfloxs1qz0kjpyd --discovery-token-ca-cert-hash sha256:e34ef064bd33fcf951a7c6a7b934f18334a169db300dba1e71d06e42d3db1c8a

检验两个节点是否已经加入到k8s集群

  • 运行kubectl get nodes命令,获得k8s集群中存在的节点,输出如下:
    NAME              STATUS     ROLES     AGE       VERSION
    ubuntu-s-master   Ready      master    34m       v1.11.2
    ubuntu-s-node1    NotReady       10s       v1.11.2
    ubuntu-s-node2    Ready          5m        v1.11.2
    

在k8s集群中运行httpd服务

  • 运行下面命令简单地部署httpd程序

    kubectl run httpd-app --image=httpd --replicas=2
    
  • 查看部署结果:

    root@ubuntu-s-master:~# kubectl get deployment
    NAME        DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
    httpd-app   2         2         2            2           1m
    
  • 查看httpd在集群内的ip地址

    root@ubuntu-s-master:~# kubectl get pod -o wide
    NAME                         READY     STATUS    RESTARTS   AGE       IP           NODE             NOMINATED NODE
    httpd-app-7bdd9f8ff4-hhszt   1/1       Running   0          6m        10.244.2.2   ubuntu-s-node1   
    httpd-app-7bdd9f8ff4-qp7wl   1/1       Running   0          6m        10.244.1.4   ubuntu-s-node2   
    
  • 在master主机上用命令curl 10.244.2.2访问httpd服务,输出如下

    It works!

  • 现在我们虽然已经成功在k8s集群中部署了一个httpd服务,但仅是上面的配置的话,外网是无法访问我们的服务的(仅能在三台云主机上访问)。

  • 所以我们要在master主机执行下面的命令,让我们的httpd服务可以被外网访问

    kubectl expose deployment httpd-app --type=NodePort --name=httpd-service --port=8080 --target-port=80
    
  • 然后我们用浏览器访问两台node云主机中任意一台的8080端口,如下

image.png

你可能感兴趣的:(最简单的kubernetes入门实验教程)