第五章

一、master节点生成密钥,发送至所有node节点

[root@localhost ~]# ssh-keygen -C "k8s-master" -t rsa -N "" -f ~/.ssh/id_rsa 
[root@localhost ~]# ssh-copy-id root@192.168.183.155
[root@localhost ~]# ssh-copy-id root@192.168.183.156
[root@localhost ~]# ssh-copy-id root@192.168.183.157
[root@localhost ~]# ssh-copy-id root@192.168.183.158

二、修改主机名

[root@localhost ~]# hostnamectl set-hostname k8s-master
[root@localhost ~]# ssh 192.168.183.156 'hostnamectl set-hostname k8s-node1'
[root@localhost ~]# ssh 192.168.183.157 'hostnamectl set-hostname k8s-node2'
[root@localhost ~]# ssh 192.168.183.158 'hostnamectl set-hostname k8s-node3'

三、主机名互相解析

[root@k8s-master ~]# cat >> /etc/hosts <<EOF
> 192.168.183.155 k8s-master
> 192.168.183.156 k8s-node1
> 192.168.183.157 k8s-node2
> 192.168.183.158 k8s-node3
> EOF
[root@k8s-master ~]# for i in k8s-node{1..3};do scp /etc/hosts $i:/etc/hosts;done
hosts                                                                                               100%  263   150.3KB/s   00:00    
hosts                                                                                               100%  263   295.5KB/s   00:00    
hosts                                                                                               100%  263   234.7KB/s   00:00    

四、关闭firewalld,selinux,清空防火墙

[root@k8s-master ~]# systemctl stop firewalld && systemctl disable firewalld
[root@k8s-master ~]# for i in k8s-node{1..3};do ssh $i 'systemctl stop firewalld && systemctl disable firewalld';done
[root@k8s-master ~]# setenforce 0
[root@k8s-master ~]# for i in k8s-node{1..3};do ssh $i 'setenforce 0';done
[root@k8s-master ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
[root@k8s-master ~]# for i in k8s-node{1..3};do ssh $i "sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config";done
再次确认下selinux状态
[root@k8s-master ~]# getenforce 
Disabled
[root@k8s-master ~]# for i in k8s-node{1..3};do ssh $i "getenforce";done
Disabled
Disabled
Disabled

五、打开核心路由转发,不对bridge桥的数据处理

[root@k8s-master ~]# modprobe br_netfilter
[root@k8s-master ~]# for i in k8s-node{1..3};do ssh $i "modprobe br_netfilter";done
[root@k8s-master ~]# cat >> /etc/sysctl.d/k8s.conf <<EOF
> net.ipv4.ip_forward = 1
> net.bridge.bridge-nf-call-ip6tables = 1
> net.bridge.bridge-nf-call-iptables = 1
> EOF
[root@k8s-master ~]# sysctl -p /etc/sysctl.d/k8s.conf
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
[root@k8s-master ~]# for i in k8s-node{1..3};do scp /etc/sysctl.d/k8s.conf $i:/etc/sysctl.d/;done
k8s.conf                                                                                            100%  103   115.3KB/s   00:00    
k8s.conf                                                                                            100%  103    80.2KB/s   00:00    
k8s.conf                                                                                            100%  103    65.8KB/s   00:00    
[root@k8s-master ~]# for i in k8s-node{1..3};do ssh $i "sysctl -p /etc/sysctl.d/k8s.conf";done
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1

docker安装

一、安装docker源(阿里云源)

[root@k8s-master ~]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
--2019-05-07 14:57:27--  https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
正在解析主机 mirrors.aliyun.com (mirrors.aliyun.com)... 183.214.130.230, 183.214.130.229, 183.214.130.227, ...
正在连接 mirrors.aliyun.com (mirrors.aliyun.com)|183.214.130.230|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:2640 (2.6K) [application/octet-stream]
正在保存至: “/etc/yum.repos.d/docker-ce.repo”

100%[============================================================================================>] 2,640       --.-K/s 用时 0.001s  

2019-05-07 14:57:27 (3.04 MB/s) - 已保存 “/etc/yum.repos.d/docker-ce.repo” [2640/2640])

[root@k8s-master ~]# for i in k8s-node{1..3};do scp /etc/yum.repos.d/docker-ce.repo $i:/etc/yum.repos.d/;done
docker-ce.repo                                                                                      100% 2640     2.1MB/s   00:00    
docker-ce.repo                                                                                      100% 2640     2.0MB/s   00:00    
docker-ce.repo                                                                                      100% 2640     2.0MB/s   00:00    

