K8S手动搭建(rpm方式)

写在前面:

K8S集群的部署需要通过手动部署多练习,以便于深入的了解他的架构和问题排查,个人建议每部署一个服务做一个快照,有助于反复折腾某一个可能不理解或者可能有问题的服务.

K8s架构图

50751fd616495ee1589d445f54180104b00.jpg

Kubernetes主要由以下几个核心组件组成:

  • etcd保存了整个集群的状态;
  • apiserver提供了资源操作的唯一入口,并提供认证、授权、访问控制、API注册和发现等机制;
  • controller manager负责维护集群的状态,比如故障检测、自动扩展、滚动更新等;
  • scheduler负责资源的调度,按照预定的调度策略将Pod调度到相应的机器上;
  • kubelet负责维护容器的生命周期,同时也负责Volume(CSI)和网络(CNI)的管理;
  • Container runtime负责镜像管理以及Pod和容器的真正运行(CRI);
  • kube-proxy负责为Service提供cluster内部的服务发现和负载均衡;

除了核心组件,还有一些推荐的插件,其中有的已经成为CNCF中的托管项目:

  • CoreDNS负责为整个集群提供DNS服务
  • Ingress Controller为服务提供外网入口
  • Prometheus提供资源监控
  • Dashboard提供GUI
  • Federation提供跨可用区的集群

一 环境

1 节点分布
Master 节点:10.10.25.149  Hostname:master  DNS:.8.8.8.8
Node1 节点:10.10.25.150  Hostname:node1  DNS:.8.8.8.8
Node2 节点:10.10.25.151  Hostname:node2  DNS:.8.8.8.8
2 添加解析
10.10.25.149 master     master.cn
10.10.25.151 node2      node2.cn
10.10.25.150 node1      node1.cn
3 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
4 关闭selinux
永久关闭修改/etc/selinux/config为SELINUX=disabled
临时关闭 setenforce 0 (不建议)
5 配置epel 源
rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
6 安装常用工具
yum install -y net-tools vim lrzsz tree screen lsof tcpdump nc mtr nmap
7 配置内核参数
vim /etc/sysctl.conf
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1

vm.swappiness = 0
net.ipv4.neigh.default.gc_stale_time=120
net.ipv4.ip_forward = 1

# see details in https://help.aliyun.com/knowledge_detail/39428.html
net.ipv4.conf.all.rp_filter=0
net.ipv4.conf.default.rp_filter=0
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_announce=2
net.ipv4.conf.all.arp_announce=2


# see details in https://help.aliyun.com/knowledge_detail/41334.html
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 1024
net.ipv4.tcp_synack_retries = 2
kernel.sysrq = 1

# iptables透明网桥的实现
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-arptables = 1

modprobe br_netfilter
sysctl -p

二  正式环境部署之系统初始化

1.安装Docker(三个节点都需要)

使用国内Docker源

cd /etc/yum.repos.d/
 wget \
 https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

Docker安装

yum install -y docker-ce

启动docker

systemctl start docker
systemctl enable docker

三 准备相关软件包

将数据包上传到 /usr/local/src/ 目录  这些包区官网下 一下是我自己的

[root@localhost src]# tree 
.
├── cfssl-1.2
│?? ├── cfssl-certinfo_linux-amd64
│?? ├── cfssljson_linux-amd64
│?? └── cfssl_linux-amd64
├── cni-plugins-amd64-v0.7.0
│?? ├── bridge
│?? ├── dhcp
│?? ├── flannel
│?? ├── host-device
│?? ├── host-local
│?? ├── ipvlan
│?? ├── loopback
│?? ├── macvlan
│?? ├── portmap
│?? ├── ptp
│?? ├── sample
│?? ├── tuning
│?? └── vlan
├── etcd-v3.3.1-linux-amd64
│?? ├── etcd
│?? └── etcdctl
├── files
├── flannel-v0.10.0-linux-amd64
│?? ├── flanneld
│?? └── mk-docker-opts.sh
├── k8s-v1.10.3
│?? └── bin
│??     ├── kube-apiserver
│??     ├── kube-controller-manager
│??     ├── kubectl
│??     ├── kubelet
│??     ├── kube-proxy
│??     └── kube-scheduler
# ll -h

drwxr-xr-x. 2 root root    6 3月   9 09:37 cfssl-1.2
drwxr-xr-x. 2 root root  195 6月   3 2018 cni-plugins-amd64-v0.7.0
drwxr-xr-x. 2 root root   33 6月   3 2018 etcd-v3.3.1-linux-amd64
drwxr-xr-x. 2 root root    6 3月   8 18:20 files
drwxr-xr-x. 2 root root   47 6月   3 2018 flannel-v0.10.0-linux-amd64
drwxr-xr-x. 3 root root   17 6月   3 2018 k8s-v1.10.3
drwxr-xr-x  9 root root  156 5月  21 2018 kubernetes
-rw-r--r--  1 root root 2.7M 3月   9 18:10 kubernetes.tar.gz
drwxr-xr-x. 2 root root 4.0K 3月   9 17:20 ssl

上面就是我们需要用到的包

添加系统路径

创建部署目录(master 和slave 节点都要创建)
mkdir -p /opt/kubernetes/{cfg,bin,ssl,log}
#cfg 存放配置文件
#bin 存放启动文件
#ssl 存放证书文件
#log 存放log文件
#vim /root/.bash_profile 添加
PATH=$PATH:$HOME/bin:/opt/kubernetes/bin
source 一下
source .bash_profile

