01 搭建K8s集群[无需科学上网]

官网https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/#installing-kubeadm-kubelet-and-kubectl

GitHubhttps://github.com/kubernetes/kubeadm

课程中:使用kubeadm搭建一个3台机器组成的k8s集群,1台master节点,2台worker节点

如果大家机器配置不够,也可以使用在线的,或者minikube的方式或者1个master和1个worker

配置要求

  • One or more machines running one of:

    • Ubuntu 16.04+
    • Debian 9+
    • CentOS 7【课程中使用】
    • Red Hat Enterprise Linux (RHEL) 7
    • Fedora 25+
    • HypriotOS v1.0.1+
    • Container Linux (tested with 1800.6.0)
  • 2 GB or more of RAM per machine (any less will leave little room for your apps)

  • 2 CPUs or more
  • Full network connectivity between all machines in the cluster (public or private network is fine)
  • Unique hostname, MAC address, and product_uuid for every node. See here for more details.
  • Certain ports are open on your machines. See here for more details.
  • Swap disabled. You MUST disable swap in order for the kubelet to work properly.

1.1 版本统一

  1. Docker 18.09.0
  2. ---
  3. kubeadm-1.14.0-0
  4. kubelet-1.14.0-0
  5. kubectl-1.14.0-0
  6. ---
  7. k8s.gcr.io/kube-apiserver:v1.14.0
  8. k8s.gcr.io/kube-controller-manager:v1.14.0
  9. k8s.gcr.io/kube-scheduler:v1.14.0
  10. k8s.gcr.io/kube-proxy:v1.14.0
  11. k8s.gcr.io/pause:3.1
  12. k8s.gcr.io/etcd:3.3.10
  13. k8s.gcr.io/coredns:1.3.1
  14. ---
  15. calico:v3.9

1.2 准备3台centos

大家根据自己的情况来准备centos7的虚拟机。

要保证彼此之间能够ping通,也就是处于同一个网络中,虚拟机的配置要求上面也描述咯。

1.3 更新并安装依赖

3台机器都需要执行

  1. yum -y update
  2. yum install -y conntrack ipvsadm ipset jq sysstat curl iptables libseccomp

1.4 安装Docker

根据之前学习的Docker方式[Docker第一节课的笔记中也有这块的说明]

在每一台机器上都安装好Docker,版本为18.09.0

```shell 01 安装必要的依赖 sudo yum install -y yum-utils \ device-mapper-persistent-data \ lvm2

02 设置docker仓库 sudo yum-config-manager —add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

【设置要设置一下阿里云镜像加速器】 sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-‘EOF’ { “registry-mirrors”: [“这边替换成自己的实际地址”] } EOF sudo systemctl daemon-reload

03 安装docker

yum install -y docker-ce-18.09.0 docker-ce-cli-18.09.0 containerd.io

04 启动docker sudo systemctl start docker && sudo systemctl enable docker

  1. <a name="c541559e"></a>
  2. ## 1.5 修改hosts文件
  3. > (1)master
  4. ```shell
  5. # 设置master的hostname,并且修改hosts文件
  6. sudo hostnamectl set-hostname m
  7. vi /etc/hosts
  8. 192.168.8.51 m
  9. 192.168.8.61 w1
  10. 192.168.8.62 w2

(2)两个worker

  1. # 设置worker01/02的hostname,并且修改hosts文件
  2. sudo hostnamectl set-hostname w1
  3. sudo hostnamectl set-hostname w2
  4. vi /etc/hosts
  5. 192.168.8.51 m
  6. 192.168.8.61 w1
  7. 192.168.8.62 w2

(3)使用ping测试一下

1.6 系统基础前提配置

  1. # (1)关闭防火墙
  2. systemctl stop firewalld && systemctl disable firewalld
  3. # (2)关闭selinux
  4. setenforce 0
  5. sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
  6. # (3)关闭swap
  7. swapoff -a
  8. sed -i '/swap/s/^\(.*\)$/#\1/g' /etc/fstab
  9. # (4)配置iptables的ACCEPT规则
  10. iptables -F && iptables -X && iptables -F -t nat && iptables -X -t nat && iptables -P FORWARD ACCEPT
  11. # (5)设置系统参数
  12. cat <<EOF > /etc/sysctl.d/k8s.conf
  13. net.bridge.bridge-nf-call-ip6tables = 1
  14. net.bridge.bridge-nf-call-iptables = 1
  15. EOF
  16. sysctl --system

