over 1 year ago

練習安裝 Kubernates 在 CentOS 使用 YUM 來安裝還挺簡單的,但是 UI 還沒裝起來,不過1.1的UI直接拉出來變成另一個項目,還搞不定怎麼裝
OS : CentOS 7

準備虛擬機 Master

Vagrantfile
$vm_gui = false
$vm_memory = 2048
$vm_cpus = 2
$vm_name = "kubemaster"

Vagrant.configure(2) do |config|
  config.vm.box = "bento/centos-7.1"
  config.vm.box_check_update = false
  config.vm.hostname = $vm_name
  
#  config.vm.network "forwarded_port", guest: 80, host: 8080

  config.vm.define "kubernete" do |v|
    v.vm.provider "virtualbox" do |vb|
      vb.name = $vm_name
      vb.gui = $vm_gui
      vb.memory = $vm_memory
      vb.cpus = $vm_cpus
    end
  end

  config.vm.network "public_network", ip: "192.168.1.21"
  
  config.vm.provision "shell", inline: <<-SHELL
    sudo yum -y upgrade
    sudo yum -y update
  SHELL
  
  config.ssh.username = "vagrant" 
  config.ssh.password = "vagrant" 
  
end

準備虛擬機 Minions *1 或 *2 IP & name 就自己改一改

Vagrantfile
$vm_gui = false
$vm_memory = 2048
$vm_cpus = 2
$vm_name = "kubenode1"

Vagrant.configure(2) do |config|
  config.vm.box = "bento/centos-7.1"
  config.vm.box_check_update = false
  config.vm.hostname = $vm_name

  config.vm.define "kubernete" do |v|
    v.vm.provider "virtualbox" do |vb|
      vb.name = $vm_name
      vb.gui = $vm_gui
      vb.memory = $vm_memory
      vb.cpus = $vm_cpus
    end
  end

  config.vm.network "public_network", ip: "192.168.1.22"
  
  config.vm.provision "shell", inline: <<-SHELL
    sudo yum -y upgrade
    sudo yum -y update
  SHELL
  
  config.ssh.username = "vagrant" 
  config.ssh.password = "vagrant" 
  
end

我們準備了兩台Minions假如IP列表如下

主機名 IP
kubemaster 192.168.1.21
kubenode1 192.168.1.22
kubenode2 192.168.1.23

Prerequisites

  1. Disable iptables on each node to avoid conflicts with Docker iptables rules.

    systemctl stop firewalld
    systemctl disable firewalld
    
  2. Install NTP and make sure it is enabled and running

    yum -y install ntp
    systemctl start ntpd
    systemctl enable ntpd
    
  3. Add Hostname

    cat << EOF >> /etc/hosts
    192.168.1.21   kube-master
    192.168.1.22   kube-node1
    192.168.1.23   kube-node2
    EOF
    

Setting up the Kubernetes Master

  1. Install etcd and Kubernetes through yum

    yum -y install etcd kubernetes
    
  2. Configure etcd to listen to all IP addresses inside /etc/etcd/etcd.conf. Ensure the following lines are uncommented, and assign the following values

    cat << EOF > /etc/etcd/etcd.conf
    # [member]
    ETCD_NAME=default
    ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
    ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379"
    #[cluster]
    ETCD_ADVERTISE_CLIENT_URLS="http://localhost:2379"
    EOF
    

    改變的部分是 ETCD_LISTEN_CLIENT_URLS 從 localhost 變成 0.0.0.0

  3. Configure Kubernetes API server inside /etc/kubernetes/apiserver. Ensure the following lines are uncommented, and assign the following values

    cat << EOF > /etc/kubernetes/apiserver
    KUBE_API_ADDRESS="--address=0.0.0.0"
    KUBE_API_PORT="--port=8080"
    KUBELET_PORT="--kubelet_port=10250"
    KUBE_ETCD_SERVERS="--etcd_servers=http://127.0.0.1:2379"
    KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16"
    KUBE_ADMISSION_CONTROL="--admission_control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"
    KUBE_API_ARGS=""
    EOF
    

    改變的部分是 KUBE_API_ADDRESS 從 localhost 變成 0.0.0.0
    KUBE_API_PORT 拿掉註解
    KUBELET_PORT 拿掉註解

  4. Start and enable etcd, kube-apiserver, kube-controller-manager and kube-scheduler

    for SERVICES in etcd kube-apiserver kube-controller-manager kube-scheduler; do
    systemctl restart $SERVICES
    systemctl enable $SERVICES
    systemctl status $SERVICES 
    done
    
  5. Define flannel network configuration in etcd. This configuration will be pulled by flannel service on minions

    etcdctl mk /atomic.io/network/config '{"Network":"172.17.0.0/16"}'
    
  6. At this point, we should notice that nodes' status returns nothing because we haven’t started any of them yet

    $ kubectl get nodes
    NAME             LABELS              STATUS
    