二、安装docker-ce

[root@k8s-master ~]# yum install docker-ce -y
[root@k8s-master ~]# for i in k8s-node{1..3};do ssh $i "yum install docker-ce -y";done

三、docker镜像加速

[root@k8s-master ~]# mkdir -p /etc/docker
[root@k8s-master ~]# cat <<EOF > /etc/docker/daemon.json
> {
>   "registry-mirrors": ["https://7f28zkr3.mirror.aliyuncs.com"]
> }
> EOF
[root@k8s-master ~]# systemctl daemon-reload
[root@k8s-master ~]# systemctl start docker
[root@k8s-master ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@k8s-master ~]# for i in k8s-node{1..3};do ssh $i 'mkdir -p /etc/docker';done
[root@k8s-master ~]# for i in k8s-node{1..3};do scp /etc/docker/daemon.json $i:/etc/docker/;done
daemon.json                                                                                         100%   67    70.2KB/s   00:00    
daemon.json                                                                                         100%   67    63.4KB/s   00:00    
daemon.json                                                                                         100%   67    48.9KB/s   00:00    
[root@k8s-master ~]# for i in k8s-node{1..3};do ssh $i 'systemctl daemon-reload';done
[root@k8s-master ~]# for i in k8s-node{1..3};do ssh $i 'systemctl start docker';done
[root@k8s-master ~]# for i in k8s-node{1..3};do ssh $i 'systemctl enable docker';done
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

kubernetes组件安装

一、配置kubernetes源

[root@k8s-master ~]# cat <<EOF > /etc/yum.repos.d/kubernetes.repo
> [kubernetes]
> name=Kubernetes
> baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
> enabled=1
> gpgcheck=1
> repo_gpgcheck=1
> gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg \
> https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
> EOF
[root@k8s-master ~]# for i in k8s-node{1..3};do scp /etc/yum.repos.d/kubernetes.repo $i:/etc/yum.repos.d/;done
kubernetes.repo                                                                                     100%  276   190.4KB/s   00:00    
kubernetes.repo                                                                                     100%  276   195.3KB/s   00:00    
kubernetes.repo                                                                                     100%  276   218.1KB/s   00:00    

二、安装kubeadm kubectl kubelet

[root@k8s-master ~]# yum install -y kubelet kubeadm kubectl
[root@k8s-master ~]# for i in k8s-node{1..3};do ssh $i 'yum install -y kubelet kubeadm kubectl';done

三、将kubelet加入开机启动

[root@k8s-master ~]# systemctl enable kubelet
Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.
[root@k8s-master ~]# for i in k8s-node{1..3};do ssh $i 'systemctl enable kubelet';done
Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.

master节点初始化

一、拖取镜像的脚本如下:

[root@k8s-master ~]# cat pull-1.14.1.sh 
registry="k8s.gcr.io"
path="registry.cn-shanghai.aliyuncs.com/kdc/"
kube_tag="v1.14.1"
images="kube-proxy:${kube_tag} kube-apiserver:${kube_tag}  
  kube-controller-manager:${kube_tag} kube-scheduler:${kube_tag}  
  coredns:1.3.1 etcd:3.3.10 pause:3.1 flannel:v0.11.0-amd64"

for i in ${images}
do
    if [ ${i} = 'flannel:v0.11.0-amd64' ];then
      registry="quay.io/coreos"
    fi
    docker pull ${path}$i
    docker tag ${path}$i ${registry}/${i}
    docker image rm ${path}$i
    registry="k8s.gcr.io"
done

在master和node节点分别执行脚本

[root@k8s-master ~]# bash pull-1.14.1.sh
[root@k8s-master ~]# for i in k8s-node{1..3};do scp pull-1.14.1.sh $i:/root/;done
pull-1.14.1.sh                                                                                      100%  513   238.8KB/s   00:00    
pull-1.14.1.sh                                                                                      100%  513   354.4KB/s   00:00    
pull-1.14.1.sh                                                                                      100%  513   387.9KB/s   00:00   
[root@k8s-master ~]# for i in k8s-node{1..3};do ssh $i 'bash pull-1.14.1.sh';done 

二、配置kubelet

[root@k8s-master ~]# vim /etc/sysconfig/kubelet
[root@k8s-master ~]# cat /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS="--fail-swap-on=false"

三、初始化master

