k8s官方案例练习-公开外部 IP 地址以访问集群中应用程序

先准备镜像:

[[email protected] node]# ls
Dockerfile  server.js
[[email protected] node]# cat Dockerfile
FROM node:6.14.2
EXPOSE 8080
COPY server.js .
CMD node server.js
[[email protected] node]# cat server.js
var http = require(‘http‘);

var handleRequest = function(request, response) {
  console.log(‘Received request for URL: ‘ + request.url);
  response.writeHead(200);
  response.end(‘Hello World!‘);
};
var www = http.createServer(handleRequest);
www.listen(8080);
[[email protected] node]#

build 镜像

[[email protected] node]# docker build -t hello-world .
Sending build context to Docker daemon  3.072kB
Step 1/4 : FROM node:6.14.2
6.14.2: Pulling from library/node
3d77ce4481b1: Pull complete
7d2f32934963: Pull complete
0c5cf711b890: Pull complete
9593dc852d6b: Pull complete
4e3b8a1eb914: Pull complete
ddcf13cc1951: Pull complete
2e460d114172: Pull complete
d94b1226fbf2: Pull complete
Digest: sha256:62b9d88be259a344eb0b4e0dd1b12347acfe41c1bb0f84c3980262f8032acc5a
Status: Downloaded newer image for node:6.14.2
 ---> 00165cd5d0c0
Step 2/4 : EXPOSE 8080
 ---> Running in 960b23083ba5
Removing intermediate container 960b23083ba5
 ---> 0f92fce429ef
Step 3/4 : COPY server.js .
 ---> b2d70b5cead9
Step 4/4 : CMD node server.js
 ---> Running in 598d107259b9
Removing intermediate container 598d107259b9
 ---> 931d59ffd088
Successfully built 931d59ffd088
Successfully tagged hello-world:latest

更改镜像名再推上仓库

[[email protected] node]# docker tag hello-world 192.168.9.14/public/hello-world
[[email protected] node]# docker push 192.168.9.14/public/hello-world
The push refers to repository [192.168.9.14/public/hello-world]#更改配置

[[email protected] node]# vi /etc/docker/
daemon.json key.json
[[email protected] node]# vi /etc/docker/daemon.json
[[email protected] node]# systemctl restart docker

[[email protected] node]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://8l7c94kc.mirror.aliyuncs.com"],
"insecure-registries": [
"192.168.9.14"
]
}

登陆并推上去

[[email protected] node]# docker login 192.168.9.14
Username: admin
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[[email protected] node]# docker push 192.168.9.14/public/hello-world
The push refers to repository [192.168.9.14/public/hello-world]
8e98d6b5cc41: Pushed
aeaa1edefd60: Pushed
6e650662f0e3: Pushed
8c825a971eaf: Pushed
bf769027dbbd: Pushed
f3693db46abb: Pushed
bb6d734b467e: Pushed
5f349fdc9028: Pushed
2c833f307fd8: Pushed
latest: digest: sha256:b05a2e511c8ef72e4f9783e16f6f4e76f224aed3b69be5efce0ddecffaac0d8c size: 2214

为一个在五个 pod 中运行的应用程序创建服务

在集群中运行 Hello World 应用程序:

[[email protected] node]# kubectl run hello-world --replicas=5 --labels="run=load-balancer-example" --image=192.168.9.14/public/hello-world  --port=8080
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/hello-world created
[[email protected] node]# kubectl get pods
NAME                         READY   STATUS              RESTARTS   AGE
hello-world-c66bdd88-2kcxc   0/1     ContainerCreating   0          22s
hello-world-c66bdd88-gv82g   0/1     ContainerCreating   0          22s
hello-world-c66bdd88-l2b74   0/1     ContainerCreating   0          22s
hello-world-c66bdd88-s7jpk   0/1     ContainerCreating   0          22s
hello-world-c66bdd88-vpjnt   0/1     ContainerCreating   0          22s
kuard-5cd647675b-65cwg       1/1     Running             0          2d
kuard-5cd647675b-65r9c       1/1     Running             0          2d
kuard-5cd647675b-f9r9f       1/1     Running             0          2d
nginx                        1/1     Running             0          46h
nginx-78b75497b7-rpt8t       1/1     Running             0          23h

显示 显示有关 ReplicaSet 对象的信息

[[email protected] node]# kubectl describe replicasets
Name:           hello-world-c66bdd88
Namespace:      default
Selector:       pod-template-hash=c66bdd88,run=load-balancer-example
Labels:         pod-template-hash=c66bdd88
                run=load-balancer-example
Annotations:    deployment.kubernetes.io/desired-replicas: 5
                deployment.kubernetes.io/max-replicas: 7
                deployment.kubernetes.io/revision: 1
