部署kube-apiserver集群

集群规划

  主机名		        角色			   ip
HDSS1-21.host.com	kube-apiserver		192.168.1.21
HDSS1-22.host.com	kube-apiserver		192.168.1.22
HDSS1-11.host.com	4层负载均衡		    192.168.1.11
HDSS1-12.host.com	4层负载均衡	     	192.168.1.12

注意:这里192.168.1.11和192.168.1.12使用nginx做4层负载均衡器,用keepalive跑一个vip:192.168.1.10,代理两个kube-apiserver,实现高可用

这里部署文档以HDSS1-21.host.com主机为例,另外一台(22)运算节点部署方法类似

下载软件,解压

[root@hdss-1-21 src]# ll
total 442992
-rw-r--r-- 1 root root   9850227 Feb  9 18:12 etcd-v3.1.20-linux-amd64.tar.gz
-rw-r--r-- 1 root root 443770238 Feb  9 20:34 kubernetes-server-linux-amd64-v1.15.2.tar.gz
[root@hdss-1-21 src]# tar xzvf kubernetes-server-linux-amd64-v1.15.2.tar.gz -C /opt

做软连接,安装1.15.2

[root@hdss-1-21 opt]# mv kubernetes/ kubernetes-v1.15.2
[root@hdss-1-21 opt]# ln -s /opt/kubernetes-v1.15.2/ /opt/kubernetes
[root@hdss-1-21 opt]# ll
total 0
drwx--x--x  4 root root  28 Feb  9 15:44 containerd
lrwxrwxrwx  1 root root  17 Feb  9 18:13 etcd -> /opt/etcd-v3.1.20
drwxr-xr-x  4 etcd etcd 166 Feb  9 19:20 etcd-v3.1.20
lrwxrwxrwx  1 root root  24 Feb 10 09:31 kubernetes -> /opt/kubernetes-v1.15.2/
drwxr-xr-x  4 root root  50 Feb 10 09:30 kubernetes-v1.15.2
drwxr-xr-x. 2 root root   6 Oct 31  2018 rh
drwxr-xr-x  2 root root  97 Feb  9 20:32 src

 删除源码包和没用的docker镜像

[root@hdss-1-21 opt]# cd kubernetes-v1.15.2/
[root@hdss-1-21 kubernetes-v1.15.2]# rm -rf kubernetes-src.tar.gz 
[root@hdss-1-21 kubernetes-v1.15.2]# cd server/bin/
[root@hdss-1-21 bin]# ll
total 1548800
-rwxr-xr-x 1 root root  43534816 Aug  5  2019 apiextensions-apiserver
-rwxr-xr-x 1 root root 100548640 Aug  5  2019 cloud-controller-manager
-rw-r--r-- 1 root root         8 Aug  5  2019 cloud-controller-manager.docker_tag
-rw-r--r-- 1 root root 144437760 Aug  5  2019 cloud-controller-manager.tar
-rwxr-xr-x 1 root root 200648416 Aug  5  2019 hyperkube
-rwxr-xr-x 1 root root  40182208 Aug  5  2019 kubeadm
-rwxr-xr-x 1 root root 164501920 Aug  5  2019 kube-apiserver
-rw-r--r-- 1 root root         8 Aug  5  2019 kube-apiserver.docker_tag
-rw-r--r-- 1 root root 208390656 Aug  5  2019 kube-apiserver.tar
-rwxr-xr-x 1 root root 116397088 Aug  5  2019 kube-controller-manager
-rw-r--r-- 1 root root         8 Aug  5  2019 kube-controller-manager.docker_tag
-rw-r--r-- 1 root root 160286208 Aug  5  2019 kube-controller-manager.tar
-rwxr-xr-x 1 root root  42985504 Aug  5  2019 kubectl
-rwxr-xr-x 1 root root 119616640 Aug  5  2019 kubelet
-rwxr-xr-x 1 root root  36987488 Aug  5  2019 kube-proxy
-rw-r--r-- 1 root root         8 Aug  5  2019 kube-proxy.docker_tag
-rw-r--r-- 1 root root  84282368 Aug  5  2019 kube-proxy.tar
-rwxr-xr-x 1 root root  38786144 Aug  5  2019 kube-scheduler
-rw-r--r-- 1 root root         8 Aug  5  2019 kube-scheduler.docker_tag
-rw-r--r-- 1 root root  82675200 Aug  5  2019 kube-scheduler.tar
-rwxr-xr-x 1 root root   1648224 Aug  5  2019 mounter
[root@hdss-1-21 bin]# rm -rf *tar
[root@hdss-1-21 bin]# rm -rf *_tag
[root@hdss-1-21 bin]# ll
total 884636
-rwxr-xr-x 1 root root  43534816 Aug  5  2019 apiextensions-apiserver
-rwxr-xr-x 1 root root 100548640 Aug  5  2019 cloud-controller-manager
-rwxr-xr-x 1 root root 200648416 Aug  5  2019 hyperkube
-rwxr-xr-x 1 root root  40182208 Aug  5  2019 kubeadm
-rwxr-xr-x 1 root root 164501920 Aug  5  2019 kube-apiserver
-rwxr-xr-x 1 root root 116397088 Aug  5  2019 kube-controller-manager
-rwxr-xr-x 1 root root  42985504 Aug  5  2019 kubectl
-rwxr-xr-x 1 root root 119616640 Aug  5  2019 kubelet
-rwxr-xr-x 1 root root  36987488 Aug  5  2019 kube-proxy
-rwxr-xr-x 1 root root  38786144 Aug  5  2019 kube-scheduler
-rwxr-xr-x 1 root root   1648224 Aug  5  2019 mounter
[root@hdss-1-21 bin]# 

