k8s安装记录

k8s是一个docker集群的管理工具

K8S介绍

安装步骤

环境配置

  1. 设置主机名与时区

    1
    2
    3
    4
    timedatectl set-timezone Asia/Shanghai #都要执行
    hostnamectl set-hostname master # 172.20.5.182
    hostnamectl set-hostname node1 # 172.20.5.224
    hostnamectl set-hostname node2 # 172.20.5.238
  2. 添加hosts网络主机配置,三台虚拟机都要设置,方便寻找主机(X3)

    1
    2
    3
    4
    5
    # vim /etc/hosts

    172.20.5.182 master
    172.20.5.224 node1
    172.20.5.238 node2
  3. 关闭防火墙(X3)

    1
    2
    3
    4
    sed -i 's/SELINUX=enforcing/SELINUX=disable/g' /etc/selinux/config
    setenforce 0
    systemctl disable firewalld
    systemctl stop firewalld
  4. 安装并启动docker(X3)

    1
    2
    3
    4
    5
    6
    7
    8
    yum -y install wget
    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-18.06.1.ce-3.el7
    systemctl enable docker && systemctl start docker
    docker --version

    systemctl start docker
    systemctl enable docker
  5. 配置Docker的镜像加速器

    1
    2
    3
    4
    5
    6
    7
    8
    mkdir -p /etc/docker
    tee /etc/docker/daemon.json <<-'EOF'
    {
    "registry-mirrors": ["https://你自己阿里加速的地址.mirror.aliyuncs.com"]
    }
    EOF
    systemctl daemon-reload
    systemctl restart docker
  6. 配置阿里云的Kubernetes源(X3)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    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
  7. 安装kubeadmkubeletkubectl(X3)

    1
    2
    yum install -y kubelet-1.15.0 kubeadm-1.15.0 kubectl-1.15.0
    systemctl enable kubelet
  8. 关闭交换区 (可以理解为虚拟内存,k8s使用时尽量不使用交换区,防止出现预料之外的问题)(X3)

    1
    2
    3
    4
    swapoff -a
    vi /etc/fstab
    #swap一行注释
    # /dev/mapper/cl-swap swap swap defaults 0 0
  9. 配置网桥(X3)

    1
    2
    3
    4
    5
    6
    cat <<EOF > /etc/sysctl.d/k8s.conf
    net.bridge.bridge-nf-call-ip6tables = 1
    net.bridge.bridge-nf-call-iptables = 1
    EOF

    sysctl --system

使用kubeadmin部署K8s集群

  • 部署Kubernetes Master(在master节点执行)

    1
    2
    3
    4
    5
    6
    kubeadm init \
    --apiserver-advertise-address=172.20.5.182 \
    --image-repository registry.aliyuncs.com/google_containers \
    --kubernetes-version v1.15.0 \
    --service-cidr=10.1.0.0/16 \
    --pod-network-cidr=10.244.0.0/16

    日记记录

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    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 172.20.5.182:6443 --token lkzogs.dka9ui4ppsvs4gx9 \
    --discovery-token-ca-cert-hash sha256:e3b7ba5d1439ee0ab08f29b4ecd3d6574d0ae87b80099c74ffd435468ac20a2b
  • 使用命令 kubectl get node 查看集群节点信息

    1
    2
    3
    [root@master softwore]# kubectl get node
    NAME STATUS ROLES AGE VERSION
    master Ready master 3h49m v1.15.0
  • 查看存在问题的pod,命令kubectl get pod –all-namespaces

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [root@master softwore]# kubectl get pod --all-namespaces
    NAMESPACE NAME READY STATUS RESTARTS AGE
    kube-system coredns-bccdc95cf-95nws 1/1 Running 0 3h56m
    kube-system coredns-bccdc95cf-j69b9 1/1 Running 0 3h56m
    kube-system etcd-master 1/1 Running 0 3h55m
    kube-system kube-apiserver-master 1/1 Running 0 3h55m
    kube-system kube-controller-manager-master 1/1 Running 0 3h55m
    kube-system kube-flannel-ds-amd64-qczd8 1/1 Running 0 3h20m
    kube-system kube-proxy-p46tl 1/1 Running 0 3h56m
    kube-system kube-scheduler-master 1/1 Running 0 3h55m
  • 向集群添加新节点(所有的Node节点执行)。执行在kubeadm init输出的kubeadm join命令

    1
    2
    kubeadm join 172.20.5.182:6443 --token lkzogs.dka9ui4ppsvs4gx9 \
    --discovery-token-ca-cert-hash sha256:e3b7ba5d1439ee0ab08f29b4ecd3d6574d0ae87b80099c74ffd435468ac20a2b

