一、环境描述

192.168.153.10	k8s-master
192.168.153.11	k8s-node01
192.168.153.12	k8s-node02

二、基础环境准备

1、关闭防火墙、SELinux, 时间同步

2、配置免密SSH

3、添加主机名解析

[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.153.10	k8s-master
192.168.153.11	k8s-node01
192.168.153.12	k8s-node02

4、禁用swap

# swapoff -a

5、调整内核参数,将流量转发到 iptables链

[root@k8s-master ~]# cat /etc/sysctl.d/k8s.conf 
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
​
[root@k8s-master ~]# sysctl --system

三、在所有主机上安装docker

[root@k8s-master ~]# cat /etc/yum.repos.d/docker-ce.repo 
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://repo.huaweicloud.com/docker-ce/linux/centos/$releasever/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://repo.huaweicloud.com/docker-ce/linux/centos/gpg
...................
​
[root@k8s-master ~]# yum install -y docker-ce-18*
​
[root@k8s-master ~]# systemctl start docker
[root@k8s-master ~]# systemctl enable docker
​
[root@k8s-master ~]# cat /etc/docker/daemon.json 
{"registry-mirrors": ["http://f1361db2.m.daocloud.io"]}
​
[root@k8s-master ~]# systemctl restart docker
​
[root@k8s-master ~]# rpm -q docker-ce
docker-ce-18.09.9-3.el7.x86_64
​

四、在所有主机安装kubeadm相关工具

[root@k8s-master ~]# cat /etc/yum.repos.d/k8s.repo
[k8s]
name=k8s
baseurl=https://mirrors.tuna.tsinghua.edu.cn/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
​
​
[root@k8s-master ~]# yum install -y kubeadm-1.15* kubectl-1.15* kubelet-1.15*
​
[root@k8s-master ~]# systemctl enable kubelet.service 

五、在Master节点上初始化集群

[root@k8s-master ~]# kubeadm init --apiserver-advertise-address=192.168.153.10 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.15.0 --service-cidr=10.96.0.0/16 --pod-network-cidr=172.168.0.0/16
[init] Using Kubernetes version: v1.15.0
[preflight] Running pre-flight checks
	[WARNING Service-Docker]: docker service is not enabled, please run 'systemctl enable docker.service'
	[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/
[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 "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.1.0.1 192.168.153.10]
[certs] Generating "apiserver-kubelet-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 [k8s-master localhost] and IPs [192.168.153.10 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.153.10 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-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 22.002368 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.15" 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 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: hpx386.x1u8n28h4anu1g4w
[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.153.10:6443 --token hpx386.x1u8n28h4anu1g4w \
    --discovery-token-ca-cert-hash sha256:d73477aa7862ec3a1bc4acbdcf081641b6fe5432579cbade52459fd3168e8984 
[root@k8s-master ~]# 

根据上述提示创建kubeconfig文件

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

六、将两个节点添加到集群

kubeadm join 192.168.153.10:6443 --token hpx386.x1u8n28h4anu1g4w \
    --discovery-token-ca-cert-hash sha256:d73477aa7862ec3a1bc4acbdcf081641b6fe5432579cbade52459fd3168e8984 
    
将节点添加到集群后,此时通过kubectl get node查看工作状态均为NotReady状态,需要部署flannel网络 

[root@k8s-master ~]# kubectl get nodes
NAME                   STATUS     ROLES    AGE   VERSION
k8s-master   NotReady   master   13m   v1.15.12
k8s-node01   NotReady   <none>   91s   v1.15.12
k8s-node02   NotReady   <none>   52s   v1.15.12

七、在Master节点上部署flannel网络

[root@k8s-master ~]# wget https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml
​
需要将该文件中的镜像下载地址修改为国内可下载的地址
​
[root@k8s-master kube-flannel.yml]# grep -i "image" kube-flannel.yml 
        image: lizhenliang/flannel:v0.11.0-amd64
        image: lizhenliang/flannel:v0.11.0-amd64
        image: lizhenliang/flannel:v0.11.0-amd64
        image: lizhenliang/flannel:v0.11.0-amd64
        image: lizhenliang/flannel:v0.11.0-amd64
        
[root@k8s-master kube-flannel.yml]# kubectl create -f kube-flannel.yml 
​
[root@k8s-master ~]# ps -elf | grep flannel
4 S root      41135  41116  0  80   0 - 120397 futex_ 04:36 ?       00:00:20 /opt/bin/flanneld --ip-masq --kube-subnet-mgr
0 S root      60763  48639  0  80   0 - 28165 pipe_w 09:44 pts/2    00:00:00 grep --color=auto flannel

八、查看集群状态

[root@k8s-master ~]# kubectl cluster-info
Kubernetes master is running at https://192.168.153.10:6443
KubeDNS is running at https://192.168.153.10:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
​
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
[root@k8s-master ~]# kubectl get nodes
NAME                   STATUS   ROLES    AGE   VERSION
k8s-master  Ready    master   17h   v1.15.0
k8s-node01   Ready    <none>   16h   v1.15.0
k8s-node02  Ready    <none>   16h   v1.15.0
[root@k8s-master ~]# kubectl get pods -n kube-system
NAME                                           READY   STATUS    RESTARTS   AGE
coredns-bccdc95cf-6pvgq                        1/1     Running   0          17h
coredns-bccdc95cf-7bwb8                        1/1     Running   0          17h
etcd-k8s-master                      1/1     Running   0          17h
kube-apiserver-k8s-master            1/1     Running   0          17h
kube-controller-manager-k8s-master   1/1     Running   0          17h
kube-flannel-ds-amd64-pdvfl                    1/1     Running   0          16h
kube-flannel-ds-amd64-rr2k9                    1/1     Running   1          16h
kube-flannel-ds-amd64-zg9kg                    1/1     Running   1          16h
kube-proxy-jtkxt                               1/1     Running   0          17h
kube-proxy-k8lz4                               1/1     Running   2          16h
kube-proxy-kp79g                               1/1     Running   2          16h
kube-scheduler-k8s-master            1/1     Running   0          17h

九、测试集群部署应用

[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 svc
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.1.0.1       <none>        443/TCP        17h
nginx        NodePort    10.1.209.129   <none>        80:31704/TCP   15h

[root@k8s-master ~]# kubectl get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP           NODE                   NOMINATED NODE   READINESS GATES
nginx-554b9c67f9-dzdt8   1/1     Running   0          15h   10.244.2.3   k8s-node02   <none>           <none>

十、部署kubernetes-dashboard

[root@k8s-master ~]# wget https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml

     spec:
      nodeName: k8s-master							//将dashboard调度到master节点运行
      containers:
      - name: kubernetes-dashboard
        image: cuiyf/kubernetes-dashboard-amd64:v1.10.1			//镜像下载地址
        ports:
        - containerPort: 8443
          protocol: TCP
        args:
          - --auto-generate-certificates

kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kube-system
spec:
  type: NodePort            //添加服务类型
  ports:
    - port: 443
      targetPort: 8443
      nodePort: 30001       //指定发布端口
  selector:
    k8s-app: kubernetes-dashboard

[root@k8s-master ~]# kubectl create -f k8s-dashboard.yaml
[root@k8s-master ~]# kubectl get pod -n kube-system -o wide
NAME                                           READY   STATUS    RESTARTS   AGE   IP               NODE                   NOMINATED NODE   READINESS 
kubernetes-dashboard-86cd9cbc54-shwr2          1/1     Running   0          15h   10.244.1.5       k8s-node01   <none>           <none>
[root@k8s-master ~]# kubectl get service -n kube-system -o wide
NAME                   TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE   SELECTOR
kube-dns               ClusterIP   10.1.0.10    <none>        53/UDP,53/TCP,9153/TCP   17h   k8s-app=kube-dns
kubernetes-dashboard   NodePort    10.1.63.1    <none>        443:30001/TCP            15h   k8s-app=kubernetes-dashboard

https://IP:port访问dashboard

创建service account并绑定管理员权限

[root@k8s-master ~]# kubectl create serviceaccount dashboard-admin -n kube-system
serviceaccount/dashboard-admin created
[root@k8s-master ~]# kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin
--serviceaccount=kube-system:dashboard-admin
[root@k8s-master ~]# kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')
Name:         dashboard-admin-token-6g6pr
Namespace:    kube-system
Labels:       <none>
Annotations:  kubernetes.io/service-account.name: dashboard-admin
              kubernetes.io/service-account.uid: 6e9c51fb-1300-4f84-a541-fbe6949d9fbe
​
Type:  kubernetes.io/service-account-token
​
Data
====
ca.crt:     1025 bytes
namespace:  11 bytes
token:      eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkYXNoYm9hcmQtYWRtaW4tdG9rZW4tNmc2cHIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGFzaGJvYXJkLWFkbWluIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiNmU5YzUxZmItMTMwMC00Zjg0LWE1NDEtZmJlNjk0OWQ5ZmJlIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmRhc2hib2FyZC1hZG1pbiJ9.NBPoIuPrecDsFCzT3YZ-NXd6OFiNffRXU_x57sJrg5G_Qj5Q-t35Lwqa_VIzTLsy7Y56OJ60lnTKEErMbcsGn4ClLqQ5iU_BYevWngSJs4uNLHdKFgTsA0H5zEsN_LgYcRq3GvZtx3Ij-pn84ec5kSHwrTm1l-c2i2EI328lhEZfs82JqTXuvMgGwTBQd2qD5xkqj_tOgKWN6d_2sGJNC4Tunl8eYasjkYvXQiMNR35-BYIDyuywnLwp_wgkYgIImFdbNPRcaqhKJOE3fIf8fjT4gGGHQAWuqnaRWqsBwzJx8BBBLSZUQfJvqjMZfOUjbNPPYBjp31kExGHqAqq2mw
Logo

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

更多推荐