签发apiserver-client证书:apiserver与etc通信用的证书。apiserver是客户端,etcd是服务端,  运维主机HDSS-1-200.host.com上

(1) 编辑/opt/certs/client-csr.json文件

[root@hdss-1-200 certs]# vi /opt/certs/client-csr.json

{
    "CN": "k8s-node",
    "hosts": [
    ],
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "ST": "beijing",
            "L": "beijing",
            "O": "od",
            "OU": "ops"
        }
    ]
}

 (2) 生成证书

[root@hdss-1-200 certs]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=client client-csr.json |cfssl-json -bare client
[root@hdss-1-200 certs]# ll
total 52
-rw-r--r-- 1 root root  836 Feb  9 16:52 ca-config.json
-rw-r--r-- 1 root root  993 Feb  9 15:30 ca.csr
-rw-r--r-- 1 root root  346 Feb  9 15:29 ca-csr.json
-rw------- 1 root root 1675 Feb  9 15:30 ca-key.pem
-rw-r--r-- 1 root root 1346 Feb  9 15:30 ca.pem
-rw-r--r-- 1 root root  993 Feb 10 09:34 client.csr
-rw-r--r-- 1 root root  280 Feb 10 09:34 client-csr.json
-rw------- 1 root root 1675 Feb 10 09:34 client-key.pem
-rw-r--r-- 1 root root 1363 Feb 10 09:34 client.pem
-rw-r--r-- 1 root root 1062 Feb  9 16:53 etcd-peer.csr
-rw-r--r-- 1 root root  375 Feb  9 16:53 etcd-peer-csr.json
-rw------- 1 root root 1675 Feb  9 16:53 etcd-peer-key.pem
-rw-r--r-- 1 root root 1428 Feb  9 16:53 etcd-peer.pem
[root@hdss-1-200 certs]# 

(3) 创建签名请求(csr)的apiserver-csr.json配置文件

[root@hdss-1-200 certs]# vim apiserver-csr.json 
{
    "CN": "k8s-apiserver",
    "hosts": [
        "127.0.0.1",
        "10.254.0.1",
        "kubernetes.default",
        "kubernetes.default.svc",
        "kubernetes.default.svc.cluster",
        "kubernetes.default.svc.cluster.local",
        "192.168.1.10",
        "192.168.1.21",
        "192.168.1.22",
        "192.168.1.23"
    ],
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "ST": "beijing",
            "L": "beijing",
            "O": "od",
            "OU": "ops"
        }
    ]
}

 (4) 生成apiserver端证书

