安装部署

kubeadm是官方社区推出的一个用于快速部署kubernetes集群的工具。

这个工具能通过两条指令完成一个kubernetes集群的部署:

# 创建一个 Master 节点
$ kubeadm init

# 将一个 Node 节点加入到当前集群中
$ kubeadm join <Master节点的IP和端口 >

1. 安装要求

在开始之前,部署Kubernetes集群机器需要满足以下几个条件:

  • 一台或多台机器,操作系统 CentOS7.x-86_x64
  • 硬件配置:2GB或更多RAM,2个CPU或更多CPU,硬盘30GB或更多
  • 集群中所有机器之间网络互通
  • 可以访问外网,需要拉取镜像
  • 禁止swap分区

2. 准备环境

kubernetes架构图

角色IP
k8s-master192.168.100.61
k8s-node1192.168.100.62
k8s-node2192.168.100.63
k8s-node3192.168.100.64
关闭防火墙:
$ systemctl stop firewalld && systemctl disable firewalld

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

关闭swap:
$ swapoff -a  # 临时
$ vim /etc/fstab  # 永久,注释swap行

设置主机名:
$ hostnamectl set-hostname <hostname>

在master添加hosts:
$ cat >> /etc/hosts << EOF
192.168.100.61 k8s-master
192.168.100.62 k8s-node1
192.168.100.63 k8s-node2
EOF

将桥接的IPv4流量传递到iptables的链:
$ cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
$ sysctl --system  # 生效

时间同步:
$ yum install ntpdate -y
$ ntpdate time.windows.com

3. 安装Docker/kubeadm/kubelet【所有节点】

Kubernetes默认CRI(容器运行时)为Docker,因此先安装Docker。

3.1 安装Docker

$ 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
$ systemctl enable docker && systemctl start docker

配置镜像下载加速器:

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

3.2 添加阿里云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

3.3 安装kubeadm,kubelet和kubectl

由于版本更新频繁,这里指定版本号部署:

$ yum install -y kubelet-1.19.0 kubeadm-1.19.0 kubectl-1.19.0
$ systemctl enable kubelet

4. 部署Kubernetes Master

https://kubernetes.io/zh/docs/reference/setup-tools/kubeadm/kubeadm-init/#config-file

https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/#initializing-your-control-plane-node

在192.168.100.61(Master)执行。

$ kubeadm init \
  --apiserver-advertise-address=192.168.100.61 \
  --image-repository registry.aliyuncs.com/google_containers \
  --kubernetes-version v1.19.0 \
  --service-cidr=10.96.0.0/12 \
  --pod-network-cidr=10.244.0.0/16 \
  --ignore-preflight-errors=all
  • –apiserver-advertise-address 集群通告地址
  • –image-repository 由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里指定阿里云镜像仓库地址
  • –kubernetes-version K8s版本,与上面安装的一致
  • –service-cidr 集群内部虚拟网络,Pod统一访问入口
  • –pod-network-cidr Pod网络,,与下面部署的CNI网络组件yaml中保持一致

执行成功有如下显示:

Your Kubernetes control-plane 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/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.100.61:6443 --token c1cop4.kybvz0dxxennntnk \
    --discovery-token-ca-cert-hash sha256:14d7cc993ae57319e66000c2ef766049f20860cea4769c096fb044f206af17b9


或者使用配置文件引导:

$ vi kubeadm.conf
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
kubernetesVersion: v1.18.0
imageRepository: registry.aliyuncs.com/google_containers 
networking:
  podSubnet: 10.244.0.0/16 
  serviceSubnet: 10.96.0.0/12 

$ kubeadm init --config kubeadm.conf --ignore-preflight-errors=all  

拷贝kubectl使用的连接k8s认证文件到默认路径:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
$ kubectl get nodes
NAME         STATUS   ROLES    AGE   VERSION
k8s-master   Ready    master   2m   v1.18.0




[root@localhost ~]# mkdir -p $HOME/.kube
[root@localhost ~]# cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@localhost ~]# chown $(id -u):$(id -g) $HOME/.kube/config
[root@localhost ~]# kubectl get nodes
NAME         STATUS     ROLES    AGE     VERSION
k8s-master   NotReady   master   5m52s   v1.19.0
[root@localhost ~]#