四 证书部署之ectd集群CA证书制作

1.安装 CFSSL

cd /usr/local/src
cd cfssl-1.2/
chmod +x cfssl*
mv cfssl-certinfo_linux-amd64 /opt/kubernetes/bin/cfssl-certinfo
mv cfssl_linux-amd64  /opt/kubernetes/bin/cfssl
mv cfssljson_linux-amd64  /opt/kubernetes/bin/cfssljson

复制cfssl命令文件到k8s-node1和k8s-node2节点。如果实际中多个节点,就都需要同步复制。

scp /opt/kubernetes/bin/cfssl* 10.10.25.151:/opt/kubernetes/bin
scp /opt/kubernetes/bin/cfssl* 10.10.25.150:/opt/kubernetes/bin

2.初始化CFSSL

cd ..
mkdir ssl && cd ssl
#ssl 目录用于存放生成证书的json文件和生成的csr,pem文件,以后每一步涉及到认证证书生成的时候都默认到这个目录下生成,生成后拷贝到真正存放证书的目录 /opt/kubernetes/ssl/
cfssl print-defaults config > config.json
cfssl print-defaults csr > csr.json

3.创建用来生成 CA 文件的 JSON 配置文件

vim ca-config.json

{
  "signing": {
    "default": {
      "expiry": "8760h"
    },
    "profiles": {
      "kubernetes": {
        "usages": [
            "signing",
            "key encipherment",
            "server auth",
            "client auth"
        ],
        "expiry": "8760h"
      }
    }
  }
}

4.创建用来生成 CA 证书签名请求(CSR)的 JSON 配置文件

vim ca-csr.json

{
  "CN": "kubernetes",
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "ST": "ShenZhen",
      "L": "ShenZhen",
      "O": "k8s",
      "OU": "System"
    }
  ]
}

5.生成CA证书(ca.pem)和密钥(ca-key.pem)

# cfssl gencert -initca ca-csr.json | cfssljson -bare ca
2019/03/09 10:15:21 [INFO] generating a new CA key and certificate from CSR
2019/03/09 10:15:21 [INFO] generate received request
2019/03/09 10:15:21 [INFO] received CSR
2019/03/09 10:15:21 [INFO] generating key: rsa-2048
2019/03/09 10:15:21 [INFO] encoded CSR
2019/03/09 10:15:21 [INFO] signed certificate with serial number 47966651507733441265673034873213746755078188541
# ls
ca-config.json  ca.csr  ca-csr.json  ca-key.pem  ca.pem  config.json  csr.json  vim
# ls -l ca*
-rw-r--r--. 1 root root  290 3月   9 09:50 ca-config.json
-rw-r--r--. 1 root root 1005 3月   9 10:15 ca.csr
-rw-r--r--. 1 root root  210 3月   9 10:14 ca-csr.json
-rw-------. 1 root root 1679 3月   9 10:15 ca-key.pem
-rw-r--r--. 1 root root 1363 3月   9 10:15 ca.pem

这里我们生成了ca.csr  ca-key.pem ca.pem 三个整数文件

 

6.分发证书(master 和 node节点都要颁发)

cp ca.csr ca.pem ca-key.pem ca-config.json /opt/kubernetes/ssl
scp ca.csr ca.pem ca-key.pem ca-config.json 10.10.25.150:/opt/kubernetes/ssl
scp ca.csr ca.pem ca-key.pem ca-config.json 10.10.25.151:/opt/kubernetes/ssl

五 正式环境部署之etcd集群部署

1 准备etcd软件包 (我们之前已经上传了包,在/usr/local/src目录)

etcd-v3.3.1-linux-amd64.tar.gz
cd etcd-v3.3.1-linux-amd64/
scp etcd etcdctl 10.10.25.150:/opt/kubernetes/bin/
scp etcd etcdctl 10.10.25.151:/opt/kubernetes/bin/
chmod +x /opt/kubernetes/bin/etcd*

2.创建 etcd 证书签名请求

cd /usr/local/src/ssl/
vim etcd-csr.json

{
  "CN": "etcd",
  "hosts": [
    "127.0.0.1",
"10.10.25.151",
"10.10.25.150",
"10.10.25.149"
  ],
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "ST": "BeiJing",
      "L": "BeiJing",
      "O": "k8s",
      "OU": "System"
    }
  ]
}

3.生成 etcd 证书和私钥

cfssl gencert -ca=/opt/kubernetes/ssl/ca.pem \
  -ca-key=/opt/kubernetes/ssl/ca-key.pem \
  -config=/opt/kubernetes/ssl/ca-config.json \
  -profile=kubernetes etcd-csr.json | cfssljson -bare etcd

会生成以下证书文件

# ls -l etcd*
-rw-r--r-- 1 root root 1062 3月   9 10:48 etcd.csr
-rw-r--r-- 1 root root  283 3月   9 10:42 etcd-csr.json
-rw------- 1 root root 1675 3月   9 10:48 etcd-key.pem
-rw-r--r-- 1 root root 1436 3月   9 10:48 etcd.pem

4.将证书移动到/opt/kubernetes/ssl目录下

cp etcd*.pem /opt/kubernetes/ssl
scp etcd*.pem 10.10.25.151:/opt/kubernetes/ssl/
scp etcd*.pem 10.10.25.150:/opt/kubernetes/ssl/
rm -f etcd.csr etcd-csr.json

5.设置ETCD配置文件

vim /opt/kubernetes/cfg/etcd.conf

