创建 pod
cd ~
vi pod-demo.yaml
# 内容如下
apiVersion: v1
kind: Pod
metadata:
name: pod-demo
namespace: default
labels:
app: myapp
tier: frontend
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
- name: busybox
image: busybox:latest
command:
- "/bin/sh"
- "-c"
- "sleep 3600"
# 使用 kubectl create 创建 pod
kubectl create -f pod-demo.yaml
# 这个 pod 里面有两个容器,一个是 myapp,一个是 busybox
# 查看日志
kubectl logs pod-demo myapp
# 进入 pod 中的容器内部
kubectl exec -it pod-demo -c myapp -- /bin/sh
创建 deployment
vi deploy-demo.yaml
# 内容如下
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: myapp
release: canary
template:
metadata:
labels:
app: myapp
release: canary
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
# 使用 kubectl apply 创建 deployment
kubectl apply -f deploy-demo.yaml
# 通过 kubectl describe 查看 deployment 信息
kubectl describe deploy myapp-deploy
创建 DeamonSet
DeamonSet 用于每个 Node 最多只运行一个 Pod 副本的场景。正如其名称所揭示的,DaemonSet 通常用于运行 daemon。如果一个 yaml 文件中需要定义两种资源需要使用 --- 进行分割。
vi ds-demo.yaml
# 内容如下
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: redis
role: logstor
template:
metadata:
labels:
app: redis
role: logstor
spec:
containers:
- name: redis
image: redis:4.0-alpine
ports:
- name: redis
containerPort: 6379
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: myapp-ds
namespace: default
spec:
selector:
matchLabels:
app: filebeat
release: stable
template:
metadata:
labels:
app: filebeat
release: stable
spec:
containers:
- name: filebeat
image: ikubernetes/filebeat:5.6.5-alpine
env:
- name: REDIS_HOST
value: redis.default.svc.cluster.local
- name: REDIS_LOG_LEVEL
value: info
kubectl apply -f ds-demo.yaml
kubectl expose deployment redis --port=6379
创建 service
vi myapp-svc.yaml
# 内容如下:
apiVersion: v1
kind: Service
metadata:
name: myapp
namespace: default
spec:
selector:
app: myapp
release: canary
clusterIP: 10.99.99.99
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 30080
kubectl apply -f myapp-svc.yaml
# 说明
port :service 暴露在 cluster ip上的端口
targetPort :targetPort 是 pod 上的端口
nodePort : 暴露在集群物理节点上 node 的端口
可以使用其他机器访问 kubernets 上的 30080 端口来访问页面
eg:
192.168.31.205:30080
192.168.31.206:30080
192.168.31.207:30080