[root@hdss-1-200 certs]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=server apiserver-csr.json |cfssl-json -bare apiserver
[root@hdss-1-200 certs]# ll
total 68
-rw-r--r-- 1 root root 1249 Feb 10 09:39 apiserver.csr
-rw-r--r-- 1 root root  577 Feb 10 09:39 apiserver-csr.json
-rw------- 1 root root 1675 Feb 10 09:39 apiserver-key.pem
-rw-r--r-- 1 root root 1598 Feb 10 09:39 apiserver.pem
-rw-r--r-- 1 root root  836 Feb  9 16:52 ca-config.json
-rw-r--r-- 1 root root  993 Feb  9 15:30 ca.csr
-rw-r--r-- 1 root root  346 Feb  9 15:29 ca-csr.json
-rw------- 1 root root 1675 Feb  9 15:30 ca-key.pem
-rw-r--r-- 1 root root 1346 Feb  9 15:30 ca.pem
-rw-r--r-- 1 root root  993 Feb 10 09:34 client.csr
-rw-r--r-- 1 root root  280 Feb 10 09:34 client-csr.json
-rw------- 1 root root 1675 Feb 10 09:34 client-key.pem
-rw-r--r-- 1 root root 1363 Feb 10 09:34 client.pem
-rw-r--r-- 1 root root 1062 Feb  9 16:53 etcd-peer.csr
-rw-r--r-- 1 root root  375 Feb  9 16:53 etcd-peer-csr.json
-rw------- 1 root root 1675 Feb  9 16:53 etcd-peer-key.pem
-rw-r--r-- 1 root root 1428 Feb  9 16:53 etcd-peer.pem
[root@hdss-1-200 certs]# 

将生成的证书放到21节点上对应的目录下

[root@hdss-1-21 bin]# mkdir /opt/kubernetes/server/bin/cert 
[root@hdss-1-21 cert]# scp root@192.168.1.200:/opt/certs/ca.pem    
[root@hdss-1-21 cert]# scp root@192.168.1.200:/opt/certs/ca-key.pem .   
[root@hdss-1-21 cert]# scp root@192.168.1.200:/opt/certs/client.pem .  
[root@hdss-1-21 cert]# scp root@192.168.1.200:/opt/certs/client-key.pem .   
[root@hdss-1-21 cert]# scp root@192.168.1.200:/opt/certs/apiserver.pem .   
[root@hdss-1-21 cert]# scp root@192.168.1.200:/opt/certs/apiserver-key.pem .
[root@hdss-1-21 cert]# ll
total 24
-rw------- 1 root root 1675 Feb 10 09:49 apiserver-key.pem
-rw-r--r-- 1 root root 1598 Feb 10 09:49 apiserver.pem
-rw------- 1 root root 1675 Feb 10 09:49 ca-key.pem
-rw-r--r-- 1 root root 1346 Feb 10 09:48 ca.pem
-rw------- 1 root root 1675 Feb 10 09:49 client-key.pem
-rw-r--r-- 1 root root 1363 Feb 10 09:49 client.pem
[root@hdss-1-21 cert]# 

给API创建配置文件(audit.yaml)

[root@hdss-1-21 conf]# pwd
/opt/kubernetes/server/bin/conf
[root@hdss-1-21 conf]# vi audit.yaml

apiVersion: audit.k8s.io/v1beta1 # This is required.
kind: Policy
# Don't generate audit events for all requests in RequestReceived stage.
omitStages:
  - "RequestReceived"
