9-4 深入Pod-----pod相关的点点滴滴(pod-network,configmap,hostname,hostNetwork)

深入Pod

7-pod

设计思想

  Pod是最小调度单位

  本质还是容器的隔离

  Pause容器

容器只能运行一个服务,是单进程的。

1、pod-network

kubectl apply -f pod-network.yaml
# 查看有两 REDAY启动两个   # 部署到w2   
kubectl get pods -o wide 

 在w2上查看

docker ps|grep network

进入容器

docker exec -it 1abf0453d138  sh

netstat -ntlp
# 容器之中相互访问网络是通的
wget localhost:8080/hello?name=fffff
# 查看IP
ifconfig 

# 进入另一容器
docker exec -it 876115a652fc sh
ifconfig   # 查看ip是一样的,共享IP

2、pod-volume

 共享目录

kubectl apply -f pod-volume.yaml 
kubectl get pods -o wide
# 运行机器
docker ps|grep volume
# 进入dubbo容器
docker exec -it 4bad00e5f65d sh
# 在 /shared-dubbo/   创建: touch 123123

# 进入web容器的共享目录。 /shared-web    同步了  123123 内容

 查看 host

# 查看hosts和主机无关,保持一一致的,不能在容器上修改hosts文件
docker exec -it 4b4184cce642  cat /etc/hosts   
docker exec -it 4bad00e5f65d  cat /etc/hosts   

配置hostname域名 。增加配置  hostAliases

kubectl apply -f pod-volume.yaml 

查看host已经加上了

docker exec -it 6932b1752b07 cat /etc/hosts
docker exec -it 747d3ac98fd8 cat /etc/hosts

配置

hostNetwork: true    # 增是否使用宿主机的网络

hostPID: true    # 增使用宿主机的PID namespace

docker exec -it a178d98baafb sh

# 进程和端口包括宿主机的
ps
nestart

 增容器启动停止时执行命令 

 lifecycle:

 kubectl apply -f pod-volume.yaml 
#  登入web容器,查看日志
docker exec -it 8291babf900e sh

# 可以查看启动是插入的数据日志
tail -f /var/log/messages 

pod的几种状态

Pendding
containerCreating
Running
Succeeded
Failed 
Ready
CreashLoopBakOff     # 循环补偿,启动失败状态
Unknown  # 未知状态

3、ProjectedVolume 项目

 Secret 配置

kubectl get secret
# 用于和apiserver交互授权的。查看定义。
kubectl get secret default-token-blxt7 -o yaml

# 查看pods. secret用法。 kubectl get pods springboot-web-demo-7f8ff6b8bc-qcjsz -o yaml """ # 定义 volumes: defaultMode: 420 #权限
# 挂载位置 volumeMounts
"""
# 进入容器,查看挂载目录,为bas64解密。
docker exec -it a178d98baafb sh

 创建、使用

#### 创建。 存储到etcd中, 在pod中使用 #
kubectl apply -f secret.yaml 
#### 使用。在pod使用
kubectl apply -f pod-secret.yaml 

# 查看部署位置
kubectl get pods -o wide

# 进入容器
docker ps|grep secret
docker exec -it cfe77e3942d0 sh

cd db-secret/   # 查看secret名称
cat username

configmap配置

配置文件方式

#### 创建,指定文件
kubectl create configmap web-game --from-file game.properties 
# 查看
kubectl get cm web-game -o yaml

#### 使用,在pod
kubectl apply -f pod-game.yaml 

#查看现有的配置文件
cd /etc/config/game 
game.properties # 和上面创建的一样的。 程序可以通过配置文件拿到一些属性值

# 可以修改配置
kubectl edit cm web-game

# 登录容器查看修改后的配置
docker exec -it 42fa6b0a3509 sh 
cat /etc/config/game/game.properties 

 环境变量方式

#### 创建。 配置文件形式创建
kubectl apply -f configmap.yaml 
#### 使用。 通过环境变量使用
kubectl apply -f pod-env.yaml
# 进入容器 docker exec -it 91cd70a02112 sh / # env|grep LOG # 查看环境环境变量已经设置到了容器中。程序可以通过环境变量使用了

cmd方式

kubectl apply -f pod-cmd.yaml 
docker exec -it 91cd70a02112 sh

downwardapi配置

让程序能取到pod对象本身的一些信息

kubectl apply -f pod-downwardapi.yaml 

docker exec -it d99ae589e81e  sh
cd /etc/podinfo

# 查看拿到pod定义信息

configmap 替换https证书示例

当前目录下包含 server.crt,server.key

#!/bin/bash
kubectl create configmap server-crt --from-file=server.crt -n insightone
kubectl create configmap server-key --from-file=server.key -n insightone
server.sh
apiVersion: v1
kind: Service
metadata:
  name: insightone-ui-svc # 资源名称
  namespace: insightone # 命名空间
spec:
  type: NodePort # Service的类型,指定Service的访问方式
  ports: #端口信息
    - protocol: TCP
      port: 443 #service端口,容器间,服务调用的端口
      targetPort: 443 #pod,容器暴露的端口,与Dockerfile暴露端口保持一致
      nodePort: 32100 #指定绑定的node的端口(默认的取值范围是:30000-32767), 如果不指定,会默认分配
  sessionAffinity: None # session亲和性,支持ClientIP、None两个选项,ClientIp时需打开下面的选项
#  sessionAffinityConfig:
#    clientIP:
#      timeoutSeconds: 10
  selector:  # 标签选择器,用于确定当前Service代理那些Pod
    app: insightone-ui

---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: insightone-ui
  namespace: insightone
spec:
  serviceName: insightone-ui-svc
  replicas: 1
  selector:  # 选择器,通过它指定该控制器管理哪些pod
    matchLabels:
      app: insightone-ui  #下面pod模板设置的标签
  template:  # 模板,当副本数量不足时,会根据下面的模板创建pod副本
    metadata:
      labels:
        app: insightone-ui #给pod设置标签
    spec:
      containers:
        - name: insightone-ui
          #image: harbor.insightone.cn/insightone-ui/insightone-ui:v1.4.3-test-202210241524-eecb5894
          image: harbor.insightone.cn/insightone-ui/insightone-ui:v1.4.3-test-202211041205-4a52134b
          ports:
            - containerPort: 443
              protocol: TCP
          volumeMounts:
            - name: https-conf
              mountPath: "/usr/local/openresty/nginx/conf/conf.d/https.conf"
              subPath: https.conf
            - name: server-crt
              mountPath: "/usr/local/openresty/nginx/ssl/http/server.crt"
              subPath: server.crt
            - name: server-key
              mountPath: "/usr/local/openresty/nginx/ssl/http/server.key"
              subPath: server.key
#          resources: # 资源配额
#            limits:
#              memory: "4G"
#              cpu: "1" # CPU限制,单位是core数
#            requests: # 设置容器需要的最小资源
#              cpu: "1"  # CPU限制,单位是core数
#              memory: "2G"
      volumes:
        - name: https-conf
          configMap:
            name: https-conf
        - name: server-crt
          configMap:
            name: server-crt
        - name: server-key
          configMap:
            name: server-key
insightone-ui.yml

end...

posted @ 2021-03-25 18:22  王竹笙  阅读(113)  评论(0编辑  收藏  举报