#[member]
ETCD_NAME="etcd-node1"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
#ETCD_SNAPSHOT_COUNTER="10000"
#ETCD_HEARTBEAT_INTERVAL="100"
#ETCD_ELECTION_TIMEOUT="1000"
ETCD_LISTEN_PEER_URLS="https://10.10.25.149:2380"
ETCD_LISTEN_CLIENT_URLS="https://10.10.25.149:2379,https://127.0.0.1:2379"
#ETCD_MAX_SNAPSHOTS="5"
#ETCD_MAX_WALS="5"
#ETCD_CORS=""
#[cluster]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://10.10.25.149:2380"
# if you use different ETCD_NAME (e.g. test),
# set ETCD_INITIAL_CLUSTER value for this name, i.e. "test=http://..."
ETCD_INITIAL_CLUSTER="etcd-node1=https://10.10.25.149:2380,etcd-node2=https://10.10.25.150:2380,etcd-node3=https://10.10.25.151:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="k8s-etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="https://10.10.25.149:2379"
#[security]
CLIENT_CERT_AUTH="true"
ETCD_CA_FILE="/opt/kubernetes/ssl/ca.pem"
ETCD_CERT_FILE="/opt/kubernetes/ssl/etcd.pem"
ETCD_KEY_FILE="/opt/kubernetes/ssl/etcd-key.pem"
PEER_CLIENT_CERT_AUTH="true"
ETCD_PEER_CA_FILE="/opt/kubernetes/ssl/ca.pem"
ETCD_PEER_CERT_FILE="/opt/kubernetes/ssl/etcd.pem"
ETCD_PEER_KEY_FILE="/opt/kubernetes/ssl/etcd-key.pem"

6.创建ETCD系统服务(包括node节点)

vim /etc/systemd/system/etcd.service

[Unit]
Description=Etcd Server
After=network.target

[Service]
Type=simple
WorkingDirectory=/var/lib/etcd
EnvironmentFile=-/opt/kubernetes/cfg/etcd.conf
# set GOMAXPROCS to number of processors
ExecStart=/bin/bash -c "GOMAXPROCS=$(nproc) /opt/kubernetes/bin/etcd"
Type=notify

[Install]
WantedBy=multi-user.target

7.重新加载系统服务

systemctl daemon-reload 
systemctl enable etcd
此时先不要启动(启动会检查其他成员),因为其他成员还没有加进来

将相关文件拷贝到其他看两个节点

需要到其他两个节点修改etcd.conf 休要修名称和属于自己节点的ip,cluster那一行不需要改

etcd-node2

#[member]
ETCD_NAME="etcd-node2"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
#ETCD_SNAPSHOT_COUNTER="10000"
#ETCD_HEARTBEAT_INTERVAL="100"
#ETCD_ELECTION_TIMEOUT="1000"
ETCD_LISTEN_PEER_URLS="https://10.10.25.150:2380"
ETCD_LISTEN_CLIENT_URLS="https://10.10.25.150:2379,https://127.0.0.1:2379"
#ETCD_MAX_SNAPSHOTS="5"
#ETCD_MAX_WALS="5"
#ETCD_CORS=""
#[cluster]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://10.10.25.150:2380"
# if you use different ETCD_NAME (e.g. test),
# set ETCD_INITIAL_CLUSTER value for this name, i.e. "test=http://..."
ETCD_INITIAL_CLUSTER="etcd-node1=https://10.10.25.149:2380,etcd-node2=https://10.10.25.150:2380,etcd-node3=https://10.10.25.151:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="k8s-etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="https://10.10.25.150:2379"
#[security]
CLIENT_CERT_AUTH="true"
ETCD_CA_FILE="/opt/kubernetes/ssl/ca.pem"
ETCD_CERT_FILE="/opt/kubernetes/ssl/etcd.pem"
ETCD_KEY_FILE="/opt/kubernetes/ssl/etcd-key.pem"
PEER_CLIENT_CERT_AUTH="true"
ETCD_PEER_CA_FILE="/opt/kubernetes/ssl/ca.pem"
ETCD_PEER_CERT_FILE="/opt/kubernetes/ssl/etcd.pem"
ETCD_PEER_KEY_FILE="/opt/kubernetes/ssl/etcd-key.pem"

etcd-node3


#[member]
ETCD_NAME="etcd-node3"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
#ETCD_SNAPSHOT_COUNTER="10000"
#ETCD_HEARTBEAT_INTERVAL="100"
#ETCD_ELECTION_TIMEOUT="1000"
ETCD_LISTEN_PEER_URLS="https://10.10.25.151:2380"
ETCD_LISTEN_CLIENT_URLS="https://10.10.25.151:2379,https://127.0.0.1:2379"
#ETCD_MAX_SNAPSHOTS="5"
#ETCD_MAX_WALS="5"
#ETCD_CORS=""
#[cluster]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://10.10.25.151:2380"
# if you use different ETCD_NAME (e.g. test),
# set ETCD_INITIAL_CLUSTER value for this name, i.e. "test=http://..."
ETCD_INITIAL_CLUSTER="etcd-node1=https://10.10.25.149:2380,etcd-node2=https://10.10.25.150:2380,etcd-node3=https://10.10.25.151:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="k8s-etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="https://10.10.25.151:2379"
#[security]
CLIENT_CERT_AUTH="true"
ETCD_CA_FILE="/opt/kubernetes/ssl/ca.pem"
ETCD_CERT_FILE="/opt/kubernetes/ssl/etcd.pem"
ETCD_KEY_FILE="/opt/kubernetes/ssl/etcd-key.pem"
PEER_CLIENT_CERT_AUTH="true"
ETCD_PEER_CA_FILE="/opt/kubernetes/ssl/ca.pem"
ETCD_PEER_CERT_FILE="/opt/kubernetes/ssl/etcd.pem"
ETCD_PEER_KEY_FILE="/opt/kubernetes/ssl/etcd-key.pem"