1.7 Installing kubeadm, kubelet and kubectl

(1)配置yum源

  1. cat <<EOF > /etc/yum.repos.d/kubernetes.repo
  2. [kubernetes]
  3. name=Kubernetes
  4. baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
  5. enabled=1
  6. gpgcheck=0
  7. repo_gpgcheck=0
  8. gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
  9. http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
  10. EOF

(2)安装kubeadm&kubelet&kubectl

  1. yum install -y kubeadm-1.14.0-0 kubelet-1.14.0-0 kubectl-1.14.0-0

(3)docker和k8s设置同一个cgroup

  1. # docker
  2. vi /etc/docker/daemon.json
  3. "exec-opts": ["native.cgroupdriver=systemd"],
  4. systemctl restart docker
  5. # kubelet,这边如果发现输出directory not exist,也说明是没问题的,大家继续往下进行即可
  6. sed -i "s/cgroup-driver=systemd/cgroup-driver=cgroupfs/g" /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
  7. systemctl enable kubelet && systemctl start kubelet

1.8 proxy/pause/scheduler等国内镜像

(1)查看kubeadm使用的镜像

kubeadm config images list

可以发现这里都是国外的镜像

  1. k8s.gcr.io/kube-apiserver:v1.14.0
  2. k8s.gcr.io/kube-controller-manager:v1.14.0
  3. k8s.gcr.io/kube-scheduler:v1.14.0
  4. k8s.gcr.io/kube-proxy:v1.14.0
  5. k8s.gcr.io/pause:3.1
  6. k8s.gcr.io/etcd:3.3.10
  7. k8s.gcr.io/coredns:1.3.1

(2)解决国外镜像不能访问的问题

  • 创建kubeadm.sh脚本,用于拉取镜像/打tag/删除原有镜像
  1. #!/bin/bash
  2. set -e
  3. KUBE_VERSION=v1.14.0
  4. KUBE_PAUSE_VERSION=3.1
  5. ETCD_VERSION=3.3.10
  6. CORE_DNS_VERSION=1.3.1
  7. GCR_URL=k8s.gcr.io
  8. ALIYUN_URL=registry.cn-hangzhou.aliyuncs.com/google_containers
  9. images=(kube-proxy:${KUBE_VERSION}
  10. kube-scheduler:${KUBE_VERSION}
  11. kube-controller-manager:${KUBE_VERSION}
  12. kube-apiserver:${KUBE_VERSION}
  13. pause:${KUBE_PAUSE_VERSION}
  14. etcd:${ETCD_VERSION}
  15. coredns:${CORE_DNS_VERSION})
  16. for imageName in ${images[@]} ; do
  17. docker pull $ALIYUN_URL/$imageName
  18. docker tag $ALIYUN_URL/$imageName $GCR_URL/$imageName
  19. docker rmi $ALIYUN_URL/$imageName
  20. done
  • 运行脚本和查看镜像
  1. # 运行脚本
  2. sh ./kubeadm.sh
  3. # 查看镜像
  4. docker images
  • 将这些镜像推送到自己的阿里云仓库【可选,根据自己实际的情况】
  1. # 登录自己的阿里云仓库
  2. docker login --username=xxx registry.cn-hangzhou.aliyuncs.com
  1. #!/bin/bash
  2. set -e
  3. KUBE_VERSION=v1.14.0
  4. KUBE_PAUSE_VERSION=3.1
  5. ETCD_VERSION=3.3.10
  6. CORE_DNS_VERSION=1.3.1
  7. GCR_URL=k8s.gcr.io
  8. ALIYUN_URL=xxx
  9. images=(kube-proxy:${KUBE_VERSION}
  10. kube-scheduler:${KUBE_VERSION}
  11. kube-controller-manager:${KUBE_VERSION}
  12. kube-apiserver:${KUBE_VERSION}
  13. pause:${KUBE_PAUSE_VERSION}
  14. etcd:${ETCD_VERSION}
  15. coredns:${CORE_DNS_VERSION})
  16. for imageName in ${images[@]} ; do
  17. docker tag $GCR_URL/$imageName $ALIYUN_URL/$imageName
  18. docker push $ALIYUN_URL/$imageName
  19. docker rmi $ALIYUN_URL/$imageName
  20. done