Controlled By:  Deployment/hello-world
Replicas:       5 current / 5 desired
Pods Status:    5 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  pod-template-hash=c66bdd88
           run=load-balancer-example
  Containers:
   hello-world:
    Image:        192.168.9.14/public/hello-world
    Port:         8080/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age    From                   Message
  ----    ------            ----   ----                   -------
  Normal  SuccessfulCreate  9m39s  replicaset-controller  Created pod: hello-world-c66bdd88-l2b74
  Normal  SuccessfulCreate  9m39s  replicaset-controller  Created pod: hello-world-c66bdd88-vpjnt
  Normal  SuccessfulCreate  9m39s  replicaset-controller  Created pod: hello-world-c66bdd88-gv82g
  Normal  SuccessfulCreate  9m39s  replicaset-controller  Created pod: hello-world-c66bdd88-s7jpk
  Normal  SuccessfulCreate  9m38s  replicaset-controller  Created pod: hello-world-c66bdd88-2kcxc

Name:           kuard-5cd647675b
Namespace:      default
Selector:       app=kuard,pod-template-hash=5cd647675b
Labels:         app=kuard
                pod-template-hash=5cd647675b
Annotations:    deployment.kubernetes.io/desired-replicas: 3
                deployment.kubernetes.io/max-replicas: 4
                deployment.kubernetes.io/revision: 1
Controlled By:  Deployment/kuard
Replicas:       3 current / 3 desired
Pods Status:    3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=kuard
           pod-template-hash=5cd647675b
  Containers:
   kuard:
    Image:        192.168.9.14/library/kuard-amd64:1
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:           <none>

Name:           nginx-78b75497b7
Namespace:      default
Selector:       app=nginx,pod-template-hash=78b75497b7
Labels:         app=nginx
                pod-template-hash=78b75497b7
Annotations:    deployment.kubernetes.io/desired-replicas: 1
                deployment.kubernetes.io/max-replicas: 2
                deployment.kubernetes.io/revision: 1
Controlled By:  Deployment/nginx
Replicas:       1 current / 1 desired
Pods Status:    1 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=nginx
           pod-template-hash=78b75497b7
  Containers:
   nginx:
    Image:        nginx:1.17.7
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:           <none>
[[email protected] node]# kubectl get replicasets
NAME                   DESIRED   CURRENT   READY   AGE
hello-world-c66bdd88   5         5         5       9m47s
kuard-5cd647675b       3         3         3       2d1h
nginx-78b75497b7       1         1         1       23h

创建公开 deployment 的 Service 对象

[[email protected] node]# kubectl expose deployment hello-world --type=LoadBalancer --name=my-service
service/my-service exposed

[[email protected] node]# kubectl get services my-service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-service LoadBalancer 10.108.42.200 <pending> 8080:30798/TCP 58s

获取 service 详情

[[email protected] node]# kubectl describe services my-service
Name:                     my-service
Namespace:                default
Labels:                   run=load-balancer-example
Annotations:              <none>
Selector:                 run=load-balancer-example
Type:                     LoadBalancer
IP:                       10.108.42.200
Port:                     <unset>  8080/TCP
TargetPort:               8080/TCP
NodePort:                 <unset>  30798/TCP
Endpoints:                10.244.3.13:8080,10.244.3.14:8080,10.244.4.15:8080 + 2 more...
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

查看

[[email protected] node]# kubectl get pods --output=wide
NAME                         READY   STATUS    RESTARTS   AGE    IP            NODE       NOMINATED NODE   READINESS GATES
hello-world-c66bdd88-2kcxc   1/1     Running   0          21m    10.244.4.17   worker01   <none>           <none>
hello-world-c66bdd88-gv82g   1/1     Running   0          21m    10.244.3.14   worker02   <none>           <none>
hello-world-c66bdd88-l2b74   1/1     Running   0          21m    10.244.3.13   worker02   <none>           <none>
hello-world-c66bdd88-s7jpk   1/1     Running   0          21m    10.244.4.15   worker01   <none>           <none>
hello-world-c66bdd88-vpjnt   1/1     Running   0          21m    10.244.4.16   worker01   <none>           <none>
kuard-5cd647675b-65cwg       1/1     Running   0          2d1h   10.244.4.2    worker01   <none>           <none>
kuard-5cd647675b-65r9c       1/1     Running   0          2d1h   10.244.4.5    worker01   <none>           <none>
kuard-5cd647675b-f9r9f       1/1     Running   0          2d1h   10.244.4.4    worker01   <none>           <none>
nginx                        1/1     Running   0          47h    10.244.3.10   worker02   <none>           <none>
nginx-78b75497b7-rpt8t       1/1     Running   0          24h    10.244.4.14   worker01   <none>           <none>
[[email protected] node]#
[[email protected] node]#
[[email protected] node]# curl 192.168.9.15:30798
Hello World![[email protected] node]#

清理现场

[[email protected] node]# kubectl delete services my-service
service "my-service" deleted
[[email protected] node]# kubectl delete deployment hello-world
deployment.apps "hello-world" deleted

原文地址:https://www.cnblogs.com/jackluo/p/12228722.html

时间: 2024-08-29 17:17:12

k8s官方案例练习-公开外部 IP 地址以访问集群中应用程序的相关文章

【K8S学习笔记】Part1:使用端口转发访问集群内的应用

