五、在Master节点部署组件
在部署Kubernetes之前一定要确保etcd、flannel、docker是正常工作的,否则先解决问题再继续。
5.1 生成证书
5.1.1 创建CA证书:
[root@k8s-master ~]# mkdir /opt/ssl/kubernetes/
[root@k8s-master ~]# cd /opt/ssl/kubernetes/
[root@k8s-master kubernetes]# vim ca-config.json
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"kubernetes": {
"expiry": "87600h",
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
[root@k8s-master kubernetes]# vim ca-csr.json
{
"CN": "kubernetes",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "Beijing",
"ST": "Beijing",
"O": "k8s",
"OU": "System"
}
]
}
[root@k8s-master kubernetes]# cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
5.1.2 生成apiserver证书:
[root@k8s-master kubernetes]# vim server-csr.json
{
"CN": "kubernetes",
"hosts": [
"10.0.0.1", //这个是后边dns要用的虚拟网络的网关,不用改,就用这个切忌
"127.0.0.1",
"192.168.0.150",
"192.168.0.151",
"192.168.0.152",
"kubernetes",
"kubernetes.default",
"kubernetes.default.svc",
"kubernetes.default.svc.cluster",
"kubernetes.default.svc.cluster.local"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing",
"O": "k8s",
"OU": "System"
}
]
}
[root@k8s-master kubernetes]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare server
5.1.3 生成kube-proxy证书:
[root@k8s-master kubernetes]# vim kube-proxy-csr.json
{
"CN": "system:kube-proxy",
"hosts": [],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing",
"O": "k8s",
"OU": "System"
}
]
}
[root@k8s-master kubernetes]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxy
5.1.4 最终生成的证书文件:
[root@k8s-master kubernetes]# ls *pem
ca-key.pem ca.pem kube-proxy-key.pem kube-proxy.pem server-key.pem server.pem
5.2 部署apiserver组件
下载二进制包:https://dl.k8s.io/v1.11.0/kubernetes-server-linux-amd64.tar.gz
下载这个包(kubernetes-server-linux-amd64.tar.gz)就够了,包含了所需的所有组件。
[root@k8s-master ~]# mkdir /opt/kubernetes/{bin,cfg,ssl} -pv
[root@k8s-master ~]# tar zxvf kubernetes-server-linux-amd64.tar.gz
[root@k8s-master ~]# cd kubernetes/server/bin/
[root@k8s-master bin]# cp kube-apiserver kube-scheduler kube-controller-manager kubectl /opt/kubernetes/bin
5.2.1 把证书拷贝到/opt/kubernetes/ssl/
[root@k8s-master kubernetes]# pwd
/opt/ssl/kubernetes
[root@k8s-master kubernetes]# cp server.pem server-key.pem ca.pem ca-key.pem /opt/kubernetes/ssl/
5.2.2 创建token文件
[root@k8s-master kubernetes]# vim /opt/kubernetes/cfg/token.csv
674c457d4dcf2eefe4920d7dbb6b0ddc,kubelet-bootstrap,10001,"system:kubelet-bootstrap"
第一列:随机字符串,自己可生成第二列:用户名第三列:UID第四列:用户组
5.2.3 创建apiserver配置文件:
[root@k8s-master kubernetes]# vim /opt/kubernetes/cfg/kube-apiserver
KUBE_APISERVER_OPTS="--logtostderr=true \
--v=4 \--etcd-servers=https://192.168.0.150:2379,https://192.168.0.151:2379,https://192.168.0.152:2379 \
--bind-address=192.168.0.150 \
--secure-port=6443 \
--advertise-address=192.168.0.150 \
--allow-privileged=true \
--service-cluster-ip-range=10.0.0.0/24 \--enable-admission-plugins=NamespaceLifecycle,LimitRanger,ServiceAccount,ResourceQuota,NodeRestriction \ ------跟5.1.2配置的网关相对应
--authorization-mode=RBAC,Node \
--enable-bootstrap-token-auth \
--token-auth-file=/opt/kubernetes/cfg/token.csv \
--service-node-port-range=30000-50000 \
--tls-cert-file=/opt/kubernetes/ssl/server.pem \
--tls-private-key-file=/opt/kubernetes/ssl/server-key.pem \
--client-ca-file=/opt/kubernetes/ssl/ca.pem \
--service-account-key-file=/opt/kubernetes/ssl/ca-key.pem \
--etcd-cafile=/opt/etcd/ssl/ca.pem \
--etcd-certfile=/opt/etcd/ssl/server.pem \
--etcd-keyfile=/opt/etcd/ssl/server-key.pem"
配置好前面生成的证书,确保能连接etcd。
参数说明:
* --logtostderr 启用日志
* --v 日志等级
* --etcd-servers etcd集群地址
* --bind-address 监听地址
* --secure-port https安全端口
* --advertise-address 集群通告地址
* --allow-privileged 启用授权
* --service-cluster-ip-range Service虚拟IP地址段 //这里就用这个网段,切忌不要改
* --enable-admission-plugins 准入控制模块
* --authorization-mode 认证授权,启用RBAC授权和节点自管理
* --enable-bootstrap-token-auth 启用TLS bootstrap功能,后面会讲到
* --token-auth-file token文件
* --service-node-port-range Service Node类型默认分配端口范围
5.2.4 systemd管理apiserver:
[root@k8s-master ~]# vim /usr/lib/systemd/system/kube-apiserver.service
[Unit]
Description=Kubernetes API Server
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=-/opt/kubernetes/cfg/kube-apiserver
ExecStart=/opt/kubernetes/bin/kube-apiserver $KUBE_APISERVER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
5.2.5启动:
[root@k8s-master ~]# systemctl daemon-reload
[root@k8s-master ~]# systemctl start kube-apiserver
[root@k8s-master ~]# systemctl enable kube-apiserver
5.3 部署schduler组件
5.3.1 创建schduler配置文件:
[root@k8s-master ~]# vim /opt/kubernetes/cfg/kube-scheduler
KUBE_SCHEDULER_OPTS="--logtostderr=true \
--v=4 \
--master=127.0.0.1:8080 \
--leader-elect"
参数说明:
* --master 连接本地apiserver
* --leader-elect 当该组件启动多个时,自动选举(HA)
5.3.2 systemd管理schduler组件:
[root@k8s-master ~]# vim /usr/lib/systemd/system/kube-scheduler.service
[Unit]
Description=Kubernetes Scheduler
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=-/opt/kubernetes/cfg/kube-scheduler
ExecStart=/opt/kubernetes/bin/kube-scheduler $KUBE_SCHEDULER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
5.3.3 启动:
[root@k8s-master ~]# systemctl daemon-reload
[root@k8s-master ~]# systemctl start kube-scheduler
[root@k8s-master ~]# systemctl enable kube-scheduler
5.4 部署controller-manager组件
5.4.1 创建controller-manager配置文件:
[root@k8s-master ~]# vim /opt/kubernetes/cfg/kube-controller-manager
KUBE_CONTROLLER_MANAGER_OPTS="--logtostderr=true \
--v=4 \
--master=127.0.0.1:8080 \
--leader-elect=true \
--address=127.0.0.1 \
--service-cluster-ip-range=10.0.0.0/24 \
--cluster-name=kubernetes \
--cluster-signing-cert-file=/opt/kubernetes/ssl/ca.pem \
--cluster-signing-key-file=/opt/kubernetes/ssl/ca-key.pem \
--root-ca-file=/opt/kubernetes/ssl/ca.pem \
--service-account-private-key-file=/opt/kubernetes/ssl/ca-key.pem"
5.4.2 systemd管理controller-manager组件:
[root@k8s-master ~]# vim /usr/lib/systemd/system/kube-controller-manager.service
[Unit]
Description=Kubernetes Controller Manager
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=-/opt/kubernetes/cfg/kube-controller-manager
ExecStart=/opt/kubernetes/bin/kube-controller-manager $KUBE_CONTROLLER_MANAGER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
5.4.3 启动:
[root@k8s-master ~]# systemctl daemon-reload
[root@k8s-master ~]# systemctl start kube-controller-manager
[root@k8s-master ~]# systemctl enable kube-controller-manager
所有组件都已经启动成功,通过kubectl工具查看当前集群组件状态:
[root@k8s-master ~]# /opt/kubernetes/bin/kubectl get cs
NAME STATUS MESSAGE ERROR
scheduler Healthy ok
controller-manager Healthy ok
etcd-2 Healthy {"health": "true"}
etcd-1 Healthy {"health": "true"}
etcd-0 Healthy {"health": "true"}
如上输出说明组件都正常。
六、在Node节点部署组件
Master apiserver启用TLS认证后,Node节点kubelet组件想要加入集群,必须使用CA签发的有效证书才能与apiserver通信,当Node节点很多时,签署证书是一件很繁琐的事情,因此有了TLS Bootstrapping机制,kubelet会以一个低权限用户自动向apiserver申请证书,kubelet的证书由apiserver动态签署。
6.1 下面这些操作在master节点完成:
6.1.1 将kubelet-bootstrap用户绑定到系统集群角色
[root@k8s-master ~]# /opt/kubernetes/bin/kubectl create clusterrolebinding kubelet-bootstrap \
> --clusterrole=system:node-bootstrapper \
> --user=kubelet-bootstrap
6.1.2 创建kubeconfig认证文件:
[root@k8s-master ~]# cd /opt/ssl/kubernetes/
在生成kubernetes证书的目录下执行以下命令生成kubeconfig文件:
#指定apiserver 内网负载均衡地址
[root@k8s-master kubernetes]# KUBE_APISERVER="https://192.168.0.150:6443"
[root@k8s-master kubernetes]# BOOTSTRAP_TOKEN=674c457d4dcf2eefe4920d7dbb6b0ddc //跟前面创建的token文件里的字符串一样
# 设置集群参数
[root@k8s-master kubernetes]# /opt/kubernetes/bin/kubectl config set-cluster kubernetes \
> --certificate-authority=./ca.pem \
> --embed-certs=true \
> --server=${KUBE_APISERVER} \
> --kubeconfig=bootstrap.kubeconfig
# 设置客户端认证参数
[root@k8s-master kubernetes]# /opt/kubernetes/bin/kubectl config set-credentials kubelet-bootstrap \
> --token=${BOOTSTRAP_TOKEN} \
> --kubeconfig=bootstrap.kubeconfig
# 设置上下文参数
[root@k8s-master kubernetes]# /opt/kubernetes/bin/kubectl config set-context default \
> --cluster=kubernetes \
> --user=kubelet-bootstrap \
> --kubeconfig=bootstrap.kubeconfig
# 设置默认上下文
[root@k8s-master kubernetes]# /opt/kubernetes/bin/kubectl config use-context default --kubeconfig=bootstrap.kubeconfig
6.1.3 创建kube-proxy kubeconfig认证文件
[root@k8s-master kubernetes]# /opt/kubernetes/bin/kubectl config set-cluster kubernetes \
> --certificate-authority=./ca.pem \
> --embed-certs=true \
> --server=${KUBE_APISERVER} \
> --kubeconfig=kube-proxy.kubeconfig
Cluster "kubernetes" set.
[root@k8s-master kubernetes]# /opt/kubernetes/bin/kubectl config set-credentials kube-proxy \
> --client-certificate=./kube-proxy.pem \
> --client-key=./kube-proxy-key.pem \
> --embed-certs=true \
> --kubeconfig=kube-proxy.kubeconfig
User "kube-proxy" set.
[root@k8s-master kubernetes]# /opt/kubernetes/bin/kubectl config set-context default \
> --cluster=kubernetes \
> --user=kube-proxy \
> --kubeconfig=kube-proxy.kubeconfig
Context "default" created.
[root@k8s-master kubernetes]# /opt/kubernetes/bin/kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig
Switched to context "default".
6.1.4 将这两个文件拷贝到Node节点/opt/kubernetes/cfg目录下
[root@k8s-master kubernetes]# scp bootstrap.kubeconfig kube-proxy.kubeconfig k8s-node1:/opt/kubernetes/cfg/
[root@k8s-master kubernetes]# scp bootstrap.kubeconfig kube-proxy.kubeconfig k8s-node1:2opt/kubernetes/cfg/
6.1.5将前面下载的二进制包中的kubelet和kube-proxy拷贝到node节点/opt/kubernetes/bin目录下。
[root@k8s-master ~]# cd kubernetes/server/bin
[root@k8s-master bin]# scp kubelet kube-proxy k8s-node1:/opt/kubernetes/bin
[root@k8s-master bin]# scp kubelet kube-proxy k8s-node2:/opt/kubernetes/bin
------------------下面这些操作没有特殊说明都在node节点完成------------------------
6.2 部署kubelet组件
node节点部署步骤一样
6.2.1 创建kubelet配置文件:
[root@k8s-node1 ~]# vim /opt/kubernetes/cfg/kubelet
KUBELET_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.0.151 \ ----需要修改
--kubeconfig=/opt/kubernetes/cfg/kubelet.kubeconfig \
--bootstrap-kubeconfig=/opt/kubernetes/cfg/bootstrap.kubeconfig \
--config=/opt/kubernetes/cfg/kubelet.config \
--cert-dir=/opt/kubernetes/ssl \
--pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0"
参数说明:
* --hostname-override 在集群中显示的主机名
* --kubeconfig 指定kubeconfig文件位置,会自动生成
* --bootstrap-kubeconfig 指定刚才生成的bootstrap.kubeconfig文件
* --cert-dir 颁发证书存放位置
* --pod-infra-container-image 管理Pod网络的镜像
6.2.2 创建kubelet.config配置文件
[root@k8s-node1 ~]# vim /opt/kubernetes/cfg/kubelet.config
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
address: 192.168.0.151
port: 10250
readOnlyPort: 10255
cgroupDriver: cgroupfs
clusterDNS: ["10.0.0.2"]
clusterDomain: cluster.local.
failSwapOn: false
authentication:
anonymous:
enabled: true
webhook:
enabled: false
注释: clusterDns跟5.1.2证书里配置的是相对应的
6.2.3 systemd管理kubelet组件:
[root@k8s-node1 ~]# vim /usr/lib/systemd/system/kubelet.service
[Unit]
Description=Kubernetes Kubelet
After=docker.service
Requires=docker.service
[Service]
EnvironmentFile=/opt/kubernetes/cfg/kubelet
ExecStart=/opt/kubernetes/bin/kubelet $KUBELET_OPTS
Restart=on-failure
KillMode=process
[Install]
WantedBy=multi-user.target
6.2.4 启动:
[root@k8s-node1 ~]# systemctl daemon-reload
[root@k8s-node1 ~]# systemctl start kubelet
[root@k8s-node1 ~]# systemctl enable kubelet
6.3 在Master审批Node加入集群:
启动后还没加入到集群中,需要手动允许该节点才可以。
在Master节点查看请求签名的Node:
[root@k8s-master ~]# /opt/kubernetes/bin/kubectl get csr
NAME AGE REQUESTOR CONDITION
node-csr-KZRJjCyvZUEUDxZL7kjSEo42EPPUDsE3B2cHEvqHxbA 1m kubelet-bootstrap Pending
node-csr-tKJ5lJxw83oySeo38fK4MgPbdDerma8YFlbqoGYLnpA 1m kubelet-bootstrap Pending
允许加入:
[root@k8s-master ~]# /opt/kubernetes/bin/kubectl certificate approve node-csr-KZRJjCyvZUEUDxZL7kjSEo42EPPUDsE3B2cHEvqHxbA ---这里是上面查询到的NAME
certificatesigningrequest.certificates.k8s.io/node-csr-KZRJjCyvZUEUDxZL7kjSEo42EPPUDsE3B2cHEvqHxbA approved
[root@k8s-master ~]# /opt/kubernetes/bin/kubectl certificate approve node-csr-tKJ5lJxw83oySeo38fK4MgPbdDerma8YFlbqoGYLnpA
certificatesigningrequest.certificates.k8s.io/node-csr-tKJ5lJxw83oySeo38fK4MgPbdDerma8YFlbqoGYLnpA approved
查看是否成功加入集群
[root@k8s-master ~]# /opt/kubernetes/bin/kubectl get node
NAME STATUS ROLES AGE VERSION
192.168.0.151 Ready <none> 2m v1.11.0
192.168.0.152 Ready <none> 3m v1.11.0
6.4 部署kube-proxy组件
6.4.1 创建kube-proxy配置文件:
[root@k8s-node1 ~]# vim /opt/kubernetes/cfg/kube-proxy
KUBE_PROXY_OPTS="--logtostderr=true \
--v=4 \
--hostname-override=192.168.0.151 \
--cluster-cidr=10.0.0.0/24 \ ---------和5.1.2对应
--kubeconfig=/opt/kubernetes/cfg/kube-proxy.kubeconfig"
6.4.2 systemd管理kube-proxy组件:
[root@k8s-node1 ~]# vim /usr/lib/systemd/system/kube-proxy.service
[Unit]
Description=Kubernetes Proxy
After=network.target
[Service]
EnvironmentFile=-/opt/kubernetes/cfg/kube-proxy
ExecStart=/opt/kubernetes/bin/kube-proxy $KUBE_PROXY_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
6.4.3 启动
[root@k8s-node1 ~]# systemctl daemon-reload
[root@k8s-node1 ~]# systemctl enable kube-proxy
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-proxy.service to /usr/lib/systemd/system/kube-proxy.service.
[root@k8s-node1 ~]# systemctl start kube-proxy
七、查看集群状态
[root@k8s-master ~]# /opt/kubernetes/bin/kubectl get node
NAME STATUS ROLES AGE VERSION
192.168.0.151 Ready <none> 13m v1.11.0
192.168.0.152 Ready <none> 14m v1.11.0
[root@k8s-master ~]# /opt/kubernetes/bin/kubectl get cs
NAME STATUS MESSAGE ERROR
controller-manager Healthy ok
scheduler Healthy ok
etcd-1 Healthy {"health": "true"}
etcd-2 Healthy {"health": "true"}
etcd-0 Healthy {"health": "true"}
八、运行一个测试示例
创建一个Nginx Web,判断集群是否正常工作:
[root@k8s-master ~]# /opt/kubernetes/bin/kubectl run nginx --image=nginx --replicas=3
deployment.apps/nginx created
[root@k8s-master ~]# /opt/kubernetes/bin/kubectl expose deployment nginx --port=88 --target-port=80 --type=NodePort ----端口映射
service/nginx exposed
查看Pod,Service:
[root@k8s-master ~]# /opt/kubernetes/bin/kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-64f497f8fd-6hq6w 1/1 Running 0 2m
nginx-64f497f8fd-c4mfd 1/1 Running 0 2m
nginx-64f497f8fd-pd9fg 1/1 Running 0 2m
查看pod详细信息:
[root@k8s-master ~]# /opt/kubernetes/bin/kubectl describe pod nginx-64f497f8fd-6hq6w
查看端口
[root@k8s-master ~]# /opt/kubernetes/bin/kubectl get svc
浏览器访问任何一个node节点的此端口
集群部署成功
九、部署Dashboard(Web UI)
部署UI有三个文件:
* dashboard-deployment.yaml // 部署Pod,提供Web服务
* dashboard-rbac.yaml // 授权访问apiserver获取信息
* dashboard-service.yaml // 发布服务,提供对外访问
9.1 创建目录
[root@k8s-master ~]# mkdir /opt/yaml/dashboard -p
[root@k8s-master ~]# cd /opt/yaml/dashboard/
9.2 创建配置文件
[root@k8s-master dashboard]# vim dashboard-deployment.yaml
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: kubernetes-dashboard
namespace: kube-system
labels:
k8s-app: kubernetes-dashboard
kubernetes.io/cluster-service: "true"
addonmanager.kubernetes.io/mode: Reconcile
spec:
selector:
matchLabels:
k8s-app: kubernetes-dashboard
template:
metadata:
labels:
k8s-app: kubernetes-dashboard
annotations:
scheduler.alpha.kubernetes.io/critical-pod: ''
spec:
serviceAccountName: kubernetes-dashboard
containers:
- name: kubernetes-dashboard
image: registry.cn-hangzhou.aliyuncs.com/kube_containers/kubernetes-dashboard-amd64:v1.8.1
resources:
limits:
cpu: 100m
memory: 300Mi
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 9090
protocol: TCP
livenessProbe:
httpGet:
scheme: HTTP
path: /
port: 9090
initialDelaySeconds: 30
timeoutSeconds: 30
tolerations:
- key: "CriticalAddonsOnly"
operator: "Exists"
[root@k8s-master dashboard]# vim dashboard-rbac.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
k8s-app: kubernetes-dashboard
addonmanager.kubernetes.io/mode: Reconcile
name: kubernetes-dashboard
namespace: kube-system
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: kubernetes-dashboard-minimal
namespace: kube-system
labels:
k8s-app: kubernetes-dashboard
addonmanager.kubernetes.io/mode: Reconcile
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: kubernetes-dashboard
namespace: kube-system
[root@k8s-master dashboard]# vim dashboard-service.yaml
apiVersion: v1
kind: Service
metadata:
name: kubernetes-dashboard
namespace: kube-system
labels:
k8s-app: kubernetes-dashboard
kubernetes.io/cluster-service: "true"
addonmanager.kubernetes.io/mode: Reconcile
spec:
type: NodePort
selector:
k8s-app: kubernetes-dashboard
ports:
- port: 80
targetPort: 9090
9.3 创建:
[root@k8s-master dashboard]# /opt/kubernetes/bin/kubectl create -f dashboard-rbac.yaml
serviceaccount/kubernetes-dashboard created
clusterrolebinding.rbac.authorization.k8s.io/kubernetes-dashboard-minimal created
[root@k8s-master dashboard]# /opt/kubernetes/bin/kubectl create -f dashboard-deployment.yaml
deployment.apps/kubernetes-dashboard created
[root@k8s-master dashboard]# /opt/kubernetes/bin/kubectl create -f dashboard-service.yaml
service/kubernetes-dashboard created
注:要按顺序创建,不然会报错
9.4 等待一会儿,查看资源状态:
[root@k8s-master dashboard]# /opt/kubernetes/bin/kubectl get all -n kube-system
或者:
访问任何一个node节点IP加上此端口