1. 程式人生 > >CentOS7.5中安裝kubernetes - v1.12.1

CentOS7.5中安裝kubernetes - v1.12.1

文章目錄

1 機器環境

1.1 機器1

hostname:k8s-master1
ip:172.16.159.130

1.2 機器2

hostname:k8s-node1
ip:172.16.159.131

1.3 機器3

hostname:k8s-node2
ip:172.16.159.132

2 軟體介紹

2.1 作業系統

centos:CentOS Linux release 7.5.1804
核心:3.10.0-862.el7.x86_64

2.2 Docker

docker:docker-18.03.1-ce.tgz

2.3 etcd

etcd:etcd-v3.2.9-linux-amd64.tar.gz

2.4 flanneld

flanneld:flannel-v0.7.1-linux-amd64.tar.gz

3 安裝步驟

3.1 安裝和配置 docker(master和node節點部署)

k8s-master1,k8s-node1,k8s-node2都得安裝docker。

3.1.1 下載最新的 docker 二進位制檔案

#  mkdir -p /Data/apps/docker
#  cd /Data/apps/docker
#  wget https://download.docker.com/linux/static/stable/x86_64/docker-18.03.1-ce.tgz
#  tar -xvf docker-18.03.1-ce.tgz
#  cp docker/docker* /usr/bin

3.1.2 建立 docker 的 啟動檔案

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

[Unit]
Description=Docker Application Container Engine
Documentation=http://docs.docker.io

[Service]
Environment="PATH=/root/local/bin:/bin:/sbin:/usr/bin:/usr/sbin"
EnvironmentFile=-/run/flannel/docker
ExecStart=/usr/bin/dockerd --log-level=error $DOCKER_NETWORK_OPTIONS
ExecReload=/bin/kill -s HUP $MAINPID
Restart=on-failure
RestartSec=5
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Delegate=yes
KillMode=process

[Install]
WantedBy=multi-user.target

3.1.3 啟動 dockerd

# systemctl daemon-reload
# systemctl stop firewalld
# systemctl disable firewalld
# iptables -F && iptables -X && iptables -F -t nat && iptables -X -t nat
# systemctl enable docker
# systemctl start docker

3.1.4 檢查 docker 服務

# docker version
Client:
 Version:      18.03.1-ce
 API version:  1.37
 Go version:   go1.9.2
 Git commit:   9ee9f40
 Built:        Thu Apr 26 07:12:25 2018
 OS/Arch:      linux/amd64
 Experimental: false
 Orchestrator: swarm

Server:
 Engine:
  Version:      18.03.1-ce
  API version:  1.37 (minimum version 1.12)
  Go version:   go1.9.5
  Git commit:   9ee9f40
  Built:        Thu Apr 26 07:23:03 2018
  OS/Arch:      linux/amd64
  Experimental: false

3.2 安裝和配置 etcd(master節點部署)

k8s-master1安裝etcd-v3.2.9-linux-amd64.tar.gz

3.2.1 etcd資料庫安裝

# mkdir -p /Data/apps/etcd
# cd /Data/apps/etcd
# wget https://github.com/coreos/etcd/releases/download/v3.2.9/etcd-v3.2.9-linux-amd64.tar.gz
# tar -xvf etcd-v3.2.9-linux-amd64.tar.gz 
# cd etcd-v3.2.9-linux-amd64
# cp etcd etcdctl /usr/bin/

3.2.2 建立etcd作目錄

# mkdir  -p  /var/lib/etcd

3.2.3 建立 etcd 的 啟動檔案

vi /etc/systemd/system/etcd.service