本文介绍如何使用kubectl port-forward命令连接K8S集群中运行的Redis服务.这种连接方式有助于数据库的调试工作. 注意:本文针对K8S的版本号为v1.9,其他版本可能会有少许不同. 0x00 准备工作 在进行该操作之前,需要满足以下条件: 需要有一个K8S集群,并且配置好了kubectl命令行工具来与集群通信.如果未准备好集群,那么你可以使用Minikube创建一个K8S集群,或者你也可以使用下面K8S环境二者之一: Katacoda Play with Kubernete

实操教程丨如何在K8S集群中部署Traefik Ingress Controller

注:本文使用的Traefik为1.x的版本 在生产环境中,我们常常需要控制来自互联网的外部进入集群中,而这恰巧是Ingress的职责. Ingress的主要目的是将HTTP和HTTPS从集群外部暴露给该集群中运行的服务.这与Ingress控制如何将外部流量路由到集群有异曲同工之妙.接下来,我们举一个实际的例子来更清楚的说明Ingress的概念. 首先,想象一下在你的Kubernetes集群中有若干个微服务(小型应用程序之间彼此通信).这些服务能够在集群内部被访问,但我们想让我们的用户从集群外部也

Redis3.0集群crc16算法php客户端实现方法(php取得redis3.0集群中redis数据所在的redis分区插槽,并根据分区插槽取得分区所在redis服务器地址)

数据分区        Redis集群将数据分区后存储在多个节点上,即不同的分区存储在不同的节点上,每个节点可以存储多个分区.每个分区在Redis中也被称为"hash slot",Redis集群中总共规划了16384个分区. 例如:当集群中有3个节点时,节点A将包含0-5460分区,节点B将包含5461-10922分区,节点C将包含10923-16383分区. 每个key将会存储到一个唯一的分区中,每个分区其实就是一组key的集合,两者对应关系为:key的CRC16校验码%16384=

mysql数据库可以远程连接或者说用IP地址可以访问

mysql数据库可以远程连接或者说用IP地址可以访问 一般情况不建议直接修改root的权限, 先看下,自己mysql数据库的用户级权限 mysql -u root -p----->用root登陆   use mysql------->切换到mysql数据库(这个mysql是数据库的名字,---->安装的时候系统自带的吧) 可以看到我的是这样的,至于为什么有2个root我还不太清楚,我记得mysql安装的时候有个选项是---(是否可以远程访问)可能第一个root是这个作用吧,而我们常用的是

Java 域名能访问IP地址不能访问的问题.

公司GPS信息从GPS服务商那里获取.域名  http://ips2.huoyunren.com/ . 但是最近服务器DNS一直出错,要求接口访问从域名换成IP地址. ping 上面的网址 可以得到IP地址. 奇怪的事情发生了!!! 用域名可以访问,但是通过IP访问就报404错误.类似的情况在 淘宝网(www.taobao.com)上也出现过. 程序拿到域名后,去DNS服务器解析成IP地址,之后根据IP地址访问.这样的话,为什么根据IP地址不能访问网站了呢? 打开Google浏览器,F12进入调

java struts2入门学习实例--将客户端IP地址和访问方式输出到浏览器

实例1:实现客户端IP地址和访问方式输出到浏览器. IpAction.java package com.amos.web.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSup

LVS集群中的IP负载均衡技术

章文嵩 ([email protected]) 转自LVS官方参考资料 2002 年 4 月 本文在分析服务器集群实现虚拟网络服务的相关技术上,详细描述了LVS集群中实现的三种IP负载均衡技术(VS/NAT.VS/TUN和VS/DR)的工作原理,以及它们的优缺点. 1.前言在 前面文章中,讲述了可伸缩网络服务的几种结构,它们都需要一个前端的负载调度器(或者多个进行主从备份).我们先分析实现虚拟网络服务的主要技术,指出 IP负载均衡技术是在负载调度器的实现技术中效率最高的.在已有的IP负载均衡技术

k8s集群中的存储持久化概述

存储分类:直连式存储,集中式共享存储,分布式存储文件存储,块存储,对象存储DAS,NAS,SANDAS属于直连式存储,将存储设备通过SCSI接口或者光纤通道直接和主板连接,不能实现数据共享NAS和SAN属于集中式共享存储NAS使用NFS和CIFS(原来叫SMB,微软的)协议SAN分为FC SAN和IP SANIP SAN使用iSCSI技术NFS实现linux之间共享,smaba基于CIFS协议,实现linux和windows之间文件共享ceph属于分布式共享系统 k8s集群中支持的持久存储主要包

k8s集群中的EFK日志搜集系统

Kubernetes 集群本身不提供日志收集的解决方案,一般来说有主要的3种方案来做日志收集:1.在每个节点上运行一个 agent 来收集日志由于这种 agent 必须在每个节点上运行,所以直接使用 DaemonSet 控制器运行该应用程序即可这种方法也仅仅适用于收集输出到 stdout 和 stderr 的应用程序日志简单来说,本方式就是在每个node上各运行一个日志代理容器,对本节点/var/log和 /var/lib/docker/containers/两个目录下的日志进行采集2.在每个