腾讯云CVM上用kubeadm安装Kubernetes集群(版本1.14.0)

2019-04-08 16:47:54 浏览数 (1)

kubeadmKubernetes官方提供的用于快速安装 Kubernetes 集群的工具,通过将集群的各个组件进行容器化安装管理,通过kubeadm的方式安装集群比二进制的方式安装要方便

这里先申明一点,本环境最重要的是能够拉取国外镜像,如无法拉取国外镜像请绕道。

环境

地域:中国香港一区

配置:2 核 4 GB 1 Mbps

系统盘:普通云硬盘

系统: CentOS 7.5 64位

vpc信息:

vpc cidr:10.0.0.0/16

子网:10.0.0.0/24


先创建vpc

创建私有网络

1. 创建cvm

这里创建两台cvm,一台master、一台node此步不赘述请按需配置

2. 基础环境配置主机名

代码语言:txt复制
#master 10.0.0.15
hostnamectl set-hostname kube-master

#配置node 的hostname
hostnamectl set-hostname kube-node

# 停防火墙
systemctl stop firewalld
systemctl disable firewalld其他配置

#关闭Swap
swapoff -a 
sed -i 's/.\*swap.\*/#&/' /etc/fstab

#关闭Selinux
setenforce  0 
sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux 
sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config 
sed -i "s/^SELINUX=permissive/SELINUX=disabled/g" /etc/sysconfig/selinux 
sed -i "s/^SELINUX=permissive/SELINUX=disabled/g" /etc/selinux/config  

#加载br_netfilter
modprobe br_netfilter

#添加配置内核参数
cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
#加载配置
sysctl -p /etc/sysctl.d/k8s.conf

3. 安装docker

对于版本的选择,官方有个支持列表:

