Skip to content

Commit 83fb161

Browse files
authored
Merge pull request #9 from tanakaryo/how-to-use-k8s
How to use k8s
2 parents d6698f3 + eaad9aa commit 83fb161

File tree

13 files changed

+326
-0
lines changed

13 files changed

+326
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM nginx:alpine
2+
COPY index.html /usr/share/nginx/html/index.html
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM ubuntu:latest
2+
RUN apt-get update && apt-get -y install gcc libc-dev
3+
COPY . /work/ipc
4+
WORKDIR /work/ipc
5+
RUN gcc -o ipc ipc.c -lrt
6+
ENTRYPOINT ["./ipc"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
#include <time.h>
5+
#include <unistd.h>
6+
#include <sys/stat.h>
7+
#include <sys/types.h>
8+
#include <errno.h>
9+
#include <mqueue.h>
10+
11+
#define Q_NAME "/ch6_ipc"
12+
#define MAX_SIZE 1024
13+
#define M_EXIT "done"
14+
#define SRV_FLAG "-producer"
15+
16+
int main(int argc, char *argv[])
17+
{
18+
if (argc < 2)
19+
{
20+
producer();
21+
}
22+
else if (argc >= 2 && 0 == strncmp(argv[1], SRV_FLAG, strlen(SRV_FLAG)))
23+
{
24+
producer();
25+
}
26+
else
27+
{
28+
consumer();
29+
}
30+
}
31+
32+
int producer()
33+
{
34+
mqd_t mq;
35+
struct mq_attr attr;
36+
char buffer[MAX_SIZE];
37+
int msg, i;
38+
39+
attr.mq_flags = 0;
40+
attr.mq_maxmsg = 10;
41+
attr.mq_msgsize = MAX_SIZE;
42+
attr.mq_curmsgs = 0;
43+
44+
mq = mq_open(Q_NAME, O_CREAT | O_WRONLY, 0644, &attr);
45+
46+
/* seed random */
47+
srand(time(NULL));
48+
49+
i = 0;
50+
while (i < 500)
51+
{
52+
msg = rand() % 256;
53+
memset(buffer, 0, MAX_SIZE);
54+
sprintf(buffer, "%x", msg);
55+
printf("Produced: %s\n", buffer);
56+
fflush(stdout);
57+
mq_send(mq, buffer, MAX_SIZE, 0);
58+
i=i+1;
59+
}
60+
memset(buffer, 0, MAX_SIZE);
61+
sprintf(buffer, M_EXIT);
62+
mq_send(mq, buffer, MAX_SIZE, 0);
63+
64+
mq_close(mq);
65+
mq_unlink(Q_NAME);
66+
return 0;
67+
}
68+
69+
int consumer()
70+
{
71+
struct mq_attr attr;
72+
char buffer[MAX_SIZE + 1];
73+
ssize_t bytes_read;
74+
mqd_t mq = mq_open(Q_NAME, O_RDONLY);
75+
if ((mqd_t)-1 == mq) {
76+
printf("Either the producer has not been started or maybe I cannot access the same memory...\n");
77+
exit(1);
78+
}
79+
do {
80+
bytes_read = mq_receive(mq, buffer, MAX_SIZE, NULL);
81+
buffer[bytes_read] = '\0';
82+
printf("Consumed: %s\n", buffer);
83+
} while (0 != strncmp(buffer, M_EXIT, strlen(M_EXIT)));
84+
85+
mq_close(mq);
86+
return 0;
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!DOCTYPE html>
2+
<meta lang="ja">
3+
<head>
4+
<title>hello</title>
5+
</head>
6+
<body>
7+
<p>Hello,world.</p>
8+
</body>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: kind.x-k8s.io/v1alpha4
2+
kind: Cluster
3+
nodes:
4+
- role: control-plane
5+
- role: worker
6+
- role: worker
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
resources:
4+
- ./ns.yml
5+
- ./pod.yml
6+
- ./pod2.yml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
apiVersion: v1
2+
kind: Namespace
3+
metadata:
4+
name: k8s-test
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: mc1
5+
namespace: k8s-test
6+
spec:
7+
volumes:
8+
- name: html
9+
emptyDir: {}
10+
containers:
11+
- name: 1st
12+
image: nginx
13+
volumeMounts:
14+
- name: html
15+
mountPath: /usr/share/nginx/html
16+
- name: 2nd
17+
image: debian
18+
volumeMounts:
19+
- name: html
20+
mountPath: /html
21+
command: ["/bin/sh", "-c"]
22+
args:
23+
- while true; do date >> /html/index.html; sleep 1; done
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: mc2
5+
namespace: k8s-test
6+
spec:
7+
containers:
8+
- name: 1st
9+
image: allingeek/ch6_ipc:latest
10+
command: ["./ipc", "-producer"]
11+
imagePullPolicy: IfNotPresent
12+
- name: 2nd
13+
image: allingeek/ch6_ipc:latest
14+
command: ["./ipc", "-consumer"]
15+
imagePullPolicy: IfNotPresent
16+
restartPolicy: Never
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
static_resources:
2+
3+
listeners:
4+
- name: listener_0
5+
address:
6+
socket_address:
7+
address: 0.0.0.0
8+
port_value: 10000
9+
filter_chains:
10+
- filters:
11+
- name: envoy.filters.network.http_connection_manager
12+
typed_config:
13+
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
14+
stat_prefix: ingress_http
15+
access_log:
16+
- name: envoy.access_loggers.stdout
17+
typed_config:
18+
"@type": type.googleapis.com/envoy.extensions.access_loggers.stream.v3.StdoutAccessLog
19+
http_filters:
20+
- name: envoy.filters.http.router
21+
typed_config:
22+
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
23+
route_config:
24+
name: local_route
25+
virtual_hosts:
26+
- name: local_service
27+
domains: ["*"]
28+
routes:
29+
- match:
30+
prefix: "/"
31+
route:
32+
host_rewrite_literal: www.envoyproxy.io
33+
cluster: service_envoyproxy_io
34+
35+
clusters:
36+
- name: service_envoyproxy_io
37+
type: LOGICAL_DNS
38+
# Comment out the following line to test on v6 networks
39+
dns_lookup_family: V4_ONLY
40+
load_assignment:
41+
cluster_name: service_envoyproxy_io
42+
endpoints:
43+
- lb_endpoints:
44+
- endpoint:
45+
address:
46+
socket_address:
47+
address: www.envoyproxy.io
48+
port_value: 443
49+
transport_socket:
50+
name: envoy.transport_sockets.tls
51+
typed_config:
52+
"@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext
53+
sni: www.envoyproxy.io
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
admin:
2+
address:
3+
socket_address:
4+
address: 127.0.0.1
5+
port_value: 9902

0 commit comments

Comments
 (0)