在所有节点上创建etcd存储目录并启动etcd 先初始化在master

mkdir /var/lib/etcd
systemctl start etcd
systemctl status etcd

检查集群状态
etcdctl --endpoints=https://10.10.25.149:2379 \
  --ca-file=/opt/kubernetes/ssl/ca.pem \
  --cert-file=/opt/kubernetes/ssl/etcd.pem \
  --key-file=/opt/kubernetes/ssl/etcd-key.pem cluster-health

member 470b36414bb2a372 is healthy: got healthy result from https://10.10.25.151:2379
member 8ac4a593172eaf24 is healthy: got healthy result from https://10.10.25.150:2379
member af4085ed2ef49345 is healthy: got healthy result from https://10.10.25.149:2379
cluster is healthy

六 正式环境部署之master部署(Kubernetes API,Controller Manager,Kubernetes Scheduler)

部署Kubernetes API服务部署

1 软件包拷贝到相关目录

cp kube-apiserver /opt/kubernetes/bin/
cp kube-controller-manager /opt/kubernetes/bin/
cp kube-scheduler /opt/kubernetes/bin/
chmod +x /opt/kubernetes/bin/*

2  创建生成CSR的 JSON 配置文件

vim kubernetes-csr.json


{
  "CN": "kubernetes",
  "hosts": [
    "127.0.0.1",
    "10.10.25.149",
    "10.1.0.1",
    "kubernetes",
    "kubernetes.default",
    "kubernetes.default.svc",
    "kubernetes.default.svc.cluster",
    "kubernetes.default.svc.cluster.local"
  ],
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "ST": "BeiJing",
      "L": "BeiJing",
      "O": "k8s",
      "OU": "System"
    }
  ]
}

10.1.0.1 定义了集群VIP的地址网关

3.生成 kubernetes 证书和私钥

cfssl gencert -ca=/opt/kubernetes/ssl/ca.pem \
   -ca-key=/opt/kubernetes/ssl/ca-key.pem \
   -config=/opt/kubernetes/ssl/ca-config.json \
   -profile=kubernetes kubernetes-csr.json | cfssljson -bare kubernetes
将证书文件拷贝到存放证书的目录,node节点同步操作
cp kubernetes*.pem /opt/kubernetes/ssl/
scp kubernetes*.pem 10.10.25.150:/opt/kubernetes/ssl/
scp kubernetes*.pem 10.10.25.151:/opt/kubernetes/ssl/

4.创建 kube-apiserver 使用的客户端 token 文件

head -c 16 /dev/urandom | od -An -t x | tr -d ' '
此token需要记住后面需要用
a2db55b582f753cb27eac4405f033efc

创建启动文件 
vim /opt/kubernetes/ssl/bootstrap-token.csv 写入
a2db55b582f753cb27eac4405f033efc,kubelet-bootstrap,10001,"system:kubelet-bootstrap"

5.创建基础用户名/密码认证配置

vim /opt/kubernetes/ssl/basic-auth.csv

admin,admin,1
readonly,readonly,2

6.部署Kubernetes API Server

#创建系统服务
vim /usr/lib/systemd/system/kube-apiserver.service

[Unit]
Description=Kubernetes API Server
Documentation=https://github.com/GoogleCloudPlatform/kubernetes
After=network.target

[Service]
ExecStart=/opt/kubernetes/bin/kube-apiserver \
  --admission-control=NamespaceLifecycle,LimitRanger,ServiceAccount,DefaultStorageClass,ResourceQuota,NodeRestriction \
  --bind-address=10.10.25.149 \
  --insecure-bind-address=127.0.0.1 \
  --authorization-mode=Node,RBAC \
  --runtime-config=rbac.authorization.k8s.io/v1 \
  --kubelet-https=true \
  --anonymous-auth=false \
  --basic-auth-file=/opt/kubernetes/ssl/basic-auth.csv \
  --enable-bootstrap-token-auth \
  --token-auth-file=/opt/kubernetes/ssl/bootstrap-token.csv \
  --service-cluster-ip-range=10.1.0.0/16 \
  --service-node-port-range=20000-40000 \
  --tls-cert-file=/opt/kubernetes/ssl/kubernetes.pem \
  --tls-private-key-file=/opt/kubernetes/ssl/kubernetes-key.pem \
  --client-ca-file=/opt/kubernetes/ssl/ca.pem \
  --service-account-key-file=/opt/kubernetes/ssl/ca-key.pem \
  --etcd-cafile=/opt/kubernetes/ssl/ca.pem \
  --etcd-certfile=/opt/kubernetes/ssl/kubernetes.pem \
  --etcd-keyfile=/opt/kubernetes/ssl/kubernetes-key.pem \
  --etcd-servers=https://10.10.25.149:2379,https://10.10.25.150:2379,https://10.10.25.151:2379 \
  --enable-swagger-ui=true \
  --allow-privileged=true \
  --audit-log-maxage=30 \
  --audit-log-maxbackup=3 \
  --audit-log-maxsize=100 \
  --audit-log-path=/opt/kubernetes/log/api-audit.log \
  --event-ttl=1h \
  --v=2 \
  --logtostderr=false \
  --log-dir=/opt/kubernetes/log
Restart=on-failure
RestartSec=5
Type=notify
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

7.启动API Server服务

systemctl daemon-reload
systemctl enable kube-apiserver
systemctl start kube-apiserver

查看API Server服务状态

systemctl status kube-apiserver

部署Controller Manager服务

1 创建系统服务

#创建系统服务
vim /usr/lib/systemd/system/kube-controller-manager.service

[Unit]
Description=Kubernetes Controller Manager
Documentation=https://github.com/GoogleCloudPlatform/kubernetes

[Service]
ExecStart=/opt/kubernetes/bin/kube-controller-manager \
  --address=127.0.0.1 \
  --master=http://127.0.0.1:8080 \
  --allocate-node-cidrs=true \
  --service-cluster-ip-range=10.1.0.0/16 \
  --cluster-cidr=10.2.0.0/16 \
  --cluster-name=kubernetes \
  --cluster-signing-cert-file=/opt/kubernetes/ssl/ca.pem \
  --cluster-signing-key-file=/opt/kubernetes/ssl/ca-key.pem \
  --service-account-private-key-file=/opt/kubernetes/ssl/ca-key.pem \
  --root-ca-file=/opt/kubernetes/ssl/ca.pem \
  --leader-elect=true \
  --v=2 \
  --logtostderr=false \
  --log-dir=/opt/kubernetes/log

Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target



10.2.0.0 定义了集群POD的地址段

10.1.0.0 定义了集群vip 的 地址段

2 启动Controller Manager

systemctl daemon-reload
systemctl enable kube-controller-manager
systemctl start kube-controller-manager

3 查看服务状态

systemctl status kube-controller-manager

部署Kubernetes Scheduler

1 创建系统服务

vim /usr/lib/systemd/system/kube-scheduler.service

[Unit]
Description=Kubernetes Scheduler
Documentation=https://github.com/GoogleCloudPlatform/kubernetes

[Service]
ExecStart=/opt/kubernetes/bin/kube-scheduler \
  --address=127.0.0.1 \
  --master=http://127.0.0.1:8080 \
  --leader-elect=true \
  --v=2 \
  --logtostderr=false \
  --log-dir=/opt/kubernetes/log

Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

2 启动服务

systemctl daemon-reload
systemctl enable kube-scheduler
systemctl start kube-scheduler

3 查看服务状态

systemctl status kube-scheduler

部署kubectl 命令行工具

1 将包拷贝到相关目录

cp kubectl /opt/kubernetes/bin/

2 创建 admin 证书签名请求

cd /usr/local/src/ssl/

vim admin-csr.json

{
  "CN": "admin",
  "hosts": [],
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "ST": "BeiJing",
      "L": "BeiJing",
      "O": "system:masters",
      "OU": "System"
    }
  ]
}

3 生成 admin 证书和私钥

cfssl gencert -ca=/opt/kubernetes/ssl/ca.pem \
   -ca-key=/opt/kubernetes/ssl/ca-key.pem \
   -config=/opt/kubernetes/ssl/ca-config.json \
   -profile=kubernetes admin-csr.json | cfssljson -bare admin

4 拷贝证书文件到ssl目录

mv admin*.pem /opt/kubernetes/ssl/

5 设置集群参数

kubectl config set-cluster kubernetes \
   --certificate-authority=/opt/kubernetes/ssl/ca.pem \
   --embed-certs=true \
   --server=https://10.10.25.149:6443

Cluster "kubernetes" set.

6 设置客户端认证参数

kubectl config set-credentials admin \
   --client-certificate=/opt/kubernetes/ssl/admin.pem \
   --embed-certs=true \
   --client-key=/opt/kubernetes/ssl/admin-key.pem

7 设置上下文参数

kubectl config set-context kubernetes \
   --cluster=kubernetes \
   --user=admin

8 设置默认上下文

kubectl config use-context kubernetes

上几步就是在/root/目录下创建一个.kube/config 文件 未来kubectl 与 API通讯会使用到这个文件 如果在其他几点运行kubectl 需要将这个文件拷贝过去

9 使用kubectl工具

kubectl get cs

NAME                 STATUS    MESSAGE             ERROR
scheduler            Healthy   ok                  
controller-manager   Healthy   ok                  
etcd-0               Healthy   {"health":"true"}   
etcd-2               Healthy   {"health":"true"}   
etcd-1               Healthy   {"health":"true"}  

小总结:

ETCD端口: 2379

APIServer端口:6443 8080

Kube-scheduler端口:10251

Kube-controller 端口:10252

七 正式环境之node部署kubelet(node1,node2部署)

1 部署kubelet

由于通信使用kube-proxy 所以master节点也要部署

cp kubelet kube-proxy /opt/kubernetes/bin/
scp kubelet kube-proxy 10.10.25.150:/opt/kubernetes/bin/
scp kubelet kube-proxy 10.10.25.151:/opt/kubernetes/bin/
chmod +x /opt/kubernetes/bin/*

2 创建角色绑定

kubectl create clusterrolebinding kubelet-bootstrap --clusterrole=system:node-bootstrapper --user=kubelet-bootstrap

clusterrolebinding "kubelet-bootstrap" created

3 创建 kubelet bootstrapping kubeconfig 文件 设置集群参数

kubectl config set-cluster kubernetes \
   --certificate-authority=/opt/kubernetes/ssl/ca.pem \
   --embed-certs=true \
   --server=https://10.10.25.149:6443 \
   --kubeconfig=bootstrap.kubeconfig

4 设置客户端认证参数

kubectl config set-credentials kubelet-bootstrap \
   --token=a2db55b582f753cb27eac4405f033efc \
   --kubeconfig=bootstrap.kubeconfig   

5 设置上下文参数

kubectl config set-context default \
   --cluster=kubernetes \
   --user=kubelet-bootstrap \
   --kubeconfig=bootstrap.kubeconfig

6 选择默认上下文

kubectl config use-context default --kubeconfig=bootstrap.kubeconfig

其实就是为了生成 bootstrap.kubeconfig

7 将配置文件拷贝到相关 cfg目录

cp bootstrap.kubeconfig /opt/kubernetes/cfg
scp bootstrap.kubeconfig 10.10.25.150:/opt/kubernetes/cfg
scp bootstrap.kubeconfig 10.10.25.151:/opt/kubernetes/cfg

NODE 部署kubelet

1.设置CNI支持(三个节点,master可不用)

创建目录
mkdir -p /etc/cni/net.d

vim /etc/cni/net.d/10-default.conf

{
        "name": "flannel",
        "type": "flannel",
        "delegate": {
            "bridge": "docker0",
            "isDefaultGateway": true,
            "mtu": 1400
        }
}

2 创建kubelet目录 三个节点master不用

mkdir /var/lib/kubelet

3.创建kubelet服务配置(三个节点master可不用)

node1 

vim /usr/lib/systemd/system/kubelet.service

[Unit]
Description=Kubernetes Kubelet
Documentation=https://github.com/GoogleCloudPlatform/kubernetes
After=docker.service
Requires=docker.service

[Service]
WorkingDirectory=/var/lib/kubelet
ExecStart=/opt/kubernetes/bin/kubelet \
  --address=10.10.25.150 \
  --hostname-override=10.10.25.150 \
  --pod-infra-container-image=mirrorgooglecontainers/pause-amd64:3.0 \
  --experimental-bootstrap-kubeconfig=/opt/kubernetes/cfg/bootstrap.kubeconfig \
  --kubeconfig=/opt/kubernetes/cfg/kubelet.kubeconfig \
  --cert-dir=/opt/kubernetes/ssl \
  --network-plugin=cni \
  --cni-conf-dir=/etc/cni/net.d \
  --cni-bin-dir=/opt/kubernetes/bin/cni \
  --cluster-dns=10.1.0.2 \
  --cluster-domain=cluster.local. \
  --hairpin-mode hairpin-veth \
  --allow-privileged=true \
  --fail-swap-on=false \
  --logtostderr=true \
  --v=2 \
  --logtostderr=false \
  --log-dir=/opt/kubernetes/log
Restart=on-failure
RestartSec=5

node2 

vim /usr/lib/systemd/system/kubelet.service

[Unit]
Description=Kubernetes Kubelet
Documentation=https://github.com/GoogleCloudPlatform/kubernetes
After=docker.service
Requires=docker.service

[Service]
WorkingDirectory=/var/lib/kubelet
ExecStart=/opt/kubernetes/bin/kubelet \
  --address=10.10.25.151 \
  --hostname-override=10.10.25.151 \
  --pod-infra-container-image=mirrorgooglecontainers/pause-amd64:3.0 \
  --experimental-bootstrap-kubeconfig=/opt/kubernetes/cfg/bootstrap.kubeconfig \
  --kubeconfig=/opt/kubernetes/cfg/kubelet.kubeconfig \
  --cert-dir=/opt/kubernetes/ssl \
  --network-plugin=cni \
  --cni-conf-dir=/etc/cni/net.d \
  --cni-bin-dir=/opt/kubernetes/bin/cni \
  --cluster-dns=10.1.0.2 \
  --cluster-domain=cluster.local. \
  --hairpin-mode hairpin-veth \
  --allow-privileged=true \
  --fail-swap-on=false \
  --logtostderr=true \
  --v=2 \
  --logtostderr=false \
  --log-dir=/opt/kubernetes/log
Restart=on-failure
RestartSec=5

4 启动Kubelet

systemctl daemon-reload
systemctl enable kubelet
systemctl start kubelet

5 查看服务状态

systemctl status kubelet

6 master 查看csr请求

kubectl get csr

kubectl get csr
NAME                                                   AGE       REQUESTOR           CONDITION
node-csr-C_MAaItufJNJ7Ntr4WvQZI-j3b2OGjb8o8Q6zxJ3-DA   1m        kubelet-bootstrap   Pending
node-csr-Io5Snx5fGX-wFbfde3uajhe1bQULiO-uQ0IxgM9MvpM   1m        kubelet-bootstrap   Pending

发现是pending状态,需要批准TLS请求

7 批准kubelet 的 TLS 证书请求

master 节点执行

kubectl get csr|grep 'Pending' | awk 'NR>0{print $1}'| xargs kubectl certificate approve

一下返回内容说明已经批准:
certificatesigningrequest.certificates.k8s.io "node-csr-C_MAaItufJNJ7Ntr4WvQZI-j3b2OGjb8o8Q6zxJ3-DA" approved
certificatesigningrequest.certificates.k8s.io "node-csr-Io5Snx5fGX-wFbfde3uajhe1bQULiO-uQ0IxgM9MvpM" approved

执行完毕后,查看节点状态已经是Ready的状态了

kubectl get node

NAME           STATUS    ROLES     AGE       VERSION
10.10.25.150   Ready     <none>    3m        v1.10.3
10.10.25.151   Ready     <none>    3m        v1.10.3

这里是生成了一个kubelet-client.crt 在 ssl 目录(mode节点中)

八 正式环境之node部署Kubernetes Proxy (这里只做一个节点另外一个节点相同)

1 配置kube-proxy使用LVS

yum install -y ipvsadm ipset conntrack

2 master创建 kube-proxy 证书请求

vim kube-proxy-csr.json

{
  "CN": "system:kube-proxy",
  "hosts": [],
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "ST": "BeiJing",
      "L": "BeiJing",
      "O": "k8s",
      "OU": "System"
    }
  ]
}

3 生成证书

cfssl gencert -ca=/opt/kubernetes/ssl/ca.pem \
   -ca-key=/opt/kubernetes/ssl/ca-key.pem \
   -config=/opt/kubernetes/ssl/ca-config.json \
   -profile=kubernetes  kube-proxy-csr.json | cfssljson -bare kube-proxy

4 分发证书到所有Node节点

cp kube-proxy*.pem /opt/kubernetes/ssl/

scp kube-proxy*.pem 10.10.25.150:/opt/kubernetes/ssl/
scp kube-proxy*.pem 10.10.25.151:/opt/kubernetes/ssl/

5 创建kube-proxy配置文件(master)

kubectl config set-cluster kubernetes \
   --certificate-authority=/opt/kubernetes/ssl/ca.pem \
   --embed-certs=true \
   --server=https://10.10.25.149:6443 \
   --kubeconfig=kube-proxy.kubeconfig

kubectl config set-credentials kube-proxy \
   --client-certificate=/opt/kubernetes/ssl/kube-proxy.pem \
   --client-key=/opt/kubernetes/ssl/kube-proxy-key.pem \
   --embed-certs=true \
   --kubeconfig=kube-proxy.kubeconfig

kubectl config set-context default \
   --cluster=kubernetes \
   --user=kube-proxy \
   --kubeconfig=kube-proxy.kubeconfig

kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig

执行这么多命令目的是为了 生成kube-proxy.kubeconfig

6 分发kubeconfig配置文件

cp kube-proxy.kubeconfig /opt/kubernetes/cfg/
scp kube-proxy.kubeconfig 10.10.25.150:/opt/kubernetes/cfg/
scp kube-proxy.kubeconfig 10.10.25.151:/opt/kubernetes/cfg/

7 创建kube-proxy服务配置(三个节点都创建,master可以选择不创建)

创建工作目录

mkdir /var/lib/kube-proxy

创建 系统服务

vim /usr/lib/systemd/system/kube-proxy.service

[Unit]
Description=Kubernetes Kube-Proxy Server
Documentation=https://github.com/GoogleCloudPlatform/kubernetes
After=network.target

[Service]
WorkingDirectory=/var/lib/kube-proxy
ExecStart=/opt/kubernetes/bin/kube-proxy \
  --bind-address=10.10.25.150 \
  --hostname-override=10.10.25.150 \
  --kubeconfig=/opt/kubernetes/cfg/kube-proxy.kubeconfig \
--masquerade-all \
  --feature-gates=SupportIPVSProxyMode=true \
  --proxy-mode=ipvs \
  --ipvs-min-sync-period=5s \
  --ipvs-sync-period=5s \
  --ipvs-scheduler=rr \
  --logtostderr=true \
  --v=2 \
  --logtostderr=false \
  --log-dir=/opt/kubernetes/log

Restart=on-failure
RestartSec=5
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

另外一个node也是一样的配置 只需要改变IP 即可

8 启动Kubernetes Proxy

systemctl daemon-reload
systemctl enable kube-proxy
systemctl start kube-proxy

9 查看服务状态 查看kube-proxy服务状态(node节点上检测)

systemctl status kube-proxy

10 检查LVS状态

ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.1.0.1:443 rr persistent 10800
  -> 10.10.25.149:6443            Masq    1      0          0  

node2 节点与这个相同 把证书和config文件拷贝到目标目录,创建系统系统文件启动即可

九 正式环境之部署Flannel网络

1 为Flannel生成证书

vim flanneld-csr.json

{
  "CN": "flanneld",
  "hosts": [],
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "ST": "BeiJing",
      "L": "BeiJing",
      "O": "k8s",
      "OU": "System"
    }
  ]
}

2 生成证书

cfssl gencert -ca=/opt/kubernetes/ssl/ca.pem \
   -ca-key=/opt/kubernetes/ssl/ca-key.pem \
   -config=/opt/kubernetes/ssl/ca-config.json \
   -profile=kubernetes flanneld-csr.json | cfssljson -bare flanneld

3 分发证书

cp flanneld*.pem /opt/kubernetes/ssl/
scp flanneld*.pem 10.10.25.150:/opt/kubernetes/ssl/
scp flanneld*.pem 10.10.25.151:/opt/kubernetes/ssl/

4.拷贝Flannel软件包

cp flanneld mk-docker-opts.sh /opt/kubernetes/bin/

复制到node
scp flanneld mk-docker-opts.sh 10.10.25.151:/opt/kubernetes/bin/
scp flanneld mk-docker-opts.sh 10.10.25.150:/opt/kubernetes/bin/

5 复制对应脚本到/opt/kubernetes/bin目录下

cd /usr/local/src/kubernetes/cluster/centos/node/bin/
cp remove-docker0.sh /opt/kubernetes/bin/
scp remove-docker0.sh 10.10.25.150:/opt/kubernetes/bin/
scp remove-docker0.sh 10.10.25.151:/opt/kubernetes/bin/

6 配置Flannel

vim /opt/kubernetes/cfg/flannel

FLANNEL_ETCD="-etcd-endpoints=https://10.10.25.149:2379,https://10.10.25.150:2379,https://10.10.25.151:2379"
FLANNEL_ETCD_KEY="-etcd-prefix=/kubernetes/network"
FLANNEL_ETCD_CAFILE="--etcd-cafile=/opt/kubernetes/ssl/ca.pem"
FLANNEL_ETCD_CERTFILE="--etcd-certfile=/opt/kubernetes/ssl/flanneld.pem"
FLANNEL_ETCD_KEYFILE="--etcd-keyfile=/opt/kubernetes/ssl/flanneld-key.pem"

复制配置到其它节点上

scp /opt/kubernetes/cfg/flannel 10.10.25.150:/opt/kubernetes/cfg/
scp /opt/kubernetes/cfg/flannel 10.10.25.151:/opt/kubernetes/cfg/

6 设置Flannel系统服务

vim /usr/lib/systemd/system/flannel.service

[Unit]
Description=Flanneld overlay address etcd agent
After=network.target
Before=docker.service

[Service]
EnvironmentFile=-/opt/kubernetes/cfg/flannel
ExecStartPre=/opt/kubernetes/bin/remove-docker0.sh
ExecStart=/opt/kubernetes/bin/flanneld --ip-masq ${FLANNEL_ETCD} ${FLANNEL_ETCD_KEY} ${FLANNEL_ETCD_CAFILE} ${FLANNEL_ETCD_CERTFILE} ${FLANNEL_ETCD_KEYFILE}
ExecStartPost=/opt/kubernetes/bin/mk-docker-opts.sh -d /run/flannel/docker

Type=notify

[Install]
WantedBy=multi-user.target
RequiredBy=docker.service

复制系统服务脚本到其它节点上

scp /usr/lib/systemd/system/flannel.service 10.10.25.150:/usr/lib/systemd/system/
scp /usr/lib/systemd/system/flannel.service 10.10.25.151:/usr/lib/systemd/system/

十 Flannel CNI集成

1 创建工作目录

mkdir /opt/kubernetes/bin/cni

cd cni-plugins-amd64-v0.7.0/
mv * /opt/kubernetes/bin/cni
scp -r /opt/kubernetes/bin/cni/* 10.10.25.150:/opt/kubernetes/bin/cni/
scp -r /opt/kubernetes/bin/cni/* 10.10.25.151:/opt/kubernetes/bin/cni/

2 创建Etcd的key

/opt/kubernetes/bin/etcdctl --ca-file /opt/kubernetes/ssl/ca.pem --cert-file /opt/kubernetes/ssl/flanneld.pem --key-file /opt/kubernetes/ssl/flanneld-key.pem \
--no-sync -C https://10.10.25.149:2379,https://10.10.25.150:2379,https://10.10.25.151:2379 \
mk /kubernetes/network/config '{ "Network": "10.2.0.0/16", "Backend": { "Type": "vxlan", "VNI": 1 }}' >/dev/null 2>&1

3  启动flannel

systemctl daemon-reload
systemctl enable flannel
chmod +x /opt/kubernetes/bin/*
chmod +x opt/kubernetes/bin/cni/*

systemctl start flannel
查看服务状态
systemctl status flannel

4 配置Docker使用Flannel

vim /usr/lib/systemd/system/docker.service

[Unit] #在Unit下面修改After和增加Requires
After=network-online.target firewalld.service flannel.service
Wants=network-online.target
Requires=flannel.service

[Service] #增加EnvironmentFile=-/run/flannel/docker
Type=notify
EnvironmentFile=-/run/flannel/docker
ExecStart=/usr/bin/dockerd $DOCKER_OPTS


将配置复制到另外两个节点
# scp /usr/lib/systemd/system/docker.service 10.10.25.150:/usr/lib/systemd/system/
# scp /usr/lib/systemd/system/docker.service 10.10.25.151:/usr/lib/systemd/system/

重启Docker
systemctl daemon-reload
systemctl restart docker

查看网络
ifconfig

b8d1e8bcc3948add21121f8733cf4ebe8bc.jpg

保证了 在同一网段,在其他机器查看也是一样说明已经成功

十一 正式环境部署之创建docker应用

1.创建一个测试用的deployment 已测试我们的所有功能包括网络

kubectl run net-test --image=alpine --replicas=2 sleep 360000

2.查看获取IP情况

# kubectl get pod -o wide
NAME                        READY     STATUS    RESTARTS   AGE       IP          NODE
net-test-5767cb94df-8rg5c   1/1       Running   1          9m        10.2.91.3   10.10.25.151
net-test-5767cb94df-qt7zc   1/1       Running   1          9m        10.2.46.3   10.10.25.150

3.测试联通性(直接ping在master上)

# ping 10.2.91.3
PING 10.2.91.3 (10.2.91.3) 56(84) bytes of data.
64 bytes from 10.2.91.3: icmp_seq=1 ttl=63 time=0.632 ms
64 bytes from 10.2.91.3: icmp_seq=2 ttl=63 time=0.429 ms
64 bytes from 10.2.91.3: icmp_seq=3 ttl=63 time=0.486 ms

至此已经K8S集群已经部署完成,过程复杂重要在生成证书认证,如果证书错误是无法继续进行的,边部署边看日志是很好的学习习惯.

 

 

 

 

 

 

转载于:https://my.oschina.net/54188zz/blog/3019796

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值