The list of validated docker versions remain unchanged at 1.11.1, 1.12.1, 1.13.1, 17.03, 17.06, 17.09, 18.06 since (https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG-1.13.md#external-dependencies)

这里我安装的是最新的,如果你在根据本文档搭建,可以通过查看版本,指定版本安装,如果不加版本直接yum install 默认是最新的版本。

代码语言:txt复制
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install -y docker-ce
systemctl start docker && systemctl enable docker

查看docker版本

代码语言:txt复制
$ docker  version
Client:
 Version:           18.09.4
 API version:       1.39
 Go version:        go1.10.8
 Git commit:        d14af54266
 Built:             Wed Mar 27 18:34:51 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.4
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.8
  Git commit:       d14af54
  Built:            Wed Mar 27 18:04:46 2019
  OS/Arch:          linux/amd64
  Experimental:     false

4. 安装kubeadm,kubelet,kubectl

这里如果不指定默认也是安装最新的

代码语言:txt复制
$ cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-$basearch
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF

$ yum install -y kubelet kubeadm kubectl

$ systemctl enable kubelet && systemctl start kubelet

5. 下载所需的master组件镜像

--kubernetes-version v1.14.0 指定k8s版本

代码语言:txt复制
$ kubeadm config images pull --kubernetes-version v1.14.0
config/images Pulled k8s.gcr.io/kube-apiserver:v1.14.0
config/images Pulled k8s.gcr.io/kube-controller-manager:v1.14.0
config/images Pulled k8s.gcr.io/kube-scheduler:v1.14.0
config/images Pulled k8s.gcr.io/kube-proxy:v1.14.0
config/images Pulled k8s.gcr.io/pause:3.1
config/images Pulled k8s.gcr.io/etcd:3.3.10
config/images Pulled k8s.gcr.io/coredns:1.3.1
$ docker images
REPOSITORY                           TAG                 IMAGE ID            CREATED             SIZE
k8s.gcr.io/kube-proxy                v1.14.0             5cd54e388aba        6 days ago          82.1MB
k8s.gcr.io/kube-apiserver            v1.14.0             ecf910f40d6e        6 days ago          210MB
k8s.gcr.io/kube-controller-manager   v1.14.0             b95b1efa0436        6 days ago          158MB
k8s.gcr.io/kube-scheduler            v1.14.0             00638a24688b        6 days ago          81.6MB
k8s.gcr.io/coredns                   1.3.1               eb516548c180        2 months ago        40.3MB
k8s.gcr.io/etcd                      3.3.10              2c4adeb21b4f        4 months ago        258MB
k8s.gcr.io/pause                     3.1                 da86e6ba6ca1        15 months ago       742kB

6. 初始化master

kebeadm init需要加上参数 详细的参数介绍可看:https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm-init/

代码语言:txt复制
$ kubeadm init --apiserver-advertise-address=10.0.0.15 --kubernetes-version=v1.14.0 --pod-network-cidr=172.16.0.0/16
[init] Using Kubernetes version: v1.14.0
[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 Hostname]: hostname "kube-master" could not be reached
        [WARNING Hostname]: hostname "kube-master": lookup kube-master on 183.60.83.19:53: no such host
[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 [kube-master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 10.0.0.15]
[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/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [kube-master localhost] and IPs [10.0.0.15 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [kube-master localhost] and IPs [10.0.0.15 127.0.0.1 ::1]
[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 18.502803 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 kube-master as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node kube-master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: 63lvgx.obnzzp2auuecvgno
[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 10.0.0.15:6443 --token 63lvgx.obnzzp2auuecvgno 
    --discovery-token-ca-cert-hash sha256:e15bdd9c102ecb3ae4213c75e90f908cddbf4e3bee8a085f1d06bf6cfb1278ca 

7. 配置常规用户如何使用kubectl访问集群

代码语言:txt复制
  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

8. 安装Flannel

代码语言:txt复制
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

9. 查看所有pod

代码语言:txt复制
$ kubectl get pod -n kube-system
NAME                                  READY   STATUS    RESTARTS   AGE
coredns-fb8b8dccf-dh8r2               1/1     Running   0          4m21s
coredns-fb8b8dccf-djf7p               1/1     Running   0          4m21s
etcd-kube-master                      1/1     Running   0          3m38s
kube-apiserver-kube-master            1/1     Running   0          3m18s
kube-controller-manager-kube-master   1/1     Running   0          3m20s
kube-flannel-ds-amd64-69jzk           1/1     Running   0          96s
kube-proxy-fhhp9                      1/1     Running   0          4m21s
kube-scheduler-kube-master            1/1     Running   0          3m24s

10.查看节点

代码语言:txt复制
 $ kubectl get node
NAME          STATUS   ROLES    AGE   VERSION
kube-master   Ready    master   21h   v1.14.0

11.加入node节点

执行上述1-5步骤

然后执行初始化master时底下的 kubeadm join信息

代码语言:txt复制
[root@VM_0_10_centos etc]# kubeadm join 10.0.0.15:6443 --token 63lvgx.obnzzp2auuecvgno 
>     --discovery-token-ca-cert-hash sha256:e15bdd9c102ecb3ae4213c75e90f908cddbf4e3bee8a085f1d06bf6cfb1278ca
[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/
[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.

返回master节点用watch kubectl get nodes查看节点,看到node变成ready即代表成功

12.创建一个nginx

代码语言:txt复制
$ cat my-nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
spec:
  selector:
    matchLabels:
      run: my-nginx
  replicas: 2
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx
        ports:
        - containerPort: 80
---

apiVersion: v1
kind: Service
metadata:
  name: my-nginx
  labels:
    run: my-nginx
spec:
  ports:
  - nodePort: 31250
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    run: my-nginx
  type: NodePort

保存好yaml文件后通过kubectl create -f my-nginx.yaml 创建

代码语言:txt复制
$  kubectl create -f  my-nginx.yaml 

deployment.apps/my-nginx created

service/my-nginx created

查看创建出来的pod和service

代码语言:txt复制
$ kubectl get pod -o wide
NAME                        READY   STATUS    RESTARTS   AGE     IP            NODE         NOMINATED NODE   READINESS GATES
my-nginx-86459cfc9f-sppzq   1/1     Running   0          3m40s   172.16.1.16   kube-node1   <none>           <none>
my-nginx-86459cfc9f-zjcp7   1/1     Running   0          3m40s   172.16.1.17   kube-node1   <none>           <none>

$ kubectl get svc 
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        114m
my-nginx     NodePort    10.98.233.226   <none>        80:31524/TCP   3m43s

通过 curl 10.0.0.10:31524获取nginx内容

代码语言:txt复制
$ curl 10.0.0.10:31524
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    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 人点赞