10分钟入门kubernetes(上)
kubernetes简称k8s, 主要用途是automate deployment, scaling, and managment of containerized applications。是目前非常火的docker集群部署管理方案,适用于部署microservice架构。是google15年的经验积淀。这就要说说google为啥要用container了,因为那个时候还没有virtual machine这个技术,当然现在发现这个方向的应用前景比virtual machine集群效果要好。提到kubernetes,不能不说CNCF,CNCF出品,必属精品。现在k8s和envoy(lyft的神作)打得火热,感觉是未来云服务商的主流架构,GKE是google k8s engine也是kubernetes的原始应用,不过Azure好像现在也开始支持k8s,amazon的ecs如果不是用的k8s做后端那么也势必会迁移到k8s。
alternative solution to k8s:
- docker swarm
- hashicorp nomad
- apache mesos
- rancher
k8s的诞生
k8s的前身是google的Borg,google没有选择开源Borg,估计是因为内部依赖比较多,而是从头build了k8s,所以说k8s的架构从一开始就站在google15年大型container管理经验的肩上,根正苗红。Google 2015年发表了著名的Borg paper “Large-scale cluster management at Google with Borg”,从而将秘密昭示天下。
下面是一个趣闻,说mesos是怎么诞生的,当时伯克利的一群科学家和google做Borg的工程师酒后闲聊,弄明白了Borg的核心原理,然后就搞出了mesos,所以mesos反而是抢在了k8s的前面,提前成为了很多大公司包括twitter,verizon在内的解决方案,k8s这个正品反而由硅谷的startup率先尝试。所以江湖传言说需求大还是要靠mesos就是这么来的,但是k8s正在飞速的证明自己的成熟度。
k8s 架构
k8s could be deployed as services on VMs or bare-metal machines.
k8s 的安装,本文仅限于单机版minikube
安装minikube
https://github.com/kubernetes/minikube
安装kubectl
https://kubernetes.io/docs/tasks/tools/install-kubectl/#install-kubectl-binary-via-curl
安装virtualbox
https://www.virtualbox.org/wiki/Downloads
k8s dashboard
minikube dashboard
在dashboard里create app redis,name redis,然后查看:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
redis-76d7657885-rk6tm 1/1 Running 0 1m
$ kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
redis 1 1 1 1 2m
$ kubectl get replicaset
NAME DESIRED CURRENT READY AGE
redis-76d7657885 1 1 1 2m
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready <none> 14d v1.9.0
kubectl 应用
check redis logs并与redis进行简单交互:
$ kubectl logs redis-76d7657885-rk6tm
1:C 27 Feb 21:47:54.141 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 27 Feb 21:47:54.141 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 27 Feb 21:47:54.141 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 27 Feb 21:47:54.146 * Running mode=standalone, port=6379.
1:M 27 Feb 21:47:54.147 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 27 Feb 21:47:54.147 # Server initialized
1:M 27 Feb 21:47:54.147 * Ready to accept connections
$ kubectl exec -ti redis-76d7657885-rk6tm -- redis-cli
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
查看pod的yaml输出,object存于etcd中
$get pods redis-76d7657885-rk6tm -o yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: 2018-02-27T21:47:43Z
generateName: redis-76d7657885-
labels:
k8s-app: redis
pod-template-hash: "3283213441"
name: redis-76d7657885-rk6tm
...
刚才是通过dashboard的create button创建的pod,下面通过yaml创建pod:
$ cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: qingge
spec:
containers:
- name: nginx
image: nginx
$ kubectl create -f pod.yaml
pod "qingge" created
显示pods
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
qingge 0/1 ContainerCreating 0 6s
redis-76d7657885-rk6tm 1/1 Running 0 16m
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
qingge 1/1 Running 0 1m
redis-76d7657885-rk6tm 1/1 Running 0 17m
启动proxy:
$ kubectl proxy
Starting to serve on 127.0.0.1:8001
访问proxy:
$ curl localhost:8001
{
"paths": [
"/api",
"/api/v1",
"/apis",
"/apis/",
...
$ curl localhost:8001/api/v1
{
"kind": "APIResourceList",
"groupVersion": "v1",
"resources": [
{
"name": "bindings",
"singularName": "",
"namespaced": true,
"kind": "Binding",
"verbs": [
"create"
]
...
delete pods:
$ kubectl delete pods qingge
pod "qingge" deleted
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
redis-76d7657885-rk6tm 1/1 Running 0 28m
namespace:
$ kubectl get ns
NAME STATUS AGE
default Active 14d
kube-public Active 14d
kube-system Active 14d
$ kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
default redis-76d7657885-rk6tm 1/1 Running 0 33m
kube-system kube-addon-manager-minikube 1/1 Running 1 14d
kube-system kube-dns-54cccfbdf8-mhr9h 3/3 Running 3 14d
kube-system kubernetes-dashboard-77d8b98585-5r7qv 1/1 Running 1 14d
kube-system storage-provisioner 1/1 Running 1 14d
添加quota 限制:
$ kubectl create quota qingge --hard=pods=1
resourcequota "qingge" created
$ kubectl get quota
NAME AGE
qingge 12s
$ kubectl get quota qingge -o yaml
apiVersion: v1
kind: ResourceQuota
metadata:
creationTimestamp: 2018-02-27T22:24:08Z
name: qingge
namespace: default
resourceVersion: "2185"
selfLink: /api/v1/namespaces/default/resourcequotas/qingge
uid: ed5ec545-1c0c-11e8-aa17-08002725a271
spec:
hard:
pods: "1"
status:
hard:
pods: "1"
used:
pods: "1"
$ kubectl create -f pod.yaml
Error from server (Forbidden): error when creating "pod.yaml": pods "qingge" is forbidden: exceeded quota: qingge, requested: pods=1, used: pods=1, limited: pods=1
修改quota 把spec中的pods改为2,然后就可以添加pod了
$ kubectl edit resourcequota oreilly
resourcequota "qingge" edited
$ kubectl create -f pod.yaml
pod "qingge" created
观察删除redis后会自动被replica恢复:
$ kubectl delete pods redis-76d7657885-rk6tm
pod "redis-76d7657885-rk6tm" deleted
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
redis-76d7657885-lnmps 0/1 ContainerCreating 0 2s
redis-76d7657885-rk6tm 0/1 Terminating 0 43m
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
redis-76d7657885-lnmps 1/1 Running 0 6s
redis-76d7657885-rk6tm 0/1 Terminating 0 44m
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
redis-76d7657885-lnmps 1/1 Running 0 8s
添加nginx
$ more rs.yaml
apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
name: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx-containers
image: nginx
$ kubectl create -f rs.yaml
replicaset "nginx" created
$ kubectl get replicaset
NAME DESIRED CURRENT READY AGE
nginx 2 1 1 7s
redis-76d7657885 1 1 1 50m
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-dr4t9 1/1 Running 0 33s
redis-76d7657885-lnmps 1/1 Running 0 6m
测试删除后自动回复:
$ kubectl delete pods nginx-dr4t9
pod "nginx-dr4t9" deleted
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-g8spl 1/1 Running 0 3s
nginx-mflm7 0/1 ContainerCreating 0 2s
redis-76d7657885-lnmps 1/1 Running 0 8m
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-g8spl 1/1 Running 0 6s
nginx-mflm7 1/1 Running 0 5s
redis-76d7657885-lnmps 1/1 Running 0 8m
选择label:
$ kubectl get pods -l app=nginx
NAME READY STATUS RESTARTS AGE
nginx-g8spl 1/1 Running 0 3h
nginx-mflm7 1/1 Running 0 3h
添加label:
$ kubectl get pods -Lapp
NAME READY STATUS RESTARTS AGE APP
nginx-g8spl 1/1 Running 0 3h nginx
nginx-mflm7 1/1 Running 0 3h nginx
redis-76d7657885-lnmps 1/1 Running 0 3h
$ kubectl label pods redis-76d7657885-lnmps app=redis
pod "redis-76d7657885-lnmps" labeled
$ kubectl get pods -Lapp
NAME READY STATUS RESTARTS AGE APP
nginx-g8spl 1/1 Running 0 3h nginx
nginx-mflm7 1/1 Running 0 3h nginx
redis-76d7657885-lnmps 1/1 Running 0 3h redis
添加多个label,显示所有label
$ kubectl label pods nginx-g8spl foo=bar
pod "nginx-g8spl" labeled
$ kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-g8spl 1/1 Running 0 3h app=nginx,foo=bar
nginx-mflm7 1/1 Running 0 3h app=nginx
redis-76d7657885-lnmps 1/1 Running 0 3h app=redis,k8s-app=redis,pod-template-hash=3283213441
思考:如果此时overwrite 一个app=nginx的pod会有什么后果:
$ kubectl label pods nginx-g8spl app=foobar --overwrite
pod "nginx-g8spl" labeled
$ kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-g8spl 1/1 Running 0 3h app=foobar,foo=baz
nginx-mflm7 1/1 Running 0 3h app=nginx
nginx-mlhjs 1/1 Running 0 1m app=nginx
redis-76d7657885-lnmps 1/1 Running 0 3h app=redis,k8s-app=redis,pod-template-hash=3283213441
答:会额外多生成一个nginx pod,因为replicaset为label app=nginx定义的数量是2,所以它会始终维护数量为2
添加service:
$ cat svc.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
ports:
- name: main
port: 80
selector:
app: nginx
type: NodePort
$ kubectl create -f svc.yaml
service "nginx" created
查看并启动服务,会在浏览器中看到nginx的欢迎界面,port number是30978
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 14d
nginx NodePort 10.110.210.218 <none> 80:30978/TCP 5s
$ minikube service nginx
$ kubectl get endpoints
NAME ENDPOINTS AGE
kubernetes 10.0.2.15:8443 14d
nginx 172.17.0.6:80,172.17.0.7:80 1h
下面来个牛逼的操作,scale:
$ kubectl scale rs nginx --replicas=5
replicaset "nginx" scaled
$ kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx 5 5 5 5h
redis-76d7657885 1 1 1 5h
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-d8n5h 1/1 Running 0 13s
nginx-g8spl 1/1 Running 0 5h
nginx-mflm7 1/1 Running 0 5h
nginx-mlhjs 1/1 Running 0 1h
nginx-nc5z7 1/1 Running 0 13s
nginx-t228t 1/1 Running 0 13s
redis-76d7657885-lnmps 1/1 Running 0 5h
我已经create了一个busybox,下面通过busybox来观察nginx:
$ kubectl exec -ti busybox-7c6c97f7cb-s6trg -- /bin/sh
/ #
/ # nslookup nginx
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local Name: nginx
Address 1: 10.110.210.218 nginx.default.svc.cluster.local
访问nginx:
/ # wget 10.110.210.218
Connecting to 10.110.210.218 (10.110.210.218:80)
index.html 100% |*************************************************************************************************************************| 612 0:00:00 ETA
/ # head index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
好了,今天到此为止,更多内容下回分解
10分钟入门kubernetes(上)的更多相关文章
- Apache Shiro系列三,概述 —— 10分钟入门
一.介绍 看完这个10分钟入门之后,你就知道如何在你的应用程序中引入和使用Shiro.以后你再在自己的应用程序中使用Shiro,也应该可以在10分钟内搞定. 二.概述 关于Shiro的废话就不多说了 ...
- JavaScript 10分钟入门
JavaScript 10分钟入门 随着公司内部技术分享(JS进阶)投票的失利,先译一篇不错的JS入门博文,方便不太了解JS的童鞋快速学习和掌握这门神奇的语言. 以下为译文,原文地址:http://w ...
- kafka原理和实践(一)原理:10分钟入门
系列目录 kafka原理和实践(一)原理:10分钟入门 kafka原理和实践(二)spring-kafka简单实践 kafka原理和实践(三)spring-kafka生产者源码 kafka原理和实践( ...
- Markdown - Typora 10分钟入门 - 精简归纳
Markdown - Typora 10分钟入门 - 精简归纳 JERRY_Z. ~ 2020 / 8 / 22 转载请注明出处! 目录 Markdown - Typora 10分钟入门 - 精简归纳 ...
- [入门到吐槽系列] Webix 10分钟入门 一 管理后台制作
前言 本人是服务端程序员,同时需要兼职前端开发.常用的就是原生态的HTML.Javascript,也用过ExtJS.Layui.可是ExtJS变公司后非常难用.Layui上手还行,用过一段时间,会觉得 ...
- [入门到吐槽系列] Webix 10分钟入门 二 表单Form的使用
前言 继续接着上一篇的webix入门:https://www.cnblogs.com/zc22/p/15912342.html.今天完成剩下两个最重要的控件,表单和表格的使用.掌握了这两个,整个Web ...
- 「从零单排canal 01」 canal 10分钟入门(基于1.1.4版本)
1.简介 canal [kə'næl],译意为水道/管道/沟渠,主要用途是基于 MySQL 数据库增量日志解析,提供增量数据 订阅 和 消费.应该是阿里云DTS(Data Transfer Servi ...
- Webpack 10分钟入门
可以说现在但凡开发Single page application,webpack是一个不可或缺的工具. WebPack可以看做是一个模块加工器,如上图所示.它做的事情是,接受一些输入,经过加工产生一些 ...
- python 10分钟入门pandas
本文是对pandas官方网站上<10 Minutes to pandas>的一个简单的翻译,原文在这里.这篇文章是对pandas的一个简单的介绍,详细的介绍请参考:Cookbook .习惯 ...
随机推荐
- Mysql覆盖索引 covering index 或者 index coverage
组合索引 提到组合索引,大家都知道"最左前缀"原则.例如,创建索引 idx_name_age (name,age) ,通常情况下,where age=50 或者 where age ...
- 2017-07-02(free uptime cat /proc/cpuinfo uname lsb_release)
free -b 字节为单位显示 -k KB为单位显示,默认显示就是KB -m 以MB为单位显示 -g 以GB为单位显示 free -m free 显示说明 uptime 跟top命令第一行内容相同 c ...
- pycharm python模版样式
问题: 我想在创建新的一些python程序的时候,希望在新文件开头添加python版本声明和一些关于时间相关的模版数据 那,如何解决? 1. pycharm ---> setting ---&g ...
- List迭代过滤操作注意点
今天在写一段很简单的代码,本来以为肯定没什么问题,然后直接跑的时候,吆,简单的一个List的操作报错了.仔细一看代码,确实有问题,但是一般真的是如果稍微不小心就会犯下面这种愚蠢的操作. 这里我把代码贴 ...
- POI--HSSFCell类
用POI创建单元格,使用「HSSFCell」类 该类包含三个构造方法. protected HSSFCell(Workbook book, Sheet sheet, int row, CellVal ...
- 20165220 学习基础和C语言基础调查
# # # # 我觉得我打游戏(不知道算不算一技之长)毕竟从小学一年级就接触到了各种形形色色的游戏,讲道理其实我的游戏天赋毕竟还是很大的,从意识到感觉我觉得都比大多数人好一些,其实吧打游戏打得好也是很 ...
- @@IDENTITY详细测试
今天看数据库SQL,有发现存储过程中有使用到SCOPE_IDENTITY()这个函数,后来问了下谷歌大婶,搜到一个比较重要的博客,链接如下:https://dotblogs.com.tw/kkman0 ...
- HTML5入门要点
要点 HTML5是HTML的最新版本.通过引入心的标签.新的语义和媒体元素,同时要依赖一组支持Web应用的JavaScript库 XHTML不再是Web页面开发标准.开发人员和W3C组织觉决定还是继续 ...
- win10预览版无开始菜单解决方案
1.按下Win+R键打开“运行”程序,键入gpedit.msc 回车以打开本地组策略编辑器 2.调到图示位置将windows设置->安全设置->本地策略->安全选项->“用户账 ...
- 企业级Docker私有仓库之Harbor部署(http)
部署环境 Centos7.3 x64 docker-ce-17.06.0 docker-compose-1.15.0 Python-2.7.5(系统默认) Docker及Docker-compose安 ...