K8S记录

1. 环境准备

2. K8S安装

2-1.安装方式介绍

  • 使用现成的二进制文件
  • 使用源码编译为二进制文件
  • 使用镜像的方式

使用镜像优雅,不适用于初学者。因为会花费很多时间去配置相关的容器等。这里使用编译好的二进制文件。

2-2.二进制安装步骤

  1. 下载二进制文件地址:
  2. 上传至三台服务器:我是使用XFTP工具上传
  3. tar -xvf 解压缩
  4. 修改解压后的文件夹名为bin 因为bin默认在系统的环境中变量中,如果换成其他的文件夹名, 可以自己配置一下环境变量。

2-3.设置环境变量

# 首先用命令find / -name kubectl   查找kubectl所在的位置
root@server02:~# find / -name kubectl
/root/bin/kubectl
# 修改/etc/profile 文件
root@server02:~# vim /etc/profile
# 根据这个找到的路劲在最下面添加
export PATH="/root/bin/:$PATH"

# 执行下面的命令使配置立即生效
source  /etc/profile

2-4.测试安装结果

测试一下K8S的命令:
source /etc/profilesource /etc/profile

root@server02:~# kubectl -h
kubectl controls the Kubernetes cluster manager. 

Find more information at https://github.com/kubernetes/kubernetes.

Basic Commands (Beginner):
  create         Create a resource from a file or from stdin.
  expose         Take a replication controller, service, deployment or pod and expose it as a new Kubernetes Service
  run            Run a particular image on the cluster
  set            Set specific features on objects
  run-container  Run a particular image on the cluster. This command is deprecated, use "run" instead

Basic Commands (Intermediate):
  get            Display one or many resources
  explain        Documentation of resources
  edit           Edit a resource on the server
  delete         Delete resources by filenames, stdin, resources and names, or by resources and label selector

Deploy Commands:
  rollout        Manage the rollout of a resource
  rolling-update Perform a rolling update of the given ReplicationController
  scale          Set a new size for a Deployment, ReplicaSet, Replication Controller, or Job
  autoscale      Auto-scale a Deployment, ReplicaSet, or ReplicationController

Cluster Management Commands:
  certificate    Modify certificate resources.
  cluster-info   Display cluster info
  top            Display Resource (CPU/Memory/Storage) usage.
  cordon         Mark node as unschedulable
  uncordon       Mark node as schedulable
  drain          Drain node in preparation for maintenance
  taint          Update the taints on one or more nodes

Troubleshooting and Debugging Commands:
  describe       Show details of a specific resource or group of resources
  logs           Print the logs for a container in a pod
  attach         Attach to a running container
  exec           Execute a command in a container
  port-forward   Forward one or more local ports to a pod
  proxy          Run a proxy to the Kubernetes API server
  cp             Copy files and directories to and from containers.
  auth           Inspect authorization

Advanced Commands:
  apply          Apply a configuration to a resource by filename or stdin
  patch          Update field(s) of a resource using strategic merge patch
  replace        Replace a resource by filename or stdin
  convert        Convert config files between different API versions

Settings Commands:
  label          Update the labels on a resource
  annotate       Update the annotations on a resource
  completion     Output shell completion code for the specified shell (bash or zsh)

Other Commands:
  api-versions   Print the supported API versions on the server, in the form of "group/version"
  config         Modify kubeconfig files
  help           Help about any command
  plugin         Runs a command-line plugin
  version        Print the client and server version information

Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).

出现以上信息证明成功。

3. K8S集群部署

3-1.配置文件

上一步我们下载了kubernetes各个组件的二进制文件,这些可执行文件的运行也是需要添加很多参数的,包括有的还会依赖一些配置文件。现在我们就把运行它们需要的参数和配置文件都准备好。

3-1-1. 下载配置文件

#到home目录下载项目
$ cd
$ git clone https://github.com/liuyi01/kubernetes-starter.git
#看看git内容
$ cd ~/kubernetes-starter && ls

