Kubernetes快速部署

Kubernetes快速部署

1. 安装要求

在开始之前,部署Kubernetes集群机器需要满足以下几个条件:

  • 至少3台机器,操作系统 CentOS7+
  • 硬件配置:2GB或更多RAM,2个CPU或更多CPU,硬盘20GB或更多
  • 集群中所有机器之间网络互通
  • 可以访问外网,需要拉取镜像
  • 禁止swap分区

2.目标

  1. 在所有节点上安装Docker和kubeadm
  2. 部署Kubernetes Master
  3. 部署容器网络插件
  4. 部署 Kubernetes Node,将节点加入Kubernetes集群中

3. 准备环境

在这里插入图片描述

角色IP内存核心
master192.168.72.1422G2核
node1192.168.72.1452G2核
node2192.168.72.1432G2核
//关闭防火墙和selinux,三台都要
[root@k8s-node1 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@k8s-node1 ~]# vim /etc/selinux/config 
[root@k8s-node1 ~]# setenforce 0

//关闭swap:
[root@k8s-master ~]# vim /etc/fstab

//在master添加hosts:
[root@k8s-master ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.72.142 k8s-master master.example.com
192.168.72.145 k8s-node1 node1.example.com
192.168.72.143 k8s-node2 node2.example.com

//将桥接的IPv4流量传递到iptables的链
[root@k8s-master ~]# vim /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
[root@k8s-master ~]# sysctl --system
* Applying /usr/lib/sysctl.d/10-default-yama-scope.conf ...
kernel.yama.ptrace_scope = 0
* Applying /usr/lib/sysctl.d/50-coredump.conf ...
kernel.core_pattern = |/usr/lib/systemd/systemd-coredump %P %u %g %s %t %c %h %e
* Applying /usr/lib/sysctl.d/50-default.conf ...
kernel.sysrq = 16
kernel.core_uses_pid = 1
kernel.kptr_restrict = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.all.promote_secondaries = 1
net.core.default_qdisc = fq_codel
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
* Applying /usr/lib/sysctl.d/50-libkcapi-optmem_max.conf ...
net.core.optmem_max = 81920
* Applying /usr/lib/sysctl.d/50-pid-max.conf ...
kernel.pid_max = 4194304
* Applying /etc/sysctl.d/99-sysctl.conf ...
* Applying /etc/sysctl.d/k8s.conf ...
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
* Applying /etc/sysctl.conf ...

//时间同步:
[root@k8s-master ~]# vim /etc/chrony.conf 
pool time1.aliyun.com iburst
[root@k8s-master ~]# systemctl enable --now chronyd
[root@k8s-master ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:2k1xHl2Fxl8cS1uRa724g0PiIGDafsvvnfBr+8fAtUw root@k8s-master
The key's randomart image is:
+---[RSA 3072]----+
|             . *O|
|             .=o*|
|          . o..++|
|   o       + E oo|
|  + .   S o = + .|
| . . . + + + + . |
|  .   o.+ + + .  |
|   ...  +o.o =   |
|    .o+o.*+.o .  |
+----[SHA256]-----+
[root@k8s-master ~]# ssh-copy-id k8s-master
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'k8s-master (192.168.72.142)' can't be established.
ECDSA key fingerprint is SHA256:N8aQLTAW/2NWjGCAckEdAtjRKXZnqw88p7h082wGzx0.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@k8s-master's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'k8s-master'"
and check to make sure that only the key(s) you wanted were added.

[root@k8s-master ~]# ssh-copy-id k8s-node1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'k8s-node1 (192.168.72.145)' can't be established.
ECDSA key fingerprint is SHA256:kR2dEg/hbyhEztgjV4BRzp4wZSZq4FX1HmPX8zjjiMw.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@k8s-node1's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'k8s-node1'"
and check to make sure that only the key(s) you wanted were added.

[root@k8s-master ~]# ssh-copy-id k8s-node2
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'k8s-node2 (192.168.72.143)' can't be established.
ECDSA key fingerprint is SHA256:22zLu9SYwRwHT+UWeYdFN2P2qT96UvfjZIJ5Gb0k/uw.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@k8s-node2's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'k8s-node2'"
and check to make sure that only the key(s) you wanted were added.


4. 所有节点安装Docker/kubeadm/kubelet

Kubernetes默认CRI(容器运行时)为Docker,因此先安装Docker

4.1 安装Docker

[root@k8s-master ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2021-12-17 21:31:35 EST; 35min ago
     Docs: https://docs.docker.com
 Main PID: 130407 (dockerd)
    Tasks: 8
   Memory: 35.9M
   CGroup: /system.slice/docker.service
           └─130407 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

12月 17 21:31:34 k8s-master dockerd[130407]: time="2021-12-17T21:31:34.822742109-05:00" level=info msg="Firewalld: docker>
12月 17 21:31:35 k8s-master dockerd[130407]: time="2021-12-17T21:31:35.360716336-05:00" level=info msg="Firewalld: interf>
12月 17 21:31:35 k8s-master dockerd[130407]: time="2021-12-17T21:31:35.383984309-05:00" level=info msg="Firewalld: interf>
12月 17 21:31:35 k8s-master dockerd[130407]: time="2021-12-17T21:31:35.562336936-05:00" level=info msg="Default bridge (d>
12月 17 21:31:35 k8s-master dockerd[130407]: time="2021-12-17T21:31:35.643400453-05:00" level=info msg="Firewalld: interf>
12月 17 21:31:35 k8s-master dockerd[130407]: time="2021-12-17T21:31:35.732995430-05:00" level=info msg="Loading container>
12月 17 21:31:35 k8s-master dockerd[130407]: time="2021-12-17T21:31:35.751043471-05:00" level=info msg="Docker daemon" co>
12月 17 21:31:35 k8s-master dockerd[130407]: time="2021-12-17T21:31:35.751107234-05:00" level=info msg="Daemon has comple>
[root@k8s-node1 ~]# cat /etc/docker/daemon.json 
{
    "registry-mirrors": ["https://uemehekj.mirror.aliyuncs.com"],
    "exec-opts": ["native.cgroupdriver=systemd"],
    "log-driver": "json-file",
    "log-opts": {
      "max-size": "100m"
    },
    "storage-driver": "overlay2"
}

[root@k8s-master ~]# docker --version
Docker version 20.10.12, build e91ed57

4.2 添加kubernetes阿里云YUM软件源

[root@k8s-master ~]# cat /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

4.3 安装kubeadm,kubelet和kubectl

[root@k8s-node1 ~]# yum install -y kubelet-1.20.0 kubeadm-1.20.0 kubectl-1.20.0
[root@k8s-node1 ~]# systemctl enable kubelet

5. 部署Kubernetes Master

在192.168.72.142(Master)执行。

[root@k8s-master ~]# kubeadm init --apiserver-advertise-address=192.168.72.142 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.20.0 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16
....................

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

kubeadm join 192.168.72.142:6443 --token imfevb.20zvyayzgc5m2uc2 \
    --discovery-token-ca-cert-hash sha256:70cec970659ba19a76ecd5079f7335c82cae447316560b0588344766802aa93d 

由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里指定阿里云镜像仓库地址。

[root@k8s-master ~]# echo 'export KUBECONFIG=/etc/kubernetes/admin.conf' > /etc/profile.d/k8s.sh
[root@k8s-master ~]# sourch /etc/profile.d/k8s.sh

6. 安装Pod网络插件(CNI)

[root@k8s-master ~]# kubectl apply -f https://github.com/flannel-io/flannel/blob/master/Documentation/kube-flannel.yml
podsecuritypolicy.policy/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.apps/kube-flannel-ds created

7. 加入Kubernetes Node

在192.168.72.145、192.168.72.143上(Node)执行。

向集群添加新节点,执行在kubeadm init输出的kubeadm join命令:

kubeadm join 192.168.72.142:6443 --token imfevb.20zvyayzgc5m2uc2 \
    --discovery-token-ca-cert-hash sha256:70cec970659ba19a76ecd5079f7335c82cae447316560b0588344766802aa93d
[root@k8s-node1 ~]# kubeadm join 192.168.72.142:6443 --token imfevb.20zvyayzgc5m2uc2 \
>     --discovery-token-ca-cert-hash sha256:70cec970659ba19a76ecd5079f7335c82cae447316560b0588344766802aa93d
[preflight] Running pre-flight checks
        [WARNING FileExisting-tc]: tc not found in system path
        [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 20.10.12. Latest validated version: 19.03
        [WARNING Hostname]: hostname "k8s-node1" could not be reached
        [WARNING Hostname]: hostname "k8s-node1": lookup k8s-node1 on 192.168.72.2:53: no such host
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[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] Starting the kubelet
[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    control-plane,master   3h8m   v1.20.0
k8s-node1    Ready    <none>                 173m   v1.20.0
k8s-node2    Ready    <none>                 173m   v1.20.0

8. 测试kubernetes集群

在Kubernetes集群中创建一个pod,验证是否正常运行:

[root@k8s-master ~]# kubectl create deployment nginx 
--image=nginx
deployment.apps/nginx created
[root@k8s-master ~]# kubectl expose deployment nginx 
--port=80 --type=NodePort
service/nginx exposed
[root@k8s-master ~]# kubectl get pod,svc
NAME                         READY   STATUS              RESTARTS   AGE
pod/nginx-6799fc88d8-hlcrv   0/1     ContainerCreating   0          23s

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        3h9m
service/nginx        NodePort    10.103.18.141   <none>        80:32722/TCP   13s

[root@k8s-master ~]# curl 10.103.18.141:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值