0%

Kubernetes(简称K8S)快速入门教程

之前的k8s博客我们已经把k8s的基本概念了解了一遍,现在我们用一个简单的案例来熟悉k8s的基本知识

Pod

Pod是k8s的基本操作单元,也是应用运行的载体。整个kubernetes系统都是围绕着Pod展开的。

我们来创建一个nginxapp来开始介绍Pod
新建一个nginxapp.yaml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
apiVersion: v1
kind: Pod
metadata:
name: nginxapp
labels:
app: nginxapp
spec:
containers:
- name: nginxapp
image: nginx
resources:
limits:
memory: "128Mi"
cpu: "500m"
ports:
- containerPort: 80

上述的定义文件中描述了Pod的属性和行为,其中主要的要素如下

  • apiVersion: 声明k8s的API的版本,当前是v1.
  • kind:声明 API对象的类型,这里是Pod
  • metadata:设置Pod的元数据
    • name:指定Pod的名称,注意,Pod名称必须在NameSpace内是唯一的。
    • labels: 指定kv 标志
  • spec:配置Pod的具体规格。
    • restartPolicy: 设置Pod的重启策略
    • containers:设置Pod中窗口的规格,数组形式,每一项定义一个容器。
      • name: 指定容器的名称,在Pod的定义中是唯一有
      • image: 指定容器的镜像
      • command: 设置容器的启动命令
      • resources: 设置容器启动的资源的情况

创建好这个Pod之后,使用kubectl get pod 可以查看到这个Pod的执行情况。

执行 kubectl exec 之后可以看到nginx已经正常的运行中

Service

此时的pod还不能访问到,我们需要创建一个service

我们创建一个service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
apiVersion: v1
kind: Service
metadata:
name: nginxapp
labels:
app: nginxapp
spec:
type: NodePort
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30002
selector:
app: nginxapp

从上面的yaml的配置信息可以看出 使用了 nodePort的 service协议,指定了30002访问 nginxapp 并且 nginxapp的规则是 nodePort: 30002 -> port: 80 ->targetPort: 80 并且 匹配的labels规则是 app: nginxapp 就是之前定义的Pod

创建之后访问可以看到 nodeip:30002 可以看到nginx的默认页面。

ConfigMap

  • ConfigMap API资源用来保存key-value pair配置数据,这个数据可以在pods里使用,或者被用来为像controller一样的系统组件存储配置数据。虽然ConfigMap跟Secrets类似,但是ConfigMap更方便的处理不含敏感信息的字符串。 注意:ConfigMaps不是属性配置文件的替代品。ConfigMaps只是作为多个properties文件的引用。你可以把它理解为Linux系统中的/etc目录,专门用来存储配置文件的目录。

现在我想在访问 nodeip:30002 的时候可以访问到 http://yahui.wang/的内容 只需要给nginx中添加一个代理配置

现在我们来定义 需要的 ConfigMap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
apiVersion: v1
kind: ConfigMap
metadata:
name: nginxconfs
data:
nginxconf: |
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;
events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] \"$request\" '
# '$status $body_bytes_sent \"$http_referer\" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;

#tcp_nopush on;

#keepalive_timeout 0;

keepalive_timeout 65;
server_tokens off;

#gzip on;

include conf.d/*.conf;
}
nginxconfddefaultconf: |
upstream yahui{
server yahui.wang:80 ;
keepalive 64;
}
server{
listen 80;

#charset koi8-r;

#access_log logs/host.access.log main;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Connection "";
proxy_pass http://yahui;
client_max_body_size 1000m;

}
}

我们修改nginxapp来使用定义好的ConfigMap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
apiVersion: v1
kind: Pod
metadata:
name: nginxapp
labels:
name: nginxapp
spec:
containers:
- name: nginxapp
image: nginx
resources:
limits:
memory: "128Mi"
cpu: "500m"
ports:
- containerPort: 80
volumeMounts:
- name: nginx-config
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
- name: nginx-config
mountPath: /etc/nginx/conf.d/default.conf
subPath: default.conf
volumes:
- name: nginx-config
configMap:
name: nginxconfs
items:
- key: nginxconf
path: nginx.conf
- key: nginxconfddefaultconf
path: default.conf

执行 kubectl repleace 替换 nginxapp Pod

访问 nodeip:30002可以看到页面内容和 http://yahui.wang 的内容一样

多个Service

接下来我们创建一个自定义的Service来让nginx转发http数据

定义 Dockerfile

1
2
3
4
5
FROM node:7

ADD app.js /app.js

ENTRYPOINT ["node","app.js"]

在Dockerfile 同目录创建 app.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const   http=require("http");
const os=require("os");
console.log("yahui server staring...");
var header=function (request,response) {
console.log("Recoive request from "+request.connection.remoteAddress)
response.writeHead(200);
response.end("you have hit "+os.hostname()+"\n");
}

var www=http.createServer(header);
www.listen(8080);const http=require("http");
const os=require("os");
console.log("yahui server staring...");
var header=function (request,response) {
console.log("Recoive request from "+request.connection.remoteAddress)
response.writeHead(200);
response.end("you have hit "+os.hostname()+"\n");
}

var www=http.createServer(header);
www.listen(8080);

在执行 docker build -t yahuiwong/yahui .
生成 yahuiwong/yahui:latest 镜像
然后执行 docker push yahuiwong/yahui:latest

接下来使用这个镜像创建 Pod和Service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
apiVersion: v1
kind: Pod
metadata:
name: yahuiapp
labels:
name: yahuiapp
spec:
containers:
- name: yahuiapp
image: yahuiwong/yahui
resources:
limits:
memory: "128Mi"
cpu: "500m"
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: yahuiapp1
labels:
app: yahuiapp1
spec:
ports:
- protocol: TCP
port: 8080
selector:
name: yahuiapp

并且 修改 ConfigMap中 nginxconfddefaultconf 中的upstream 为 server yahuiapp1:8080 ;

创建 pod和service之后并且更新nginxapp之后 访问nodeip:30002 可以看到 Pod yahuiapp的内容

ReplicationController

ReplicationController是k8s的另外一个核心的概念。ReplicationController保证应用可以在k8s中可以持续运行。用以确保在任何时候在k8s中有指定数量的Pod实例运行。当然 在ReplicationController的基础上可以做一些高级的事情,比如 弹性伸缩、滚动升级等。

接下来我们定义一个ReplicationController 确保 yahuiapp有两个副本运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
apiVersion: v1
kind: ReplicationController
metadata:
name: yahuiapp
labels:
name: yahuiapp
spec:
replicas: 2
selector:
app: yahuiapp
template:
metadata:
labels:
app: yahuiapp
name: yahuiapp
spec:
containers:
- name: yahuiapp
image: yahuiwong/yahui
resources:
limits:
memory: "128Mi"
cpu: "500m"
ports:
- containerPort: 8080

通过上面的配置信息可以看到 template中的内容和之前的Pod的内容一样
ReplicationController用到的主要配置是

  • spec: 配置ReplicationController的具体规格
    • replicas:设置ReplicationController 控制的Pod的副本数目
    • selector:制定ReplicationController的Label Selector来匹配Pod的Label
    • template:设置Pod的模板,同Pod的定义一致。

创建ReplicationController之后可以看到Pod的名称后五位是随机字符串

访问 nodeip:30002多刷新几次可以看到

you have hit yahuiapp-x7hnq 或者 you have hit yahuiapp-1xusp

证明此时运行状态正常,我们的服务部署成功。