Kubernetes快速部署

安装要求

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

-至少3台机器,操作系统 CentOS7+

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

学习目标

  1. 在所有节点上安装Docker和kubeadm
  2. 部署Kubernetes Master
  3. 部署容器网络插件
  4. 部署 Kubernetes Node,将节点加入Kubernetes集群中
  5. 部署Dashboard Web页面,可视化查看Kubernetes资源

准备环境

系统环境 角色 IP
centos8 master 192.168.200.140
centos8 node1 192.168.200.142
centos7 node2 192.168.200.143

在这里插入图片描述

//更改主机名
[root@localhost ~]# hostnamectl set-hostname master.example.com
[root@localhost ~]# bash
[root@master ~]# hostname
master.example.com

[root@master ~]# hostnamectl set-hostname node1.example.com
[root@master ~]# bash
[root@node1 ~]# hostname
node1.example.com

[root@localhost ~]# hostnamectl set-hostname node2.example.com
[root@localhost ~]# 
[root@localhost ~]# bash
[root@node2 ~]# hostname
node2.example.com


//关闭防火墙
//master主机上操作
[root@master ~]# systemctl disable --now firewalld
[root@master ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config

//node1主机上操作
[root@node1 ~]# systemctl disable --now firewalld
[root@node1 ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config

//node2主机上操作
[root@node2 ~]# systemctl disable --now firewalld
[root@node2 ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config

//master主机上操作
// 关闭swap
注释掉或删掉swap分区(我这里是注释掉的)
[root@master ~]# vim /etc/fstab
#/dev/mapper/cs-swap     none                    swap    defaults        0 0

//node1主机上操作
[root@node1 ~]# vim /etc/fstab 
#/dev/mapper/centos-swap swap                    swap    defaults        0 0

//node2主机上操作
[root@node2 ~]# vim /etc/fstab
#/dev/mapper/cs-swap     none                    swap    defaults        0 0
/在master添加hosts
[root@master ~]# vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.140 master master.example.com
192.168.200.142 node1 node1.example.com
192.168.200.143 node2 node2.example.com
//将桥接的IPv4流量传递到iptables的链
//master主机上操作
[root@master ~]# vi /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
[root@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 ...
* Applying /etc/sysctl.conf ...
//配置时间同步
//所有主机上操作
[root@master ~]# yum -y install chrony
[root@master ~]# vim /etc/chrony.conf 
  3 pool time1.aliyun.com iburst
[root@master ~]# systemctl enable --now chronyd

[root@node1 ~]# yum -y install chrony
[root@node1 ~]# vim /etc/chrony.conf 
  3 pool time1.aliyun.com iburst
[root@node1 ~]# systemctl enable --now chronyd

[root@node2 ~]# yum -y install chrony
[root@node2 ~]# vim /etc/chrony.conf 
  3 server time1.aliyun.com iburst
  4 #server 0.centos.pool.ntp.org iburst
  5 #server 1.centos.pool.ntp.org iburst
  6 #server 2.centos.pool.ntp.org iburst
  7 #server 3.centos.pool.ntp.org iburst
[root@node2 ~]# systemctl enable --now chronyd
//配置免密认证
//master主机上操作
[root@master ~]# ssh-keygen -t rsa
[root@master ~]# ssh-copy-id master
[root@master ~]# ssh-copy-id node1
[root@master ~]# ssh-copy-id node2

//测试
[root@master ~]# for i in master node1 node2;do ssh $i 'date';done
20211218日 星期六 18:31:39 CST
20211218日 星期六 18:31:40 CST
20211218日 星期六 18:31:40 CST

//重启让刚刚的操作生效
[root@master ~]# reboot
[root@node1 ~]# reboot
[root@node2 ~]# reboot

所有节点安装Docker/kubeadm/kubelet

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

安装docker

//所有主机上操作
// 下载docker源
[root@master ~]# cd /etc/yum.repos.d/
[root@master yum.repos.d]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo

[root@node1 ~]# cd /etc/yum.repos.d/
[root@node1 yum.repos.d]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo

[root@node2 ~]# cd /etc/yum.repos.d/
[root@node2 yum.repos.d]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo

//安装docker
[root@master ~]# yum -y install docker-ce

[root@node1 ~]# yum -y install docker-ce

[root@node2 ~]# yum -y install docker-ce

//设置docker开机自启
[root@master ~]# systemctl enable --now docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.

[root@node1 ~]# systemctl enable --now docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.

[root@node2 ~]# systemctl enable --now docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.

//查看docker版本号
[root@master ~]# docker --version
Docker version 20.10.12, build e91ed57

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

[root@node2 ~]# docker --version
Docker version 20.10.12, build e91ed57
//配置加速器
//所有主机上操作
[root@master ~]# cat > /etc/docker/daemon.json << EOF
{
  "registry-mirrors": ["https://7z2g0ixw.mirror.aliyuncs.com"],     ## 配置加速器
  "exec-opts": ["native.cgroupdriver=systemd"],       ## 用systemd的方式来管理cgroup
  "log-driver": "json-file",         ## 日志格式用json
  "log-opts": {
    "max-size": "100m"      ## 最大的大小100M,达到100M就滚动
  },
  "storage-driver": "overlay2"      ## 存储驱动用overlay2
}
EOF

[root@node1 ~]# cat > /etc/docker/daemon.json << EOF
> {
>   "registry-mirrors": ["https://7z2g0ixw.mirror.aliyuncs.com"],
>   "exec-opts": ["native.cgroupdriver=systemd"],
>   "log-driver": "json-file",
>   "log-opts": {
>     "max-size": "100m"
>   },
>   "storage-driver": "overlay2"
> }
> EOF

[root@node2 ~]# cat > /etc/docker/daemon.json << EOF
> {
>   "registry-mirrors": ["https://7z2g0ixw.mirror.aliyuncs.com"],
>   "exec-opts": ["native.cgroupdriver=systemd"],
>   "log-driver": "json-file",
>   "log-opts": {
>     "max-size": "100m"
>   },
>   "storage-driver": "overlay2"
> }
> EOF

添加kubernetes阿里云YUM软件源

//所有主机上操作
[root@master ~]# cat > /etc/yum.repos.d/kubernetes.repo << EOF
> [kubernetes]
> name=Kubernetes
> baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
> enabled=1
> gpgcheck=0
> repo_gpgcheck=0
> gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
> EOF
[root@master ~]# yum clean all
[root@master ~]# yum makecache

[root@node1 ~]# cat > /etc/yum.repos.d/kubernetes.repo << EOF
> [kubernetes]
> name=Kubernetes
> baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
> enabled=1
> gpgcheck=0
> repo_gpgcheck=0
> gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
> EOF
[root@node1 ~]# yum clean all
[root@node1 ~]# yum makecache

[root@node2 ~]# cat > /etc/yum.repos.d/kubernetes.repo << EOF
> [kubernetes]
> name=Kubernetes
> baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
> enabled=1
> gpgcheck=0
> repo_gpgcheck=0
> gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
> EOF
[root@node2 ~]# yum clean all
[root@node2 ~]# yum makecache

安装kubeadm,kubelet和kubectl

由于版本更新频繁,这里指定版本号部署:

[root@master ~]# yum install -y kubelet-1.20.0 kubeadm-1.20.0 kubectl-1.20.0
[root@master ~]# systemctl enable kubelet
Created symlink /etc/systemd/system/multi-user.target.wants/kubelet.service → /usr/lib/systemd/system/kubelet.service.

[root@node1 ~]# yum install -y kubelet-1.20.0 kubeadm-1.20.0 kubectl-1.20.0
[root@node1 ~]# systemctl enable kubelet
Created symlink /etc/systemd/system/multi-user.target.wants/kubelet.service → /usr/lib/systemd/system/kubelet.service.

[root@node2 ~]# yum install -y kubelet-1.20.0 kubeadm-1.20.0 kubectl-1.20.0
[root@node2 ~]# systemctl enable kubelet
Created symlink /etc/systemd/system/multi-user.target.wants/kubelet.service → /usr/lib/systemd/system/kubelet.service.

部署Kubernetes Master

在192.168.200.140(Master)执行。

[root@master ~]# kubeadm init --apiserver-advertise-address=192.168.200.140 --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
[init] Using Kubernetes version: v1.20.0
[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
[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'
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local master.example.com] and IPs [10.96.0.1 192.168.200.140]
[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 "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [localhost master.example.com] and IPs [192.168.200.140 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [localhost master.example.com] and IPs [192.168.200.140 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-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
[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] Starting the kubelet
[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 11.005206 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.20" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node master.example.com as control-plane by adding the labels "node-role.kubernetes.io/master=''" and "node-role.kubernetes.io/control-plane='' (deprecated)"
[mark-control-plane] Marking the node master.example.com as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: 5nzjif.22729e6uph9sfmyt
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes
[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
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[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

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf                ## 如果是管理员就运行下面一条命令

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.200.140:6443 --token 5nzjif.22729e6uph9sfmyt \
    --discovery-token-ca-cert-hash sha256:787d26c0edf3e421d7fc8d24e051a7219125612604f33d2710635f8105fbc20f 


//将上面这段写到一个文件中,方便以后用到
[root@master ~]# vim init
[root@master ~]# cat init 
kubeadm join 192.168.200.140:6443 --token 5nzjif.22729e6uph9sfmyt \
    --discovery-token-ca-cert-hash sha256:787d26c0edf3e421d7fc8d24e051a7219125612604f33d2710635f8105fbc20f

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

使用kubectl工具:

//让环境变量永久生效
//master主机上操作
[root@master ~]# echo 'export KUBECONFIG=/etc/kubernetes/admin.conf' > /etc/profile.d/k8s.sh
[root@master ~]# source /etc/profile.d/k8s.sh 

//查看是否有控制节点
[root@master ~]# kubectl get nodes
NAME                 STATUS   ROLES                  AGE     VERSION
master.example.com   Ready    control-plane,master   15m     v1.20.0

安装Pod网络插件(CNI)

[root@master ~]# vi kube-flannel.yml  //注意在编辑文件时,此处应使用vi(vi 复制粘贴是保持文本的 格式而vim 则会有所改变)
---
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
  name: psp.flannel.unprivileged
  annotations:
    seccomp.security.alpha.kubernetes.io/allowedProfileNames: docker/default
    seccomp.security.alpha.kubernetes.io/defaultProfileName: docker/default
    apparmor.security.beta.kubernetes.io/allowedProfileNames: runtime/default
    apparmor.security.beta.kubernetes.io/defaultProfileName: runtime/default
spec:
  privileged: false
  volumes:
  - configMap
  - secret
  - emptyDir
  - hostPath
  allowedHostPaths:
  - pathPrefix: "/etc/cni/net.d"
  - pathPrefix: "/etc/kube-flannel"
  - pathPrefix: "/run/flannel"
  readOnlyRootFilesystem: false
  # Users and groups
  runAsUser:
    rule: RunAsAny
  supplementalGroups:
    rule: RunAsAny
  fsGroup:
    rule: RunAsAny
  # Privilege Escalation
  allowPrivilegeEscalation: false
  defaultAllowPrivilegeEscalation: false
  # Capabilities
  allowedCapabilities: ['NET_ADMIN', 'NET_RAW']
  defaultAddCapabilities: []
  requiredDropCapabilities: []
  # Host namespaces
  hostPID: false
  hostIPC: false
  hostNetwork: true
  hostPorts:
  - min: 0
    max: 65535
  # SELinux
  seLinux:
    # SELinux is unused in CaaSP
    rule: 'RunAsAny'
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: flannel
rules:
- apiGroups: ['extensions']
  resources: ['podsecuritypolicies']
  verbs: ['use']
  resourceNames: ['psp.flannel.unprivileged']
- apiGroups:
  - ""
  resources:
  - pods
  verbs:
  - get
- apiGroups:
  - ""
  resources:
  - nodes
  verbs:
  - list
  - watch
- apiGroups:
  - ""
  resources:
  - nodes/status
  verbs:
  - patch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: flannel
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: flannel
subjects:
- kind: ServiceAccount
  name: flannel
  namespace: kube-system
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: flannel
  namespace: kube-system
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: kube-flannel-cfg
  namespace: kube-system
  labels:
    tier: node
    app: flannel
data:
  cni-conf.json: |
    {
      "name": "cbr0",
      "cniVersion": "0.3.1",
      "plugins": [
        {
          "type": "flannel",
          "delegate": {
            "hairpinMode": true,
            "isDefaultGateway": true
          }
        },
        {
          "type": "portmap",
          "capabilities": {
            "portMappings": true
          }
        }
      ]
    }
  net-conf.json: |
    {
      "Network": "10.244.0.0/16",
      "Backend": {
        "Type": "vxlan"
      }
    }
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: kube-flannel-ds
  namespace: kube-system
  labels:
    tier: node
    app: flannel
spec:
  selector:
    matchLabels:
      app: flannel
  template:
    metadata:
      labels:
        tier: node
        app: flannel
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/os
                operator: In
                values:
                - linux
      hostNetwork: true
      priorityClassName: system-node-critical
      tolerations:
      - operator: Exists
        effect: NoSchedule
      serviceAccountName: flannel
      initContainers:
      - name: install-cni-plugin
        image: rancher/mirrored-flannelcni-flannel-cni-plugin:v1.0.0
        command:
        - cp
        args:
        - -f
        - /flannel
        - /opt/cni/bin/flannel
        volumeMounts:
        - name: cni-plugin
          mountPath: /opt/cni/bin
      - name: install-cni
        image: quay.io/coreos/flannel:v0.15.1
        command:
        - cp
        args:
        - -f
        - /etc/kube-flannel/cni-conf.json
        - /etc/cni/net.d/10-flannel.conflist
        volumeMounts:
        - name: cni
          mountPath: /etc/cni/net.d
        - name: flannel-cfg
          mountPath: /etc/kube-flannel/
      containers:
      - name: kube-flannel
        image: quay.io/coreos/flannel:v0.15.1
        command:
        - /opt/bin/flanneld
        args:
        - --ip-masq
        - --kube-subnet-mgr
        resources:
          requests:
            cpu: "100m"
            memory: "50Mi"
          limits:
            cpu: "100m"
            memory: "50Mi"
        securityContext:
          privileged: false
          capabilities:
            add: ["NET_ADMIN", "NET_RAW"]
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        volumeMounts:
        - name: run
          mountPath: /run/flannel
        - name: flannel-cfg
          mountPath: /etc/kube-flannel/
      volumes:
      - name: run
        hostPath:
          path: /run/flannel
      - name: cni-plugin
        hostPath:
          path: /opt/cni/bin
      - name: cni
        hostPath:
          path: /etc/cni/net.d
      - name: flannel-cfg
        configMap:
          name: kube-flannel-cfg


[root@master ~]# ls
anaconda-ks.cfg    init  kube-flannel.yml  

[root@master ~]# kubectl apply -f 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

加入Kubernetes Node

在192.168.200.142、192.168.200.143上(Node)执行。

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

[root@node1 ~]# kubeadm join 192.168.200.140:6443 --token 5nzjif.22729e6uph9sfmyt \
>     --discovery-token-ca-cert-hash sha256:787d26c0edf3e421d7fc8d24e051a7219125612604f33d2710635f8105fbc20f
[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 "node1.example.com" could not be reached
        [WARNING Hostname]: hostname "node1.example.com": lookup node1.example.com on 192.168.200.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@node2 ~]# kubeadm join 192.168.200.140:6443 --token 5nzjif.22729e6uph9sfmyt \
>     --discovery-token-ca-cert-hash sha256:787d26c0edf3e421d7fc8d24e051a7219125612604f33d2710635f8105fbc20f
[preflight] Running pre-flight checks
        [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 20.10.12. Latest validated version: 19.03
        [WARNING Hostname]: hostname "node2.example.com" could not be reached
        [WARNING Hostname]: hostname "node2.example.com": lookup node2.example.com on 192.168.200.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.

//查看,下面node2还是notready,是因为它的后台还有东西在运行,等一会就可以了,等它变成ready
[root@master ~]# kubectl get nodes
NAME                 STATUS   ROLES                  AGE     VERSION
master.example.com   Ready    control-plane,master   15m     v1.20.0
node1.example.com    Ready    <none>                 7m53s   v1.20.0
node2.example.com    Ready    <none>                 6m49s   v1.20.0
//部署成功后node1和node2会自动运行一些容器,镜像
[root@node1 ~]# docker images
REPOSITORY                                           TAG       IMAGE ID       CREATED         SIZE
quay.io/coreos/flannel                               v0.15.1   e6ea68648f0c   5 weeks ago     69.5MB
rancher/mirrored-flannelcni-flannel-cni-plugin       v1.0.0    cd5235cd7dc2   7 weeks ago     9.03MB
registry.aliyuncs.com/google_containers/kube-proxy   v1.20.0   10cc881966cf   12 months ago   118MB
registry.aliyuncs.com/google_containers/pause        3.2       80d28bedfe5d   22 months ago   683kB
[root@node1 ~]# docker ps
CONTAINER ID   IMAGE                                                COMMAND                  CREATED          STATUS          PORTS     NAMES
2b3f3e7ef49e   e6ea68648f0c                                         "/opt/bin/flanneld -…"   26 minutes ago   Up 26 minutes             k8s_kube-flannel_kube-flannel-ds-hms85_kube-system_4451bf8c-3481-422b-82d7-f4d36db0e820_0
eb6202befbbb   registry.aliyuncs.com/google_containers/pause:3.2    "/pause"                 28 minutes ago   Up 27 minutes             k8s_POD_kube-flannel-ds-hms85_kube-system_4451bf8c-3481-422b-82d7-f4d36db0e820_0
f5a8811d22f3   registry.aliyuncs.com/google_containers/kube-proxy   "/usr/local/bin/kube…"   30 minutes ago   Up 30 minutes             k8s_kube-proxy_kube-proxy-lm26z_kube-system_d4b9fdce-1425-46b7-9b30-23ad6e00d4b4_0
f052f660e3c0   registry.aliyuncs.com/google_containers/pause:3.2    "/pause"                 31 minutes ago   Up 31 minutes             k8s_POD_kube-proxy-lm26z_kube-system_d4b9fdce-1425-46b7-9b30-23ad6e00d4b4_0


[root@node2 ~]# docker images
REPOSITORY                                           TAG       IMAGE ID       CREATED         SIZE
nginx                                                latest    f652ca386ed1   2 weeks ago     141MB
quay.io/coreos/flannel                               v0.15.1   e6ea68648f0c   5 weeks ago     69.5MB
rancher/mirrored-flannelcni-flannel-cni-plugin       v1.0.0    cd5235cd7dc2   7 weeks ago     9.03MB
registry.aliyuncs.com/google_containers/kube-proxy   v1.20.0   10cc881966cf   12 months ago   118MB
registry.aliyuncs.com/google_containers/pause        3.2       80d28bedfe5d   22 months ago   683kB
[root@node2 ~]# docker ps
CONTAINER ID   IMAGE                                                COMMAND                  CREATED          STATUS          PORTS     NAMES
2f8dda1f4cf2   nginx                                                "/docker-entrypoint.…"   22 minutes ago   Up 22 minutes             k8s_nginx_nginx-6799fc88d8-d97xz_default_94d29f93-3384-43aa-a359-28c872d1aaeb_0
fa143d70104d   registry.aliyuncs.com/google_containers/pause:3.2    "/pause"                 23 minutes ago   Up 23 minutes             k8s_POD_nginx-6799fc88d8-d97xz_default_94d29f93-3384-43aa-a359-28c872d1aaeb_0
9437112cf83b   e6ea68648f0c                                         "/opt/bin/flanneld -…"   27 minutes ago   Up 27 minutes             k8s_kube-flannel_kube-flannel-ds-shjdh_kube-system_44e8f447-7c14-4855-adfb-8462a43da152_0
0a1f888f5fc0   registry.aliyuncs.com/google_containers/pause:3.2    "/pause"                 28 minutes ago   Up 28 minutes             k8s_POD_kube-flannel-ds-shjdh_kube-system_44e8f447-7c14-4855-adfb-8462a43da152_0
1570165ba280   registry.aliyuncs.com/google_containers/kube-proxy   "/usr/local/bin/kube…"   30 minutes ago   Up 30 minutes             k8s_kube-proxy_kube-proxy-gg296_kube-system_1cb756a1-0a45-47ca-86cf-fed5a0563f2c_0
027500a9d9e7   registry.aliyuncs.com/google_containers/pause:3.2    "/pause"                 30 minutes ago   Up 30 minutes             k8s_POD_kube-proxy-gg296_kube-system_1cb756a1-0a45-47ca-86cf-fed5a0563f2c_0
//查看名称空间
[root@master ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   40m
kube-node-lease   Active   40m
kube-public       Active   40m
kube-system       Active   40m


//指定名称空间去看pods的信息
[root@master ~]# kubectl get pods -n kube-system
NAME                                         READY   STATUS    RESTARTS   AGE
coredns-7f89b7bc75-7f99f                     1/1     Running   0          40m
coredns-7f89b7bc75-trgz6                     1/1     Running   0          40m
etcd-master.example.com                      1/1     Running   0          40m
kube-apiserver-master.example.com            1/1     Running   0          40m
kube-controller-manager-master.example.com   1/1     Running   0          40m
kube-flannel-ds-2t2hk                        1/1     Running   0          30m
kube-flannel-ds-hms85                        1/1     Running   0          30m
kube-flannel-ds-shjdh                        1/1     Running   0          30m
kube-proxy-gg296                             1/1     Running   0          32m
kube-proxy-lm26z                             1/1     Running   0          33m
kube-proxy-vv24b                             1/1     Running   0          40m
kube-scheduler-master.example.com            1/1     Running   0          40m


//查看pods在哪运行
[root@master ~]# kubectl get pods -n kube-system -o wide
NAME                                         READY   STATUS    RESTARTS   AGE   IP                NODE                 NOMINATED NODE   READINESS GATES
coredns-7f89b7bc75-7f99f                     1/1     Running   0          41m   10.244.0.3        master.example.com   <none>           <none>
coredns-7f89b7bc75-trgz6                     1/1     Running   0          41m   10.244.0.2        master.example.com   <none>           <none>
etcd-master.example.com                      1/1     Running   0          41m   192.168.200.140   master.example.com   <none>           <none>
kube-apiserver-master.example.com            1/1     Running   0          41m   192.168.200.140   master.example.com   <none>           <none>
kube-controller-manager-master.example.com   1/1     Running   0          41m   192.168.200.140   master.example.com   <none>           <none>
kube-flannel-ds-2t2hk                        1/1     Running   0          30m   192.168.200.140   master.example.com   <none>           <none>
kube-flannel-ds-hms85                        1/1     Running   0          30m   192.168.200.142   node1.example.com    <none>           <none>
kube-flannel-ds-shjdh                        1/1     Running   0          30m   192.168.200.143   node2.example.com    <none>           <none>
kube-proxy-gg296                             1/1     Running   0          33m   192.168.200.143   node2.example.com    <none>           <none>
kube-proxy-lm26z                             1/1     Running   0          34m   192.168.200.142   node1.example.com    <none>           <none>
kube-proxy-vv24b                             1/1     Running   0          41m   192.168.200.140   master.example.com   <none>           <none>
kube-scheduler-master.example.com            1/1     Running   0          41m   192.168.200.140   master.example.com   <none>           <none>

测试kubernetes集群

[root@master ~]# kubectl create deployment nginx --image=nginx 
deployment.apps/nginx created

//暴露端口号,暴露的是service的端口号,不能用pod的IP地址去访问,也不能用pod的主机名去访问,要访问的话只能用service访问
[root@master ~]# kubectl expose deployment nginx --port=80 --type=NodePort
service/nginx exposed
[root@master ~]# kubectl get pods
NAME                     READY   STATUS              RESTARTS   AGE
nginx-6799fc88d8-d97xz   0/1     ContainerCreating   0          24s

[root@master ~]# kubectl get pod,svc
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-6799fc88d8-d97xz   1/1     Running   0          115s

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        17m
service/nginx        NodePort    10.103.78.103   <none>        80:30904/TCP   103s
// 测试能否访问
[root@master ~]# curl 10.103.78.103
<!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>

网页上访问
master IP:service端口号(我这里的端口号是30904)
在这里插入图片描述

Logo

开源、云原生的融合云平台

更多推荐