2

前言

最近开始接触Google的Kubernetes(简称k8s,由于字母k与字母s之间省略8个字母),这篇文章记录一下我的初体验。

环境准备

我是使用Vagrant来快速构建基本的虚拟机环境,有人说干嘛不用Docker,哈哈,也是可以的,但是怎么感觉有点『骑马找马』的感觉,如果你使用Docker,记得开放30001口,我们后面会用到。

全程最好开着VPN
全程最好开着VPN
全程最好开着VPN
全程最好开着VPN
全程最好开着VPN
全程最好开着VPN
全程最好开着VPN
全程最好开着VPN
全程最好开着VPN

Vagrant安装非常简单,下载地址:

https://www.vagrantup.com/downloads.html

选择你对应的平台,会自带安装VirtualBox

vagrant我选用了bento/centos-7.1这个镜像。

Mac用户创建一个文件夹并进入初始化它。

cd ~
mkdir k8s
cd k8s
vagrant init bento/centos-7.1

将配置修改Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "bento/centos-7.1"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network "private_network", ip: "192.168.8.8"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  config.vm.synced_folder ".", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the d you are using for more
  # information on available options.

  # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
  # such as FTP and Heroku are also available. See the documentation at
  # https://docs.vagrantup.com/v2/push/atlas.html for more information.
  # config.push.define "atlas" do |push|
  #   push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
  # end

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   sudo apt-get update
  #   sudo apt-get install -y apache2
  # SHELL
end

修改完毕后,运行vagrant up启动环境。

一下是我的项目文件夹结构:
图片描述

我们对应创建好这几个文件,为了避免新手错误的书写格式导致启动失败带来挫败感,我把我的配置文件上传一份到GitHub:

https://github.com/cnBruceHong/learn-k8s

运行:

vagrant ssh

进入虚拟机。

安装K8s

关闭防火墙

systemctl disable firewalld
systemctl stop firewalld

安装etcd和k8s

sudo yum install etcd kubernetes vim

修改配置文件

sudo vim /etc/sysconfig/docker

修改里面对应的内容为:

OPTIONS='--selinux-enabled=false --insecure-registry gcr.io'

还要修改:

sudo vim /etc/kubernetes/apiserver 

修改里面的内容为:

KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"

启动所有服务:

sudo systemctl start etcd docker kube-apiserver kube-controller-manager kube-scheduler kubelet kube-proxy

然后顺序运行:

cd /vagrant_data/
kubectl create -f redis-master-controller.yaml
kubectl create -f redis-master-service.yaml
kubectl create -f redis-slave-controller.yaml
kubectl create -f redis-slave-service.yaml
kubectl create -f frontend-controller.yaml
kubectl create -f frontend-service.yaml

然后在宿主机上浏览器访问:http://192.168.8.8:30001就可以看到界面了。

你可以通过kubectl get pods来查看pods,里面的要running才算成功哦!~
图片描述

最后

里面会有一些你不懂的名词,没关系,你可以自主学习这些名词。
这篇文章关键是:

ヾ(。`Д´。)你成功搭起来了K8s,成就感暴满有木有?


洪旭
4.1k 声望134 粉丝

欢迎关注我的微博:@码云