此文为学习《Kubernetes权威指南》的相关笔记
个人思路:
根据docker的设计理念,容器作为一个运行单位,在使用上应当秉持“一个容器只运行一项功能”的设计原则,这个原则让容器具有高复用性和低耦合的特征,但在工业应用场景中,以这个原则所设计的服务并不能够独立看作一项完整的“微服务”进行调度(毕竟“一项功能”所能实现的事情有限),在K8s的架构中,以Pod(豆荚)作为一个调度和使用的基本单位,一个pod中大于等于一个的容器作为同一个单元,共同组成一个整体进行运行,Pod内部的容器自然有相互之间的松散耦合关系,最终提供对外调用的接口。
于此同时,Pod接收ReplicationControll、ReplicaSet、Deployment、job等资源对象的控制,可以动态生成和增删其自身的副本,这一控制由Label机制实现,在对外服务方面,Pod可以建立一个Service资源对象,获得统一的ClusterIP,统一对外提供服务,这是K8S架构中“微服务”这一慨念的具体实现,后面将继续学习这些机制,增强对其理解。
下面运行一个由多个容器组成一个Pod对外提供服务的例子,在这个例子中,Pod里包含了一个PHP前台容器和一个后台Redis数据库容器,在PHP容器设计时就指定好未来访问Redis数据库的目标端口,再在Pod建立时指定好Redis容器的端口参数一致,即实现了Pod内部两个容器的耦合,该Pod通过PHP容器开放端口对外提供服务。
# vim frontend-localredis-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: redis-php
labels:
name: redis-php
spec:
containers: #定义前台容器,开放80端口
- name: frontend
image: kubeguide/guestbook-php-frontend:localredis
ports:
- containerPort: 80
- name: redis #定义Redis容器,开放6379端口
image: kubeguide/redis-master
ports:
- containerPort: 6379
# kubectl create -f frontend-localredis-pod.yaml
pod/redis-php created
# kubectl get pods
NAME READY STATUS RESTARTS AGE
redis-php 2/2 ContainerCreating 0 23m
# kubectl describe pod redis-php
Name: redis-php
Namespace: default #所在命名空间
Priority: 0 #优先权
Node: xu.node1/192.168.31.189 #运行节点/IP
Start Time: Mon, 09 Dec 2019 22:51:31 +0800
Labels: name=redis-php #Label定义
Annotations:
Status: Running #运行状态
IP: 10.44.0.1 #Pod-IP
IPs:
IP: 10.44.0.1
Containers: #容器信息
frontend: #容器1:frontend
Container ID: docker://acc90df5e0e30039d8e0b81fea3f8efe590064da1539d8928060aea3c6091239
Image: kubeguide/guestbook-php-frontend:localredis
Image ID: docker-pullable://kubeguide/guestbook-php-frontend@sha256:37c2c1dcfcf0a51bf9531430fe057bcb1d4b94c64048be40ff091f01e384f81e
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Mon, 09 Dec 2019 22:51:32 +0800
Ready: True
Restart Count: 0
Environment:
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-nrtb9 (ro) #卷挂载点
redis: #容器2:redis容器
Container ID: docker://028e960b3eb927cb2864422edf977829342abed341e5ddec94184a1cef3df812
Image: kubeguide/redis-master
Image ID: docker-pullable://kubeguide/redis-master@sha256:e11eae36476b02a195693689f88a325b30540f5c15adbf531caaecceb65f5b4d
Port: 6379/TCP
Host Port: 0/TCP
State: Running
Started: Mon, 09 Dec 2019 22:52:30 +0800
Ready: True
Restart Count: 0
Environment:
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-nrtb9 (ro)
Conditions: #Pod整体状态
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-nrtb9:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-nrtb9
Optional: false
QoS Class: BestEffort
Node-Selectors:#节点选择器,用于指定Pod运行节点
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s #耐性概念 和污点taint对应
node.kubernetes.io/unreachable:NoExecute for 300s
Events: #Pod运行事件
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduleddefault-scheduler Successfully assigned default/redis-php to xu.node1
Normal Pulled 28m kubelet, xu.node1 Container image "kubeguide/guestbook-php-frontend:localredis" already present on machine
Normal Created 28m kubelet, xu.node1 Created container frontend
Normal Started 28m kubelet, xu.node1 Started container frontend
Normal Pulling 28m kubelet, xu.node1 Pulling image "kubeguide/redis-master"
Normal Pulled 27m kubelet, xu.node1 Successfully pulled image "kubeguide/redis-master"
Normal Created 27m kubelet, xu.node1 Created container redis
Normal Started 27m kubelet, xu.node1 Started container redis
访问成功
在真正运行该Pod的Node上,查看正在运行的容器,找到前端容器:
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
028e960b3eb9 kubeguide/redis-master "redis-server /etc/r…" 48 minutes ago Up 48 minutes k8s_redis_redis-php_default_3f9e6bb4-731f-4132-8597-871f4ce863fd_0
acc90df5e0e3 d014f67384a1 "apache2-foreground" 49 minutes ago Up 49 minutes k8s_frontend_redis-php_default_3f9e6bb4-731f-4132-8597-871f4ce863fd_0
8fe4d7daff6c k8s.gcr.io/pause:3.1 "/pause" 49 minutes ago Up 49 minutes k8s_POD_redis-php_default_3f9e6bb4-731f-4132-8597-871f4ce863fd_0
def4d95388db 174e0e8ef23d "/home/weave/launch.…" About an hour ago Up About an hour k8s_weave_weave-net-xwm7d_kube-system_3cb47f34-93d6-4462-bc9a-061100a947b7_23
fa7c600684b1 5105e13e253e "/usr/bin/launch.sh" 2 hours ago Up 2 hours k8s_weave-npc_weave-net-xwm7d_kube-system_3cb47f34-93d6-4462-bc9a-061100a947b7_5
a7e5d13dbdc5 c21b0c7400f9 "/usr/local/bin/kube…" 2 hours ago Up 2 hours k8s_kube-proxy_kube-proxy-lpbbl_kube-system_bdf76648-a240-47f3-947d-07895ebced8b_5
eefa6d29e22a k8s.gcr.io/pause:3.1 "/pause" 2 hours ago Up 2 hours k8s_POD_weave-net-xwm7d_kube-system_3cb47f34-93d6-4462-bc9a-061100a947b7_5
a8fad91c6afb k8s.gcr.io/pause:3.1 "/pause" 2 hours ago Up 2 hours k8s_POD_kube-proxy-lpbbl_kube-system_bdf76648-a240-47f3-947d-07895ebced8b_5
可以使用docker exec -it
下面进入前端容器:
# docker exec -it acc90df5e0e3 /bin/bash
查看数据库访问配置文件guestbook.php,可以清楚的看到,在该容器建立时,就设定好了未来访问数据库的方式。
root@redis-php:/var/www/html# cat guestbook.php
set_include_path('.:/usr/local/lib/php');
error_reporting(E_ALL);
ini_set('display_errors', 1);require 'Predis/Autoloader.php';
Predis\Autoloader::register();
if (isset($_GET['cmd']) === true) {
$host = 'localhost';
if (getenv('REDIS_HOST') && strlen(getenv('REDIS_HOST')) > 0 ) {
$host = getenv('REDIS_HOST');
}
header('Content-Type: application/json');
if ($_GET['cmd'] == 'set') {
$client = new Predis\Client([
'scheme' => 'tcp',
'host' => $host,
'port' => 6379,
]);$client->set($_GET['key'], $_GET['value']);
print('{"message": "Updated"}');
} else {
$host = 'localhost';
if (getenv('REDIS_HOST') && strlen(getenv('REDIS_HOST')) > 0 ) {
$host = getenv('REDIS_HOST');
}
$client = new Predis\Client([
'scheme' => 'tcp',
'host' => $host,
'port' => 6379,
]);$value = $client->get($_GET['key']);
print('{"data": "' . $value . '"}');
}
} else {
phpinfo();
} ?>
可以以这种方式探究更多运行中镜像信息
查看环境变量:
root@redis-php:/var/www/html# env
TOMCAT_SERVICE_PORT_8080_TCP_PORT=8080
HOSTNAME=redis-php
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP_PORT=443
TERM=xterm
PHP_INI_DIR=/usr/local/etc/php
TOMCAT_SERVICE_PORT_8080_TCP_ADDR=10.104.104.195
TOMCAT_SERVICE_PORT_8080_TCP_PROTO=tcp
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_HOST=10.96.0.1
TOMCAT_SERVICE_SERVICE_HOST=10.104.104.195
TOMCAT_SERVICE_PORT_8080_TCP=tcp://10.104.104.195:8080
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
GPG_KEYS=0BD78B5F97500D450838F95DFE857D9A90D90EC1 6E4F6AB321FDC07F2C332E3AC2BF0BC433CFC8B3
PWD=/var/www/html
TOMCAT_SERVICE_SERVICE_PORT=8080
SHLVL=1
HOME=/root
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_SERVICE_PORT_HTTPS=443
PHP_EXTRA_BUILD_DEPS=apache2-dev
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
PHP_VERSION=5.6.12
PHP_EXTRA_CONFIGURE_ARGS=--with-apxs2
TOMCAT_SERVICE_PORT=tcp://10.104.104.195:8080
_=/usr/bin/env
查看hosts文件
root@redis-php:/var/www/html# cat /etc/hosts
# Kubernetes-managed hosts file.
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
fe00::0 ip6-mcastprefix
fe00::1 ip6-allnodes
fe00::2 ip6-allrouters
10.44.0.1 redis-php