运行脚本 sh ./kubeadm-push-aliyun.sh

1.9 kube init初始化master

(1)kube init流程

  1. 01-进行一系列检查,以确定这台机器可以部署kubernetes
  2. 02-生成kubernetes对外提供服务所需要的各种证书可对应目录
  3. /etc/kubernetes/pki/*
  4. 03-为其他组件生成访问kube-ApiServer所需的配置文件
  5. ls /etc/kubernetes/
  6. admin.conf controller-manager.conf kubelet.conf scheduler.conf
  7. 04-为 Master组件生成Pod配置文件。
  8. ls /etc/kubernetes/manifests/*.yaml
  9. kube-apiserver.yaml
  10. kube-controller-manager.yaml
  11. kube-scheduler.yaml
  12. 05-生成etcd的Pod YAML文件。
  13. ls /etc/kubernetes/manifests/*.yaml
  14. kube-apiserver.yaml
  15. kube-controller-manager.yaml
  16. kube-scheduler.yaml
  17. etcd.yaml
  18. 06-一旦这些 YAML 文件出现在被 kubelet 监视的/etc/kubernetes/manifests/目录下,kubelet就会自动创建这些yaml文件定义的pod,即master组件的容器。master容器启动后,kubeadm会通过检查localhost:6443/healthz这个master组件的健康状态检查URL,等待master组件完全运行起来
  19. 07-为集群生成一个bootstrap token
  20. 08-将ca.crt等 Master节点的重要信息,通过ConfigMap的方式保存在etcd中,工后续部署node节点使用
  21. 09-最后一步是安装默认插件,kubernetes默认kube-proxy和DNS两个插件是必须安装的

(2)初始化master节点

官网:https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm/

注意此操作是在主节点上进行

  1. # 本地有镜像
  2. kubeadm init --kubernetes-version=1.14.0 --apiserver-advertise-address=192.168.8.51 --pod-network-cidr=10.244.0.0/16
  3. 【若要重新初始化集群状态:kubeadm reset,然后再进行上述操作】

记得保存好最后kubeadm join的信息

(3)根据日志提示

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

此时kubectl cluster-info查看一下是否成功

(4)查看pod验证一下

等待一会儿,同时可以发现像etc,controller,scheduler等组件都以pod的方式安装成功了

注意:coredns没有启动,需要安装网络插件

  1. kubectl get pods -n kube-system

(5)健康检查

  1. curl -k https://localhost:6443/healthz

1.10 部署calico网络插件

选择网络插件:https://kubernetes.io/docs/concepts/cluster-administration/addons/

calico网络插件:https://docs.projectcalico.org/v3.9/getting-started/kubernetes/

calico,同样在master节点上操作

  1. # 在k8s中安装calico
  2. kubectl apply -f https://docs.projectcalico.org/v3.9/manifests/calico.yaml
  3. # 确认一下calico是否安装成功
  4. kubectl get pods --all-namespaces -w

1.11 kube join

记得保存初始化master节点的最后打印信息【注意这边大家要自己的,下面我的只是一个参考】

  1. kubeadm join 192.168.0.51:6443 --token yu1ak0.2dcecvmpozsy8loh \
  2. --discovery-token-ca-cert-hash sha256:5c4a69b3bb05b81b675db5559b0e4d7972f1d0a61195f217161522f464c307b0

(1)在woker01和worker02上执行上述命令

(2)在master节点上检查集群信息

  1. kubectl get nodes
  2. NAME STATUS ROLES AGE VERSION
  3. master-kubeadm-k8s Ready master 19m v1.14.0
  4. worker01-kubeadm-k8s Ready <none> 3m6s v1.14.0
  5. worker02-kubeadm-k8s Ready <none> 2m41s v1.14.0

1.12 再次体验Pod

(1)定义pod.yml文件,比如pod_nginx_rs.yaml

  1. cat > pod_nginx_rs.yaml <<EOF
  2. apiVersion: apps/v1
  3. kind: ReplicaSet
  4. metadata:
  5. name: nginx
  6. labels:
  7. tier: frontend
  8. spec:
  9. replicas: 3
  10. selector:
  11. matchLabels:
  12. tier: frontend
  13. template:
  14. metadata:
  15. name: nginx
  16. labels:
  17. tier: frontend
  18. spec:
  19. containers:
  20. - name: nginx
  21. image: nginx
  22. ports:
  23. - containerPort: 80
  24. EOF

(2)根据pod_nginx_rs.yml文件创建pod

  1. kubectl apply -f pod_nginx_rs.yaml

(3)查看pod

  1. kubectl get pods
  2. kubectl get pods -o wide
  3. kubectl describe pod nginx

(4)感受通过rs将pod扩容

  1. kubectl scale rs nginx --replicas=5
  2. kubectl get pods -o wide

(5)删除pod

  1. kubectl delete -f pod_nginx_rs.yaml

02 Basic

2.1 yaml文件

2.1.1 简介

YAML(IPA: /ˈjæməl/)是一个可读性高的语言,参考了XML、C、Python等。

理解:Yet Another Markup Language

后缀:可以是.yml或者是.yaml,更加推荐.yaml,其实用任意后缀都可以,只是阅读性不强

2.1.2 基础

  • 区分大小写
  • 缩进表示层级关系,相同层级的元素左对齐
  • 缩进只能使用空格,不能使用TAB
  • “#”表示当前行的注释
  • 是JSON文件的超级,两个可以转换
  • —-表示分隔符,可以在一个文件中定义多个结构
  • 使用key: value,其中”:”和value之间要有一个英文空格

2.1.3 Maps

2.1.3.1 简单

  1. apiVersion: v1
  2. kind: Pod

—-表示分隔符,可选。要定义多个结构一定要分隔

apiVersion表示key,v1表示value,英文”:”后面要有一个空格

kind表示key,Pod表示value

也可以这样写apiVersion: “v1”

转换为JSON格式

  1. {
  2. "apiVersion": "v1",
  3. "kind": "Pod"
  4. }
  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx-deployment
  5. labels:
  6. app: nginx

2.1.2.2 复杂

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx-deployment
  5. labels:
  6. app: nginx

metadata表示key,下面的内容表示value,该value中包含两个直接的key:name和labels

name表示key,nginx-deployment表示value

labels表示key,下面的表示value,这个值又是一个map

app表示key,nginx表示value

相同层级的记得使用空间缩进,左对齐

转换为JSON格式

  1. {
  2. "apiVersion": "apps/v1",
  3. "kind": "Deployment",
  4. "metadata": {
  5. "name": "nginx-deployment",
  6. "labels": {
  7. "app": "nginx"
  8. }
  9. }
  10. }

2.1.4 Lists

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: myapp-pod
  5. labels:
  6. app: myapp
  7. spec:
  8. containers:
  9. - name: myapp-container01
  10. image: busybox:1.28
  11. - name: myapp-container02
  12. image: busybox:1.28

containers表示key,下面的表示value,其中value是一个数组

数组中有两个元素,每个元素里面包含name和image

image表示key,myapp-container表示value

转换成JSON格式

  1. {
  2. "apiVersion": "v1",
  3. "kind": "Pod",
  4. "metadata": {
  5. "name": "myapp",
  6. "labels": {
  7. "app": "myapp"
  8. }
  9. },
  10. "spec": {
  11. "containers": [{
  12. "name": "myapp-container01",
  13. "image": "busybox:1.28",
  14. },
  15. {
  16. "name": "myapp-container02",
  17. "image": "busybox:1.28",
  18. }]
  19. }
  20. }

2.1.5 找个k8s的yaml文件

官网https://kubernetes.io/docs/reference/

  1. # yaml格式对于Pod的定义:
  2. apiVersion: v1 #必写,版本号,比如v1
  3. kind: Pod #必写,类型,比如Pod
  4. metadata: #必写,元数据
  5. name: nginx #必写,表示pod名称
  6. namespace: default #表示pod名称属于的命名空间
  7. labels:
  8. app: nginx #自定义标签名字
  9. spec: #必写,pod中容器的详细定义
  10. containers: #必写,pod中容器列表
  11. - name: nginx #必写,容器名称
  12. image: nginx #必写,容器的镜像名称
  13. ports:
  14. - containerPort: 80 #表示容器的端口

2.2 Container

官网https://kubernetes.io/docs/concepts/containers/

2.2.1 Docker世界中

可以通过docker run运行一个容器

或者定义一个yml文件,本机使用docker-compose,多机通过docker swarm创建

2.2.2 K8S世界中

同样以一个yaml文件维护,container运行在pod中

2.3 Pod

官网https://kubernetes.io/docs/concepts/workloads/pods/pod-overview/

2.3.1 What is Pod

  1. A Pod is the basic execution unit of a Kubernetes application
  2. A Pod encapsulates an applications container (or, in some cases, multiple containers), storage resources, a unique network IP, and options that govern how the container(s) should run

2.3.2 Pod初体验

(1)创建一个pod的yaml文件,名称为nginx_pod.yaml

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: nginx-pod
  5. labels:
  6. app: nginx
  7. spec:
  8. containers:
  9. - name: nginx-container
  10. image: nginx
  11. ports:
  12. - containerPort: 80

(2)根据该nginx_pod.yaml文件创建pod

  1. kubectl apply -f nginx_pod.yaml

(3)查看pod

01 kubectl get pods

  1. NAME READY STATUS RESTARTS AGE
  2. nginx-pod 1/1 Running 0 29s

02 kubectl get pods -o wide

  1. NAME READY STATUS RESTARTS AGE IP NODE
  2. nginx-pod 1/1 Running 0 40m 192.168.80.194 w2

03 kubectl describe pod nginx-pod

  1. Name: nginx-pod
  2. Namespace: default
  3. Priority: 0
  4. PriorityClassName: <none>
  5. Node: w2/192.168.0.62
  6. Start Time: Sun, 06 Oct 2019 20:45:35 +0000
  7. Labels: app=nginx
  8. Annotations: cni.projectcalico.org/podIP: 192.168.80.194/32
  9. kubectl.kubernetes.io/last-applied-configuration:
  10. {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"labels":{"app":"nginx"},"name":"nginx-pod","namespace":"default"},"spec":{"c...
  11. Status: Running
  12. IP: 192.168.80.194
  13. Containers:
  14. nginx-container:
  15. Container ID: docker://eb2fd0b2906f53e9892e22a6fd791c9ac68fb8e5efce3bbf94ec12bae96e1984
  16. Image: nginx
  17. Image ID: docker-pullable:/

(4)可以发现该pod运行在worker02节点上

于是来到worker02节点,docker ps一下

  1. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  2. eb2fd0b2906f nginx "nginx -g 'daemon of…" 6 minutes ago Up 6 minutes k8s_nginx-container_nginx-pod_default_3ee0706d-e87a-11e9-a904-5254008afee6_0

不妨进入该容器试试[可以发现只有在worker02上有该容器,因为pod运行在worker02上]:

docker exec -it k8s_nginx-container_nginx-pod_default_3ee0706d-e87a-11e9-a904-5254008afee6_0 bash

  1. root@nginx-pod:/#

(5)访问nginx容器

  1. curl 192.168.80.194 OK,并且在任何一个集群中的Node上访问都成功

(6)删除Pod

  1. kubectl delete -f nginx_pod.yaml
  2. kubectl get pods

2.3.3 Storage and Networking

官网https://kubernetes.io/docs/concepts/workloads/pods/pod-overview/#networking

  • Networking
  1. Each Pod is assigned a unique IP address. Every container in a Pod shares the network namespace, including the IP address and network ports.

官网https://kubernetes.io/docs/concepts/workloads/pods/pod-overview/#storage

  • Storage
  1. A Pod can specify a set of shared storage Volumes. All containers in the Pod can access the shared volumes, allowing those containers to share data.