rules:
  # Log pod changes at RequestResponse level
  - level: RequestResponse
    resources:
    - group: ""
      # Resource "pods" doesn't match requests to any subresource of pods,
      # which is consistent with the RBAC policy.
      resources: ["pods"]
  # Log "pods/log", "pods/status" at Metadata level
  - level: Metadata
    resources:
    - group: ""
      resources: ["pods/log", "pods/status"]

  # Don't log requests to a configmap called "controller-leader"
  - level: None
    resources:
    - group: ""
      resources: ["configmaps"]
      resourceNames: ["controller-leader"]

  # Don't log watch requests by the "system:kube-proxy" on endpoints or services
  - level: None
    users: ["system:kube-proxy"]
    verbs: ["watch"]
    resources:
    - group: "" # core API group
      resources: ["endpoints", "services"]

  # Don't log authenticated requests to certain non-resource URL paths.
  - level: None
    userGroups: ["system:authenticated"]
    nonResourceURLs:
    - "/api*" # Wildcard matching.
    - "/version"

  # Log the request body of configmap changes in kube-system.
  - level: Request
    resources:
    - group: "" # core API group
      resources: ["configmaps"]
    # This rule only applies to resources in the "kube-system" namespace.
    # The empty string "" can be used to select non-namespaced resources.
    namespaces: ["kube-system"]

  # Log configmap and secret changes in all other namespaces at the Metadata level.
  - level: Metadata
    resources:
    - group: "" # core API group
      resources: ["secrets", "configmaps"]

  # Log all other resources in core and extensions at the Request level.
  - level: Request
    resources:
    - group: "" # core API group
    - group: "extensions" # Version of group should NOT be included.

  # A catch-all rule to log all other requests at the Metadata level.
  - level: Metadata
    # Long-running requests like watches that fall under this rule will not
    # generate an audit event in RequestReceived.
    omitStages:
      - "RequestReceived"

配置API启动脚本    查看帮助命令,查看每行的意思

[root@hdss7-21 bin]# ./kube-apiserver --help|grep -A 5 target-ram-mb 

编辑脚本如下

[root@hdss-1-21 bin]# pwd
/opt/kubernetes/server/bin
[root@hdss-1-21 bin]# vim kube-apiserver.sh 

#!/bin/bash
./kube-apiserver \
  --apiserver-count 2 \
  --audit-log-path /data/logs/kubernetes/kube-apiserver/audit-log \
  --audit-policy-file ./conf/audit.yaml \
  --authorization-mode RBAC \
  --client-ca-file ./cert/ca.pem \
  --requestheader-client-ca-file ./cert/ca.pem \
  --enable-admission-plugins NamespaceLifecycle,LimitRanger,ServiceAccount,DefaultStorageClass,DefaultTolerationSeconds,MutatingAdmissionWebhook,ValidatingAdmissionWebhook,ResourceQuota \
  --etcd-cafile ./cert/ca.pem \
  --etcd-certfile ./cert/client.pem \
  --etcd-keyfile ./cert/client-key.pem \
  --etcd-servers https://192.168.1.12:2379,https://192.168.1.21:2379,https://192.168.1.22:2379 \
  --service-account-key-file ./cert/ca-key.pem \
  --service-cluster-ip-range 10.254.0.0/16 \
  --service-node-port-range 3000-29999 \
  --target-ram-mb=1024 \
  --kubelet-client-certificate ./cert/client.pem \
  --kubelet-client-key ./cert/client-key.pem \
  --log-dir  /data/logs/kubernetes/kube-apiserver \
  --tls-cert-file ./cert/apiserver.pem \
  --tls-private-key-file ./cert/apiserver-key.pem \
  --v 2

 将脚本赋予执行权限

[root@hdss-1-21 bin]# chmod u+x kube-apiserver.sh

创建supervisord的后台启动配置