增加nginx服务

  • 测试kubernetes集群。在Kubernetes集群中创建一个pod,运行Nginx服务,验证是否正常运行

    1
    2
    3
    kubectl create deployment nginx --image=nginx
    kubectl expose deployment nginx --port=80 --type=NodePort
    kubectl get pod,svc
    1
    2
    3
    4
    5
    6
    NAME                         READY   STATUS    RESTARTS   AGE
    pod/nginx-554b9c67f9-llv5s 1/1 Running 0 11m

    NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    service/kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 16h
    service/nginx NodePort 10.1.41.250 <none> 80:32644/TCP 10m

    通过浏览器访问32644端口测试是否成功

  • pod进行扩容

    1
    2
    kubectl scale deployment nginx --replicas=3
    kubectl get pod

常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
基本命令(初级):
create 创建一个新的资源从一个文件或者stdin
expose 获取replication controller,service,deployment和pod,并将其作为一个新的服务.

run 在集群内运行特定镜像.
set 在对象上设置特定功能
run-container 在集群上运行特定镜像,已被run替代

基本命令(中级):
get 现实一个或多个资源
explain 资源文档
edit 编辑服务器上的资源
delete 按照文件名称,标准输入,资源和名称来刪除资源,还可以通过资源和标签选择器

部署命令:
rollout 管理资源的部署
rolling-update 执行给定的ReplicationController的滚动更新
scale 为Deployment,ReplicaSet,Replication等控制器设置一个新的大小或者任务
autoscale 自动扩展 Deployment, ReplicaSet, 或 ReplicationController

集群管理命令:
certificate 修改证书资源
cluster-info 显示集群信息
top 显示资源 (CPU/Memory/Storage)使用情况
cordon 将节点标记为不可调度
uncordon 将节点标记为可调度
drain 节点准备维护
taint 更新一个或多个节点上的错误

故障排除与调试命令:
describe 显示特定资源或资源组的详细信息
logs 打印pod中容器的日志
attach 进入正在运行的容器
exec 在容器中执行命令
port-forward 将一个或多个本地端口转发到pod
proxy 运行代理到kubernetes的API Server
cp 从容器中复制一个文件或者目录
auth 检查授权

高级命令:
apply 通过filename或stdin将配置应用到资源
patch 更新资源的字段,通过策略合并
replace 用filename或stdin替换资源
convert 在不同的API版本之间转换配置文件

配置命令:
label 为资源更新标签
annotate 为资源更新注释
completion 输出指定shell的shell代码(bash or zsh)

其它命令:
api-versions 输出服务器支持的API版本
config 修改kubeconfig配置文件
help 查看帮助信息
plugin 运行命令行插件
version 打印客户端和服务端的版本信息

获取节点信息kubectl get node [-o wide]

1
2
3
4
5
[root@master ~]# kubectl get node -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
master Ready master 16h v1.15.0 172.20.5.182 <none> CentOS Linux 7 (Core) 3.10.0-514.el7.x86_64 docker://18.6.1
node1 Ready <none> 12h v1.15.0 172.20.5.224 <none> CentOS Linux 7 (Core) 3.10.0-514.el7.x86_64 docker://18.6.1
node2 Ready <none> 12h v1.15.0 172.20.5.238 <none> CentOS Linux 7 (Core) 3.10.0-514.el7.x86_64 docker://18.6.1

查看集群所有 pod 列表kubectl get pods --all-namespaces [-o wide]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@master ~]# kubectl get pods --all-namespaces -o wide
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kube-system coredns-bccdc95cf-95nws 1/1 Running 3 16h 10.244.0.7 master <none> <none>
kube-system coredns-bccdc95cf-j69b9 1/1 Running 3 16h 10.244.0.6 master <none> <none>
kube-system etcd-master 1/1 Running 1 16h 172.20.5.182 master <none> <none>
kube-system kube-apiserver-master 1/1 Running 1 16h 172.20.5.182 master <none> <none>
kube-system kube-controller-manager-master 1/1 Running 2 16h 172.20.5.182 master <none> <none>
kube-system kube-flannel-ds-amd64-cb8qj 1/1 Running 0 12h 172.20.5.224 node1 <none> <none>
kube-system kube-flannel-ds-amd64-mbzkx 1/1 Running 0 12h 172.20.5.238 node2 <none> <none>
kube-system kube-flannel-ds-amd64-qczd8 1/1 Running 1 15h 172.20.5.182 master <none> <none>
kube-system kube-proxy-djf6t 1/1 Running 3 12h 172.20.5.224 node1 <none> <none>
kube-system kube-proxy-p46tl 1/1 Running 1 16h 172.20.5.182 master <none> <none>
kube-system kube-proxy-qcmd6 1/1 Running 0 12h 172.20.5.238 node2 <none> <none>
kube-system kube-scheduler-master 1/1 Running 2 16h 172.20.5.182 master <none> <none>