3-1-2. 配置文件说明

  • /gen-config.sh
    shell脚本,用来根据每个人自己的集群环境(ip,hostname等),根据下面的模板,生成适合大家各自环境的配置文件。生成的文件会放到target文件夹下。

  • kubernetes-simple
    简易版kubernetes配置模板(剥离了认证授权)。 适合刚接触kubernetes的同学,首先会让大家在和kubernetes初次见面不会印象太差(太复杂啦~~),再有就是让大家更容易抓住kubernetes的核心部分,把注意力集中到核心组件及组件的联系,从整体上把握kubernetes的运行机制。

  • kubernetes-with-ca
    在simple基础上增加认证授权部分。大家可以自行对比生成的配置文件,看看跟simple版的差异,更容易理解认证授权的(认证授权也是kubernetes学习曲线较高的重要原因)

  • service-config
    这个先不用关注,它是我们曾经开发的那些微服务配置。 等我们熟悉了kubernetes后,实践用的,通过这些配置,把我们的微服务都运行到kubernetes集群中。

3-2.基础集群部署(不含认证)

3-2-1. 生成基础版本配置

使用shell脚本来生成一个不带认证授权的配置文件

root@server02:~# ls
bin  kubernetes-starter
# 进入clone下的文件夹
root@server02:~# cd kubernetes-starter/
# 编辑 config.properties
root@server02:~/kubernetes-starter# vi config.properties 
#运行shell生成简单版本的K8S配置文件
root@server02:~/kubernetes-starter# ./gen-config.sh simple
====替换变量列表====
BIN_PATH=/root/bin
NODE_IP=192.168.0.169
ETCD_ENDPOINTS=http://192.168.0.169:2379
MASTER_IP=192.168.0.169
====================
====替换配置文件====
all-node/kube-calico.service
master-node/etcd.service
master-node/kube-apiserver.service
master-node/kube-controller-manager.service
master-node/kube-scheduler.service
services/kube-dns.yaml
worker-node/10-calico.conf
worker-node/kubelet.kubeconfig
worker-node/kubelet.service
worker-node/kube-proxy.kubeconfig
worker-node/kube-proxy.service
=================
配置生成成功,位置: /root/kubernetes-starter/target

#使用find命令 看一下target下面的配置文件
root@server02:~/kubernetes-starter# find target/ -type f
target/worker-node/kubelet.kubeconfig
target/worker-node/10-calico.conf
target/worker-node/kube-proxy.kubeconfig
target/worker-node/kube-proxy.service
target/worker-node/kubelet.service
target/services/kube-dns.yaml
target/master-node/kube-controller-manager.service
target/master-node/etcd.service
target/master-node/kube-scheduler.service
target/master-node/kube-apiserver.service
target/all-node/kube-calico.service

3-2-2. 部署ETCD

3-2-2-1. 介绍

kubernetes需要存储很多东西,像它本身的节点信息,组件信息,还有通过kubernetes运行的pod,deployment,service等等。都需要持久化。etcd就是它的数据中心。生产环境中为了保证数据中心的高可用和数据的一致性,一般会部署最少三个节点。我们这里以学习为主就只在主节点部署一个实例

3-2-2-2. 部署

etcd的二进制文件及配置都已经生成了, 只需要把他作为服务运行起来

#把服务配置文件copy到系统服务目录
$ cp ~/kubernetes-starter/target/master-node/etcd.service /lib/systemd/system/
# enable服务
$ systemctl enable etcd.service
#创建工作目录(保存数据的地方)
$ mkdir -p /var/lib/etcd
# 启动服务
$ service etcd start
# 查看服务日志,看是否有错误信息,确保服务正常
$ journalctl -f -u etcd.service 
3-2-2-3. 配置说明

3-2-3. 部署APIServer(主节点)

3-2-3-1. 介绍

kube-apiserver是Kubernetes最重要的核心组件之一,主要提供以下的功能

  • 提供集群管理的REST API接口,包括认证授权(我们现在没有用到)数据校验以及集群状态变更等
  • 提供其他模块之间的数据交互和通信的枢纽(其他模块通过API Server查询或修改数据,只有API Server才直接操作etcd)