[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
Type=notify
WorkingDirectory=/var/lib/etcd/
EnvironmentFile=-/etc/etcd/etcd.conf          
# set GOMAXPROCS to number of processors
ExecStart=/bin/bash -c "GOMAXPROCS=$(nproc) /usr/bin/etcd --name=\"${ETCD_NAME}\" --data-dir=\"${ETCD_DATA_DIR}\" --listen-client-urls=\"${ETCD_LISTEN_CLIENT_URLS}\""
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

3.2.4 建立配置/etc/etcd/etcd.conf檔案

mkdir -p /etc/etcd/
vi /etc/etcd/etcd.conf

ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379"
ETCD_NAME="default"
ETCD_ADVERTISE_CLIENT_URLS="http://0.0.0.0:2379"```

3.2.5 配置開機啟動

# systemctl daemon-reload
# systemctl enable etcd.service
# systemctl start etcd.service

3.2.6 檢驗etcd是否安裝成功

# etcdctl cluster-health
member 8e9e05c52164694d is healthy: got healthy result from http://0.0.0.0:2379
cluster is healthy

3.3 安裝和配置 kubernetes(master節點部署)

k8s-master1安裝kubernetes。

3.3.1 安裝kubernetes 二進位制檔案

#  mkdir -p /Data/apps/k8s
#  cd /Data/apps/k8s
#  wget https://dl.k8s.io/v1.11.1/kubernetes-server-linux-amd64.tar.gz
#  tar -xzvf kubernetes-server-linux-amd64.tar.gz
#  cd kubernetes/server/bin/
#  cp kube-apiserver kube-controller-manager kube-scheduler kubectl /usr/bin/

3.3.2 配置kube-apiserver.service

1 配置kube-apiserver.service啟動專案
vi /usr/lib/systemd/system/kube-apiserver.service

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

[Service]
EnvironmentFile=-/etc/kubernetes/config
EnvironmentFile=-/etc/kubernetes/apiserver
ExecStart=/usr/bin/kube-apiserver \
            $KUBE_LOGTOSTDERR \
            $KUBE_LOG_LEVEL \
            $KUBE_ETCD_SERVERS \
            $KUBE_API_ADDRESS \
            $KUBE_API_PORT \
            $KUBELET_PORT \
            $KUBE_ALLOW_PRIV \
            $KUBE_SERVICE_ADDRESSES \
            $KUBE_ADMISSION_CONTROL \
            $KUBE_API_ARGS
Restart=on-failure
Type=notify
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

2 配置kube-apiserver.service引數
mkdir -p /etc/kubernetes/
vi /etc/kubernetes/apiserver

KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0"
KUBE_API_PORT="--port=8080"
KUBE_ETCD_SERVERS="--etcd-servers=http://172.16.159.130:2379"
KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=172.17.0.1/16"
KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"
KUBE_API_ARGS=""

vi /etc/kubernetes/config

KUBE_LOGTOSTDERR="--logtostderr=true"
KUBE_LOG_LEVEL="--v=0"
KUBE_ALLOW_PRIV="--allow-privileged=false"
KUBE_MASTER="--master=http://172.16.159.130:8080"

3.3.3 配置kube-controller-manager

1 配置啟動檔案
vi /usr/lib/systemd/system/kube-controller-manager.service

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

[Service]
EnvironmentFile=-/etc/kubernetes/config
EnvironmentFile=-/etc/kubernetes/controller-manager
ExecStart=/usr/bin/kube-controller-manager \
            $KUBE_LOGTOSTDERR \
            $KUBE_LOG_LEVEL \
            $KUBE_MASTER \
            $KUBE_CONTROLLER_MANAGER_ARGS
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

2 配置引數
vi /etc/kubernetes/controller-manager

KUBE_CONTROLLER_MANAGER_ARGS=" "

3.3.4 配置kube-scheduler

1 配置啟動檔案
vi /usr/lib/systemd/system/kube-scheduler.service

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

[Service]
EnvironmentFile=-/etc/kubernetes/config
EnvironmentFile=-/etc/kubernetes/scheduler
ExecStart=/usr/bin/kube-scheduler \
            $KUBE_LOGTOSTDERR \
            $KUBE_LOG_LEVEL \
            $KUBE_MASTER \
            $KUBE_SCHEDULER_ARGS
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

2 配置引數
mkdir -p /Data/logs/kubernetes
vi /etc/kubernetes/scheduler

KUBE_SCHEDULER_ARGS="--logtostderr=true --log-dir=/Data/logs/kubernetes --v=2"

3.3.5 將各元件加入開機自啟

# systemctl daemon-reload 
# systemctl enable kube-apiserver.service
# systemctl start kube-apiserver.service
# systemctl enable kube-controller-manager.service
# systemctl start kube-controller-manager.service
# systemctl enable kube-scheduler.service
# systemctl start kube-scheduler.service

3.3.6 驗證 master 節點功能

# kubectl get componentstatuses
NAME                 STATUS    MESSAGE              ERROR
controller-manager   Healthy   ok                   
scheduler            Healthy   ok                   
etcd-0               Healthy   {"health": "true"}   

3.4 安裝和配置 kubernetes(node節點部署)

k8s-node1,k8s-node2安裝kubernetes。

3.4.1 安裝kubernetes 二進位制檔案

#  mkdir -p /Data/apps/k8s
#  cd /Data/apps/k8s
#  wget https://dl.k8s.io/v1.11.1/kubernetes-server-linux-amd64.tar.gz
#  tar -xzvf kubernetes-server-linux-amd64.tar.gz
#  cd kubernetes/server/bin/
#  cp kube-proxy kubelet /usr/bin/

3.4.2 配置kubelet

1 配置kubelet啟動項
vi /usr/lib/systemd/system/kubelet.service

[Unit]
Description=Kubernetes Kubelet
After=docker.service
Requires=docker.service
[Service]
EnvironmentFile=-/etc/kubernetes/kubelet
ExecStart=/usr/bin/kubelet \
${KUBE_LOGTOSTDERR} \
${KUBE_LOG_LEVEL} \
${NODE_ADDRESS} \
${NODE_PORT} \
${NODE_HOSTNAME} \
${KUBELET_KUBECONFIG} \
${KUBE_ALLOW_PRIV} \
${KUBELET_DNS_IP} \
${KUBELET_DNS_DOMAIN} \
${KUBELET_SWAP}
Restart=on-failure
KillMode=process
[Install]
WantedBy=multi-user.target

2 配置kubelet引數
mkdir -p /etc/kubernetes/
vi /etc/kubernetes/kubelet

 啟用日誌標準錯誤
KUBE_LOGTOSTDERR="--logtostderr=true"
# 日誌級別
KUBE_LOG_LEVEL="--v=0"
# Kubelet服務IP地址
NODE_ADDRESS="--address=172.16.159.131"
# Kubelet服務埠
NODE_PORT="--port=10250"
# 自定義節點名稱
NODE_HOSTNAME="--hostname-override=172.16.159.131"
# kubeconfig路徑,指定連線API伺服器
KUBELET_KUBECONFIG="--kubeconfig=/etc/kubernetes/kubelet.kubeconfig"
# 允許容器請求特權模式,預設false
KUBE_ALLOW_PRIV="--allow-privileged=false"
#KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest"
KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0"
# DNS資訊
KUBELET_DNS_IP="--cluster-dns=10.254.0.2"
KUBELET_DNS_DOMAIN="--cluster-domain=cluster.local"
# 禁用使用Swap
KUBELET_SWAP="--fail-swap-on=false"

vi /etc/kubernetes/kubelet.kubeconfig

apiVersion: v1
kind: Config
clusters:
  - cluster:
      server: http://172.16.159.130:8080
    name: local
contexts:
  - context:
      cluster: local
    name: local
current-context: local

vi /etc/kubernetes/config

KUBE_LOGTOSTDERR="--logtostderr=true"
KUBE_LOG_LEVEL="--v=0"
KUBE_ALLOW_PRIV="--allow-privileged=false"
KUBE_MASTER="--master=http://172.16.159.130:8080"

3 啟動 kubelet

# systemctl daemon-reload
# systemctl enable kubelet.service
# systemctl start kubelet.service
# systemctl status kubelet.service

3.4.3 配置kube-proxy

1 配置啟動檔案
vi /usr/lib/systemd/system/kube-proxy.service

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

[Service]
EnvironmentFile=-/etc/kubernetes/config
EnvironmentFile=-/etc/kubernetes/proxy
ExecStart=/usr/bin/kube-proxy \
            $KUBE_LOGTOSTDERR \
            $KUBE_LOG_LEVEL \
            $KUBE_MASTER \
            $KUBE_PROXY_ARGS
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

2 配置引數
vi /etc/kubernetes/proxy

KUBE_PROXY_ARGS=""

3 啟動 kube-proxy

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

3.4.4 檢查節點狀態

進入到k8s-master1機器

[[email protected] k8s]# kubectl get nodes
NAME             STATUS   ROLES    AGE   VERSION
172.16.159.131   Ready    <none>   17m   v1.12.1

3.5 安裝和配置 flanneld(master和node節點部署)

k8s-master1,k8s-node1,k8s-node2都得安裝flanneld。

3.5.1 下載安裝flanneld

# mkdir -p /Data/apps/flannel
# cd /Data/apps/flannel
# wget https://github.com/coreos/flannel/releases/download/v0.7.1/flannel-v0.7.1-linux-amd64.tar.gz
# tar -xzvf flannel-v0.7.1-linux-amd64.tar.gz
# cp flanneld mk-docker-opts.sh /usr/bin

3.5.2 配置flanneld

1 建立啟動檔案
vi /usr/lib/systemd/system/flanneld.service

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

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/flanneld
EnvironmentFile=-/etc/sysconfig/docker-network
ExecStart=/usr/bin/flanneld-start $FLANNEL_OPTIONS
ExecStartPost=/usr/bin/mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/docker
Restart=on-failure

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

2 配置flanneld配置檔案
mkdir -p /Data/apps/k8s/network
vi /etc/sysconfig/flanneld

# Flanneld configuration options  

# etcd url location.  Point this to the server where etcd runs
FLANNEL_ETCD_ENDPOINTS="http://172.16.159.130:2379"

# etcd config key.  This is the configuration key that flannel queries
# For address range assignment
FLANNEL_ETCD_PREFIX="/Data/apps/k8s/network"

# Any additional options that you want to pass
#FLANNEL_OPTIONS=""

3 配置docker-network
vi /etc/sysconfig/docker-network

DOCKER_NETWORK_OPTIONS=

4 flanneld-star
vi /usr/bin/flanneld-start

#!/bin/sh

exec /usr/bin/flanneld \
        -etcd-endpoints=${FLANNEL_ETCD_ENDPOINTS:-${FLANNEL_ETCD}} \
        -etcd-prefix=${FLANNEL_ETCD_PREFIX:-${FLANNEL_ETCD_KEY}} \
        "[email protected]"

賦執行許可權

chmod +x /usr/bin/flanneld-start

3.5.3 配置etcd中關於flannel的key(只用在k8s-master1上執行)

[[email protected] flannel]# etcdctl set /Data/apps/k8s/network/config '{"Network": "172.20.0.0/16"}'
{"Network": "172.20.0.0/16"}
[[email protected] flannel]# etcdctl get /Data/apps/k8s/network/config
{"Network": "172.20.0.0/16"}
[[email protected] flannel]# 

3.5.4 啟動flanneld

啟動Flannel之後,需要依次重啟docker、kubernete。
1 在master執行:

[[email protected] flannel]# systemctl daemon-reload
[[email protected] flannel]# systemctl enable flanneld.service
[[email protected] flannel]# systemctl start flanneld.service 
[[email protected] flannel]# service docker restart
[[email protected] flannel]# systemctl restart kube-apiserver.service
[[email protected] flannel]# systemctl restart kube-controller-manager.service
[[email protected] flannel]# systemctl restart kube-scheduler.service

2 在node執行:

[[email protected] ~]# systemctl daemon-reload
[[email protected] ~]# systemctl enable flanneld.service 
[[email protected] ~]# systemctl start flanneld.service 
[[email protected] ~]# service docker restart
[[email protected] ~]# systemctl restart kubelet.service
[[email protected] ~]# systemctl restart kube-proxy.service

4 參考文件

1 entOS7安裝kubernetes1.11.2:
https://blog.csdn.net/zzq900503/article/details/81710319

2 二進位制離線安裝
https://www.jianshu.com/p/dadafc62ed24

http://blog.51cto.com/12480612/2287449?source=dra

https://blog.csdn.net/ljx1528/article/details/81545187

https://blog.csdn.net/a735131232/article/details/83352517 (有軟體包)

http://blog.51cto.com/13120271/2115310

3 工作原理:
https://baijiahao.baidu.com/s?id=1602795888204860650&wfr=spider&for=pc