【K8s集群离线安装-kubeadm】

1、kubeadm概述

kubeadm是官方社区推出的一个用于快速部署kubernetes集群的工具。这个工具能通过两条指令快速完成一个kubernetes集群的部署。

2、环境准备

2.1 软件环境

软件 版本
操作系统 CentOS 7
Docker 19.03.13
K8s 1.23

2.2 服务器

 最小硬件配置:2核CPU、2G内存、20G硬盘。

名称 IP
master 192.168.18.134
node1 192.168.18.135
node2 192.168.18.136

master节点需要至少2个CPU,不然kubeadm init时会报错:

error execution phase preflight: [preflight] Some fatal errors occurred:
	[ERROR NumCPU]: the number of available CPUs 1 is less than the required 2

2.3 关闭防火墙

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld

2.4 关闭selinux

# 重启生效,永久
[root@localhost ~]# sed -i s/SELINUX=enforcing/SELINUX=disabled/g /etc/selinux/config
# 立即生效,临时
[root@localhost ~]# setenforce 0
# 查看selinux的状态
[root@localhost ~]# getenforce
Disabled

2.5 关闭Swp

# 重启生效,永久
[root@localhost ~]# vi /etc/fstab
...
# 找到并注释下面这行(这一行定义了 swap 分区,并在启动时进行挂载。)
# /dev/mapper/centos-swap swap                    swap    defaults        0 0
...

# 立即生效,临时
[root@localhost ~]# swapoff -a	