生产环境为了保证apiserver的高可用一般会部署2+个节点,在上层做一个lb做负载均衡,比如haproxy。由于单节点和多节点在apiserver这一层说来没什么区别,所以我们目前部署一个节点就足够了

3-2-3-2. 部署

部署和上面部署etcd差不多 最后看一哈日志 无错即可

$ cp target/master-node/kube-apiserver.service /lib/systemd/system/
$ systemctl enable kube-apiserver.service
$ service kube-apiserver start
$ journalctl -f -u kube-apiserver
3-2-3-3. 配置说明

[Unit]
Description=Kubernetes API Server
...
[Service]
#可执行文件的位置
ExecStart=/home/michael/bin/kube-apiserver
#非安全端口(8080)绑定的监听地址 这里表示监听所有地址
--insecure-bind-address=0.0.0.0
#不使用https
--kubelet-https=false
#kubernetes集群的虚拟ip的地址范围
--service-cluster-ip-range=10.68.0.0/16
#service的nodeport的端口范围限制
--service-node-port-range=20000-40000
#很多地方都需要和etcd打交道,也是唯一可以直接操作etcd的模块
--etcd-servers=http://192.168.1.102:2379
...

3-2-4. 部署ControllerManager(主节点)

3-2-4-1. 简介

Controller Manager由kube-controller-manager和cloud-controller-manager组成,是Kubernetes的大脑,它通过apiserver监控整个集群的状态,并确保集群处于预期的工作状态。 kube-controller-manager由一系列的控制器组成,像Replication Controller控制副本,Node Controller节点控制,Deployment Controller管理deployment等等 cloud-controller-manager在Kubernetes启用Cloud Provider的时候才需要,用来配合云服务提供商的控制

controller-manager、scheduler和apiserver 三者的功能紧密相关,一般运行在同一个机器上,我们可以把它们当做一个整体来看,所以保证了apiserver的高可用即是保证了三个模块的高可用。也可以同时启动多个controller-manager进程,但只有一个会被选举为leader提供服务

3-2-4-2. 部署
$ cp target/master-node/kube-controller-manager.service /lib/systemd/system/
$ systemctl enable kube-controller-manager.service
$ service kube-controller-manager start
$ journalctl -f -u kube-controller-manager
3-2-4-3. 配置说明

[Unit]
Description=Kubernetes Controller Manager
...
[Service]
ExecStart=/home/michael/bin/kube-controller-manager
#对外服务的监听地址,这里表示只有本机的程序可以访问它
--address=127.0.0.1
#apiserver的url
--master=http://127.0.0.1:8080
#服务虚拟ip范围,同apiserver的配置
--service-cluster-ip-range=10.68.0.0/16
#pod的ip地址范围
--cluster-cidr=172.20.0.0/16
#下面两个表示不使用证书,用空值覆盖默认值
--cluster-signing-cert-file=
--cluster-signing-key-file=
...

3-2-5. 部署Scheduler(主节点)

3-2-5-1. 简介

kube-scheduler负责分配调度Pod到集群内的节点上,它监听kube-apiserver,查询还未分配Node的Pod,然后根据调度策略为这些Pod分配节点。我们前面讲到的kubernetes的各种调度策略就是它实现的。

3-2-5-2. 部署
$ cp target/master-node/kube-scheduler.service /lib/systemd/system/
$ systemctl enable kube-scheduler.service
$ service kube-scheduler start
$ journalctl -f -u kube-scheduler
3-2-5-3. 配置
[Unit]
Description=Kubernetes Scheduler
...
[Service]
ExecStart=/home/michael/bin/kube-scheduler \
#对外服务的监听地址,这里表示只有本机的程序可以访问它
--address=127.0.0.1 \
#apiserver的url
--master=http://127.0.0.1:8080 \
...

3-2-6. 部署Calico

posted @ 2020-07-15 17:23  LtPpp  阅读(130)  评论(0)    收藏  举报