Setting up Kubernetes Minions (Nodes)

The following steps should be performed on minion1, minion2 and minion3 unless specified otherwise.

  1. Install flannel and Kubernetes using yum

    yum -y install flannel kubernetes
    
  2. Configure etcd server for flannel service. Update the following line inside /etc/sysconfig/flanneld to connect to the respective master

    cat << EOF > /etc/sysconfig/flanneld
    FLANNEL_ETCD="http://kube-master:2379"
    FLANNEL_ETCD_KEY="/atomic.io/network"
    EOF
    

    變更 FLANNEL_ETCD IP 為 主機名稱

  3. Configure Kubernetes default config at /etc/kubernetes/config, ensure you update the KUBE_MASTER value to connect to the Kubernetes master API server

    cat << EOF > /etc/kubernetes/config
    KUBE_LOGTOSTDERR="--logtostderr=true"
    KUBE_LOG_LEVEL="--v=0"
    KUBE_ALLOW_PRIV="--allow_privileged=false"
    KUBE_MASTER="--master=http://kube-master:8080"
    EOF
    

    變更 KUBE_MASTER IP 為 主機名稱

  4. Configure kubelet service inside /etc/kubernetes/kubelet as below
    這邊要注意 KUBELET_HOSTNAME 須帶入的是 minion 自己的 IP

    cat << EOF > /etc/kubernetes/kubelet
    KUBELET_ADDRESS="--address=0.0.0.0"
    KUBELET_PORT="--port=10250"
    # change the hostname to this host’s IP address
    KUBELET_HOSTNAME="--hostname_override=kube-node1"
    KUBELET_API_SERVER="--api_servers=http://kube-master:8080"
    KUBELET_ARGS=""
    EOF
    
  5. Start and enable kube-proxy, kubelet, docker and flanneld services

    for SERVICES in kube-proxy kubelet docker flanneld; do
    systemctl restart $SERVICES
    systemctl enable $SERVICES
    systemctl status $SERVICES 
    done
    
  6. On each minion, you should notice that you will have two new interfaces added, docker0 and flannel0. You should get different range of IP addresses on flannel0 interface on each minion, similar to below

    $ ip a | grep flannel | grep inet
    inet 172.17.45.0/16 scope global flannel0
    
  7. Now login to Kubernetes master node and verify the minions’ status

    kubectl get nodes
    NAME           LABELS                                STATUS
    kube-node1     kubernetes.io/hostname=kube-node1     Ready
    

這邊還有更多指令可以參考
https://github.com/kubernetes/kubernetes/blob/master/docs/user-guide/kubectl/kubectl.md

Creating Pods (Containers)

mkdir pods
cd pods
cat << EOF > mysql.yaml
apiVersion: v1
kind: Pod
metadata:
  name: mysql
  labels:
    name: mysql
spec:
  containers:
    - resources:
        limits :
          cpu: 1
      image: mysql
      name: mysql
      env:
        - name: MYSQL_ROOT_PASSWORD
          # change this
          value: yourpassword
      ports:
        - containerPort: 3306
          name: mysql
EOF

Create the pod

kubectl create -f mysql.yaml
# kubectl get pods

NAME      READY     STATUS    RESTARTS   AGE
mysql     1/1       Running   0          1m

Creating Service

At this point, we have a MySQL pod inside 192.168.1.22. Define a mysql-service.yaml as below

cat << EOF > ./mysql-service.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    name: mysql
  name: mysql
spec:
  publicIPs:
    - 192.168.1.22
  ports:
    # the port that this service should serve on
    - port: 3306
  # label keys and values that must match in order to receive traffic for this service
  selector:
    name: mysql
EOF

Start the service

kubectl create -f mysql-service.yaml
# kubectl get services

NAME         LABELS                                    SELECTOR     IP(S)           PORT(S)
kubernetes   component=apiserver,provider=kubernetes   <none>       10.254.0.1      443/TCP
mysql        name=mysql                                name=mysql   10.254.94.195   3306/TCP

參考
http://severalnines.com/blog/installing-kubernetes-cluster-minions-centos7-manage-pods-services

手動安裝
http://www.dockone.io/article/758
http://www.2cto.com/net/201601/488074.html

← Raspberry Pi 2 安裝 Arch Linux akka cluster example →
 
comments powered by Disqus