[root@k8s-master ~]# kubeadm init \
> --kubernetes-version=v1.14.1 \
> --pod-network-cidr=10.244.0.0/16 \
> --service-cidr=10.96.0.0/12 \
> --apiserver-advertise-address=192.168.183.155 \
> --ignore-preflight-errors=Swap
[init] Using Kubernetes version: v1.14.1
[preflight] Running pre-flight checks
	[WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
	[WARNING Swap]: running with swap on is not supported. Please disable swap
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Activating the kubelet service
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.183.155 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.183.155 127.0.0.1 ::1]
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [k8s-master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.183.155]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 24.029034 seconds
[upload-config] storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.14" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --experimental-upload-certs
[mark-control-plane] Marking the node k8s-master as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node k8s-master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: vr9l5g.t4ssmzicu5udr2i3
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] creating the "cluster-info" ConfigMap in the "kube-public" namespace
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.183.155:6443 --token vr9l5g.t4ssmzicu5udr2i3 \
    --discovery-token-ca-cert-hash sha256:aa1571ef86e5d000b0272c35c64c6f70552edcbc3f7d3730baf3e2ca6418e319 
[root@k8s-master ~]# kubeadm init \
> --kubernetes-version=v1.14.1 \
> --pod-network-cidr=10.244.0.0/16 \
> --service-cidr=10.96.0.0/12 \
> --apiserver-advertise-address=192.168.183.155 \
> --ignore-preflight-errors=Swap
[init] Using Kubernetes version: v1.14.1
[preflight] Running pre-flight checks
...

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.183.155:6443 --token vr9l5g.t4ssmzicu5udr2i3 \
    --discovery-token-ca-cert-hash sha256:aa1571ef86e5d000b0272c35c64c6f70552edcbc3f7d3730baf3e2ca6418e319 

四、设定kubectl的配置文件

[root@k8s-master ~]# mkdir -p $HOME/.kube
[root@k8s-master ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
五、部署flannel网络插件
[root@k8s-master ~]# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
podsecuritypolicy.extensions/psp.flannel.unprivileged created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.extensions/kube-flannel-ds-amd64 created
daemonset.extensions/kube-flannel-ds-arm64 created
daemonset.extensions/kube-flannel-ds-arm created
daemonset.extensions/kube-flannel-ds-ppc64le created
daemonset.extensions/kube-flannel-ds-s390x created

将Node节点加入到集群中来

一、配置kubelet,忽略swap

[root@k8s-master ~]#  for i in k8s-node{1..3};do scp /etc/sysconfig/kubelet $i:/etc/sysconfig/;done
kubelet                                                                                             100%   42    13.5KB/s   00:00    
kubelet                                                                                             100%   42    20.0KB/s   00:00    
kubelet                                                                                             100%   42    20.6KB/s   00:00    

二、将node节点加入到集群中来

[root@k8s-master ~]#  for i in k8s-node{1..3};do ssh $i 'kubeadm join 192.168.183.155:6443 --token vr9l5g.t4ssmzicu5udr2i3 --discovery-token-ca-cert-hash sha256:aa1571ef86e5d000b0272c35c64c6f70552edcbc3f7d3730baf3e2ca6418e319 --ignore-preflight-errors=Swap';done
[preflight] Running pre-flight checks
	[WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
	[WARNING Swap]: running with swap on is not supported. Please disable swap
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.14" ConfigMap in the kube-system namespace
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Activating the kubelet service
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

[preflight] Running pre-flight checks
	[WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
	[WARNING Swap]: running with swap on is not supported. Please disable swap
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.14" ConfigMap in the kube-system namespace
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Activating the kubelet service
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

[preflight] Running pre-flight checks
	[WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
	[WARNING Swap]: running with swap on is not supported. Please disable swap
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.14" ConfigMap in the kube-system namespace
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Activating the kubelet service
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

三、查询节点的状态等

[root@k8s-master ~]# kubectl get nodes
NAME         STATUS   ROLES    AGE   VERSION
k8s-master   Ready    master   91m   v1.14.1
k8s-node1    Ready    <none>   11m   v1.14.1
k8s-node2    Ready    <none>   10m   v1.14.1
k8s-node3    Ready    <none>   10m   v1.14.1

四、获取集群状态信息

[root@k8s-master ~]# kubectl cluster-info
Kubernetes master is running at https://192.168.183.155:6443
KubeDNS is running at https://192.168.183.155:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

查询Server端和Client端的版本等信息

[root@k8s-master ~]# kubectl version --short=true
Client Version: v1.14.1
Server Version: v1.14.1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值