# 查看一下Swap已经全部为0了
[root@localhost ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           2117         253        1351           9         512        1704
Swap:             0           0           0

如果没有关闭swp,kubeadm init 初始化会报错:

[kubelet-check] It seems like the kubelet isn't running or healthy.
[kubelet-check] The HTTP call equal to 'curl -sSL http://localhost:10248/healthz' failed with error: Get "http://localhost:10248/healthz": dial tcp [::1]:10248: connect: connection refused.

# systemctl status kubelet

# journalctl -xeu kubelet
..."Failed to run kubelet" err="failed to run Kubelet: running with swap on is not sup

2.6 设置hosts

在master添加hosts

cat >> /etc/hosts <<EOF
192.168.18.134   master
192.168.18.135   node1
192.168.18.136   node2
EOF

3、安装Docker

3.1 下载docker软件包【联网节点】

 找一台可以联网的机器上下载安装所需的软件。

资源下载:【docker软件包】

# 配置docker-ce源
wget -P /etc/yum.repos.d/ https://download.docker.com/linux/centos/docker-ce.repo

# 查看docker所有版本
yum list docker-ce --showduplicates

# 选择安装docker 19.03.13
# docker及其依赖下载到~/dockerPackges文件夹中
yum install --downloadonly --downloaddir ~/dockerPackges docker-ce-19.03.13 docker-ce-cli-19.03.13

3.2 安装docker

 在master及node节点创建~/k8s/docker目录,把下载好的docker软件包上传到各节点的此目录。

cd ~/k8s/docker
# 安装
yum install ./*.rpm

3.3 启动docker

# 启动docker
systemctl start docker
# 设置开机启动
systemctl enable docker

# 查看docker服务信息
docker info

4、安装k8s组件【所有节点】

  • kubeadm:是一个工具,它可以初始化集群、引导新的节点加入集群等。
  • kubelet:是运行在集群中所有节点上的代理。它确保容器都在运行状态。
  • kubectl:是 Kubernetes 的命令行工具。可以使用它来管理 Kubernetes 集群。

kubeadmkubelet 应该在每个节点上都安装,而 kubectl 通常只安装在你打算执行管理命令的机器上。

4.1 下载k8s组件【联网节点】

 需要安装组件: kubeadmkubeletkubectl ,版本要一致。在可以连外网的机器上下载组件,同上面docker。

资源下载:【k8s相关组件】

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

# k8s组件及其依赖下载到~/k8sPackges文件夹中
yum install --downloadonly --downloaddir ~/k8sPackges kubelet-1.23.0 kubeadm-1.23.0 kubectl-1.23.0

4.2 安装组件

 在所有节点创建~/k8s/kubernetes目录,把下载好的k8s组件软件包上传到各节点的此目录。

cd ~/k8s/kubernetes
# 安装
yum install ./*.rpm

4.3 设置开机启动

 设置kubelet的开机启动。暂时不需要启动kubelet,就算启动,也是不能成功的。执行kubeadm命令,会生成一些配置文件 ,这时才会让kubelet启动成功的。

# 先设置kubelet 为自启动
systemctl enable kubelet

5、拉取并导入kubeadm所需镜像

 执行kubeadm时,要用到一些镜像,所以需要提前准备。

资源下载:【kubeadm依赖镜像】

5.1 查看依赖哪些镜像

#【master节点】
[root@k8s-master ~]# kubeadm config images list
k8s.gcr.io/kube-apiserver:v1.23.0
k8s.gcr.io/kube-controller-manager:v1.23.0
k8s.gcr.io/kube-scheduler:v1.23.0
k8s.gcr.io/kube-proxy:v1.23.0
k8s.gcr.io/pause:3.6
k8s.gcr.io/etcd:3.5.1-0
k8s.gcr.io/coredns/coredns:v1.8.6

在生产环境,是肯定访问不了k8s.gcr.io这个地址的。所以需要使用国内镜像先下载下来。
在一台可以上网的机器上使用docker命令搜索所需的镜像:

# 查找官方仓库中的镜像 【联网节点】

# 镜像很多,一般选择 STARS 梳理多的。 
# --filter=stars=N  以指定仅显示收藏数量为 N 以上的镜像
[root@repo yum.repos.d]# docker search kube-apiserver
NAME                                    DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
aiotceo/kube-apiserver                  end of support, please pull kubestation/kube…   20                   
mirrorgooglecontainers/kube-apiserver                                                   19                   
kubesphere/kube-apiserver                                                               7                   ...
[root@repo yum.repos.d]# docker search kube-controller-manager
...

# 整理出 kubeadm 所需镜像列表,后面脚本使用
registry.aliyuncs.com/google_containers/kube-apiserver:v1.23.0
registry.aliyuncs.com/google_containers/kube-controller-manager:v1.23.0
registry.aliyuncs.com/google_containers/kube-scheduler:v1.23.0
registry.aliyuncs.com/google_containers/kube-proxy:v1.23.0
registry.aliyuncs.com/google_containers/pause:3.6
registry.aliyuncs.com/google_containers/etcd:3.5.1-0
registry.aliyuncs.com/google_containers/coredns:v1.8.6

5.2 镜像拉取到本地【联网节点】

# pull
[root@repo ~]# vim pull_images.sh
#!/bin/bash
images=(
registry.aliyuncs.com/google_containers/kube-apiserver:v1.23.0
registry.aliyuncs.com/google_containers/kube-controller-manager:v1.23.0
registry.aliyuncs.com/google_containers/kube-scheduler:v1.23.0
registry.aliyuncs.com/google_containers/kube-proxy:v1.23.0
registry.aliyuncs.com/google_containers/pause:3.6
registry.aliyuncs.com/google_containers/etcd:3.5.1-0
registry.aliyuncs.com/google_containers/coredns:v1.8.6
)
for pullimageName in ${images[@]} ; do
docker pull $pullimageName
done
[root@repo ~]# chmod +x pull_images.sh
[root@repo ~]# ./pull_images.sh
# 查看拉取的镜像
[root@repo ~]# docker images

5.3 导出本地镜像【联网节点】

# save
[root@repo ~]# vim save_images.sh
#!/bin/bash
images=(
registry.aliyuncs.com/google_containers/kube-apiserver:v1.23.0
registry.aliyuncs.com/google_containers/kube-controller-manager:v1.23.0
registry.aliyuncs.com/google_containers/kube-scheduler:v1.23.0
registry.aliyuncs.com/google_containers/kube-proxy:v1.23.0
registry.aliyuncs.com/google_containers/pause:3.6
registry.aliyuncs.com/google_containers/etcd:3.5.1-0
registry.aliyuncs.com/google_containers/coredns:v1.8.6
)

for imageName in ${images[@]}; do
key=`echo $imageName | awk -F '\\\/' '{print $3}' | awk -F ':' '{print $1}'`
docker save -o $key.tar $imageName
done
[root@repo ~]# chmod +x save_images.sh
[root@repo ~]# ./save_images.sh
[root@repo ~]# ll
total 755536
-rw------- 1 root root  46967296 Sep  4 02:37 coredns.tar
-rw------- 1 root root 293936128 Sep  4 02:37 etcd.tar
-rw------- 1 root root 136559616 Sep  4 02:36 kube-apiserver.tar
-rw------- 1 root root 126385152 Sep  4 02:37 kube-controller-manager.tar
-rw------- 1 root root 114243584 Sep  4 02:37 kube-proxy.tar
-rw------- 1 root root  54864896 Sep  4 02:37 kube-scheduler.tar
-rw------- 1 root root    692736 Sep  4 02:37 pause.tar

5.4 导入到部署节点【所有节点】

# 将从联网节点导出的镜像上传到集群的各个节点
[root@master ~]# ll
total 755536
-rw------- 1 root root  46967296 Sep  4 02:37 coredns.tar
-rw------- 1 root root 293936128 Sep  4 02:37 etcd.tar
-rw------- 1 root root 136559616 Sep  4 02:36 kube-apiserver.tar
-rw------- 1 root root 126385152 Sep  4 02:37 kube-controller-manager.tar
-rw------- 1 root root 114243584 Sep  4 02:37 kube-proxy.tar
-rw------- 1 root root  54864896 Sep  4 02:37 kube-scheduler.tar
-rw------- 1 root root    692736 Sep  4 02:37 pause.tar

# load
# 编写 load 脚本:
[root@master ~]# vim load_images.sh
#!/bin/bash
images=(
kube-apiserver
kube-controller-manager
kube-scheduler
kube-proxy
pause
etcd
coredns
)
for imageName in ${images[@]} ; do
key=.tar
docker load -i $imageName$key
done
[root@master ~]# chmod +x load_images.sh
[root@master ~]# ./load_images.sh
[root@master ~]# docker images
REPOSITORY                                                        TAG        IMAGE ID       CREATED         SIZE
registry.aliyuncs.com/google_containers//kube-apiserver            v1.23.0   9ca5fafbe8dc   2 weeks ago     135MB
registry.aliyuncs.com/google_containers//kube-proxy                v1.23.0   71b9bf9750e1   2 weeks ago     112MB
registry.aliyuncs.com/google_containers//kube-controller-manager   v1.23.0   91a4a0d5de4e   2 weeks ago     125MB
registry.aliyuncs.com/google_containers//kube-scheduler            v1.23.0   d5c0efb802d9   2 weeks ago     53.5MB
registry.aliyuncs.com/google_containers//etcd                      3.5.1-0    25f8c7f3da61   10 months ago   293MB
registry.aliyuncs.com/google_containers//coredns                   v1.8.6     a4ca41631cc7   11 months ago   46.8MB
registry.aliyuncs.com/google_containers//pause                     3.6        6270bb605e12   12 months ago   683kB

6、初始化Master节点【master执行】

6.1 kubeadm初始化

[root@master ~]# kubeadm init \
  --apiserver-advertise-address=192.168.18.134 \
  --image-repository registry.aliyuncs.com/google_containers \
  --kubernetes-version v1.23.0 \
  --service-cidr=10.96.0.0/12 \
  --pod-network-cidr=10.244.0.0/16 

–image-repository:镜像仓库,离线安装需要把相关镜像先拉取下来
–apiserver-advertise-address:集群通告地址
–image-repository:由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里指定镜像仓库地址
–kubernetes-version:K8s版本,与上面安装的一致
–service-cidr:集群内部虚拟网络,Pod统一访问入口
–pod-network-cidr:Pod网络,与下面部署的CNI网络组件yaml中保持一致

 初始化完成之后,会输出一个kubeadm join信息,先保存下来。node节点加入master会使用。

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

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf

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:
# 记录下来,用于向集群添加节点(有效期24小时)
kubeadm join 192.168.18.134:6443 --token 6m4wt4.y90169m53e6nen8d \
	--discovery-token-ca-cert-hash sha256:0ea734ba54d630659ed78463d0f38fc6c407fabe9c8a0d41913b626160981402 

6.2、拷贝k8s认证文件

[root@master ~]# mkdir -p $HOME/.kube
[root@master ~]# cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@master ~]# chown $(id -u):$(id -g) $HOME/.kube/config

6.3 查看节点信息

[root@master ~]# kubectl get nodes
NAME     STATUS     ROLES                  AGE     VERSION
master   NotReady   control-plane,master   6m46s   v1.23.0

# 由于网络插件还没有部署,还没有准备就绪 NotReady。

7、往集群加入node节点【node节点】

7.1 创建token

默认token有效期为24小时。token过期后,就需要重新创建token,可以直接使用命令快捷生成。

# 在master节点重新生成 Join Token,然后复制生成的内容,到从节点,执行下
[root@master ~]# kubeadm token create --print-join-command
kubeadm join 192.168.18.134:6443 --token h9g5rn.y07uajj3d9r3v5hh     --discovery-token-ca-cert-hash sha256:cfb734386ee0d27d4864900648c3eaf0e2f84b1e9f98d04b483ad9e702653c9e

7.2 向集群添加新节点

 执行在 kubeadm init 输出的kubeadm join命令。

[root@node1 ~]# kubeadm join 192.168.18.134:6443 --token 8y4nd8.ww9f2npklyebtjqp \
    --discovery-token-ca-cert-hash sha256:c5f01fe144020785cb82b53bcda3b64c2fb8d955af3ca863b8c31d9980c32023

[preflight] Running pre-flight checks
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.15" ConfigMap in the kube-system namespace
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Activating the kubelet service
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

7.3 查看节点信息

[root@master ~]# kubectl get nodes
NAME        STATUS     ROLES                  AGE   VERSION
node1   	NotReady   <none>                 47s   v1.23.0
node2   	NotReady   <none>                 8s    v1.23.0
master      NotReady   control-plane,master   10m   v1.23.0

# 由于网络插件还没有部署,还没有准备就绪 NotReady。

8、安装Network插件

 网络插件是必要部件,常用的有Flannel、Calico等。云厂商一般是结合VPC有自己的一套实现。

注意,安装一种网络插件即可。本文使用了 Flannel

8.1 Flannel插件

Flannel:是一个流行的CNI插件,它使用虚拟网络覆盖技术(overlay network)来连接不同节点上的容器。Flannel支持多种后端驱动,如VXLAN、UDP、Host-GW等。

资源下载:【flannel插件】

8.1.1 查看安装方法

 查看flannel的官网https://github.com/coreos/flannel,找到安装方法。

kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml

【K8s集群离线安装-kubeadm】_第1张图片

8.1.2 下载yml文件

在有网络的机器上下载kube-flannel.yml文件。

直接浏览器访问下载:https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml

或者联网服务器:

[root@repo ~]# wget https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml

把下载好的kube-flannel.yml文件上传到k8s集群的master节点上。

8.1.3 下载镜像
# 查看需要哪些镜像【master节点】
[root@master ~]# cat kube-flannel.yml | grep image
        image: docker.io/flannel/flannel:v0.23.0
        image: docker.io/flannel/flannel-cni-plugin:v1.2.0
        
# pull:拉取镜像【联网节点】
docker pull docker.io/flannel/flannel:v0.23.0
docker pull docker.io/flannel/flannel-cni-plugin:v1.2.0

# save:导出镜像【联网节点】
docker save -o flannel_v0.23.0.tar flannel/flannel:v0.23.0
docker save -o flannel-cni-plugin_v1.2.0.tar flannel/flannel-cni-plugin:v1.2.0
8.1.4 导入镜像【所有节点】

把导出的镜像上传到集群节点

# load:集群各节点导入镜像
docker load -i flannel_v0.23.0.tar
docker load -i flannel-cni-plugin_v1.2.0.tar
8.1.5 安装flannel【Master节点】
[root@master ~]# kubectl apply -f kube-flannel.yml
namespace/kube-flannel created
serviceaccount/flannel created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
configmap/kube-flannel-cfg created
daemonset.apps/kube-flannel-ds created

# 查看节点信息
[root@master ~]# kubectl get node
NAME     STATUS   ROLES    AGE     VERSION
master   Ready    master   5h46m   v1.23.0
node1    Ready    <none>   5h41m   v1.23.0
node2    Ready    <none>   5h38m   v1.23.0
# 网络插件部署后,状态变为:Ready

# 查看pod
[root@master ~]# kubectl get pods -n kube-system

8.2 Calico 插件(可选)

如果之前安装了Flannel插件的话,不用继续安装Calico。

Calico:是一个开源的网络和安全解决方案,它使用BGP协议来实现容器之间的路由。Calico支持灵活的网络策略和安全规则,可用于大规模部署。

资源下载:【calico插件及依赖】

8.2.1 下载镜像
# pull:拉取镜像【联网节点】
[root@repo ~]# vim pull_calico_images.sh
#!/bin/bash
images=(
docker.io/calico/cni:v3.22.1
docker.io/calico/pod2daemon-flexvol:v3.22.1
docker.io/calico/node:v3.22.1
docker.io/calico/kube-controllers:v3.22.1
)
for pullimageName in ${images[@]} ; do
docker pull $pullimageName
done
[root@repo ~]# chmod +x pull_calico_images.sh
[root@repo ~]# ./pull_calico_images.sh

# save:导出镜像【联网节点】
[root@repo ~]# vim save_calico_images.sh
#!/bin/bash
images=(
docker.io/calico/cni:v3.22.1
docker.io/calico/pod2daemon-flexvol:v3.22.1
docker.io/calico/node:v3.22.1
docker.io/calico/kube-controllers:v3.22.1
)
for imageName in ${images[@]}; do
key=`echo $imageName | awk -F '\\\/' '{print $3}' | awk -F ':' '{print $1}'`
docker save -o $key.tar $imageName
done
[root@repo ~]# chmod +x save_calico_images.sh
[root@repo ~]# ./save_calico_images.sh
8.2.2 导入镜像【所有节点】

 将从联网节点导出的镜像上传到集群的各个节点。

[root@master ~]# vim load_calico_images.sh 

#!/bin/bash
images=(
cni
kube-controllers
node
pod2daemon-flexvol
)
for imageName in ${images[@]} ; do
key=.tar
docker load -i $imageName$key
done
[root@master ~]# chmod +x load_calico_images.sh
[root@master ~]# ./load_calico_images.sh
8.2.3 下载calico.yaml文件【联网节点】
[root@repo ~]# wget --no-check-certificate https://docs.projectcalico.org/manifests/calico.yaml

把下载好的 calico.yaml 文件上传到k8s集群的master节点上。

8.2.4 编辑calico.yaml【master节点】
# 查看pod网段
[root@master ~]# cat /etc/kubernetes/manifests/kube-controller-manager.yaml | grep "cluster-cidr="
    - --cluster-cidr=10.244.0.0/16

 1、把calico.yaml里pod所在网段更新为kubeadm init时选项--pod-network-cidr所指定的网段。

 2、指定网卡。

[root@master ~]# vim calico.yaml

# 找到下面的内容进行修改
            # no effect. This should fall within `--cluster-cidr`.
            - name: CALICO_IPV4POOL_CIDR	# 去掉注释
              value: "10.244.0.0/16"		# 去掉注释,更新地址
            # Disable file logging so `kubectl logs` works.
            - name: CALICO_DISABLE_FILE_LOGGING
              value: "true"

# 指定网卡,不然创建pod时会有报错 
			# 找到这里
            - name: CLUSTER_TYPE
              value: "k8s,bgp"
            # 在下面添加
            - name: IP_AUTODETECTION_METHOD
              value: "interface=ens33"	# ens33为本地网卡名

没有指定网卡,创建 pod 会有报错:

报错信息 network: error getting ClusterInformation: connection is unauthorized: Unauthorized

8.2.5 安装 Calico【Master节点】
[root@master ~]# kubectl apply -f calico.yaml
# 查看节点信息
[root@master ~]# kubectl get node
# 查看pod
[root@master ~]# kubectl get pods -n kube-system

9、部署nginx服务【验证】

# 1.创建namespace.yaml文件
[root@k8s-master1 ~]# vi nginx-namespase.yaml
apiVersion: v1 #类型为Namespace
kind: Namespace  #类型为Namespace
metadata:
  name: ssx-nginx-ns  #命名空间名称
  labels:
    name: lb-ssx-nginx-ns

# 然后应用到k8s中:
[root@master ~]# kubectl create -f nginx-namespase.yaml 
namespace/ssx-nginx-ns created

# 2.创建nginx-deployment.yaml文件
vi nginx-deployment.yaml

[root@k8s-master1 ~]# vi nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx #为该Deployment设置key为app,value为nginx的标签
  name: ssx-nginx-dm
  namespace: ssx-nginx-ns
spec:
  replicas: 2 #副本数量
  selector: #标签选择器,与上面的标签共同作用
    matchLabels: #选择包含标签app:nginx的资源
      app: nginx
  template: #这是选择或创建的Pod的模板
    metadata: #Pod的元数据
      labels: #Pod的标签,上面的selector即选择包含标签app:nginx的Pod
        app: nginx
    spec: #期望Pod实现的功能(即在pod中部署)
      containers: #生成container,与docker中的container是同一种
      - name: ssx-nginx-c
        image: nginx:latest #使用镜像nginx: 创建container,该container默认80端口可访问
        ports:
        - containerPort: 80  # 开启本容器的80端口可访问
        volumeMounts:  #挂载持久存储卷
        - name: volume #挂载设备的名字,与volumes[*].name 需要对应 
          mountPath: /usr/share/nginx/html #挂载到容器的某个路径下  
      volumes:
      - name: volume #和上面保持一致 这是本地的文件路径,上面是容器内部的路径
        hostPath:
          path: /opt/web/dist #此路径需要实现创建
     
[root@master ~]# kubectl create -f nginx-deployment.yaml
  
# 3.创建service.yaml文件        
[root@k8s-master1 ~]# vi nginx-service.yaml 
apiVersion: v1
kind: Service
metadata:
  labels:
   app: nginx
  name: ssx-nginx-sv
  namespace: ssx-nginx-ns
spec:
  ports:
  - port: 80 #写nginx本身端口
    name: ssx-nginx-last
    protocol: TCP
    targetPort: 80 # 容器nginx对外开放的端口 上面的dm已经指定了
    nodePort: 31090 #外网访问的端口
  selector:
    app: nginx    #选择包含标签app:nginx的资源
  type: NodePort

kubectl create -f ./nginx-service.yaml

[root@master ~]# kubectl get pods,svc -n ssx-nginx-ns -owide
NAME                               READY   STATUS    RESTARTS   AGE   IP               NODE        NOMINATED NODE   READINESS GATES
pod/ssx-nginx-dm-686cdf7d5-72hhv   1/1     Running   0          98s   10.244.169.135   k8s-node2   <none>           <none>
pod/ssx-nginx-dm-686cdf7d5-qppqc   1/1     Running   0          98s   10.244.36.87     k8s-node1   <none>           <none>

NAME                   TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE   SELECTOR
service/ssx-nginx-sv   NodePort   10.110.26.245   <none>        80:31090/TCP   46s   app=nginx

# 测试nginx
[root@master ~]# curl 192.168.32.129:31090
<h1>This is 192.168.32.129</h1>

Pod状态解释

状态码 说明
CrashLoopBackOff 容器退出,kubelet正在将它重启
InvalidImageName 无法解析镜像名称
ImageInspectError 无法校验镜像
ErrImageNeverPul 策略禁止拉取镜像
ImagePullBackOff 正在重试拉取
RegistryUnavailable 连接不到镜像中心
ErrImagePull 通用的拉取镜像出错
CreateContainerConfigError 不能创建kubelet使用的容器配置
CreateContainerError 创建容器失败
m.internalLifecycle.PreStartContainer 执行hook报错
RunContainerError 启动容器失败
PostStartHookError 执行hook报错
ContainersNotInitialized 容器没有初始化完毕
ContainersNotReady 容器没有准备完毕
ContainerCreating 容器创建中
PodInitializing pod 初始化中
DockerDaemonNotReady docker还没有完全启动
NetworkPluginNotReady 网络插件还没有完全启动
Evicted 即驱赶的意思,意思是当节点出现异常时,kubernetes将有相应的机制驱赶该节点上的Pod。 多见于资源不足时导致的驱赶。

FAQ

kubeadm init 初始化报错

报错1:[ERROR Port-6443]
error execution phase preflight: [preflight] Some fatal errors occurred:
        [ERROR Port-6443]: Port 6443 is in use
        [ERROR Port-10259]: Port 10259 is in use
        [ERROR Port-10257]: Port 10257 is in use
        ...

解决:

# 重启kubeadm
[root@k8s-master ~]# kubeadm reset
报错2:

It seems like the kubelet isn’t running or healthy

[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[kubelet-check] Initial timeout of 40s passed.
[kubelet-check] It seems like the kubelet isn't running or healthy.
[kubelet-check] The HTTP call equal to 'curl -sSL http://localhost:10248/healthz' failed with error: Get "http://localhost:10248/healthz": dial tcp [::1]:10248: connect: connection refused.
[kubelet-check] It seems like the kubelet isn't running or healthy.
[kubelet-check] The HTTP call equal to 'curl -sSL http://localhost:10248/healthz' failed with error: Get "http://localhost:10248/healthz": dial tcp [::1]:10248: connect: connection refused.

[WARNING IsDockerSystemdCheck]

[WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/

解决:

 以上两个报错,是因为Docker的Cgroup Driver驱动程序为cgroupfs

 使用K8S / Kubernetes时,默认情况下Kubernetes cgroupsystemd,所以需要将Cgroup Driver设置为systemd

# 查看docker的 Cgroup Driver
[root@k8s-master ~]# docker info | grep -E "Cgroup Driver"
Cgroup Driver: cgroupfs

# 将Cgroup Driver设置为 systemd
[root@k8s-master ~]# vim /etc/docker/daemon.json
{
  "exec-opts": ["native.cgroupdriver=systemd"]
}

# 重启docker
[root@k8s-master ~]# systemctl restart docker

# 查看docker的 Cgroup Driver
[root@k8s-master ~]# docker info | grep -E "Cgroup Driver"
Cgroup Driver: systemd

# 重置kubeadm即可
[root@k8s-master ~]# kubeadm reset

你可能感兴趣的:(docker及k8s,kubernetes,容器,云原生,k8s,docker)