通过kubectl explain <资源名对象名>查看api字段

  • kubectl explain pod 查看pod的大概信息
  • kubectl explain pod --recursive 查看pod的详细信息

删除pod

直接删除对应的pod是没用的,应该删除该pod对应的deployment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 查看pods
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-554b9c67f9-gj7fm 1/1 Running 0 5m14s

# 删除pod
[root@master ~]# kubectl delete pod nginx-554b9c67f9-gj7fm -n default
pod "nginx-554b9c67f9-gj7fm" deleted

# 再次查看pod依然存在
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-554b9c67f9-2gwnl 1/1 Running 0 15s

# 查看deplayment
[root@master ~]# kubectl get deployment -n default
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 5h27m

# 删除deployment
[root@master ~]# kubectl delete deployment nginx -n default
deployment.extensions "nginx" deleted

# 查看pod发现已经删除
[root@master ~]# kubectl get pods
No resources found.

kubectl get deployments.apps

kubectl get replicasets.apps

1
2
3
4
5
6
7
8
9
10
[root@master .kube]# kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 2/2 2 2 12m
[root@master .kube]# kubectl get replicasets.apps
NAME DESIRED CURRENT READY AGE
nginx-554b9c67f9 2 2 2 15m
[root@master .kube]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-554b9c67f9-5x74m 1/1 Running 0 13m 10.244.2.11 node2 <none> <none>
nginx-554b9c67f9-qgv9c 1/1 Running 0 13m 10.244.1.10 node1 <none> <none>

获取namespace信息kubectl get namespace

1
2
3
4
5
6
[root@master .kube]# kubectl get namespace
NAME STATUS AGE
default Active 22h
kube-node-lease Active 22h
kube-public Active 22h
kube-system Active 22h

k8s创建Pod和ReplicaSet的工作流程

参考:https://blog.csdn.net/weixin_38070561/article/details/82706973

参考

使用kubeadmin部署K8s集群

k8s安装及部署步骤

补充

timedatectl命令

timedatectl命令可以查询和更改系统时钟和设置,你可以使用此命令来设置或更改当前的日期,时间和时区,或实现与远程NTP服务器的自动系统时钟同步。

【参考】Linux下使用timedatectl命令时间时区操作详解

hostnamectl命令

永久修改主机名,是对/etc/hostname文件的内容进行修改

setenforce命令

SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统。

【参考】Linux setenforce命令详解SeLinux操作

k8s 部署问题解决(节点状态为 NotReady)

使用kubectl get node查看已加入的节点出现了Status为NotReady的情况。

1
2
3
4
5
[root@master ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
master NotReady master 3h8m v1.15.0
node1 NotReady <none> 20m v1.15.0
node2 NotReady <none> 20m v1.15.0

这种情况是因为有某些关键的 pod 没有运行起来,使用如下命令查看:kubectl get pod -n kube-systemk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@master softwore]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-bccdc95cf-95nws 1/1 Running 0 4h18m
coredns-bccdc95cf-j69b9 1/1 Running 0 4h18m
etcd-master 1/1 Running 0 4h17m
kube-apiserver-master 1/1 Running 0 4h17m
kube-controller-manager-master 1/1 Running 0 4h17m
kube-flannel-ds-amd64-cb8qj 0/1 Init:ErrImagePull 0 4m58s
kube-flannel-ds-amd64-mbzkx 0/1 Init:0/1 0 2m43s
kube-flannel-ds-amd64-qczd8 1/1 Running 0 3h42m
kube-proxy-djf6t 1/1 Running 0 4m58s
kube-proxy-p46tl 1/1 Running 0 4h18m
kube-proxy-qcmd6 1/1 Running 0 2m43s
kube-scheduler-master 1/1 Running 0 4h17m

通过手动下载镜像处理

master参考:https://blog.csdn.net/yrx420909/article/details/105667618/

node参考:http://www.voidcn.com/article/p-wpuagtbj-byy.html

kubectl的svc kube-proxy 的详细说明

参考:https://blog.csdn.net/qq_42533216/article/details/110188745

-------------本文结束感谢您的阅读-------------