5. 加入Kubernetes Node

在192.168.100.62/63(Node)执行。

向集群添加新节点,执行在kubeadm init输出的kubeadm join命令:

kubeadm join 192.168.100.61:6443 --token iz94r1.lfpk4yx54hm7xev3 \
    --discovery-token-ca-cert-hash sha256:14d7cc993ae57319e66000c2ef766049f20860cea4769c096fb044f206af17b9

默认token有效期为24小时,当过期之后,该token就不可用了。这时就需要重新创建token,操作如下:

$ kubeadm token create # 重新生成新的token

[root@localhost ~]# kubeadm token create
W1208 23:09:43.352801    5888 configset.go:348] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
iz94r1.lfpk4yx54hm7xev3
[root@localhost ~]#




[root@localhost ~]#
[root@localhost ~]# kubeadm token list
TOKEN                     TTL         EXPIRES                     USAGES                   DESCRIPTION                                                EXTRA GROUPS
c1cop4.kybvz0dxxennntnk   23h         2021-12-09T23:08:10+08:00   authentication,signing   The default bootstrap token generated by 'kubeadm init'.   system:bootstrappers:kubeadm:default-node-token
iz94r1.lfpk4yx54hm7xev3   23h         2021-12-09T23:09:43+08:00   authentication,signing   <none>                                                     system:bootstrappers:kubeadm:default-node-token
[root@localhost ~]#



# 获取ca证书sha256编码hash值
# –token:用于Master验证Node身份。
# 要想集群首次引导启动时,支持bootstrap-token验证,APIServer需要开启下面的配置选项:
#  --enable-bootstrap-token-auth=true
[root@localhost ~]# openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'

14d7cc993ae57319e66000c2ef766049f20860cea4769c096fb044f206af17b9


# 节点加入集群
# –discovery-token-ca-cert-hash:用于Node验证master身份。
# 执行join时,API Server会下发ca.crt,这个证书会被node存放在/etc/kubernetes/pki目录下。
# 然后kubeadm join再用ca设置公钥证书的hash值,与discovery-token-ca-cert-hash的值进行比对。
$ kubeadm join 192.168.100.61:6443 --token iz94r1.lfpk4yx54hm7xev3 --discovery-token-ca-cert-hash sha256:14d7cc993ae57319e66000c2ef766049f20860cea4769c096fb044f206af17b9

或者直接命令快捷生成:kubeadm token create --print-join-command

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

如果超时,或加入失败,按照如下步骤重新加入

  • node节点执行
    kubeadm reset -f

  • 再执行加入

kubeadm join 192.168.100.61:6443 --token iz94r1.lfpk4yx54hm7xev3 --discovery-token-ca-cert-hash sha256:14d7cc993ae57319e66000c2ef766049f20860cea4769c096fb044f206af17b9

6. 部署容器网络(CNI)

https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/#pod-network

注意:只需要部署下面其中一个,推荐Calico。

Calico是一个纯三层的数据中心网络方案,Calico支持广泛的平台,包括Kubernetes、OpenStack等。

Calico 在每一个计算节点利用 Linux Kernel 实现了一个高效的虚拟路由器( vRouter) 来负责数据转发,而每个 vRouter 通过 BGP 协议负责把自己上运行的 workload 的路由信息向整个 Calico 网络内传播。

此外,Calico 项目还实现了 Kubernetes 网络策略,提供ACL功能。

https://docs.projectcalico.org/getting-started/kubernetes/quickstart

$ wget https://docs.projectcalico.org/manifests/calico.yaml

下载完后还需要修改里面定义Pod网络(CALICO_IPV4POOL_CIDR),与前面kubeadm init指定的一样

修改完后应用清单:

$ kubectl apply -f calico.yaml
$ kubectl get pods -n kube-system

7. 测试kubernetes集群

  • 验证Pod工作
  • 验证Pod网络通信
  • 验证DNS解析

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

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

访问地址:http://NodeIP:Port

8. 部署 Dashboard

$ wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.3/aio/deploy/recommended.yaml

默认Dashboard只能集群内部访问,修改Service为NodePort类型,暴露到外部:

$ vi recommended.yaml
...
kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
spec:
  ports:
    - port: 443
      targetPort: 8443
      nodePort: 30001
  selector:
    k8s-app: kubernetes-dashboard
  type: NodePort
...
$ kubectl apply -f recommended.yaml
$ kubectl get pods -n kubernetes-dashboard
NAME                                         READY   STATUS    RESTARTS   AGE
dashboard-metrics-scraper-6b4884c9d5-gl8nr   1/1     Running   0          13m
kubernetes-dashboard-7f99b75bf4-89cds        1/1     Running   0          13m

访问地址:https://NodeIP:30001

创建service account并绑定默认cluster-admin管理员集群角色:

# 创建用户
$ kubectl create serviceaccount dashboard-admin -n kube-system
# 用户授权
$ kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
# 获取用户Token
$ kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')

使用输出的token登录Dashboard。

讲师:李振良

官方网站: http://www.ctnrs.com

故障排查

master中“kubectl get node ”STATUS显示notready
kubernetes-故障案例-资源耗尽
k8s Pod调度失败(NoExecute)排查及分析
K8s集群node节点异常处理(Calico)

temp

[root@localhost ~]# kubectl get cs
Warning: v1 ComponentStatus is deprecated in v1.19+
NAME                 STATUS      MESSAGE                                                                                       ERROR
controller-manager   Unhealthy   Get "http://127.0.0.1:10252/healthz": dial tcp 127.0.0.1:10252: connect: connection refused
scheduler            Unhealthy   Get "http://127.0.0.1:10251/healthz": dial tcp 127.0.0.1:10251: connect: connection refused
etcd-0               Healthy     {"health":"true"}
[root@localhost ~]# kubectl  get node -owide controller-manager
Error from server (NotFound): nodes "controller-manager" not found
[root@localhost ~]# kubectl  get node -owide
NAME         STATUS   ROLES    AGE    VERSION   INTERNAL-IP      EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION                CONTAINER-RUNTIME
k8s-master   Ready    master   142m   v1.19.0   192.168.100.61   <none>        CentOS Linux 7 (Core)   3.10.0-1160.49.1.el7.x86_64   docker://20.10.11
k8s-node1    Ready    <none>   113m   v1.19.0   192.168.100.62   <none>        CentOS Linux 7 (Core)   3.10.0-1160.49.1.el7.x86_64   docker://20.10.11
k8s-node2    Ready    <none>   130m   v1.19.0   192.168.100.63   <none>        CentOS Linux 7 (Core)   3.10.0-1160.49.1.el7.x86_64   docker://20.10.11
[root@localhost ~]# kubectl get pod --all-namespaces
NAMESPACE     NAME                                       READY   STATUS    RESTARTS   AGE
default       nginx-6799fc88d8-9f2q6                     1/1     Running   0          66m
kube-system   calico-kube-controllers-558995777d-8mt42   1/1     Running   0          81m
kube-system   calico-node-f8lq7                          1/1     Running   0          81m
kube-system   calico-node-j6l9z                          1/1     Running   0          81m
kube-system   calico-node-mk76v                          1/1     Running   0          81m
kube-system   coredns-6d56c8448f-459jl                   1/1     Running   0          144m
kube-system   coredns-6d56c8448f-45hvm                   1/1     Running   0          144m
kube-system   etcd-k8s-master                            1/1     Running   0          144m
kube-system   kube-apiserver-k8s-master                  1/1     Running   0          144m
kube-system   kube-controller-manager-k8s-master         1/1     Running   0          144m
kube-system   kube-proxy-gmw76                           1/1     Running   0          132m
kube-system   kube-proxy-hzm6m                           1/1     Running   0          144m
kube-system   kube-proxy-wmf2f                           1/1     Running   0          115m
kube-system   kube-scheduler-k8s-master                  1/1     Running   0          144m
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# vim /etc/kubernetes/manifests/kube-controller-manager.yaml
[root@localhost ~]# vim /etc/kubernetes/manifests/kube-scheduler.yaml
[root@localhost ~]# kubectl get cs
Warning: v1 ComponentStatus is deprecated in v1.19+
NAME                 STATUS    MESSAGE             ERROR
controller-manager   Healthy   ok
scheduler            Healthy   ok
etcd-0               Healthy   {"health":"true"}

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