[root@hdss-1-21 bin]# vi /etc/supervisord.d/kube-apiserver.ini
[program:kube-apiserver-1-21]                                   # 21根据实际IP地址更改
command=/opt/kubernetes/server/bin/kube-apiserver.sh            ; the program (relative uses PATH, can take args)
numprocs=1                                                      ; number of processes copies to start (def 1)
directory=/opt/kubernetes/server/bin                            ; directory to cwd to before exec (def no cwd)
autostart=true                                                  ; start at supervisord start (default: true)
autorestart=true                                                ; retstart at unexpected quit (default: true)
startsecs=30                                                    ; number of secs prog must stay running (def. 1)
startretries=3                                                  ; max # of serial start failures (default 3)
exitcodes=0,2                                                   ; 'expected' exit codes for process (default 0,2)
stopsignal=QUIT                                                 ; signal used to kill process (default TERM)
stopwaitsecs=10                                                 ; max num secs to wait b4 SIGKILL (default 10)
user=root                                                       ; setuid to this UNIX account to run the program
redirect_stderr=true                                            ; redirect proc stderr to stdout (default false)
stdout_logfile=/data/logs/kubernetes/kube-apiserver/apiserver.stdout.log        ; stderr log path, NONE for none; default AUTO
stdout_logfile_maxbytes=64MB                                    ; max # logfile bytes b4 rotation (default 50MB)
stdout_logfile_backups=4                                        ; # of stdout logfile backups (default 10)
stdout_capture_maxbytes=1MB                                     ; number of bytes in 'capturemode' (default 0)
stdout_events_enabled=false                                     ; emit events on stdout writes (default false)

 创建配置文件中的目录

[root@hdss-1-21 bin]# mkdir -p /data/logs/kubernetes/kube-apiserver

启动kube

[root@hdss-1-21 bin]# supervisorctl update
kube-apiserver-1-21: added process group
[root@hdss-1-21 bin]# netstat -luntp | grep kube-api
tcp        0      0 127.0.0.1:8080          0.0.0.0:*               LISTEN      10958/./kube-apiser 
tcp6       0      0 :::6443                 :::*                    LISTEN      10958/./kube-apiser 

 22节点上的配置文件与上面过程一样,supervisord的后台启动配置有一个地方不同

[root@hdss-1-22 bin]# vi /etc/supervisord.d/kube-apiserver.ini

[program:kube-apiserver-1-22]                                   # 21根据实际IP地址更改
command=/opt/kubernetes/server/bin/kube-apiserver.sh            ; the program (relative uses PATH, can take args)
numprocs=1                                                      ; number of processes copies to start (def 1)
[program:kube-apiserver-1-22]                                   # 21根据实际IP地址更改
command=/opt/kubernetes/server/bin/kube-apiserver.sh            ; the program (relative uses PATH, can take args)
numprocs=1                                                      ; number of processes copies to start (def 1)
directory=/opt/kubernetes/server/bin                            ; directory to cwd to before exec (def no cwd)
autostart=true                                                  ; start at supervisord start (default: true)
autorestart=true                                                ; retstart at unexpected quit (default: true)
startsecs=30                                                    ; number of secs prog must stay running (def. 1)
startretries=3                                                  ; max # of serial start failures (default 3)
exitcodes=0,2                                                   ; 'expected' exit codes for process (default 0,2)
stopsignal=QUIT                                                 ; signal used to kill process (default TERM)
stopwaitsecs=10                                                 ; max num secs to wait b4 SIGKILL (default 10)
user=root                                                       ; setuid to this UNIX account to run the program
redirect_stderr=true                                            ; redirect proc stderr to stdout (default false)
stdout_logfile=/data/logs/kubernetes/kube-apiserver/apiserver.stdout.log        ; stderr log path, NONE for none; default AUTO
stdout_logfile_maxbytes=64MB                                    ; max # logfile bytes b4 rotation (default 50MB)
stdout_logfile_backups=4                                        ; # of stdout logfile backups (default 10)
stdout_capture_maxbytes=1MB                                     ; number of bytes in 'capturemode' (default 0)
stdout_events_enabled=false                                     ; emit events on stdout writes (default false)

22启动如下 

[root@hdss-1-22 bin]# vi /etc/supervisord.d/kube-apiserver.ini
[root@hdss-1-22 bin]#  mkdir -p /data/logs/kubernetes/kube-apiserver
[root@hdss-1-22 bin]#  supervisorctl update
kube-apiserver-1-22: added process group
[root@hdss-1-22 bin]# netstat -luntp | grep kube-api
tcp        0      0 127.0.0.1:8080          0.0.0.0:*               LISTEN      67359/./kube-apiser 
tcp6       0      0 :::6443                 :::*                    LISTEN      67359/./kube-apiser 

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